Search in sources :

Example 1 with ViewableExtractedText

use of com.fsck.k9.mailstore.MessageViewInfoExtractor.ViewableExtractedText in project k-9 by k9mail.

the class MessageViewInfoExtractor method extractMessageForView.

@WorkerThread
public MessageViewInfo extractMessageForView(Message message, @Nullable MessageCryptoAnnotations annotations) throws MessagingException {
    Part rootPart;
    CryptoResultAnnotation cryptoResultAnnotation;
    List<Part> extraParts;
    CryptoMessageParts cryptoMessageParts = MessageCryptoSplitter.split(message, annotations);
    if (cryptoMessageParts != null) {
        rootPart = cryptoMessageParts.contentPart;
        cryptoResultAnnotation = cryptoMessageParts.contentCryptoAnnotation;
        extraParts = cryptoMessageParts.extraParts;
    } else {
        if (annotations != null && !annotations.isEmpty()) {
            Timber.e("Got message annotations but no crypto root part!");
        }
        rootPart = message;
        cryptoResultAnnotation = null;
        extraParts = null;
    }
    List<AttachmentViewInfo> attachmentInfos = new ArrayList<>();
    ViewableExtractedText viewable = extractViewableAndAttachments(Collections.singletonList(rootPart), attachmentInfos);
    List<AttachmentViewInfo> extraAttachmentInfos = new ArrayList<>();
    String extraViewableText = null;
    if (extraParts != null) {
        ViewableExtractedText extraViewable = extractViewableAndAttachments(extraParts, extraAttachmentInfos);
        extraViewableText = extraViewable.text;
    }
    AttachmentResolver attachmentResolver = AttachmentResolver.createFromPart(rootPart);
    boolean isMessageIncomplete = !message.isSet(Flag.X_DOWNLOADED_FULL) || MessageExtractor.hasMissingParts(message);
    return MessageViewInfo.createWithExtractedContent(message, isMessageIncomplete, rootPart, viewable.html, attachmentInfos, cryptoResultAnnotation, attachmentResolver, extraViewableText, extraAttachmentInfos);
}
Also used : CryptoMessageParts(com.fsck.k9.ui.crypto.MessageCryptoSplitter.CryptoMessageParts) Part(com.fsck.k9.mail.Part) ArrayList(java.util.ArrayList) WorkerThread(android.support.annotation.WorkerThread)

Example 2 with ViewableExtractedText

use of com.fsck.k9.mailstore.MessageViewInfoExtractor.ViewableExtractedText in project k-9 by k9mail.

the class MessageViewInfoExtractorTest method testMultipartDigestWithMessages.

@Test
public void testMultipartDigestWithMessages() throws Exception {
    String data = "Content-Type: multipart/digest; boundary=\"bndry\"\r\n" + "\r\n" + "--bndry\r\n" + "\r\n" + "Content-Type: text/plain\r\n" + "\r\n" + "text body of first message\r\n" + "\r\n" + "--bndry\r\n" + "\r\n" + "Subject: subject of second message\r\n" + "Content-Type: multipart/alternative; boundary=\"bndry2\"\r\n" + "\r\n" + "--bndry2\r\n" + "Content-Type: text/plain\r\n" + "\r\n" + "text part of second message\r\n" + "\r\n" + "--bndry2\r\n" + "Content-Type: text/html\"\r\n" + "\r\n" + "html part of second message\r\n" + "\r\n" + "--bndry2--\r\n" + "\r\n" + "--bndry--\r\n";
    MimeMessage message = MimeMessage.parseMimeMessage(new ByteArrayInputStream(data.getBytes()), false);
    // Extract text
    List<Part> outputNonViewableParts = new ArrayList<>();
    ArrayList<Viewable> outputViewableParts = new ArrayList<>();
    MessageExtractor.findViewablesAndAttachments(message, outputViewableParts, outputNonViewableParts);
    String expectedExtractedText = "Subject: (No subject)\r\n" + "\r\n" + "text body of first message\r\n" + "\r\n" + "\r\n" + "------------------------------------------------------------------------\r\n" + "\r\n" + "Subject: subject of second message\r\n" + "\r\n" + "text part of second message\r\n";
    String expectedHtmlText = "<table style=\"border: 0\">" + "<tr><th style=\"text-align: left; vertical-align: top;\">Subject:</th><td>(No subject)</td></tr>" + "</table>" + "<pre dir=\"auto\" class=\"k9mail\">text body of first message<br></pre>" + "<p style=\"margin-top: 2.5em; margin-bottom: 1em; border-bottom: 1px solid #000\"></p>" + "<table style=\"border: 0\">" + "<tr><th style=\"text-align: left; vertical-align: top;\">Subject:</th><td>subject of second message</td></tr>" + "</table>" + "<pre dir=\"auto\" class=\"k9mail\">text part of second message<br></pre>";
    assertEquals(4, outputViewableParts.size());
    assertEquals("subject of second message", ((MessageHeader) outputViewableParts.get(2)).getMessage().getSubject());
    ViewableExtractedText firstMessageExtractedText = messageViewInfoExtractor.extractTextFromViewables(outputViewableParts);
    assertEquals(expectedExtractedText, firstMessageExtractedText.text);
    assertEquals(expectedHtmlText, firstMessageExtractedText.html);
}
Also used : MimeMessage(com.fsck.k9.mail.internet.MimeMessage) ByteArrayInputStream(java.io.ByteArrayInputStream) MimeBodyPart(com.fsck.k9.mail.internet.MimeBodyPart) BodyPart(com.fsck.k9.mail.BodyPart) Part(com.fsck.k9.mail.Part) ArrayList(java.util.ArrayList) Viewable(com.fsck.k9.mail.internet.Viewable) ViewableExtractedText(com.fsck.k9.mailstore.MessageViewInfoExtractor.ViewableExtractedText) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) MessageHeader(com.fsck.k9.mail.internet.Viewable.MessageHeader) K9RobolectricTest(com.fsck.k9.K9RobolectricTest) Test(org.junit.Test)

