Search in sources :

Example 16 with TextBody

use of com.fsck.k9.mail.internet.TextBody 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 17 with TextBody

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

the class PgpMessageBuilderTest method buildSign__withDetachedSignatureInResult__shouldSucceed.

@Test
public void buildSign__withDetachedSignatureInResult__shouldSucceed() throws MessagingException {
    cryptoStatusBuilder.setCryptoMode(CryptoMode.SIGN_ONLY);
    pgpMessageBuilder.setCryptoStatus(cryptoStatusBuilder.build());
    ArgumentCaptor<Intent> capturedApiIntent = ArgumentCaptor.forClass(Intent.class);
    Intent returnIntent = new Intent();
    returnIntent.putExtra(OpenPgpApi.RESULT_CODE, OpenPgpApi.RESULT_CODE_SUCCESS);
    returnIntent.putExtra(OpenPgpApi.RESULT_DETACHED_SIGNATURE, new byte[] { 1, 2, 3 });
    when(openPgpApi.executeApi(capturedApiIntent.capture(), any(OpenPgpDataSource.class), any(OutputStream.class))).thenReturn(returnIntent);
    Callback mockCallback = mock(Callback.class);
    pgpMessageBuilder.buildAsync(mockCallback);
    Intent expectedIntent = new Intent(OpenPgpApi.ACTION_DETACHED_SIGN);
    expectedIntent.putExtra(OpenPgpApi.EXTRA_SIGN_KEY_ID, TEST_SIGN_KEY_ID);
    expectedIntent.putExtra(OpenPgpApi.EXTRA_REQUEST_ASCII_ARMOR, true);
    assertIntentEqualsActionAndExtras(expectedIntent, capturedApiIntent.getValue());
    ArgumentCaptor<MimeMessage> captor = ArgumentCaptor.forClass(MimeMessage.class);
    verify(mockCallback).onMessageBuildSuccess(captor.capture(), eq(false));
    verifyNoMoreInteractions(mockCallback);
    MimeMessage message = captor.getValue();
    Assert.assertEquals("message must be multipart/signed", "multipart/signed", message.getMimeType());
    MimeMultipart multipart = (MimeMultipart) message.getBody();
    Assert.assertEquals("multipart/signed must consist of two parts", 2, multipart.getCount());
    BodyPart contentBodyPart = multipart.getBodyPart(0);
    Assert.assertEquals("first part must have content type text/plain", "text/plain", MimeUtility.getHeaderParameter(contentBodyPart.getContentType(), null));
    Assert.assertTrue("signed message body must be TextBody", contentBodyPart.getBody() instanceof TextBody);
    Assert.assertEquals(MimeUtil.ENC_QUOTED_PRINTABLE, ((TextBody) contentBodyPart.getBody()).getEncoding());
    assertContentOfBodyPartEquals("content must match the message text", contentBodyPart, TEST_MESSAGE_TEXT);
    BodyPart signatureBodyPart = multipart.getBodyPart(1);
    String contentType = signatureBodyPart.getContentType();
    Assert.assertEquals("second part must be pgp signature", "application/pgp-signature", MimeUtility.getHeaderParameter(contentType, null));
    Assert.assertEquals("second part must be called signature.asc", "signature.asc", MimeUtility.getHeaderParameter(contentType, "name"));
    assertContentOfBodyPartEquals("content must match the supplied detached signature", signatureBodyPart, new byte[] { 1, 2, 3 });
}
Also used : TextBody(com.fsck.k9.mail.internet.TextBody) BodyPart(com.fsck.k9.mail.BodyPart) Callback(com.fsck.k9.message.MessageBuilder.Callback) MimeMessage(com.fsck.k9.mail.internet.MimeMessage) MimeMultipart(com.fsck.k9.mail.internet.MimeMultipart) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) PendingIntent(android.app.PendingIntent) Intent(android.content.Intent) OpenPgpDataSource(org.openintents.openpgp.util.OpenPgpApi.OpenPgpDataSource) Test(org.junit.Test)

Example 18 with TextBody

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

the class TextBodyBuilderTest method testBuildTextPlain.

