Search in sources :

Example 1 with EmailIdentity

use of i2p.bote.email.EmailIdentity 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 EmailIdentity

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

the class OutboxProcessor method sendEmail.

/**
 * Sends an {@link Email} to all recipients specified in the header.
 * @param email
 * @throws MessagingException
 * @throws DhtException
 * @throws GeneralSecurityException
 * @throws PasswordException
 * @throws IOException
 * @throws InterruptedException
 */
void sendEmail(Email email) throws MessagingException, DhtException, GeneralSecurityException, PasswordException, IOException, InterruptedException {
    EmailIdentity senderIdentity = null;
    if (!email.isAnonymous()) {
        String sender = email.getOneFromAddress();
        senderIdentity = identities.extractIdentity(sender);
        if (senderIdentity == null) {
            log.error("No identity matches the sender/from field: " + sender + " in email: " + email);
            outbox.setStatus(email, new EmailStatus(Status.NO_IDENTITY_MATCHES, sender));
        }
    }
    // send to I2P-Bote recipients
    outbox.setStatus(email, new EmailStatus(Status.SENDING));
    Address[] recipients = email.getAllRecipients();
    boolean containsExternalRecipients = false;
    for (int i = 0; i < recipients.length; i++) {
        Address recipient = recipients[i];
        if (isExternalAddress(recipient))
            containsExternalRecipients = true;
        else
            sendToOne(senderIdentity, recipient.toString(), email);
        outbox.setStatus(email, new EmailStatus(Status.SENT_TO, i + 1, recipients.length));
    }
    // send to external recipients if there are any
    if (containsExternalRecipients) {
        if (configuration.isGatewayEnabled()) {
            sendToOne(senderIdentity, configuration.getGatewayDestination(), email);
            outbox.setStatus(email, new EmailStatus(Status.EMAIL_SENT));
        } else {
            outbox.setStatus(email, new EmailStatus(Status.GATEWAY_DISABLED));
            throw new MessagingException("The email contains external addresses, but the gateway is disabled.");
        }
    }
}
Also used : Address(javax.mail.Address) InternetAddress(javax.mail.internet.InternetAddress) MessagingException(javax.mail.MessagingException) EmailIdentity(i2p.bote.email.EmailIdentity) EmailStatus(i2p.bote.folder.Outbox.EmailStatus)

Example 3 with EmailIdentity

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

the class OutboxProcessor method sendToOne.

/**
 * Sends an {@link Email} to one recipient.
 * @param senderIdentity The sender's Email Identity, or <code>null</code> for anonymous emails
 * @param recipient
 * @param email
 * @throws MessagingException
 * @throws DhtException
 * @throws GeneralSecurityException
 * @throws PasswordException
 * @throws InterruptedException
 */
