Search in sources :

Example 16 with MimeType

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

the class TextPartFinder method findTextPartInMultipartAlternative.

private Part findTextPartInMultipartAlternative(Multipart multipart) {
    Part htmlPart = null;
    for (BodyPart bodyPart : multipart.getBodyParts()) {
        String mimeType = bodyPart.getMimeType();
        Body body = bodyPart.getBody();
        if (body instanceof Multipart) {
            Part candidatePart = findFirstTextPart(bodyPart);
            if (candidatePart != null) {
                if (isSameMimeType(candidatePart.getMimeType(), "text/html")) {
                    htmlPart = candidatePart;
                } else {
                    return candidatePart;
                }
            }
        } else if (isSameMimeType(mimeType, "text/plain")) {
            return bodyPart;
        } else if (isSameMimeType(mimeType, "text/html") && htmlPart == null) {
            htmlPart = bodyPart;
        }
    }
    if (htmlPart != null) {
        return htmlPart;
    }
    return null;
}
Also used : BodyPart(com.fsck.k9.mail.BodyPart) Multipart(com.fsck.k9.mail.Multipart) BodyPart(com.fsck.k9.mail.BodyPart) Part(com.fsck.k9.mail.Part) Body(com.fsck.k9.mail.Body)

Example 17 with MimeType

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

the class MessageCreationHelper method createMessage.

private static Message createMessage(String mimeType, Body body) {
    MimeMessage message = new MimeMessage();
    message.setBody(body);
    message.setHeader(MimeHeader.HEADER_CONTENT_TYPE, mimeType);
    return message;
}
Also used : MimeMessage(com.fsck.k9.mail.internet.MimeMessage)

Example 18 with MimeType

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

the class AttachmentInfoExtractorTest method extractInfo__withDeferredFileBody.

@Test
public void extractInfo__withDeferredFileBody() throws Exception {
    attachmentInfoExtractor = new AttachmentInfoExtractor(context) {

        @Nullable
        @Override
        protected Uri getDecryptedFileProviderUri(DeferredFileBody decryptedTempFileBody, String mimeType) {
            return TEST_URI;
        }
    };
    DeferredFileBody body = mock(DeferredFileBody.class);
    when(body.getSize()).thenReturn(TEST_SIZE);
    MimeBodyPart part = new MimeBodyPart();
    part.setBody(body);
    part.setHeader(MimeHeader.HEADER_CONTENT_TYPE, TEST_MIME_TYPE);
    AttachmentViewInfo attachmentViewInfo = attachmentInfoExtractor.extractAttachmentInfo(part);
    assertEquals(TEST_URI, attachmentViewInfo.internalUri);
    assertEquals(TEST_SIZE, attachmentViewInfo.size);
    assertEquals(TEST_MIME_TYPE, attachmentViewInfo.mimeType);
    assertFalse(attachmentViewInfo.inlineAttachment);
    assertTrue(attachmentViewInfo.isContentAvailable());
}
Also used : MimeBodyPart(com.fsck.k9.mail.internet.MimeBodyPart) Uri(android.net.Uri) Nullable(androidx.annotation.Nullable) DeferredFileBody(com.fsck.k9.mailstore.DeferredFileBody) AttachmentViewInfo(com.fsck.k9.mailstore.AttachmentViewInfo) RobolectricTest(com.fsck.k9.RobolectricTest) Test(org.junit.Test)

Example 19 with MimeType

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

the class EncryptionDetector method containsPartWithMimeType.

private boolean containsPartWithMimeType(Part part, String... wantedMimeTypes) {
    String mimeType = part.getMimeType();
    if (isMimeTypeAnyOf(mimeType, wantedMimeTypes)) {
        return true;
    }
    Body body = part.getBody();
    if (body instanceof Multipart) {
        Multipart multipart = (Multipart) body;
        for (BodyPart bodyPart : multipart.getBodyParts()) {
            if (containsPartWithMimeType(bodyPart, wantedMimeTypes)) {
                return true;
            }
        }
    }
    return false;
}
Also used : BodyPart(com.fsck.k9.mail.BodyPart) Multipart(com.fsck.k9.mail.Multipart) Body(com.fsck.k9.mail.Body)

Example 20 with MimeType

use of com.fsck.k9.mail.MimeType 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 if (isSameMimeType(part.getMimeType(), "multipart/signed")) {
            if (multipart.getCount() > 0) {
                BodyPart bodyPart = multipart.getBodyPart(0);
                findViewablesAndAttachments(bodyPart, outputViewableParts, outputNonViewableParts);
            }
        } 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")) {
            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 (isSameMimeType(part.getMimeType(), "text/rfc822-headers")) {
    // ignore this type explicitly
    } else {
        if (skipSavingNonViewableParts) {
            return;
        }
        // Everything else is treated as attachment.
        outputNonViewableParts.add(part);
    }
}
Also used : BodyPart(com.fsck.k9.mail.BodyPart) 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) 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)

Aggregations

Body (com.fsck.k9.mail.Body)8 Multipart (com.fsck.k9.mail.Multipart)8 BodyPart (com.fsck.k9.mail.BodyPart)6 Part (com.fsck.k9.mail.Part)6 MessagingException (com.fsck.k9.mail.MessagingException)4 IOException (java.io.IOException)4 Message (com.fsck.k9.mail.Message)3 MimeType (com.fsck.k9.mail.MimeType)3 File (java.io.File)3 ContentValues (android.content.ContentValues)2 Uri (android.net.Uri)2 Nullable (androidx.annotation.Nullable)2 Account (com.fsck.k9.Account)2 MimeBodyPart (com.fsck.k9.mail.internet.MimeBodyPart)2 MimeHeader (com.fsck.k9.mail.internet.MimeHeader)2 MimeMessage (com.fsck.k9.mail.internet.MimeMessage)2 MimeMultipart (com.fsck.k9.mail.internet.MimeMultipart)2 Cursor (android.database.Cursor)1 RobolectricTest (com.fsck.k9.RobolectricTest)1 Address (com.fsck.k9.mail.Address)1