Search in sources :

Example 6 with MimeMultipart

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

the class MessageCryptoHelper method getMultipartSignedContentPartIfAvailable.

@Nullable
private static MimeBodyPart getMultipartSignedContentPartIfAvailable(Part part) {
    MimeBodyPart replacementPart = NO_REPLACEMENT_PART;
    Body body = part.getBody();
    if (body instanceof MimeMultipart) {
        MimeMultipart multipart = ((MimeMultipart) part.getBody());
        if (multipart.getCount() >= 1) {
            replacementPart = (MimeBodyPart) multipart.getBodyPart(0);
        }
    }
    return replacementPart;
}
Also used : MimeMultipart(com.fsck.k9.mail.internet.MimeMultipart) MimeBodyPart(com.fsck.k9.mail.internet.MimeBodyPart) TextBody(com.fsck.k9.mail.internet.TextBody) Body(com.fsck.k9.mail.Body) Nullable(android.support.annotation.Nullable)

Example 7 with MimeMultipart

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

the class MessageBuilder method addAttachmentsToMessage.

/**
     * Add attachments as parts into a MimeMultipart container.
     * @param mp MimeMultipart container in which to insert parts.
     * @throws MessagingException
     */
private void addAttachmentsToMessage(final MimeMultipart mp) throws MessagingException {
    for (Attachment attachment : attachments) {
        if (attachment.state != Attachment.LoadingState.COMPLETE) {
            continue;
        }
        String contentType = attachment.contentType;
        if (MimeUtil.isMessage(contentType)) {
            contentType = "application/octet-stream";
        // TODO reencode message body to 7 bit
        // body = new TempFileMessageBody(attachment.filename);
        }
        Body body = new TempFileBody(attachment.filename);
        MimeBodyPart bp = new MimeBodyPart(body);
        /*
             * Correctly encode the filename here. Otherwise the whole
             * header value (all parameters at once) will be encoded by
             * MimeHeader.writeTo().
             */
        bp.addHeader(MimeHeader.HEADER_CONTENT_TYPE, String.format("%s;\r\n name=\"%s\"", contentType, EncoderUtil.encodeIfNecessary(attachment.name, EncoderUtil.Usage.WORD_ENTITY, 7)));
        bp.setEncoding(MimeUtility.getEncodingforType(contentType));
        /*
             * TODO: Oh the joys of MIME...
             *
             * From RFC 2183 (The Content-Disposition Header Field):
             * "Parameter values longer than 78 characters, or which
             *  contain non-ASCII characters, MUST be encoded as specified
             *  in [RFC 2184]."
             *
             * Example:
             *
             * Content-Type: application/x-stuff
             *  title*1*=us-ascii'en'This%20is%20even%20more%20
             *  title*2*=%2A%2A%2Afun%2A%2A%2A%20
             *  title*3="isn't it!"
             */
        bp.addHeader(MimeHeader.HEADER_CONTENT_DISPOSITION, String.format(Locale.US, "attachment;\r\n filename=\"%s\";\r\n size=%d", attachment.name, attachment.size));
        mp.addBodyPart(bp);
    }
}
Also used : TempFileBody(com.fsck.k9.mailstore.TempFileBody) Attachment(com.fsck.k9.activity.misc.Attachment) MimeBodyPart(com.fsck.k9.mail.internet.MimeBodyPart) TextBody(com.fsck.k9.mail.internet.TextBody) Body(com.fsck.k9.mail.Body) TempFileBody(com.fsck.k9.mailstore.TempFileBody)

Example 8 with MimeMultipart

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

the class PgpMessageBuilder method mimeBuildEncryptedMessage.

