Search in sources :

Example 16 with MimeBodyPart

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

the class ImapFolder method parseBodyStructure.

private void parseBodyStructure(ImapList bs, Part part, String id) throws MessagingException {
    if (bs.get(0) instanceof ImapList) {
        /*
             * This is a multipart/*
             */
        MimeMultipart mp = MimeMultipart.newInstance();
        for (int i = 0, count = bs.size(); i < count; i++) {
            if (bs.get(i) instanceof ImapList) {
                /*
                     * For each part in the message we're going to add a new BodyPart and parse
                     * into it.
                     */
                MimeBodyPart bp = new MimeBodyPart();
                if (id.equalsIgnoreCase("TEXT")) {
                    parseBodyStructure(bs.getList(i), bp, Integer.toString(i + 1));
                } else {
                    parseBodyStructure(bs.getList(i), bp, id + "." + (i + 1));
                }
                mp.addBodyPart(bp);
            } else {
                /*
                     * We've got to the end of the children of the part, so now we can find out
                     * what type it is and bail out.
                     */
                String subType = bs.getString(i);
                mp.setSubType(subType.toLowerCase(Locale.US));
                break;
            }
        }
        MimeMessageHelper.setBody(part, mp);
    } else {
        /*
             * This is a body. We need to add as much information as we can find out about
             * it to the Part.
             */
        /*
             *  0| 0  body type
             *  1| 1  body subtype
             *  2| 2  body parameter parenthesized list
             *  3| 3  body id (unused)
             *  4| 4  body description (unused)
             *  5| 5  body encoding
             *  6| 6  body size
             *  -| 7  text lines (only for type TEXT, unused)
             * Extensions (optional):
             *  7| 8  body MD5 (unused)
             *  8| 9  body disposition
             *  9|10  body language (unused)
             * 10|11  body location (unused)
             */
        String type = bs.getString(0);
        String subType = bs.getString(1);
        String mimeType = (type + "/" + subType).toLowerCase(Locale.US);
        ImapList bodyParams = null;
        if (bs.get(2) instanceof ImapList) {
            bodyParams = bs.getList(2);
        }
        String encoding = bs.getString(5);
        int size = bs.getNumber(6);
        if (MimeUtility.isMessage(mimeType)) {
            /*
                 * This will be caught by fetch and handled appropriately.
                 */
            throw new MessagingException("BODYSTRUCTURE message/rfc822 not yet supported.");
        }
        /*
             * Set the content type with as much information as we know right now.
             */
        StringBuilder contentType = new StringBuilder();
        contentType.append(mimeType);
        if (bodyParams != null) {
            /*
                 * If there are body params we might be able to get some more information out
                 * of them.
                 */
            for (int i = 0, count = bodyParams.size(); i < count; i += 2) {
                String paramName = bodyParams.getString(i);
                String paramValue = bodyParams.getString(i + 1);
                contentType.append(String.format(";\r\n %s=\"%s\"", paramName, paramValue));
            }
        }
        part.setHeader(MimeHeader.HEADER_CONTENT_TYPE, contentType.toString());
        // Extension items
        ImapList bodyDisposition = null;
        if ("text".equalsIgnoreCase(type) && bs.size() > 9 && bs.get(9) instanceof ImapList) {
            bodyDisposition = bs.getList(9);
        } else if (!("text".equalsIgnoreCase(type)) && bs.size() > 8 && bs.get(8) instanceof ImapList) {
            bodyDisposition = bs.getList(8);
        }
        StringBuilder contentDisposition = new StringBuilder();
        if (bodyDisposition != null && !bodyDisposition.isEmpty()) {
            if (!"NIL".equalsIgnoreCase(bodyDisposition.getString(0))) {
                contentDisposition.append(bodyDisposition.getString(0).toLowerCase(Locale.US));
            }
            if (bodyDisposition.size() > 1 && bodyDisposition.get(1) instanceof ImapList) {
                ImapList bodyDispositionParams = bodyDisposition.getList(1);
                /*
                     * If there is body disposition information we can pull some more information
                     * about the attachment out.
                     */
                for (int i = 0, count = bodyDispositionParams.size(); i < count; i += 2) {
                    String paramName = bodyDispositionParams.getString(i).toLowerCase(Locale.US);
                    String paramValue = bodyDispositionParams.getString(i + 1);
                    contentDisposition.append(String.format(";\r\n %s=\"%s\"", paramName, paramValue));
                }
            }
        }
        if (MimeUtility.getHeaderParameter(contentDisposition.toString(), "size") == null) {
            contentDisposition.append(String.format(Locale.US, ";\r\n size=%d", size));
        }
        /*
             * Set the content disposition containing at least the size. Attachment
             * handling code will use this down the road.
             */
        part.setHeader(MimeHeader.HEADER_CONTENT_DISPOSITION, contentDisposition.toString());
        /*
             * Set the Content-Transfer-Encoding header. Attachment code will use this
             * to parse the body.
             */
        part.setHeader(MimeHeader.HEADER_CONTENT_TRANSFER_ENCODING, encoding);
        if (part instanceof ImapMessage) {
            ((ImapMessage) part).setSize(size);
        }
        part.setServerExtra(id);
    }
}
Also used : MimeMultipart(com.fsck.k9.mail.internet.MimeMultipart) MessagingException(com.fsck.k9.mail.MessagingException) MimeBodyPart(com.fsck.k9.mail.internet.MimeBodyPart)

Example 17 with MimeBodyPart

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

the class MessageTest method testToBodyPart.