Example 3 with ViewableExtractedText

use of com.fsck.k9.mailstore.MessageViewInfoExtractor.ViewableExtractedText in project k-9 by k9mail.

the class MessageViewInfoExtractor method extractTextFromViewables.

/**
 * Extract the viewable textual parts of a message and return the rest as attachments.
 *
 * @return A {@link ViewableExtractedText} instance containing the textual parts of the message as
 *         plain text and HTML, and a list of message parts considered attachments.
 *
 * @throws com.fsck.k9.mail.MessagingException
 *          In case of an error.
 */
@VisibleForTesting
ViewableExtractedText extractTextFromViewables(List<Viewable> viewables) throws MessagingException {
    try {
        // Collect all viewable parts
        /*
             * Convert the tree of viewable parts into text and HTML
             */
        // Used to suppress the divider for the first viewable part
        boolean hideDivider = true;
        StringBuilder text = new StringBuilder();
        StringBuilder html = new StringBuilder();
        for (Viewable viewable : viewables) {
            if (viewable instanceof Textual) {
                // This is either a text/plain or text/html part. Fill the variables 'text' and
                // 'html', converting between plain text and HTML as necessary.
                text.append(buildText(viewable, !hideDivider));
                html.append(buildHtml(viewable, !hideDivider));
                hideDivider = false;
            } else if (viewable instanceof MessageHeader) {
                MessageHeader header = (MessageHeader) viewable;
                Part containerPart = header.getContainerPart();
                Message innerMessage = header.getMessage();
                addTextDivider(text, containerPart, !hideDivider);
                addMessageHeaderText(text, innerMessage);
                addHtmlDivider(html, containerPart, !hideDivider);
                addMessageHeaderHtml(html, innerMessage);
                hideDivider = true;
            } else if (viewable instanceof Alternative) {
                // Handle multipart/alternative contents
                Alternative alternative = (Alternative) viewable;
                /*
                     * We made sure at least one of text/plain or text/html is present when
                     * creating the Alternative object. If one part is not present we convert the
                     * other one to make sure 'text' and 'html' always contain the same text.
                     */
                List<Viewable> textAlternative = alternative.getText().isEmpty() ? alternative.getHtml() : alternative.getText();
                List<Viewable> htmlAlternative = alternative.getHtml().isEmpty() ? alternative.getText() : alternative.getHtml();
                // Fill the 'text' variable
                boolean divider = !hideDivider;
                for (Viewable textViewable : textAlternative) {
                    text.append(buildText(textViewable, divider));
                    divider = true;
                }
                // Fill the 'html' variable
                divider = !hideDivider;
                for (Viewable htmlViewable : htmlAlternative) {
                    html.append(buildHtml(htmlViewable, divider));
                    divider = true;
                }
                hideDivider = false;
            }
        }
        String sanitizedHtml = htmlProcessor.processForDisplay(html.toString());
        return new ViewableExtractedText(text.toString(), sanitizedHtml);
    } catch (Exception e) {
        throw new MessagingException("Couldn't extract viewable parts", e);
    }
}
Also used : Alternative(com.fsck.k9.mail.internet.Viewable.Alternative) Message(com.fsck.k9.mail.Message) Textual(com.fsck.k9.mail.internet.Viewable.Textual) MessagingException(com.fsck.k9.mail.MessagingException) MessagingException(com.fsck.k9.mail.MessagingException) Part(com.fsck.k9.mail.Part) Viewable(com.fsck.k9.mail.internet.Viewable) MessageHeader(com.fsck.k9.mail.internet.Viewable.MessageHeader) VisibleForTesting(androidx.annotation.VisibleForTesting)