private void sendToOne(EmailIdentity senderIdentity, String recipient, Email email) throws MessagingException, DhtException, GeneralSecurityException, PasswordException, InterruptedException {
    // only used for logging
    String logSuffix = null;
    try {
        logSuffix = "Recipient = '" + recipient + "' Message ID = '" + email.getMessageID() + "'";
        log.info("Sending email: " + logSuffix);
        EmailDestination recipientDest = new EmailDestination(recipient);
        EmailIdentity.IdentityConfig identityConfig;
        if (email.isAnonymous())
            identityConfig = configuration;
        else
            identityConfig = senderIdentity.getWrappedConfig(configuration);
        int hops = identityConfig.getNumStoreHops();
        long minDelay = identityConfig.getRelayMinDelay() * 60 * 1000;
        long maxDelay = identityConfig.getRelayMaxDelay() * 60 * 1000;
        int relayRedundancy = identityConfig.getRelayRedundancy();
        int maxPacketSize = getMaxEmailPacketSize(hops);
        Collection<UnencryptedEmailPacket> emailPackets = email.createEmailPackets(senderIdentity, identities, recipient, maxPacketSize);
        IndexPacket indexPacket = new IndexPacket(recipientDest);
        EmailMetadata metadata = email.getMetadata();
        for (UnencryptedEmailPacket unencryptedPacket : emailPackets) {
            EncryptedEmailPacket emailPacket = new EncryptedEmailPacket(unencryptedPacket, recipientDest);
            send(emailPacket, hops, minDelay, maxDelay, relayRedundancy);
            indexPacket.put(emailPacket);
            metadata.addPacketInfo(recipientDest, emailPacket.getDhtKey(), emailPacket.getDeleteVerificationHash());
        }
        send(indexPacket, hops, minDelay, maxDelay, relayRedundancy);
        outbox.saveMetadata(email);
    } catch (GeneralSecurityException e) {
        log.error("Invalid recipient address. " + logSuffix, e);
        outbox.setStatus(email, new EmailStatus(Status.INVALID_RECIPIENT, recipient));
        throw e;
    } catch (MessagingException e) {
        log.error("Can't create email packets. " + logSuffix, e);
        outbox.setStatus(email, new EmailStatus(Status.ERROR_CREATING_PACKETS, e.getLocalizedMessage()));
        throw e;
    } catch (DhtException e) {
        log.error("Can't store email packet on the DHT. " + logSuffix, e);
        outbox.setStatus(email, new EmailStatus(Status.ERROR_SENDING, e.getLocalizedMessage()));
        throw e;
    } catch (IOException e) {
        log.error("Can't save metadata. " + logSuffix, e);
        outbox.setStatus(email, new EmailStatus(Status.ERROR_SAVING_METADATA, e.getLocalizedMessage()));
    }
}
Also used : MessagingException(javax.mail.MessagingException) EmailIdentity(i2p.bote.email.EmailIdentity) EncryptedEmailPacket(i2p.bote.packet.dht.EncryptedEmailPacket) GeneralSecurityException(java.security.GeneralSecurityException) EmailStatus(i2p.bote.folder.Outbox.EmailStatus) EmailMetadata(i2p.bote.email.EmailMetadata) IOException(java.io.IOException) IndexPacket(i2p.bote.packet.dht.IndexPacket) EmailDestination(i2p.bote.email.EmailDestination) UnencryptedEmailPacket(i2p.bote.packet.dht.UnencryptedEmailPacket) DhtException(i2p.bote.network.DhtException)

Example 4 with EmailIdentity

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

the class EmailListActivity method identitySelected.

private void identitySelected(IProfile profile) {
    EmailIdentity identity = (EmailIdentity) ((ProfileDrawerItem) profile).getTag();
    mSharedPrefs.edit().putString(Constants.PREF_SELECTED_IDENTITY, identity == null ? null : identity.getKey()).apply();
    // Trigger the drawer folder loader to update the drawer badges
    getSupportLoaderManager().restartLoader(LOADER_DRAWER_FOLDERS, null, new DrawerFolderLoaderCallbacks());
    EmailListFragment f = (EmailListFragment) getSupportFragmentManager().findFragmentById(R.id.list_fragment);
    f.onIdentitySelected();
}
Also used : EmailIdentity(i2p.bote.email.EmailIdentity)

Example 5 with EmailIdentity

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

the class NewEmailFragment method sendEmail.

