Search in sources :

Example 86 with Body

use of com.fsck.k9.mail.Body 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 87 with Body

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

the class MessageCryptoHelper method getDataSourceForEncryptedOrInlineData.

private OpenPgpDataSource getDataSourceForEncryptedOrInlineData() {
    return new OpenPgpApi.OpenPgpDataSource() {

        @Override
        public Long getSizeForProgress() {
            Part part = currentCryptoPart.part;
            CryptoPartType cryptoPartType = currentCryptoPart.type;
            Body body;
            if (cryptoPartType == CryptoPartType.PGP_ENCRYPTED) {
                Multipart multipartEncryptedMultipart = (Multipart) part.getBody();
                BodyPart encryptionPayloadPart = multipartEncryptedMultipart.getBodyPart(1);
                body = encryptionPayloadPart.getBody();
            } else if (cryptoPartType == CryptoPartType.PGP_INLINE) {
                body = part.getBody();
            } else {
                throw new IllegalStateException("part to stream must be encrypted or inline!");
            }
            if (body instanceof SizeAware) {
                return ((SizeAware) body).getSize();
            }
            return null;
        }

        @Override
        @WorkerThread
        public void writeTo(OutputStream os) throws IOException {
            try {
                Part part = currentCryptoPart.part;
                CryptoPartType cryptoPartType = currentCryptoPart.type;
                if (cryptoPartType == CryptoPartType.PGP_ENCRYPTED) {
                    Multipart multipartEncryptedMultipart = (Multipart) part.getBody();
                    BodyPart encryptionPayloadPart = multipartEncryptedMultipart.getBodyPart(1);
                    Body encryptionPayloadBody = encryptionPayloadPart.getBody();
                    encryptionPayloadBody.writeTo(os);
                } else if (cryptoPartType == CryptoPartType.PGP_INLINE) {
                    String text = MessageExtractor.getTextFromPart(part);
                    os.write(text.getBytes());
                } else {
                    throw new IllegalStateException("part to stream must be encrypted or inline!");
                }
            } catch (MessagingException e) {
                Timber.e(e, "MessagingException while writing message to crypto provider");
            }
        }
    };
}
Also used : MimeBodyPart(com.fsck.k9.mail.internet.MimeBodyPart) BodyPart(com.fsck.k9.mail.BodyPart) Multipart(com.fsck.k9.mail.Multipart) MimeMultipart(com.fsck.k9.mail.internet.MimeMultipart) SizeAware(com.fsck.k9.mail.internet.SizeAware) MessagingException(com.fsck.k9.mail.MessagingException) MimeBodyPart(com.fsck.k9.mail.internet.MimeBodyPart) BodyPart(com.fsck.k9.mail.BodyPart) Part(com.fsck.k9.mail.Part) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) OpenPgpDataSource(org.openintents.openpgp.util.OpenPgpApi.OpenPgpDataSource) TextBody(com.fsck.k9.mail.internet.TextBody) Body(com.fsck.k9.mail.Body)

Example 88 with Body

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

the class MessageCryptoHelperTest method multipartEncrypted__shouldCallOpenPgpApiAsync.

