Search in sources :

Example 1 with Identities

use of i2p.bote.email.Identities in project i2p.i2p-bote by i2p.

the class GeneralHelper method createOrModifyIdentity.

/**
 * Updates an email identity if <code>createNew</code> is <code>false</code>,
 * or adds a new identity if <code>createNew</code> is <code>true</code>.
 * @param createNew
 * @param cryptoImplId The id value of the cryptographic algorithm set to use for the new identity; ignored if <code>createNew</code> is <code>false</code>
 * @param vanityPrefix An alphanumeric string the destination should start with; ignored if <code>createNew==false</code>.
 * @param key A base64-encoded Email Destination key
 * @param description
 * @param publicName
 * @param picture
 * @param emailAddress
 * @param setDefault If this is <code>true</code>, the identity becomes the new default identity. Otherwise, the default stays the same.
 * @throws GeneralSecurityException
 * @throws PasswordException
 * @throws IOException
 * @throws IllegalDestinationParametersException if <code>cryptoImplId</code> and <code>vanityPrefix</code> aren't compatible
 */
public static void createOrModifyIdentity(boolean createNew, int cryptoImplId, String vanityPrefix, String key, String publicName, String description, String pictureBase64, String emailAddress, Properties config, boolean setDefault, StatusListener<ChangeIdentityStatus> lsnr) throws GeneralSecurityException, PasswordException, IOException, IllegalDestinationParametersException {
    Log log = new Log(GeneralHelper.class);
    Identities identities = I2PBote.getInstance().getIdentities();
    EmailIdentity identity;
    if (createNew) {
        CryptoImplementation cryptoImpl = CryptoFactory.getInstance(cryptoImplId);
        if (cryptoImpl == null) {
            log.error("Invalid ID number for CryptoImplementation: " + cryptoImplId);
            throw new IllegalArgumentException("Invalid ID number for CryptoImplementation: " + cryptoImplId);
        }
        lsnr.updateStatus(ChangeIdentityStatus.GENERATING_KEYS);
        try {
            identity = new EmailIdentity(cryptoImpl, vanityPrefix);
        } catch (GeneralSecurityException e) {
            log.error("Can't generate email identity for CryptoImplementation: <" + cryptoImpl + "> with vanity prefix: <" + vanityPrefix + ">", e);
            throw e;
        }
    } else
        identity = identities.get(key);
    identity.setPublicName(publicName);
    identity.setDescription(description);
    identity.setPictureBase64(pictureBase64);
    identity.setEmailAddress(emailAddress);
    // update the identity config
    if (config != null)
        identity.loadConfig(config, "", false);
    if (createNew)
        identities.add(identity);
    else
        identities.identityUpdated(key);
    // update the default identity
    if (setDefault)
        identities.setDefault(identity);
}
Also used : CryptoImplementation(i2p.bote.crypto.CryptoImplementation) Log(net.i2p.util.Log) EmailIdentity(i2p.bote.email.EmailIdentity) GeneralSecurityException(java.security.GeneralSecurityException) Identities(i2p.bote.email.Identities)

Example 2 with Identities

use of i2p.bote.email.Identities in project i2p.i2p-bote by i2p.

the class GeneralHelper method deleteIdentity.

/**
 * Deletes an email identity.
 * @param key A base64-encoded email identity key
 * @return null if sucessful, or an error message if an error occured
 * @throws PasswordException
 * @throws GeneralSecurityException
 * @throws IOException
 */