private void mimeBuildEncryptedMessage(@NonNull Body encryptedBodyPart) throws MessagingException {
    if (!cryptoStatus.isEncryptionEnabled()) {
        throw new IllegalStateException("call to mimeBuildEncryptedMessage while encryption isn't enabled!");
    }
    MimeMultipart multipartEncrypted = createMimeMultipart();
    multipartEncrypted.setSubType("encrypted");
    multipartEncrypted.addBodyPart(new MimeBodyPart(new TextBody("Version: 1"), "application/pgp-encrypted"));
    MimeBodyPart encryptedPart = new MimeBodyPart(encryptedBodyPart, "application/octet-stream; name=\"encrypted.asc\"");
    encryptedPart.addHeader(MimeHeader.HEADER_CONTENT_DISPOSITION, "inline; filename=\"encrypted.asc\"");
    multipartEncrypted.addBodyPart(encryptedPart);
    MimeMessageHelper.setBody(currentProcessedMimeMessage, multipartEncrypted);
    String contentType = String.format("multipart/encrypted; boundary=\"%s\";\r\n  protocol=\"application/pgp-encrypted\"", multipartEncrypted.getBoundary());
    currentProcessedMimeMessage.setHeader(MimeHeader.HEADER_CONTENT_TYPE, contentType);
}
Also used : TextBody(com.fsck.k9.mail.internet.TextBody) MimeMultipart(com.fsck.k9.mail.internet.MimeMultipart) MimeBodyPart(com.fsck.k9.mail.internet.MimeBodyPart)

Example 9 with MimeMultipart

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

the class PgpMessageBuilder method mimeBuildSignedMessage.

private void mimeBuildSignedMessage(@NonNull BodyPart signedBodyPart, Intent result) throws MessagingException {
    if (!cryptoStatus.isSigningEnabled()) {
        throw new IllegalStateException("call to mimeBuildSignedMessage while signing isn't enabled!");
    }
    byte[] signedData = result.getByteArrayExtra(OpenPgpApi.RESULT_DETACHED_SIGNATURE);
    if (signedData == null) {
        throw new MessagingException("didn't find expected RESULT_DETACHED_SIGNATURE in api call result");
    }
    MimeMultipart multipartSigned = createMimeMultipart();
    multipartSigned.setSubType("signed");
    multipartSigned.addBodyPart(signedBodyPart);
    multipartSigned.addBodyPart(new MimeBodyPart(new BinaryMemoryBody(signedData, MimeUtil.ENC_7BIT), "application/pgp-signature; name=\"signature.asc\""));
    MimeMessageHelper.setBody(currentProcessedMimeMessage, multipartSigned);
    String contentType = String.format("multipart/signed; boundary=\"%s\";\r\n  protocol=\"application/pgp-signature\"", multipartSigned.getBoundary());
    if (result.hasExtra(OpenPgpApi.RESULT_SIGNATURE_MICALG)) {
        String micAlgParameter = result.getStringExtra(OpenPgpApi.RESULT_SIGNATURE_MICALG);
        contentType += String.format("; micalg=\"%s\"", micAlgParameter);
    } else {
        Timber.e("missing micalg parameter for pgp multipart/signed!");
    }
    currentProcessedMimeMessage.setHeader(MimeHeader.HEADER_CONTENT_TYPE, contentType);
}
Also used : MessagingException(com.fsck.k9.mail.MessagingException) MimeMultipart(com.fsck.k9.mail.internet.MimeMultipart) MimeBodyPart(com.fsck.k9.mail.internet.MimeBodyPart) BinaryMemoryBody(com.fsck.k9.mailstore.BinaryMemoryBody)

Example 10 with MimeMultipart

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

the class LocalFolder method loadMessagePart.