@Test
public void multipartEncrypted__shouldCallOpenPgpApiAsync() throws Exception {
    Body encryptedBody = spy(new TextBody("encrypted data"));
    Message message = messageFromBody(multipart("encrypted", "protocol=\"application/pgp-encrypted\"", bodypart("application/pgp-encrypted", "content"), bodypart("application/octet-stream", encryptedBody)));
    message.setFrom(Address.parse("Test <test@example.org>")[0]);
    OutputStream outputStream = mock(OutputStream.class);
    Intent resultIntent = new Intent();
    resultIntent.putExtra(OpenPgpApi.RESULT_CODE, OpenPgpApi.RESULT_CODE_SUCCESS);
    OpenPgpDecryptionResult decryptionResult = mock(OpenPgpDecryptionResult.class);
    resultIntent.putExtra(OpenPgpApi.RESULT_DECRYPTION, decryptionResult);
    OpenPgpSignatureResult signatureResult = mock(OpenPgpSignatureResult.class);
    resultIntent.putExtra(OpenPgpApi.RESULT_SIGNATURE, signatureResult);
    PendingIntent pendingIntent = mock(PendingIntent.class);
    resultIntent.putExtra(OpenPgpApi.RESULT_INTENT, pendingIntent);
    processEncryptedMessageAndCaptureMocks(message, encryptedBody, outputStream);
    MimeBodyPart decryptedPart = new MimeBodyPart(new TextBody("text"));
    capturedCallback.onReturn(resultIntent, decryptedPart);
    assertEquals(OpenPgpApi.ACTION_DECRYPT_VERIFY, capturedApiIntent.getAction());
    assertEquals("test@example.org", capturedApiIntent.getStringExtra(OpenPgpApi.EXTRA_SENDER_ADDRESS));
    assertPartAnnotationHasState(message, messageCryptoCallback, CryptoError.OPENPGP_OK, decryptedPart, decryptionResult, signatureResult, pendingIntent);
    verify(autocryptOperations).addAutocryptPeerUpdateToIntentIfPresent(message, capturedApiIntent);
    verifyNoMoreInteractions(autocryptOperations);
}
Also used : TextBody(com.fsck.k9.mail.internet.TextBody) MimeMessage(com.fsck.k9.mail.internet.MimeMessage) Message(com.fsck.k9.mail.Message) OpenPgpDecryptionResult(org.openintents.openpgp.OpenPgpDecryptionResult) OutputStream(java.io.OutputStream) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) PendingIntent(android.app.PendingIntent) MimeBodyPart(com.fsck.k9.mail.internet.MimeBodyPart) TextBody(com.fsck.k9.mail.internet.TextBody) Body(com.fsck.k9.mail.Body) TestMessageConstructionUtils.messageFromBody(com.fsck.k9.mail.TestMessageConstructionUtils.messageFromBody) OpenPgpSignatureResult(org.openintents.openpgp.OpenPgpSignatureResult) RobolectricTest(com.fsck.k9.RobolectricTest) Test(org.junit.Test)

Example 89 with Body

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

the class MimeMessageHelper method setEncoding.

public static void setEncoding(Part part, String encoding) throws MessagingException {
    Body body = part.getBody();
    if (body != null) {
        body.setEncoding(encoding);
    }
    part.setHeader(MimeHeader.HEADER_CONTENT_TRANSFER_ENCODING, encoding);
}
Also used : Body(com.fsck.k9.mail.Body)

Example 90 with Body

use of com.fsck.k9.mail.Body 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)44 BodyPart (com.fsck.k9.mail.BodyPart)35 Multipart (com.fsck.k9.mail.Multipart)32 MimeBodyPart (com.fsck.k9.mail.internet.MimeBodyPart)32 Part (com.fsck.k9.mail.Part)29 Test (org.junit.Test)29 TextBody (com.fsck.k9.mail.internet.TextBody)23 MimeMessage (com.fsck.k9.mail.internet.MimeMessage)21 MimeMultipart (com.fsck.k9.mail.internet.MimeMultipart)19 ArrayList (java.util.ArrayList)16 MessagingException (com.fsck.k9.mail.MessagingException)14 BinaryTempFileBody (com.fsck.k9.mail.internet.BinaryTempFileBody)10 ByteArrayOutputStream (java.io.ByteArrayOutputStream)10 Message (com.fsck.k9.mail.Message)9 OutputStream (java.io.OutputStream)9 Stack (java.util.Stack)9 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)9 K9RobolectricTest (com.fsck.k9.K9RobolectricTest)7 BinaryMemoryBody (com.fsck.k9.mailstore.BinaryMemoryBody)7 InputStream (java.io.InputStream)7