Search in sources :

Example 26 with MimeBodyPart

use of com.fsck.k9.mail.internet.MimeBodyPart in project k-9 by k9mail.

the class PgpMessageBuilder method launchOpenPgpApiIntent.

private PendingIntent launchOpenPgpApiIntent(@NonNull Intent openPgpIntent, boolean captureOutputPart, boolean capturedOutputPartIs7Bit, boolean writeBodyContentOnly) throws MessagingException {
    final MimeBodyPart bodyPart = currentProcessedMimeMessage.toBodyPart();
    String[] contentType = currentProcessedMimeMessage.getHeader(MimeHeader.HEADER_CONTENT_TYPE);
    if (contentType.length > 0) {
        bodyPart.setHeader(MimeHeader.HEADER_CONTENT_TYPE, contentType[0]);
    }
    OpenPgpDataSource dataSource = createOpenPgpDataSourceFromBodyPart(bodyPart, writeBodyContentOnly);
    BinaryTempFileBody pgpResultTempBody = null;
    OutputStream outputStream = null;
    if (captureOutputPart) {
        try {
            pgpResultTempBody = new BinaryTempFileBody(capturedOutputPartIs7Bit ? MimeUtil.ENC_7BIT : MimeUtil.ENC_8BIT);
            outputStream = pgpResultTempBody.getOutputStream();
            // OpenKeychain/BouncyCastle at this point use the system newline for formatting, which is LF on android.
            // we need this to be CRLF, so we convert the data after receiving.
            outputStream = new EOLConvertingOutputStream(outputStream);
        } catch (IOException e) {
            throw new MessagingException("could not allocate temp file for storage!", e);
        }
    }
    Intent result = openPgpApi.executeApi(openPgpIntent, dataSource, outputStream);
    switch(result.getIntExtra(OpenPgpApi.RESULT_CODE, OpenPgpApi.RESULT_CODE_ERROR)) {
        case OpenPgpApi.RESULT_CODE_SUCCESS:
            mimeBuildMessage(result, bodyPart, pgpResultTempBody);
            return null;
        case OpenPgpApi.RESULT_CODE_USER_INTERACTION_REQUIRED:
            PendingIntent returnedPendingIntent = result.getParcelableExtra(OpenPgpApi.RESULT_INTENT);
            if (returnedPendingIntent == null) {
                throw new MessagingException("openpgp api needs user interaction, but returned no pendingintent!");
            }
            return returnedPendingIntent;
        case OpenPgpApi.RESULT_CODE_ERROR:
            OpenPgpError error = result.getParcelableExtra(OpenPgpApi.RESULT_ERROR);
            if (error == null) {
                throw new MessagingException("internal openpgp api error");
            }
            boolean isOpportunisticError = error.getErrorId() == OpenPgpError.OPPORTUNISTIC_MISSING_KEYS;
            if (isOpportunisticError) {
                if (!cryptoStatus.isEncryptionOpportunistic()) {
                    throw new IllegalStateException("Got opportunistic error, but encryption wasn't supposed to be opportunistic!");
                }
                Timber.d("Skipping encryption due to opportunistic mode");
                return null;
            }
            throw new MessagingException(error.getMessage());
    }
    throw new IllegalStateException("unreachable code segment reached");
}
Also used : EOLConvertingOutputStream(com.fsck.k9.mail.filter.EOLConvertingOutputStream) MessagingException(com.fsck.k9.mail.MessagingException) EOLConvertingOutputStream(com.fsck.k9.mail.filter.EOLConvertingOutputStream) OutputStream(java.io.OutputStream) OpenPgpDataSource(org.openintents.openpgp.util.OpenPgpApi.OpenPgpDataSource) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) IOException(java.io.IOException) BinaryTempFileBody(com.fsck.k9.mail.internet.BinaryTempFileBody) PendingIntent(android.app.PendingIntent) OpenPgpError(org.openintents.openpgp.OpenPgpError) MimeBodyPart(com.fsck.k9.mail.internet.MimeBodyPart)

Example 27 with MimeBodyPart

use of com.fsck.k9.mail.internet.MimeBodyPart in project k-9 by k9mail.

