Search in sources :

Example 1 with Flowed

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

the class MessageViewInfoExtractor method buildText.

private StringBuilder buildText(Viewable viewable, boolean prependDivider) {
    StringBuilder text = new StringBuilder();
    if (viewable instanceof Textual) {
        Part part = ((Textual) viewable).getPart();
        addTextDivider(text, part, prependDivider);
        String t = MessageExtractor.getTextFromPart(part);
        if (t == null) {
            t = "";
        } else if (viewable instanceof Html) {
            t = HtmlConverter.htmlToText(t);
        } else if (viewable instanceof Flowed) {
            t = FlowedMessageUtils.deflow(t, false);
        } else if (!(viewable instanceof Text)) {
            throw new IllegalStateException("unhandled case!");
        }
        text.append(t);
    } else if (viewable instanceof Alternative) {
        // That's odd - an Alternative as child of an Alternative; go ahead and try to use the
        // text/plain child; fall-back to the text/html part.
        Alternative alternative = (Alternative) viewable;
        List<Viewable> textAlternative = alternative.getText().isEmpty() ? alternative.getHtml() : alternative.getText();
        boolean divider = prependDivider;
        for (Viewable textViewable : textAlternative) {
            text.append(buildText(textViewable, divider));
            divider = true;
        }
    }
    return text;
}
Also used : Alternative(com.fsck.k9.mail.internet.Viewable.Alternative) Flowed(com.fsck.k9.mail.internet.Viewable.Flowed) Textual(com.fsck.k9.mail.internet.Viewable.Textual) Part(com.fsck.k9.mail.Part) Viewable(com.fsck.k9.mail.internet.Viewable) Html(com.fsck.k9.mail.internet.Viewable.Html) Text(com.fsck.k9.mail.internet.Viewable.Text) ArrayList(java.util.ArrayList) List(java.util.List)

Example 2 with Flowed

use of com.fsck.k9.mail.internet.Viewable.Flowed 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)

Example 3 with Flowed

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

the class MessageViewInfoExtractorTest method testShouldSanitizeOutputHtml.

