Search in sources :

Example 41 with BodyPart

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

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

the class MessageExtractor method getMessageFromPart.

private static Message getMessageFromPart(Part part) {
    while (part != null) {
        if (part instanceof Message)
            return (Message) part;
        if (!(part instanceof BodyPart))
            return null;
        Multipart multipart = ((BodyPart) part).getParent();
        if (multipart == null)
            return null;
        part = multipart.getParent();
    }
    return null;
}
Also used : BodyPart(com.fsck.k9.mail.BodyPart) Multipart(com.fsck.k9.mail.Multipart) Message(com.fsck.k9.mail.Message)

Example 43 with BodyPart

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

the class Multipart method setCharset.

public void setCharset(String charset) throws MessagingException {
    if (mParts.isEmpty())
        return;
    BodyPart part = mParts.get(0);
    Body body = part.getBody();
    if (body instanceof TextBody) {
        CharsetSupport.setCharset(charset, part);
        ((TextBody) body).setCharset(charset);
    }
}
Also used : TextBody(com.fsck.k9.mail.internet.TextBody) TextBody(com.fsck.k9.mail.internet.TextBody)

Example 44 with BodyPart

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

the class PgpMessageBuilder method launchOpenPgpApiIntent.

private PendingIntent launchOpenPgpApiIntent(@NonNull Intent openPgpIntent, boolean captureOutputPart, boolean capturedOutputPartIs7Bit, boolean writeBodyContentOnly) throws MessagingException {
    final MimeBodyPart bodyPart = currentProcessedMimeMessage.toBodyPart();
    String[] contentType = currentProcessedMimeMessage.getHeader(MimeHeader.HEADER_CONTENT_TYPE);
    if (contentType.length > 0) {
        bodyPart.setHeader(MimeHeader.HEADER_CONTENT_TYPE, contentType[0]);
    }
    OpenPgpDataSource dataSource = createOpenPgpDataSourceFromBodyPart(bodyPart, writeBodyContentOnly);
    BinaryTempFileBody pgpResultTempBody = null;
    OutputStream outputStream = null;
    if (captureOutputPart) {
        try {
            pgpResultTempBody = new BinaryTempFileBody(capturedOutputPartIs7Bit ? MimeUtil.ENC_7BIT : MimeUtil.ENC_8BIT);
            outputStream = pgpResultTempBody.getOutputStream();
            // OpenKeychain/BouncyCastle at this point use the system newline for formatting, which is LF on android.
            // we need this to be CRLF, so we convert the data after receiving.
            outputStream = new EOLConvertingOutputStream(outputStream);
        } catch (IOException e) {
            throw new MessagingException("could not allocate temp file for storage!", e);
        }
    }
    Intent result = openPgpApi.executeApi(openPgpIntent, dataSource, outputStream);
    switch(result.getIntExtra(OpenPgpApi.RESULT_CODE, OpenPgpApi.RESULT_CODE_ERROR)) {
        case OpenPgpApi.RESULT_CODE_SUCCESS:
            mimeBuildMessage(result, bodyPart, pgpResultTempBody);
            return null;
        case OpenPgpApi.RESULT_CODE_USER_INTERACTION_REQUIRED:
            PendingIntent returnedPendingIntent = result.getParcelableExtra(OpenPgpApi.RESULT_INTENT);
            if (returnedPendingIntent == null) {
                throw new MessagingException("openpgp api needs user interaction, but returned no pendingintent!");
            }
            return returnedPendingIntent;
        case OpenPgpApi.RESULT_CODE_ERROR:
            OpenPgpError error = result.getParcelableExtra(OpenPgpApi.RESULT_ERROR);
            if (error == null) {
                throw new MessagingException("internal openpgp api error");
            }
            boolean isOpportunisticError = error.getErrorId() == OpenPgpError.OPPORTUNISTIC_MISSING_KEYS;
            if (isOpportunisticError) {
                if (!cryptoStatus.isEncryptionOpportunistic()) {
                    throw new IllegalStateException("Got opportunistic error, but encryption wasn't supposed to be opportunistic!");
                }
                Timber.d("Skipping encryption due to opportunistic mode");
                return null;
            }
            throw new MessagingException(error.getMessage());
    }
    throw new IllegalStateException("unreachable code segment reached");
}
Also used : EOLConvertingOutputStream(com.fsck.k9.mail.filter.EOLConvertingOutputStream) MessagingException(com.fsck.k9.mail.MessagingException) EOLConvertingOutputStream(com.fsck.k9.mail.filter.EOLConvertingOutputStream) OutputStream(java.io.OutputStream) OpenPgpDataSource(org.openintents.openpgp.util.OpenPgpApi.OpenPgpDataSource) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) IOException(java.io.IOException) BinaryTempFileBody(com.fsck.k9.mail.internet.BinaryTempFileBody) PendingIntent(android.app.PendingIntent) OpenPgpError(org.openintents.openpgp.OpenPgpError) MimeBodyPart(com.fsck.k9.mail.internet.MimeBodyPart)

Example 45 with BodyPart

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

Aggregations

BodyPart (com.fsck.k9.mail.BodyPart)49 Part (com.fsck.k9.mail.Part)38 MimeBodyPart (com.fsck.k9.mail.internet.MimeBodyPart)33 Test (org.junit.Test)32 MimeMessage (com.fsck.k9.mail.internet.MimeMessage)22 Multipart (com.fsck.k9.mail.Multipart)21 Body (com.fsck.k9.mail.Body)17 Message (com.fsck.k9.mail.Message)17 MimeMultipart (com.fsck.k9.mail.internet.MimeMultipart)13 MessageCreationHelper.createEmptyPart (com.fsck.k9.message.MessageCreationHelper.createEmptyPart)11 MessageCreationHelper.createPart (com.fsck.k9.message.MessageCreationHelper.createPart)11 MessageCreationHelper.createTextPart (com.fsck.k9.message.MessageCreationHelper.createTextPart)11 ByteArrayOutputStream (java.io.ByteArrayOutputStream)11 MessagingException (com.fsck.k9.mail.MessagingException)9 OutputStream (java.io.OutputStream)8 BinaryTempFileBody (com.fsck.k9.mail.internet.BinaryTempFileBody)7 OpenPgpDataSource (org.openintents.openpgp.util.OpenPgpApi.OpenPgpDataSource)7 ArrayList (java.util.ArrayList)6 Stack (java.util.Stack)6 TextBody (com.fsck.k9.mail.internet.TextBody)5