the class MessageBuilder method buildBody.

private void buildBody(MimeMessage message) throws MessagingException {
    // Build the body.
    // TODO FIXME - body can be either an HTML or Text part, depending on whether we're in
    // HTML mode or not.  Should probably fix this so we don't mix up html and text parts.
    TextBody body = buildText(isDraft);
    // text/plain part when messageFormat == MessageFormat.HTML
    TextBody bodyPlain = null;
    final boolean hasAttachments = !attachments.isEmpty();
    if (messageFormat == SimpleMessageFormat.HTML) {
        // HTML message (with alternative text part)
        // This is the compiled MIME part for an HTML message.
        MimeMultipart composedMimeMessage = createMimeMultipart();
        composedMimeMessage.setSubType("alternative");
        // Let the receiver select either the text or the HTML part.
        bodyPlain = buildText(isDraft, SimpleMessageFormat.TEXT);
        composedMimeMessage.addBodyPart(new MimeBodyPart(bodyPlain, "text/plain"));
        composedMimeMessage.addBodyPart(new MimeBodyPart(body, "text/html"));
        if (hasAttachments) {
            // If we're HTML and have attachments, we have a MimeMultipart container to hold the
            // whole message (mp here), of which one part is a MimeMultipart container
            // (composedMimeMessage) with the user's composed messages, and subsequent parts for
            // the attachments.
            MimeMultipart mp = createMimeMultipart();
            mp.addBodyPart(new MimeBodyPart(composedMimeMessage));
            addAttachmentsToMessage(mp);
            MimeMessageHelper.setBody(message, mp);
        } else {
            // If no attachments, our multipart/alternative part is the only one we need.
            MimeMessageHelper.setBody(message, composedMimeMessage);
        }
    } else if (messageFormat == SimpleMessageFormat.TEXT) {
        // Text-only message.
        if (hasAttachments) {
            MimeMultipart mp = createMimeMultipart();
            mp.addBodyPart(new MimeBodyPart(body, "text/plain"));
            addAttachmentsToMessage(mp);
            MimeMessageHelper.setBody(message, mp);
        } else {
            // No attachments to include, just stick the text body in the message and call it good.
            MimeMessageHelper.setBody(message, body);
        }
    }
    // If this is a draft, add metadata for thawing.
    if (isDraft) {
        // Add the identity to the message.
        message.addHeader(K9.IDENTITY_HEADER, buildIdentityHeader(body, bodyPlain));
    }
}
Also used : TextBody(com.fsck.k9.mail.internet.TextBody) MimeMultipart(com.fsck.k9.mail.internet.MimeMultipart) MimeBodyPart(com.fsck.k9.mail.internet.MimeBodyPart)

Example 28 with MimeBodyPart

use of com.fsck.k9.mail.internet.MimeBodyPart in project k-9 by k9mail.

the class MessageViewInfoExtractorTest method testMultipartPlainTextMessage.

@Test
public void testMultipartPlainTextMessage() throws MessagingException {
    String bodyText1 = "text body 1";
    String bodyText2 = "text body 2";
    // Create text/plain bodies
    TextBody body1 = new TextBody(bodyText1);
    TextBody body2 = new TextBody(bodyText2);
    // Create multipart/mixed part
    MimeMultipart multipart = MimeMultipart.newInstance();
    MimeBodyPart bodyPart1 = new MimeBodyPart(body1, "text/plain");
    MimeBodyPart bodyPart2 = new MimeBodyPart(body2, "text/plain");
    multipart.addBodyPart(bodyPart1);
    multipart.addBodyPart(bodyPart2);
    // Create message
    MimeMessage message = new MimeMessage();
    MimeMessageHelper.setBody(message, multipart);
    // Extract text
    List<Part> outputNonViewableParts = new ArrayList<>();
    ArrayList<Viewable> outputViewableParts = new ArrayList<>();
    MessageExtractor.findViewablesAndAttachments(message, outputViewableParts, outputNonViewableParts);
    ViewableExtractedText container = messageViewInfoExtractor.extractTextFromViewables(outputViewableParts);
    String expectedText = bodyText1 + "\r\n\r\n" + "------------------------------------------------------------------------\r\n\r\n" + bodyText2;
    String expectedHtml = "<pre class=\"k9mail\">" + bodyText1 + "</pre>" + "<p style=\"margin-top: 2.5em; margin-bottom: 1em; " + "border-bottom: 1px solid #000\"></p>" + "<pre class=\"k9mail\">" + bodyText2 + "</pre>";
    assertEquals(expectedText, container.text);
    assertEquals(expectedHtml, getHtmlBodyText(container.html));
}
Also used : TextBody(com.fsck.k9.mail.internet.TextBody) MimeMultipart(com.fsck.k9.mail.internet.MimeMultipart) MimeMessage(com.fsck.k9.mail.internet.MimeMessage) Part(com.fsck.k9.mail.Part) MimeBodyPart(com.fsck.k9.mail.internet.MimeBodyPart) ArrayList(java.util.ArrayList) Viewable(com.fsck.k9.mail.internet.Viewable) ViewableExtractedText(com.fsck.k9.mailstore.MessageViewInfoExtractor.ViewableExtractedText) MimeBodyPart(com.fsck.k9.mail.internet.MimeBodyPart) Test(org.junit.Test)