private void loadMessagePart(LocalMessage message, Map<Long, Part> partById, Cursor cursor) throws MessagingException {
    long id = cursor.getLong(0);
    long parentId = cursor.getLong(2);
    String mimeType = cursor.getString(3);
    long size = cursor.getLong(4);
    byte[] header = cursor.getBlob(6);
    int dataLocation = cursor.getInt(9);
    String serverExtra = cursor.getString(15);
    // TODO we don't currently cache much of the part data which is computed with AttachmentInfoExtractor,
    // TODO might want to do that at a later point?
    // String displayName = cursor.getString(5);
    // int type = cursor.getInt(1);
    // boolean inlineAttachment = (type == MessagePartType.HIDDEN_ATTACHMENT);
    final Part part;
    if (id == message.getMessagePartId()) {
        part = message;
    } else {
        Part parentPart = partById.get(parentId);
        if (parentPart == null) {
            throw new IllegalStateException("Parent part not found");
        }
        String parentMimeType = parentPart.getMimeType();
        if (MimeUtility.isMultipart(parentMimeType)) {
            BodyPart bodyPart = new LocalBodyPart(getAccountUuid(), message, id, size);
            ((Multipart) parentPart.getBody()).addBodyPart(bodyPart);
            part = bodyPart;
        } else if (MimeUtility.isMessage(parentMimeType)) {
            Message innerMessage = new LocalMimeMessage(getAccountUuid(), message, id);
            parentPart.setBody(innerMessage);
            part = innerMessage;
        } else {
            throw new IllegalStateException("Parent is neither a multipart nor a message");
        }
        parseHeaderBytes(part, header);
    }
    partById.put(id, part);
    part.setServerExtra(serverExtra);
    if (MimeUtility.isMultipart(mimeType)) {
        byte[] preamble = cursor.getBlob(11);
        byte[] epilogue = cursor.getBlob(12);
        String boundary = cursor.getString(13);
        MimeMultipart multipart = new MimeMultipart(mimeType, boundary);
        part.setBody(multipart);
        multipart.setPreamble(preamble);
        multipart.setEpilogue(epilogue);
    } else if (dataLocation == DataLocation.IN_DATABASE) {
        String encoding = cursor.getString(7);
        byte[] data = cursor.getBlob(10);
        Body body = new BinaryMemoryBody(data, encoding);
        part.setBody(body);
    } else if (dataLocation == DataLocation.ON_DISK) {
        String encoding = cursor.getString(7);
        File file = localStore.getAttachmentFile(Long.toString(id));
        if (file.exists()) {
            Body body = new FileBackedBody(file, encoding);
            part.setBody(body);
        }
    }
}
Also used : BodyPart(com.fsck.k9.mail.BodyPart) Multipart(com.fsck.k9.mail.Multipart) MimeMultipart(com.fsck.k9.mail.internet.MimeMultipart) MimeMessage(com.fsck.k9.mail.internet.MimeMessage) Message(com.fsck.k9.mail.Message) MimeMultipart(com.fsck.k9.mail.internet.MimeMultipart) BodyPart(com.fsck.k9.mail.BodyPart) Part(com.fsck.k9.mail.Part) Body(com.fsck.k9.mail.Body) BinaryTempFileBody(com.fsck.k9.mail.internet.BinaryTempFileBody) File(java.io.File)

Aggregations

MimeMultipart (com.fsck.k9.mail.internet.MimeMultipart)18 MimeBodyPart (com.fsck.k9.mail.internet.MimeBodyPart)14 Test (org.junit.Test)10 MimeMessage (com.fsck.k9.mail.internet.MimeMessage)9 Part (com.fsck.k9.mail.Part)8 BodyPart (com.fsck.k9.mail.BodyPart)7 TextBody (com.fsck.k9.mail.internet.TextBody)7 Body (com.fsck.k9.mail.Body)3 PendingIntent (android.app.PendingIntent)2 Intent (android.content.Intent)2 MessagingException (com.fsck.k9.mail.MessagingException)2 BinaryTempFileBody (com.fsck.k9.mail.internet.BinaryTempFileBody)2 Viewable (com.fsck.k9.mail.internet.Viewable)2 ViewableExtractedText (com.fsck.k9.mailstore.MessageViewInfoExtractor.ViewableExtractedText)2 Callback (com.fsck.k9.message.MessageBuilder.Callback)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 OutputStream (java.io.OutputStream)2 ArrayList (java.util.ArrayList)2 OpenPgpDataSource (org.openintents.openpgp.util.OpenPgpApi.OpenPgpDataSource)2 Nullable (android.support.annotation.Nullable)1