Example 4 with ViewableExtractedText

use of com.fsck.k9.mailstore.MessageViewInfoExtractor.ViewableExtractedText in project k-9 by k9mail.

the class MessageViewInfoExtractor method extractViewableAndAttachments.

private ViewableExtractedText extractViewableAndAttachments(List<Part> parts, List<AttachmentViewInfo> attachmentInfos) throws MessagingException {
    ArrayList<Viewable> viewableParts = new ArrayList<>();
    ArrayList<Part> attachments = new ArrayList<>();
    for (Part part : parts) {
        MessageExtractor.findViewablesAndAttachments(part, viewableParts, attachments);
    }
    attachmentInfos.addAll(attachmentInfoExtractor.extractAttachmentInfoForView(attachments));
    return extractTextFromViewables(viewableParts);
}
Also used : Part(com.fsck.k9.mail.Part) Viewable(com.fsck.k9.mail.internet.Viewable) ArrayList(java.util.ArrayList)

Example 5 with ViewableExtractedText

use of com.fsck.k9.mailstore.MessageViewInfoExtractor.ViewableExtractedText in project k-9 by k9mail.

the class MessageViewInfoExtractorTest method testTextPlainFormatFlowed.

@Test
public void testTextPlainFormatFlowed() throws MessagingException {
    // Create text/plain body
    Body body = new BinaryMemoryBody(BODY_TEXT_FLOWED.getBytes(StandardCharsets.UTF_8), "utf-8");
    // 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 dir=\"auto\" class=\"k9mail\">" + "K-9 Mail rocks :&gt; flowed line<br>not flowed line" + "</pre>";
    assertEquals(expectedText, container.text);
    assertEquals(expectedHtml, container.html);
}
Also used : MimeMessage(com.fsck.k9.mail.internet.MimeMessage) MimeBodyPart(com.fsck.k9.mail.internet.MimeBodyPart) BodyPart(com.fsck.k9.mail.BodyPart) Part(com.fsck.k9.mail.Part) ArrayList(java.util.ArrayList) Viewable(com.fsck.k9.mail.internet.Viewable) ViewableExtractedText(com.fsck.k9.mailstore.MessageViewInfoExtractor.ViewableExtractedText) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) TextBody(com.fsck.k9.mail.internet.TextBody) Body(com.fsck.k9.mail.Body) TestMessageConstructionUtils.messageFromBody(com.fsck.k9.mail.TestMessageConstructionUtils.messageFromBody) K9RobolectricTest(com.fsck.k9.K9RobolectricTest) Test(org.junit.Test)

Aggregations

Part (com.fsck.k9.mail.Part)9 Viewable (com.fsck.k9.mail.internet.Viewable)9 ArrayList (java.util.ArrayList)9 K9RobolectricTest (com.fsck.k9.K9RobolectricTest)7 MimeMessage (com.fsck.k9.mail.internet.MimeMessage)7 ViewableExtractedText (com.fsck.k9.mailstore.MessageViewInfoExtractor.ViewableExtractedText)7 Test (org.junit.Test)7 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)7 BodyPart (com.fsck.k9.mail.BodyPart)6 MimeBodyPart (com.fsck.k9.mail.internet.MimeBodyPart)6 TextBody (com.fsck.k9.mail.internet.TextBody)6 MimeMultipart (com.fsck.k9.mail.internet.MimeMultipart)2 MessageHeader (com.fsck.k9.mail.internet.Viewable.MessageHeader)2 WorkerThread (android.support.annotation.WorkerThread)1 VisibleForTesting (androidx.annotation.VisibleForTesting)1 HtmlProcessor (app.k9mail.html.cleaner.HtmlProcessor)1 TestCoreResourceProvider (com.fsck.k9.TestCoreResourceProvider)1 Address (com.fsck.k9.mail.Address)1 Body (com.fsck.k9.mail.Body)1 Message (com.fsck.k9.mail.Message)1