@Theory
public void testBuildTextPlain(boolean includeQuotedText, QuoteStyle quoteStyle, boolean isReplyAfterQuote, boolean isSignatureUse, boolean isSignatureBeforeQuotedText, boolean isDraft) {
    String expectedText;
    int expectedMessageLength;
    int expectedMessagePosition;
    // 3.signature
    if (quoteStyle == QuoteStyle.PREFIX && isReplyAfterQuote) {
        String expectedQuotedText = "";
        if (isDraft || includeQuotedText) {
            expectedQuotedText = "quoted text" + "\r\n";
        }
        expectedText = expectedQuotedText;
        expectedText += "message content";
        if (!isDraft && isSignatureUse) {
            expectedText += "\r\n" + "signature";
        }
        expectedMessageLength = "message content".length();
        expectedMessagePosition = expectedQuotedText.length();
    } else // 3.quoted text
    if (isSignatureBeforeQuotedText) {
        expectedText = "message content";
        if (!isDraft && isSignatureUse) {
            expectedText += "\r\n" + "signature";
        }
        if (isDraft || includeQuotedText) {
            expectedText += "\r\n\r\nquoted text";
        }
        expectedMessageLength = "message content".length();
        expectedMessagePosition = 0;
    } else // 1.message content
    // 2.quoted text
    // 3.signature
    {
        expectedText = "message content";
        if (isDraft || includeQuotedText) {
            expectedText += "\r\n\r\nquoted text";
        }
        if (!isDraft && isSignatureUse) {
            expectedText += "\r\n" + "signature";
        }
        expectedMessageLength = "message content".length();
        expectedMessagePosition = 0;
    }
    String quotedText = "quoted text";
    String messageText = "message content";
    String signatureText = "signature";
    TestingTextBodyBuilder textBodyBuilder = new TestingTextBodyBuilder(includeQuotedText, isDraft, quoteStyle, isReplyAfterQuote, isSignatureBeforeQuotedText, isSignatureUse, messageText, signatureText);
    textBodyBuilder.setQuotedText(quotedText);
    TextBody textBody = textBodyBuilder.buildTextPlain();
    assertThat(textBody, instanceOf(TextBody.class));
    assertThat(textBody.getRawText(), is(expectedText));
    assertThat(textBody.getComposedMessageLength(), is(expectedMessageLength));
    assertThat(textBody.getComposedMessageOffset(), is(expectedMessagePosition));
    assertThat(textBody.getRawText().substring(expectedMessagePosition, expectedMessagePosition + expectedMessageLength), is("message content"));
}
Also used : TextBody(com.fsck.k9.mail.internet.TextBody) Theory(org.junit.experimental.theories.Theory)

Example 19 with TextBody

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

the class TextBodyBuilderTest method testBuildTextHtml.

@Theory
public void testBuildTextHtml(boolean includeQuotedText, QuoteStyle quoteStyle, boolean isReplyAfterQuote, boolean isSignatureUse, boolean isSignatureBeforeQuotedText, boolean isDraft) {
    String expectedText;
    int expectedMessageLength;
    int expectedMessagePosition = 0;
    String expectedHtmlContent;
    String expectedPrefix = "";
    if (includeQuotedText && quoteStyle == QuoteStyle.PREFIX && isReplyAfterQuote && !isDraft) {
        expectedPrefix = "<br clear=\"all\">";
    }
    String expectedPostfix = "";
    if (!isDraft && includeQuotedText) {
        expectedPostfix = "<br><br>";
    }
    // 3.signature
    if (quoteStyle == QuoteStyle.PREFIX && isReplyAfterQuote) {
        expectedText = expectedPrefix + "<html>message content";
        if (!isDraft && isSignatureUse) {
            expectedText += "\r\n" + "signature";
        }
        expectedText += "</html>";
        expectedMessageLength = expectedText.length();
        String quotedContent = "quoted text";
        if (isDraft || includeQuotedText) {
            expectedHtmlContent = makeExpectedHtmlContent(expectedText, quotedContent, 0, false, expectedText, expectedText + quotedContent);
            expectedText += quotedContent;
        } else {
            expectedHtmlContent = makeExpectedHtmlContent(expectedText, quotedContent, 0, true, "", quotedContent);
        // expectedText += quotedContent;
        }
    } else // 3.quoted text
    if (isSignatureBeforeQuotedText) {
        expectedText = expectedPrefix + "<html>message content";
        if (!isDraft && isSignatureUse) {
            expectedText += "\r\n" + "signature";
        }
        expectedText += "</html>";
        expectedText += expectedPostfix;
        expectedMessageLength = expectedText.length();
        String quotedContent = "quoted text";
        if (isDraft || includeQuotedText) {
            expectedHtmlContent = makeExpectedHtmlContent(expectedText, quotedContent, 0, true, expectedText, expectedText + quotedContent);
            expectedText += quotedContent;
        } else {
            expectedHtmlContent = makeExpectedHtmlContent(expectedText, quotedContent, 0, true, "", quotedContent);
        // expectedText += quotedContent;
        }
    } else // 1.message content
    // 2.quoted text
    // 3.signature
    {
        String expectedSignature = "";
        expectedText = expectedPrefix + "<html>message content";
        if (!isDraft && isSignatureUse) {
            if (!includeQuotedText) {
                expectedText += "\r\n" + "signature";
            } else {
                expectedSignature = "<html>\r\nsignature</html>";
            }
        }
        expectedText += "</html>";
        expectedText += expectedPostfix;
        expectedMessageLength = expectedText.length();
        String quotedContent = "quoted text";
        if (isDraft || includeQuotedText) {
            expectedHtmlContent = makeExpectedHtmlContent(expectedText, expectedSignature + quotedContent, expectedSignature.length(), true, expectedText, expectedText + expectedSignature + quotedContent);
            expectedText += expectedSignature + quotedContent;
        } else {
            expectedHtmlContent = makeExpectedHtmlContent(expectedText, quotedContent, 0, true, "", quotedContent);
        // expectedText += quotedContent;
        }
    }
    InsertableHtmlContent insertableHtmlContent = new InsertableHtmlContent();
    String quotedText = "quoted text";
    insertableHtmlContent.setQuotedContent(new StringBuilder(quotedText));
    String messageText = "message content";
    String signatureText = "signature";
    TestingTextBodyBuilder textBodyBuilder = new TestingTextBodyBuilder(includeQuotedText, isDraft, quoteStyle, isReplyAfterQuote, isSignatureBeforeQuotedText, isSignatureUse, messageText, signatureText);
    textBodyBuilder.setQuotedTextHtml(insertableHtmlContent);
    TextBody textBody = textBodyBuilder.buildTextHtml();
    assertThat(textBody, instanceOf(TextBody.class));
    assertThat(textBody.getRawText(), is(expectedText));
    assertThat(textBody.getComposedMessageLength(), is(expectedMessageLength));
    assertThat(textBody.getComposedMessageOffset(), is(expectedMessagePosition));
    assertThat(insertableHtmlContent.toDebugString(), is(expectedHtmlContent));
}
Also used : TextBody(com.fsck.k9.mail.internet.TextBody) InsertableHtmlContent(com.fsck.k9.message.quote.InsertableHtmlContent) Theory(org.junit.experimental.theories.Theory)