private boolean sendEmail() {
    Email email = new Email(I2PBote.getInstance().getConfiguration().getIncludeSentTime());
    try {
        // Set sender
        EmailIdentity sender = (EmailIdentity) mSpinner.getSelectedItem();
        InternetAddress ia = new InternetAddress(sender == null ? "Anonymous" : BoteHelper.getNameAndDestination(sender.getKey()));
        email.setFrom(ia);
        // We must continue to set "Sender:" even with only one mailbox
        // in "From:", which is against RFC 2822 but required for older
        // Bote versions to see a sender (and validate the signature).
        email.setSender(ia);
        for (Object obj : mTo.getObjects()) {
            Person person = (Person) obj;
            email.addRecipient(Message.RecipientType.TO, new InternetAddress(person.getAddress(), person.getName()));
        }
        if (mMoreVisible) {
            for (Object obj : mCc.getObjects()) {
                Person person = (Person) obj;
                email.addRecipient(Message.RecipientType.CC, new InternetAddress(person.getAddress(), person.getName()));
            }
            for (Object obj : mBcc.getObjects()) {
                Person person = (Person) obj;
                email.addRecipient(Message.RecipientType.BCC, new InternetAddress(person.getAddress(), person.getName()));
            }
        }
        // Check that we have someone to send to
        Address[] rcpts = email.getAllRecipients();
        if (rcpts == null || rcpts.length == 0) {
            // No recipients
            mTo.setError(getActivity().getString(R.string.add_one_recipient));
            mTo.requestFocus();
            return false;
        } else {
            mTo.setError(null);
        }
        email.setSubject(mSubject.getText().toString(), "UTF-8");
        // Extract the attachments
        List<Attachment> attachments = new ArrayList<Attachment>();
        for (int i = 0; i < mAttachments.getChildCount(); i++) {
            View v = mAttachments.getChildAt(i);
            // Warning views don't have tags set
            if (v.getTag() != null)
                attachments.add((Attachment) v.getTag());
        }
        // Set the text and add attachments
        email.setContent(mContent.getText().toString(), attachments);
        // Cache the fact that we sent this email
        BoteHelper.setEmailSent(email, true);
        // Send the email
        I2PBote.getInstance().sendEmail(email);
        // Clean up attachments
        for (Attachment attachment : attachments) {
            if (!attachment.clean())
                Log.e(Constants.ANDROID_LOG_TAG, "Can't clean up attachment: <" + attachment + ">");
        }
        return true;
    } catch (PasswordException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (AddressException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (MessagingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (GeneralSecurityException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return false;
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) Email(i2p.bote.email.Email) Address(javax.mail.Address) InternetAddress(javax.mail.internet.InternetAddress) MessagingException(javax.mail.MessagingException) EmailIdentity(i2p.bote.email.EmailIdentity) GeneralSecurityException(java.security.GeneralSecurityException) ArrayList(java.util.ArrayList) Attachment(i2p.bote.email.Attachment) ContentAttachment(i2p.bote.android.util.ContentAttachment) IOException(java.io.IOException) ImageView(android.widget.ImageView) View(android.view.View) TextView(android.widget.TextView) ContactsCompletionView(i2p.bote.android.widget.ContactsCompletionView) SuppressLint(android.annotation.SuppressLint) PasswordException(i2p.bote.fileencryption.PasswordException) AddressException(javax.mail.internet.AddressException) Person(i2p.bote.android.util.Person)

Aggregations

EmailIdentity (i2p.bote.email.EmailIdentity)19 EmailDestination (i2p.bote.email.EmailDestination)4 GeneralSecurityException (java.security.GeneralSecurityException)4 MessagingException (javax.mail.MessagingException)4 InternetAddress (javax.mail.internet.InternetAddress)4 Before (org.junit.Before)4 Email (i2p.bote.email.Email)3 Identities (i2p.bote.email.Identities)3 IOException (java.io.IOException)3 Address (javax.mail.Address)3 View (android.view.View)2 ImageView (android.widget.ImageView)2 TextView (android.widget.TextView)2 UniqueId (i2p.bote.UniqueId)2 KeyUpdateHandler (i2p.bote.crypto.KeyUpdateHandler)2 PasswordException (i2p.bote.fileencryption.PasswordException)2 EmailStatus (i2p.bote.folder.Outbox.EmailStatus)2 EncryptedEmailPacket (i2p.bote.packet.dht.EncryptedEmailPacket)2 IndexPacket (i2p.bote.packet.dht.IndexPacket)2 UnencryptedEmailPacket (i2p.bote.packet.dht.UnencryptedEmailPacket)2