Search in sources :

Example 1 with AddressBook

use of i2p.bote.addressbook.AddressBook in project i2p.i2p-bote by i2p.

the class GeneralHelper method saveContact.

/**
 * Updates a contact in the address book if the Destination <code>destinationString</code> exists,
 * or adds a new contact to the address book.
 * @param destinationString A base64-encoded Email Destination key
 * @param name
 * @param pictureBase64
 * @param text
 * @return null if sucessful, or an error message if an error occured
 * @throws PasswordException
 * @throws GeneralSecurityException
 */
public static String saveContact(String destinationString, String name, String pictureBase64, String text) throws PasswordException, GeneralSecurityException {
    destinationString = Util.fixAddress(destinationString);
    AddressBook addressBook = getInstance().getAddressBook();
    Contact contact = addressBook.get(destinationString);
    if (contact != null) {
        contact.setName(name);
        contact.setPictureBase64(pictureBase64);
        contact.setText(text);
    } else {
        EmailDestination destination;
        try {
            destination = new EmailDestination(destinationString);
        } catch (GeneralSecurityException e) {
            Log log = new Log(GeneralHelper.class);
            log.error("Can't save contact to address book.", e);
            return e.getLocalizedMessage();
        }
        contact = new Contact(name, destination, pictureBase64, text);
        addressBook.add(contact);
    }
    try {
        addressBook.save();
        return null;
    } catch (IOException e) {
        return e.getLocalizedMessage();
    }
}
Also used : AddressBook(i2p.bote.addressbook.AddressBook) Log(net.i2p.util.Log) EmailDestination(i2p.bote.email.EmailDestination) GeneralSecurityException(java.security.GeneralSecurityException) IOException(java.io.IOException) Contact(i2p.bote.packet.dht.Contact)

Example 2 with AddressBook

use of i2p.bote.addressbook.AddressBook in project i2p.i2p-bote by i2p.

the class MigrateTo028 method migrateAddressBook.

private void migrateAddressBook(List<String> lines, Configuration configuration, PasswordHolder passwordHolder) throws IOException, GeneralSecurityException, PasswordException {
    AddressBook addressBook = new AddressBook(configuration.getAddressBookFile(), passwordHolder);
    for (String line : lines) {
        String[] fields = line.split("\\t", 2);
        try {
            EmailDestination destination = new EmailDestination(fields[0]);
            String name = null;
            if (fields.length > 1)
                name = fields[1];
            Contact contact = new Contact(name, destination);
            addressBook.add(contact);
        } catch (GeneralSecurityException e) {
            log.error("Not a valid Email Destination: <" + fields[0] + ">", e);
        }
    }
    addressBook.save();
}
Also used : AddressBook(i2p.bote.addressbook.AddressBook) EmailDestination(i2p.bote.email.EmailDestination) GeneralSecurityException(java.security.GeneralSecurityException) Contact(i2p.bote.packet.dht.Contact)

Example 3 with AddressBook

use of i2p.bote.addressbook.AddressBook in project i2p.i2p-bote by i2p.

the class GeneralHelper method deleteContact.

/**
 * Deletes a contact from the address book.
 * @param destination A base64-encoded email destination
 * @return null if sucessful, or an error message if an error occured
 * @throws GeneralSecurityException
 * @throws PasswordException
 */
public static String deleteContact(String destination) throws PasswordException, GeneralSecurityException {
    AddressBook addressBook = getInstance().getAddressBook();
    addressBook.remove(destination);
    try {
        addressBook.save();
        return null;
    } catch (IOException e) {
        return e.getLocalizedMessage();
    }
}
Also used : AddressBook(i2p.bote.addressbook.AddressBook) IOException(java.io.IOException)

Aggregations

AddressBook (i2p.bote.addressbook.AddressBook)3 EmailDestination (i2p.bote.email.EmailDestination)2 Contact (i2p.bote.packet.dht.Contact)2 IOException (java.io.IOException)2 GeneralSecurityException (java.security.GeneralSecurityException)2 Log (net.i2p.util.Log)1