Search in sources :

Example 86 with Text

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

the class ImapFolder method handleFetchResponse.

// Returns value of body field
private Object handleFetchResponse(ImapMessage message, ImapList fetchList) throws MessagingException {
    Object result = null;
    if (fetchList.containsKey("FLAGS")) {
        ImapList flags = fetchList.getKeyedList("FLAGS");
        if (flags != null) {
            for (int i = 0, count = flags.size(); i < count; i++) {
                String flag = flags.getString(i);
                if (flag.equalsIgnoreCase("\\Deleted")) {
                    message.setFlagInternal(Flag.DELETED, true);
                } else if (flag.equalsIgnoreCase("\\Answered")) {
                    message.setFlagInternal(Flag.ANSWERED, true);
                } else if (flag.equalsIgnoreCase("\\Seen")) {
                    message.setFlagInternal(Flag.SEEN, true);
                } else if (flag.equalsIgnoreCase("\\Flagged")) {
                    message.setFlagInternal(Flag.FLAGGED, true);
                } else if (flag.equalsIgnoreCase("$Forwarded")) {
                    message.setFlagInternal(Flag.FORWARDED, true);
                    /* a message contains FORWARDED FLAG -> so we can also create them */
                    store.getPermanentFlagsIndex().add(Flag.FORWARDED);
                }
            }
        }
    }
    if (fetchList.containsKey("INTERNALDATE")) {
        Date internalDate = fetchList.getKeyedDate("INTERNALDATE");
        message.setInternalDate(internalDate);
    }
    if (fetchList.containsKey("RFC822.SIZE")) {
        int size = fetchList.getKeyedNumber("RFC822.SIZE");
        message.setSize(size);
    }
    if (fetchList.containsKey("BODYSTRUCTURE")) {
        ImapList bs = fetchList.getKeyedList("BODYSTRUCTURE");
        if (bs != null) {
            try {
                parseBodyStructure(bs, message, "TEXT");
            } catch (MessagingException e) {
                if (K9MailLib.isDebug()) {
                    Log.d(LOG_TAG, "Error handling message for " + getLogId(), e);
                }
                message.setBody(null);
            }
        }
    }
    if (fetchList.containsKey("BODY")) {
        int index = fetchList.getKeyIndex("BODY") + 2;
        int size = fetchList.size();
        if (index < size) {
            result = fetchList.getObject(index);
            // Check if there's an origin octet
            if (result instanceof String) {
                String originOctet = (String) result;
                if (originOctet.startsWith("<") && (index + 1) < size) {
                    result = fetchList.getObject(index + 1);
                }
            }
        }
    }
    return result;
}
Also used : MessagingException(com.fsck.k9.mail.MessagingException) Date(java.util.Date)

Example 87 with Text

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

the class MessageExtractor method findTextPart.

/**
     * Search the children of a {@link Multipart} for {@code text/plain} parts.
     *
     * @param multipart The {@code Multipart} to search through.
     * @param directChild If {@code true}, this method will return after the first {@code text/plain} was
     *         found.
     *
     * @return A list of {@link Text} viewables.
     *
     * @throws MessagingException
     *          In case of an error.
     */
private static List<Viewable> findTextPart(Multipart multipart, boolean directChild) throws MessagingException {
    List<Viewable> viewables = new ArrayList<Viewable>();
    for (Part part : multipart.getBodyParts()) {
        Body body = part.getBody();
        if (body instanceof Multipart) {
            Multipart innerMultipart = (Multipart) body;
            /*
                 * Recurse to find text parts. Since this is a multipart that is a child of a
                 * multipart/alternative we don't want to stop after the first text/plain part
                 * we find. This will allow to get all text parts for constructions like this:
                 *
                 * 1. multipart/alternative
                 * 1.1. multipart/mixed
                 * 1.1.1. text/plain
                 * 1.1.2. text/plain
                 * 1.2. text/html
                 */
            List<Viewable> textViewables = findTextPart(innerMultipart, false);
            if (!textViewables.isEmpty()) {
                viewables.addAll(textViewables);
                if (directChild) {
                    break;
                }
            }
        } else if (isPartTextualBody(part) && isSameMimeType(part.getMimeType(), "text/plain")) {
            Text text = new Text(part);
            viewables.add(text);
            if (directChild) {
                break;
            }
        }
    }
    return viewables;
}
Also used : Multipart(com.fsck.k9.mail.Multipart) Part(com.fsck.k9.mail.Part) BodyPart(com.fsck.k9.mail.BodyPart) ArrayList(java.util.ArrayList) Text(com.fsck.k9.mail.internet.Viewable.Text) Body(com.fsck.k9.mail.Body)

Example 88 with Text

use of com.fsck.k9.mail.internet.Viewable.Text 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 89 with Text

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

the class MessageExtractor method findHtmlPart.

/**
     * Search the children of a {@link Multipart} for {@code text/html} parts.
     * Every part that is not a {@code text/html} we want to display, we add to 'attachments'.
     *
     * @param multipart The {@code Multipart} to search through.
     * @param knownTextParts A set of {@code text/plain} parts that shouldn't be added to 'attachments'.
     * @param outputNonViewableParts A list that will receive the parts that are considered attachments.
     * @param directChild If {@code true}, this method will add all {@code text/html} parts except the first
     *         found to 'attachments'.
     *
     * @return A list of {@link Text} viewables.
     *
     * @throws MessagingException In case of an error.
     */