Example 29 with MimeBodyPart

use of com.fsck.k9.mail.internet.MimeBodyPart in project k-9 by k9mail.

the class MessageViewInfoExtractorTest method testTextPlusRfc822Message.

@Test
public void testTextPlusRfc822Message() throws MessagingException {
    Locale.setDefault(Locale.US);
    TimeZone.setDefault(TimeZone.getTimeZone("GMT+01:00"));
    String innerBodyText = "Hey there. I'm inside a message/rfc822 (inline) attachment.";
    // Create text/plain body
    TextBody textBody = new TextBody(BODY_TEXT);
    // Create inner text/plain body
    TextBody innerBody = new TextBody(innerBodyText);
    // Create message/rfc822 body
    MimeMessage innerMessage = new MimeMessage();
    innerMessage.addSentDate(new Date(112, 02, 17), false);
    innerMessage.setRecipients(RecipientType.TO, new Address[] { new Address("to@example.com") });
    innerMessage.setSubject("Subject");
    innerMessage.setFrom(new Address("from@example.com"));
    MimeMessageHelper.setBody(innerMessage, innerBody);
    // Create multipart/mixed part
    MimeMultipart multipart = MimeMultipart.newInstance();
    MimeBodyPart bodyPart1 = new MimeBodyPart(textBody, "text/plain");
    MimeBodyPart bodyPart2 = new MimeBodyPart(innerMessage, "message/rfc822");
    bodyPart2.setHeader("Content-Disposition", "inline; filename=\"message.eml\"");
    multipart.addBodyPart(bodyPart1);
    multipart.addBodyPart(bodyPart2);
    // Create message
    MimeMessage message = new MimeMessage();
    MimeMessageHelper.setBody(message, multipart);
    // Extract text
    List<Part> outputNonViewableParts = new ArrayList<Part>();
    ArrayList<Viewable> outputViewableParts = new ArrayList<>();
    MessageExtractor.findViewablesAndAttachments(message, outputViewableParts, outputNonViewableParts);
    ViewableExtractedText container = messageViewInfoExtractor.extractTextFromViewables(outputViewableParts);
    String expectedText = BODY_TEXT + "\r\n\r\n" + "----- message.eml ------------------------------------------------------" + "\r\n\r\n" + "From: from@example.com" + "\r\n" + "To: to@example.com" + "\r\n" + "Sent: Sat Mar 17 00:00:00 GMT+01:00 2012" + "\r\n" + "Subject: Subject" + "\r\n" + "\r\n" + innerBodyText;
    String expectedHtml = "<pre class=\"k9mail\">" + BODY_TEXT_HTML + "</pre>" + "<p style=\"margin-top: 2.5em; margin-bottom: 1em; border-bottom: " + "1px solid #000\">message.eml</p>" + "<table style=\"border: 0\">" + "<tr>" + "<th style=\"text-align: left; vertical-align: top;\">From:</th>" + "<td>from@example.com</td>" + "</tr><tr>" + "<th style=\"text-align: left; vertical-align: top;\">To:</th>" + "<td>to@example.com</td>" + "</tr><tr>" + "<th style=\"text-align: left; vertical-align: top;\">Sent:</th>" + "<td>Sat Mar 17 00:00:00 GMT+01:00 2012</td>" + "</tr><tr>" + "<th style=\"text-align: left; vertical-align: top;\">Subject:</th>" + "<td>Subject</td>" + "</tr>" + "</table>" + "<pre class=\"k9mail\">" + innerBodyText + "</pre>";
    assertEquals(expectedText, container.text);
    assertEquals(expectedHtml, getHtmlBodyText(container.html));
}
Also used : Address(com.fsck.k9.mail.Address) ArrayList(java.util.ArrayList) Date(java.util.Date) TextBody(com.fsck.k9.mail.internet.TextBody) MimeMessage(com.fsck.k9.mail.internet.MimeMessage) MimeMultipart(com.fsck.k9.mail.internet.MimeMultipart) Part(com.fsck.k9.mail.Part) MimeBodyPart(com.fsck.k9.mail.internet.MimeBodyPart) Viewable(com.fsck.k9.mail.internet.Viewable) ViewableExtractedText(com.fsck.k9.mailstore.MessageViewInfoExtractor.ViewableExtractedText) MimeBodyPart(com.fsck.k9.mail.internet.MimeBodyPart) Test(org.junit.Test)