public static String deleteIdentity(String key) throws PasswordException, IOException, GeneralSecurityException {
    Identities identities = I2PBote.getInstance().getIdentities();
    identities.remove(key);
    try {
        identities.save();
        return null;
    } catch (PasswordException e) {
        throw e;
    } catch (Exception e) {
        return e.getLocalizedMessage();
    }
}
Also used : PasswordException(i2p.bote.fileencryption.PasswordException) Identities(i2p.bote.email.Identities) PasswordException(i2p.bote.fileencryption.PasswordException) URISyntaxException(java.net.URISyntaxException) MessagingException(javax.mail.MessagingException) GeneralSecurityException(java.security.GeneralSecurityException) IllegalDestinationParametersException(i2p.bote.email.IllegalDestinationParametersException) IOException(java.io.IOException) DhtException(i2p.bote.network.DhtException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 3 with Identities

use of i2p.bote.email.Identities in project i2p.i2p-bote by i2p.

the class MigrateTo028 method migrateIdentities.

/**
 * @param configuration
 * @throws PasswordException
 * @throws GeneralSecurityException
 * @throws IOException
 * @throws Exception
 */
private void migrateIdentities(List<String> lines, Configuration configuration, PasswordHolder passwordHolder) throws IOException, GeneralSecurityException, PasswordException {
    SortedSet<EmailIdentity> identitiesSet = new TreeSet<EmailIdentity>(new IdentityComparator());
    String defaultIdentityString = null;
    for (String line : lines) {
        if (line.toLowerCase().startsWith("default"))
            defaultIdentityString = line.substring("default ".length());
        else {
            EmailIdentity identity = parse(line);
            if (identity != null)
                identitiesSet.add(identity);
        }
    }
    // set the default identity; if none defined, make the first one the default
    EmailIdentity defaultIdentity = get(identitiesSet, defaultIdentityString);
    if (defaultIdentity != null)
        defaultIdentity.setDefaultIdentity(true);
    else if (!identitiesSet.isEmpty())
        identitiesSet.iterator().next().setDefaultIdentity(true);
    Identities identities = new Identities(configuration.getIdentitiesFile(), passwordHolder);
    for (EmailIdentity identity : identitiesSet) identities.add(identity);
    identities.save();
}
Also used : IdentityComparator(i2p.bote.email.Identities.IdentityComparator) EmailIdentity(i2p.bote.email.EmailIdentity) TreeSet(java.util.TreeSet) Identities(i2p.bote.email.Identities)

Example 4 with Identities

use of i2p.bote.email.Identities in project i2p.i2p-bote by i2p.

the class GeneralHelper method getOneLocalRecipient.

/**
 * Returns the recipient address for an email that has been received by
 * the local node.<br/>
 * If the email was sent to more than one local Email Destination, one
 * of them is returned.<br/>
 * If the email does not contain a local Email Destination, a non-local
 * recipient is returned.<br/>
 * If the email contains no recipients at all, or if an error occurred,
 * <code>null</code> is returned.
 * @param email
 * @throws PasswordException
 * @throws GeneralSecurityException
 * @throws IOException
 */
public static Address getOneLocalRecipient(Email email) throws PasswordException, IOException, GeneralSecurityException {
    Address[] recipients;
    try {
        recipients = email.getAllRecipients();
    } catch (MessagingException e) {
        return null;
    }
    if (recipients == null)
        return null;
    Identities identities = I2PBote.getInstance().getIdentities();
    Iterator<EmailIdentity> iterator = identities.iterator();
    if (iterator == null)
        return null;
    while (iterator.hasNext()) {
        EmailDestination localDestination = iterator.next();
        String base64Dest = localDestination.toBase64();
        for (Address recipient : recipients) if (recipient.toString().contains(base64Dest))
            return recipient;
    }
    if (recipients.length > 0)
        return recipients[0];
    else
        return null;
}
Also used : Address(javax.mail.Address) MessagingException(javax.mail.MessagingException) EmailIdentity(i2p.bote.email.EmailIdentity) EmailDestination(i2p.bote.email.EmailDestination) Identities(i2p.bote.email.Identities)

Aggregations

Identities (i2p.bote.email.Identities)4 EmailIdentity (i2p.bote.email.EmailIdentity)3 GeneralSecurityException (java.security.GeneralSecurityException)2 MessagingException (javax.mail.MessagingException)2 CryptoImplementation (i2p.bote.crypto.CryptoImplementation)1 EmailDestination (i2p.bote.email.EmailDestination)1 IdentityComparator (i2p.bote.email.Identities.IdentityComparator)1 IllegalDestinationParametersException (i2p.bote.email.IllegalDestinationParametersException)1 PasswordException (i2p.bote.fileencryption.PasswordException)1 DhtException (i2p.bote.network.DhtException)1 IOException (java.io.IOException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 URISyntaxException (java.net.URISyntaxException)1 TreeSet (java.util.TreeSet)1 Address (javax.mail.Address)1 Log (net.i2p.util.Log)1