Example 20 with TextBody

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

the class MessageViewInfoExtractorTest method testTextPlainFormatFlowed.

@Test
public void testTextPlainFormatFlowed() throws MessagingException {
    // Create text/plain body
    TextBody body = new TextBody(BODY_TEXT_FLOWED);
    // Create message
    MimeMessage message = new MimeMessage();
    MimeMessageHelper.setBody(message, body);
    message.setHeader(MimeHeader.HEADER_CONTENT_TYPE, "text/plain; format=flowed");
    // Extract text
    List<Part> outputNonViewableParts = new ArrayList<>();
    ArrayList<Viewable> outputViewableParts = new ArrayList<>();
    MessageExtractor.findViewablesAndAttachments(message, outputViewableParts, outputNonViewableParts);
    ViewableExtractedText container = messageViewInfoExtractor.extractTextFromViewables(outputViewableParts);
    String expectedText = "K-9 Mail rocks :> flowed line\r\n" + "not flowed line";
    String expectedHtml = "<pre class=\"k9mail\">" + "K-9 Mail rocks :&gt; flowed line<br />not flowed line" + "</pre>";
    assertEquals(expectedText, container.text);
    assertEquals(expectedHtml, getHtmlBodyText(container.html));
}
Also used : TextBody(com.fsck.k9.mail.internet.TextBody) 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) Test(org.junit.Test)

Aggregations

TextBody (com.fsck.k9.mail.internet.TextBody)27 MimeMessage (com.fsck.k9.mail.internet.MimeMessage)16 Test (org.junit.Test)15 MimeBodyPart (com.fsck.k9.mail.internet.MimeBodyPart)13 Part (com.fsck.k9.mail.Part)7 ArrayList (java.util.ArrayList)7 Viewable (com.fsck.k9.mail.internet.Viewable)6 ViewableExtractedText (com.fsck.k9.mailstore.MessageViewInfoExtractor.ViewableExtractedText)6 MimeMultipart (com.fsck.k9.mail.internet.MimeMultipart)5 BodyPart (com.fsck.k9.mail.BodyPart)3 Message (com.fsck.k9.mail.Message)3 MessagingException (com.fsck.k9.mail.MessagingException)3 Address (com.fsck.k9.mail.Address)2 InsertableHtmlContent (com.fsck.k9.message.quote.InsertableHtmlContent)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 Date (java.util.Date)2 Theory (org.junit.experimental.theories.Theory)2 PendingIntent (android.app.PendingIntent)1 Intent (android.content.Intent)1 Account (com.fsck.k9.Account)1