@Test
public void testShouldSanitizeOutputHtml() throws MessagingException {
    // Create text/plain body
    TextBody body = new TextBody(BODY_TEXT);
    // Create message
    MimeMessage message = new MimeMessage();
    MimeMessageHelper.setBody(message, body);
    message.setHeader(MimeHeader.HEADER_CONTENT_TYPE, "text/plain; format=flowed");
    // Prepare fixture
    HtmlSanitizer htmlSanitizer = mock(HtmlSanitizer.class);
    MessageViewInfoExtractor messageViewInfoExtractor = new MessageViewInfoExtractor(context, null, htmlSanitizer);
    String value = "--sanitized html--";
    when(htmlSanitizer.sanitize(any(String.class))).thenReturn(value);
    // Extract text
    List<Part> outputNonViewableParts = new ArrayList<>();
    ArrayList<Viewable> outputViewableParts = new ArrayList<>();
    MessageExtractor.findViewablesAndAttachments(message, outputViewableParts, outputNonViewableParts);
    ViewableExtractedText viewableExtractedText = messageViewInfoExtractor.extractTextFromViewables(outputViewableParts);
    assertSame(value, viewableExtractedText.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) HtmlSanitizer(com.fsck.k9.message.html.HtmlSanitizer) Test(org.junit.Test)

Example 4 with Flowed

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

the class MessageExtractor method findViewablesAndAttachments.

/** Traverse the MIME tree of a message and extract viewable parts. */
public static void findViewablesAndAttachments(Part part, @Nullable List<Viewable> outputViewableParts, @Nullable List<Part> outputNonViewableParts) throws MessagingException {
    boolean skipSavingNonViewableParts = outputNonViewableParts == null;
    boolean skipSavingViewableParts = outputViewableParts == null;
    if (skipSavingNonViewableParts && skipSavingViewableParts) {
        throw new IllegalArgumentException("method was called but no output is to be collected - this a bug!");
    }
    Body body = part.getBody();
    if (body instanceof Multipart) {
        Multipart multipart = (Multipart) body;
        if (isSameMimeType(part.getMimeType(), "multipart/alternative")) {
            /*
                 * For multipart/alternative parts we try to find a text/plain and a text/html
                 * child. Everything else we find is put into 'attachments'.
                 */
            List<Viewable> text = findTextPart(multipart, true);
            Set<Part> knownTextParts = getParts(text);
            List<Viewable> html = findHtmlPart(multipart, knownTextParts, outputNonViewableParts, true);
            if (skipSavingViewableParts) {
                return;
            }
            if (!text.isEmpty() || !html.isEmpty()) {
                Alternative alternative = new Alternative(text, html);
                outputViewableParts.add(alternative);
            }
        } else {
            // For all other multipart parts we recurse to grab all viewable children.
            for (Part bodyPart : multipart.getBodyParts()) {
                findViewablesAndAttachments(bodyPart, outputViewableParts, outputNonViewableParts);
            }
        }
    } else if (body instanceof Message && !("attachment".equalsIgnoreCase(getContentDisposition(part)))) {
        if (skipSavingViewableParts) {
            return;
        }
        /*
             * We only care about message/rfc822 parts whose Content-Disposition header has a value
             * other than "attachment".
             */
        Message message = (Message) body;
        // We add the Message object so we can extract the filename later.
        outputViewableParts.add(new MessageHeader(part, message));
        // Recurse to grab all viewable parts and attachments from that message.
        findViewablesAndAttachments(message, outputViewableParts, outputNonViewableParts);
    } else if (isPartTextualBody(part)) {
        if (skipSavingViewableParts) {
            return;
        }
        String mimeType = part.getMimeType();
        Viewable viewable;
        if (isSameMimeType(mimeType, "text/plain")) {
            if (isFormatFlowed(part.getContentType())) {
                viewable = new Flowed(part);
            } else {
                viewable = new Text(part);
            }
        } else {
            viewable = new Html(part);
        }
        outputViewableParts.add(viewable);
    } else if (isSameMimeType(part.getMimeType(), "application/pgp-signature")) {
    // ignore this type explicitly
    } else {
        if (skipSavingNonViewableParts) {
            return;
        }
        // Everything else is treated as attachment.
        outputNonViewableParts.add(part);
    }
}
Also used : Multipart(com.fsck.k9.mail.Multipart) Alternative(com.fsck.k9.mail.internet.Viewable.Alternative) Message(com.fsck.k9.mail.Message) Html(com.fsck.k9.mail.internet.Viewable.Html) Text(com.fsck.k9.mail.internet.Viewable.Text) Flowed(com.fsck.k9.mail.internet.Viewable.Flowed) MimeUtility.isFormatFlowed(com.fsck.k9.mail.internet.MimeUtility.isFormatFlowed) Part(com.fsck.k9.mail.Part) BodyPart(com.fsck.k9.mail.BodyPart) MessageHeader(com.fsck.k9.mail.internet.Viewable.MessageHeader) Body(com.fsck.k9.mail.Body)

Example 5 with Flowed

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

the class MessageViewInfoExtractor method buildHtml.

/**
     * Use the contents of a {@link com.fsck.k9.mail.internet.Viewable} to create the HTML to be displayed.
     *
     * <p>
     * This will use {@link HtmlConverter#textToHtml(String)} to convert plain text parts
     * to HTML if necessary.
     * </p>
     *
     * @param viewable
     *         The viewable part to build the HTML from.
     * @param prependDivider
     *         {@code true}, if the HTML divider should be inserted as first element.
     *         {@code false}, otherwise.
     *
     * @return The contents of the supplied viewable instance as HTML.
     */
private StringBuilder buildHtml(Viewable viewable, boolean prependDivider) {
    StringBuilder html = new StringBuilder();
    if (viewable instanceof Textual) {
        Part part = ((Textual) viewable).getPart();
        addHtmlDivider(html, part, prependDivider);
        String t = MessageExtractor.getTextFromPart(part);
        if (t == null) {
            t = "";
        } else if (viewable instanceof Flowed) {
            t = FlowedMessageUtils.deflow(t, false);
            t = HtmlConverter.textToHtml(t);
        } else if (viewable instanceof Text) {
            t = HtmlConverter.textToHtml(t);
        } else if (!(viewable instanceof Html)) {
            throw new IllegalStateException("unhandled case!");
        }
        html.append(t);
    } else if (viewable instanceof Alternative) {
        // That's odd - an Alternative as child of an Alternative; go ahead and try to use the
        // text/html child; fall-back to the text/plain part.
        Alternative alternative = (Alternative) viewable;
        List<Viewable> htmlAlternative = alternative.getHtml().isEmpty() ? alternative.getText() : alternative.getHtml();
        boolean divider = prependDivider;
        for (Viewable htmlViewable : htmlAlternative) {
            html.append(buildHtml(htmlViewable, divider));
            divider = true;
        }
    }
    return html;
}
Also used : Alternative(com.fsck.k9.mail.internet.Viewable.Alternative) Flowed(com.fsck.k9.mail.internet.Viewable.Flowed) Textual(com.fsck.k9.mail.internet.Viewable.Textual) Part(com.fsck.k9.mail.Part) Viewable(com.fsck.k9.mail.internet.Viewable) Html(com.fsck.k9.mail.internet.Viewable.Html) Text(com.fsck.k9.mail.internet.Viewable.Text) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

Part (com.fsck.k9.mail.Part)5 Viewable (com.fsck.k9.mail.internet.Viewable)4 ArrayList (java.util.ArrayList)4 Alternative (com.fsck.k9.mail.internet.Viewable.Alternative)3 Flowed (com.fsck.k9.mail.internet.Viewable.Flowed)3 Html (com.fsck.k9.mail.internet.Viewable.Html)3 Text (com.fsck.k9.mail.internet.Viewable.Text)3 MimeBodyPart (com.fsck.k9.mail.internet.MimeBodyPart)2 MimeMessage (com.fsck.k9.mail.internet.MimeMessage)2 TextBody (com.fsck.k9.mail.internet.TextBody)2 Textual (com.fsck.k9.mail.internet.Viewable.Textual)2 ViewableExtractedText (com.fsck.k9.mailstore.MessageViewInfoExtractor.ViewableExtractedText)2 List (java.util.List)2 Test (org.junit.Test)2 Body (com.fsck.k9.mail.Body)1 BodyPart (com.fsck.k9.mail.BodyPart)1 Message (com.fsck.k9.mail.Message)1 Multipart (com.fsck.k9.mail.Multipart)1 MimeUtility.isFormatFlowed (com.fsck.k9.mail.internet.MimeUtility.isFormatFlowed)1 MessageHeader (com.fsck.k9.mail.internet.Viewable.MessageHeader)1