private static List<Viewable> findHtmlPart(Multipart multipart, Set<Part> knownTextParts, @Nullable List<Part> outputNonViewableParts, boolean directChild) throws MessagingException {
    boolean saveNonViewableParts = outputNonViewableParts != null;
    List<Viewable> viewables = new ArrayList<>();
    boolean partFound = false;
    for (Part part : multipart.getBodyParts()) {
        Body body = part.getBody();
        if (body instanceof Multipart) {
            Multipart innerMultipart = (Multipart) body;
            if (directChild && partFound) {
                if (saveNonViewableParts) {
                    // We already found our text/html part. Now we're only looking for attachments.
                    findAttachments(innerMultipart, knownTextParts, outputNonViewableParts);
                }
            } else {
                /*
                     * Recurse to find HTML parts. Since this is a multipart that is a child of a
                     * multipart/alternative we don't want to stop after the first text/html part
                     * we find. This will allow to get all text parts for constructions like this:
                     *
                     * 1. multipart/alternative
                     * 1.1. text/plain
                     * 1.2. multipart/mixed
                     * 1.2.1. text/html
                     * 1.2.2. text/html
                     * 1.3. image/jpeg
                     */
                List<Viewable> htmlViewables = findHtmlPart(innerMultipart, knownTextParts, outputNonViewableParts, false);
                if (!htmlViewables.isEmpty()) {
                    partFound = true;
                    viewables.addAll(htmlViewables);
                }
            }
        } else if (!(directChild && partFound) && isPartTextualBody(part) && isSameMimeType(part.getMimeType(), "text/html")) {
            Html html = new Html(part);
            viewables.add(html);
            partFound = true;
        } else if (!knownTextParts.contains(part)) {
            if (saveNonViewableParts) {
                // Only add this part as attachment if it's not a viewable text/plain part found earlier
                outputNonViewableParts.add(part);
            }
        }
    }
    return viewables;
}
Also used : Multipart(com.fsck.k9.mail.Multipart) Part(com.fsck.k9.mail.Part) BodyPart(com.fsck.k9.mail.BodyPart) ArrayList(java.util.ArrayList) Html(com.fsck.k9.mail.internet.Viewable.Html) Body(com.fsck.k9.mail.Body)

Example 90 with Text

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

the class MessageExtractor method getTextFromPart.

public static String getTextFromPart(Part part, long textSizeLimit) {
    try {
        if ((part != null) && (part.getBody() != null)) {
            final Body body = part.getBody();
            if (body instanceof TextBody) {
                return ((TextBody) body).getRawText();
            }
            final String mimeType = part.getMimeType();
            if (mimeType != null && MimeUtility.mimeTypeMatches(mimeType, "text/*") || part.isMimeType("application/pgp")) {
                return getTextFromTextPart(part, body, mimeType, textSizeLimit);
            } else {
                throw new MessagingException("Provided non-text part: " + mimeType);
            }
        } else {
            throw new MessagingException("Provided invalid part");
        }
    } catch (IOException e) {
        Log.e(LOG_TAG, "Unable to getTextFromPart", e);
    } catch (MessagingException e) {
        Log.e(LOG_TAG, "Unable to getTextFromPart", e);
    }
    return null;
}
Also used : MessagingException(com.fsck.k9.mail.MessagingException) IOException(java.io.IOException) Body(com.fsck.k9.mail.Body)

Aggregations

Test (org.junit.Test)74 Part (com.fsck.k9.mail.Part)64 MimeBodyPart (com.fsck.k9.mail.internet.MimeBodyPart)37 BodyPart (com.fsck.k9.mail.BodyPart)34 MimeMessage (com.fsck.k9.mail.internet.MimeMessage)33 MessageCreationHelper.createTextPart (com.fsck.k9.message.MessageCreationHelper.createTextPart)27 Message (com.fsck.k9.mail.Message)22 TextBody (com.fsck.k9.mail.internet.TextBody)18 MessageCreationHelper.createEmptyPart (com.fsck.k9.message.MessageCreationHelper.createEmptyPart)16 ArrayList (java.util.ArrayList)16 Body (com.fsck.k9.mail.Body)13 MessagingException (com.fsck.k9.mail.MessagingException)13 MessageCreationHelper.createPart (com.fsck.k9.message.MessageCreationHelper.createPart)13 Multipart (com.fsck.k9.mail.Multipart)10 Viewable (com.fsck.k9.mail.internet.Viewable)10 ByteArrayOutputStream (java.io.ByteArrayOutputStream)9 MimeMultipart (com.fsck.k9.mail.internet.MimeMultipart)8 Intent (android.content.Intent)6 BinaryMemoryBody (com.fsck.k9.mailstore.BinaryMemoryBody)6 ViewableExtractedText (com.fsck.k9.mailstore.MessageViewInfoExtractor.ViewableExtractedText)6