Search in sources :

Example 66 with Body

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

the class MessageCryptoStructureDetector method findMultipartEncryptedParts.

public static List<Part> findMultipartEncryptedParts(Part startPart) {
    List<Part> encryptedParts = new ArrayList<>();
    Stack<Part> partsToCheck = new Stack<>();
    partsToCheck.push(startPart);
    while (!partsToCheck.isEmpty()) {
        Part part = partsToCheck.pop();
        Body body = part.getBody();
        if (isPartMultipartEncrypted(part)) {
            encryptedParts.add(part);
            continue;
        }
        if (body instanceof Multipart) {
            Multipart multipart = (Multipart) body;
            for (int i = multipart.getCount() - 1; i >= 0; i--) {
                BodyPart bodyPart = multipart.getBodyPart(i);
                partsToCheck.push(bodyPart);
            }
        }
    }
    return encryptedParts;
}
Also used : BodyPart(com.fsck.k9.mail.BodyPart) MimeBodyPart(com.fsck.k9.mail.internet.MimeBodyPart) MimeMultipart(com.fsck.k9.mail.internet.MimeMultipart) Multipart(com.fsck.k9.mail.Multipart) BodyPart(com.fsck.k9.mail.BodyPart) Part(com.fsck.k9.mail.Part) MimeBodyPart(com.fsck.k9.mail.internet.MimeBodyPart) ArrayList(java.util.ArrayList) Body(com.fsck.k9.mail.Body) Stack(java.util.Stack)

Example 67 with Body

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

the class MessageCryptoStructureDetector method findPrimaryPartInAlternative.

private static Part findPrimaryPartInAlternative(Part part) {
    Body body = part.getBody();
    if (part.isMimeType("multipart/alternative") && body instanceof Multipart) {
        Multipart multipart = (Multipart) body;
        if (multipart.getCount() == 0) {
            return null;
        }
        BodyPart firstBodyPart = multipart.getBodyPart(0);
        if (isPartPgpInlineEncryptedOrSigned(firstBodyPart)) {
            return firstBodyPart;
        }
    }
    return null;
}
Also used : BodyPart(com.fsck.k9.mail.BodyPart) MimeBodyPart(com.fsck.k9.mail.internet.MimeBodyPart) MimeMultipart(com.fsck.k9.mail.internet.MimeMultipart) Multipart(com.fsck.k9.mail.Multipart) Body(com.fsck.k9.mail.Body)

Example 68 with Body

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

Example 69 with Body

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

the class TextBodyBuilder method buildTextHtml.

/**
 * Build the {@link Body} that will contain the text of the message.
 *
 * @return {@link com.fsck.k9.mail.internet.TextBody} instance that contains the entered text and
 *         possibly the quoted original message.
 */
public TextBody buildTextHtml() {
    // The length of the formatted version of the user-supplied text/reply
    int composedMessageLength;
    // The offset of the user-supplied text/reply in the final text body
    int composedMessageOffset = 0;
    // Get the user-supplied text
    String text = mMessageContent;
    // Do we have to modify an existing message to include our reply?
    if (mIncludeQuotedText) {
        InsertableHtmlContent quotedHtmlContent = getQuotedTextHtml();
        if (K9.isDebugLoggingEnabled()) {
            Timber.d("insertable: %s", quotedHtmlContent.toDebugString());
        }
        // Convert the text to HTML
        text = textToHtmlFragment(text);
        // Save length of the body. This is used when thawing drafts.
        composedMessageLength = text.length();
        // Append signature to the reply
        if (mAppendSignature && (mReplyAfterQuote || mSignatureBeforeQuotedText)) {
            text += getSignatureHtml();
        }
        /*
             * Set the insertion location based upon our reply after quote
             * setting. Additionally, add some extra separators between the
             * composed message and quoted message depending on the quote
             * location. We only add the extra separators when we're
             * sending, that way when we load a draft, we don't have to know
             * the length of the separators to remove them before editing.
             */
        if (mReplyAfterQuote) {
            quotedHtmlContent.setInsertionLocation(InsertableHtmlContent.InsertionLocation.AFTER_QUOTE);
            if (mInsertSeparator) {
                final String separator = "<br clear=\"all\">";
                text = separator + text;
                composedMessageOffset = separator.length();
            }
        } else {
            quotedHtmlContent.setInsertionLocation(InsertableHtmlContent.InsertionLocation.BEFORE_QUOTE);
            if (mInsertSeparator) {
                text += "<br><br>";
            }
        }
        // Place signature immediately after the quoted text
        if (mAppendSignature && !(mReplyAfterQuote || mSignatureBeforeQuotedText)) {
            quotedHtmlContent.insertIntoQuotedFooter(getSignatureHtml());
        }
        quotedHtmlContent.setUserContent(text);
        // Save the body's offset. This is used when thawing drafts.
        composedMessageOffset += quotedHtmlContent.getInsertionPoint();
        text = quotedHtmlContent.toString();
    } else {
        // Convert the text to HTML
        text = textToHtmlFragment(text);
        composedMessageLength = text.length();
        composedMessageOffset = HTML_AND_BODY_START.length();
        // There is no text to quote so simply append the signature if available
        if (mAppendSignature) {
            text += getSignatureHtml();
        }
        text = HTML_AND_BODY_START + text + HTML_AND_BODY_END;
    }
    TextBody body = new TextBody(text);
    body.setComposedMessageLength(composedMessageLength);
    body.setComposedMessageOffset(composedMessageOffset);
    return body;
}
Also used : TextBody(com.fsck.k9.mail.internet.TextBody) InsertableHtmlContent(com.fsck.k9.message.quote.InsertableHtmlContent)

Example 70 with Body

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

the class TextBodyBuilder method buildTextPlain.

/**
 * Build the {@link Body} that will contain the text of the message.
 *
 * @return {@link TextBody} instance that contains the entered text and
 *         possibly the quoted original message.
 */
public TextBody buildTextPlain() {
    // The length of the formatted version of the user-supplied text/reply
    int composedMessageLength;
    // The offset of the user-supplied text/reply in the final text body
    int composedMessageOffset;
    // Get the user-supplied text
    String text = mMessageContent;
    // Capture composed message length before we start attaching quoted parts and signatures.
    composedMessageLength = text.length();
    composedMessageOffset = 0;
    // Do we have to modify an existing message to include our reply?
    if (mIncludeQuotedText) {
        String quotedText = getQuotedText();
        // Append signature to the text/reply
        if (mAppendSignature && (mReplyAfterQuote || mSignatureBeforeQuotedText)) {
            text += getSignature();
        }
        if (mReplyAfterQuote) {
            composedMessageOffset = quotedText.length() + "\r\n".length();
            text = quotedText + "\r\n" + text;
        } else {
            text += "\r\n\r\n" + quotedText;
        }
        // Place signature immediately after the quoted text
        if (mAppendSignature && !(mReplyAfterQuote || mSignatureBeforeQuotedText)) {
            text += getSignature();
        }
    } else {
        // There is no text to quote so simply append the signature if available
        if (mAppendSignature) {
            // Append signature to the text/reply
            text += getSignature();
        }
    }
    TextBody body = new TextBody(text);
    body.setComposedMessageLength(composedMessageLength);
    body.setComposedMessageOffset(composedMessageOffset);
    return body;
}
Also used : TextBody(com.fsck.k9.mail.internet.TextBody)

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