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);
}
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();
}
}
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();
}
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;
}
Aggregations