@Test
public void testToBodyPart() throws MessagingException, IOException {
    MimeMessage message;
    ByteArrayOutputStream out;
    BinaryTempFileBody.setTempDirectory(context.getCacheDir());
    mMimeBoundary = 101;
    message = nestedMessage(nestedMessage(sampleMessage()));
    out = new ByteArrayOutputStream();
    MimeBodyPart bodyPart = message.toBodyPart();
    bodyPart.writeTo(out);
    assertEquals(TO_BODY_PART_RESULT, out.toString());
}
Also used : MimeMessage(com.fsck.k9.mail.internet.MimeMessage) ByteArrayOutputStream(java.io.ByteArrayOutputStream) MimeBodyPart(com.fsck.k9.mail.internet.MimeBodyPart) Test(org.junit.Test)

Example 18 with MimeBodyPart

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

the class MessageCryptoHelper method onCryptoOperationFailed.

private void onCryptoOperationFailed(OpenPgpError error) {
    CryptoResultAnnotation annotation;
    if (currentCryptoPart.type == CryptoPartType.PGP_SIGNED) {
        MimeBodyPart replacementPart = getMultipartSignedContentPartIfAvailable(currentCryptoPart.part);
        annotation = CryptoResultAnnotation.createOpenPgpSignatureErrorAnnotation(error, replacementPart);
    } else {
        annotation = CryptoResultAnnotation.createOpenPgpEncryptionErrorAnnotation(error);
    }
    addCryptoResultAnnotationToMessage(annotation);
    onCryptoFinished();
}
Also used : CryptoResultAnnotation(com.fsck.k9.mailstore.CryptoResultAnnotation) MimeBodyPart(com.fsck.k9.mail.internet.MimeBodyPart)

Example 19 with MimeBodyPart

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

the class MessageCryptoHelper method callAsyncInlineOperation.

private void callAsyncInlineOperation(Intent intent) throws IOException {
    OpenPgpDataSource dataSource = getDataSourceForEncryptedOrInlineData();
    OpenPgpDataSink<MimeBodyPart> dataSink = getDataSinkForDecryptedInlineData();
    cancelableBackgroundOperation = openPgpApi.executeApiAsync(intent, dataSource, dataSink, new IOpenPgpSinkResultCallback<MimeBodyPart>() {

        @Override
        public void onProgress(int current, int max) {
            Timber.d("received progress status: %d / %d", current, max);
            callbackProgress(current, max);
        }

        @Override
        public void onReturn(Intent result, MimeBodyPart bodyPart) {
            cancelableBackgroundOperation = null;
            currentCryptoResult = result;
            onCryptoOperationReturned(bodyPart);
        }
    });
}
Also used : IOpenPgpSinkResultCallback(org.openintents.openpgp.util.OpenPgpApi.IOpenPgpSinkResultCallback) OpenPgpDataSource(org.openintents.openpgp.util.OpenPgpApi.OpenPgpDataSource) PendingIntent(android.app.PendingIntent) Intent(android.content.Intent) MimeBodyPart(com.fsck.k9.mail.internet.MimeBodyPart)

Example 20 with MimeBodyPart

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

the class MessageCryptoHelper method getDataSinkForDecryptedInlineData.

private OpenPgpDataSink<MimeBodyPart> getDataSinkForDecryptedInlineData() {
    return new OpenPgpDataSink<MimeBodyPart>() {

        @Override
        public MimeBodyPart processData(InputStream is) throws IOException {
            try {
                ByteArrayOutputStream decryptedByteOutputStream = new ByteArrayOutputStream();
                IOUtils.copy(is, decryptedByteOutputStream);
                TextBody body = new TextBody(new String(decryptedByteOutputStream.toByteArray()));
                return new MimeBodyPart(body, "text/plain");
            } catch (MessagingException e) {
                Timber.e(e, "MessagingException");
            }
            return null;
        }
    };
}
Also used : TextBody(com.fsck.k9.mail.internet.TextBody) MessagingException(com.fsck.k9.mail.MessagingException) InputStream(java.io.InputStream) OpenPgpDataSink(org.openintents.openpgp.util.OpenPgpApi.OpenPgpDataSink) ByteArrayOutputStream(java.io.ByteArrayOutputStream) MimeBodyPart(com.fsck.k9.mail.internet.MimeBodyPart)

Aggregations

MimeBodyPart (com.fsck.k9.mail.internet.MimeBodyPart)42 Test (org.junit.Test)22 Part (com.fsck.k9.mail.Part)15 MimeMultipart (com.fsck.k9.mail.internet.MimeMultipart)15 TextBody (com.fsck.k9.mail.internet.TextBody)11 AttachmentViewInfo (com.fsck.k9.mailstore.AttachmentViewInfo)11 BodyPart (com.fsck.k9.mail.BodyPart)9 MessagingException (com.fsck.k9.mail.MessagingException)7 Uri (android.net.Uri)5 MimeMessage (com.fsck.k9.mail.internet.MimeMessage)5 PendingIntent (android.app.PendingIntent)4 Body (com.fsck.k9.mail.Body)4 Multipart (com.fsck.k9.mail.Multipart)4 CryptoResultAnnotation (com.fsck.k9.mailstore.CryptoResultAnnotation)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 OutputStream (java.io.OutputStream)4 OpenPgpDataSource (org.openintents.openpgp.util.OpenPgpApi.OpenPgpDataSource)4 Intent (android.content.Intent)3 BinaryTempFileBody (com.fsck.k9.mail.internet.BinaryTempFileBody)3 InputStream (java.io.InputStream)3