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);
}
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);
}
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);
}
}
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);
}
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 :> flowed line<br>not flowed line" + "</pre>";
assertEquals(expectedText, container.text);
assertEquals(expectedHtml, container.html);
}
Aggregations