Example 30 with MimeBodyPart

use of com.fsck.k9.mail.internet.MimeBodyPart in project k-9 by k9mail.

the class AttachmentResolverTest method buildCidMap__onMultipartWithEmptyBodyPart__shouldReturnEmptyMap.

@Test
public void buildCidMap__onMultipartWithEmptyBodyPart__shouldReturnEmptyMap() throws Exception {
    Multipart multipartBody = MimeMultipart.newInstance();
    BodyPart bodyPart = spy(new MimeBodyPart());
    Part multipartPart = new MimeBodyPart(multipartBody);
    multipartBody.addBodyPart(bodyPart);
    Map<String, Uri> result = AttachmentResolver.buildCidToAttachmentUriMap(attachmentInfoExtractor, multipartPart);
    verify(bodyPart).getContentId();
    assertTrue(result.isEmpty());
}
Also used : BodyPart(com.fsck.k9.mail.BodyPart) MimeBodyPart(com.fsck.k9.mail.internet.MimeBodyPart) MimeMultipart(com.fsck.k9.mail.internet.MimeMultipart) Multipart(com.fsck.k9.mail.Multipart) BodyPart(com.fsck.k9.mail.BodyPart) Part(com.fsck.k9.mail.Part) MimeBodyPart(com.fsck.k9.mail.internet.MimeBodyPart) MimeBodyPart(com.fsck.k9.mail.internet.MimeBodyPart) Uri(android.net.Uri) Test(org.junit.Test)

Aggregations

MimeBodyPart (com.fsck.k9.mail.internet.MimeBodyPart)42 Test (org.junit.Test)22 Part (com.fsck.k9.mail.Part)15 MimeMultipart (com.fsck.k9.mail.internet.MimeMultipart)15 TextBody (com.fsck.k9.mail.internet.TextBody)11 AttachmentViewInfo (com.fsck.k9.mailstore.AttachmentViewInfo)11 BodyPart (com.fsck.k9.mail.BodyPart)9 MessagingException (com.fsck.k9.mail.MessagingException)7 Uri (android.net.Uri)5 MimeMessage (com.fsck.k9.mail.internet.MimeMessage)5 PendingIntent (android.app.PendingIntent)4 Body (com.fsck.k9.mail.Body)4 Multipart (com.fsck.k9.mail.Multipart)4 CryptoResultAnnotation (com.fsck.k9.mailstore.CryptoResultAnnotation)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 OutputStream (java.io.OutputStream)4 OpenPgpDataSource (org.openintents.openpgp.util.OpenPgpApi.OpenPgpDataSource)4 Intent (android.content.Intent)3 BinaryTempFileBody (com.fsck.k9.mail.internet.BinaryTempFileBody)3 InputStream (java.io.InputStream)3