Search in sources :

Example 71 with Body

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

the class LocalStore method writeRawBodyToStream.

private void writeRawBodyToStream(Cursor cursor, SQLiteDatabase db, OutputStream outputStream) throws IOException, MessagingException {
    long partId = cursor.getLong(ATTACH_PART_ID_INDEX);
    String rootPart = cursor.getString(ATTACH_ROOT_INDEX);
    LocalMessage message = loadLocalMessageByRootPartId(db, rootPart);
    if (message == null) {
        throw new MessagingException("Unable to find message for attachment!");
    }
    Part part = findPartById(message, partId);
    if (part == null) {
        throw new MessagingException("Unable to find attachment part in associated message (db integrity error?)");
    }
    Body body = part.getBody();
    if (body == null) {
        throw new MessagingException("Attachment part isn't available!");
    }
    body.writeTo(outputStream);
}
Also used : MessagingException(com.fsck.k9.mail.MessagingException) BodyPart(com.fsck.k9.mail.BodyPart) Part(com.fsck.k9.mail.Part) Body(com.fsck.k9.mail.Body)

Example 72 with Body

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

the class LocalStore method findPartById.

static Part findPartById(Part searchRoot, long partId) {
    if (searchRoot instanceof LocalMessage) {
        LocalMessage localMessage = (LocalMessage) searchRoot;
        if (localMessage.getMessagePartId() == partId) {
            return localMessage;
        }
    }
    Stack<Part> partStack = new Stack<>();
    partStack.add(searchRoot);
    while (!partStack.empty()) {
        Part part = partStack.pop();
        if (part instanceof LocalPart) {
            LocalPart localBodyPart = (LocalPart) part;
            if (localBodyPart.getPartId() == partId) {
                return part;
            }
        }
        Body body = part.getBody();
        if (body instanceof Multipart) {
            Multipart innerMultipart = (Multipart) body;
            for (BodyPart innerPart : innerMultipart.getBodyParts()) {
                partStack.add(innerPart);
            }
        }
        if (body instanceof Part) {
            partStack.add((Part) body);
        }
    }
    return null;
}
Also used : BodyPart(com.fsck.k9.mail.BodyPart) Multipart(com.fsck.k9.mail.Multipart) BodyPart(com.fsck.k9.mail.BodyPart) Part(com.fsck.k9.mail.Part) Body(com.fsck.k9.mail.Body) Stack(java.util.Stack)

Example 73 with Body

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

the class MessageBuilder method buildBody.

private void buildBody(MimeMessage message) throws MessagingException {
    // Build the body.
    // TODO FIXME - body can be either an HTML or Text part, depending on whether we're in
    // HTML mode or not.  Should probably fix this so we don't mix up html and text parts.
    TextBody body = buildText(isDraft);
    // text/plain part when messageFormat == MessageFormat.HTML
    TextBody bodyPlain = null;
    final boolean hasAttachments = !attachments.isEmpty();
    if (messageFormat == SimpleMessageFormat.HTML) {
        // HTML message (with alternative text part)
        // This is the compiled MIME part for an HTML message.
        MimeMultipart composedMimeMessage = createMimeMultipart();
        composedMimeMessage.setSubType("alternative");
        // Let the receiver select either the text or the HTML part.
        bodyPlain = buildText(isDraft, SimpleMessageFormat.TEXT);
        composedMimeMessage.addBodyPart(MimeBodyPart.create(bodyPlain, "text/plain"));
        MimeBodyPart htmlPart = MimeBodyPart.create(body, "text/html");
        if (inlineAttachments != null && inlineAttachments.size() > 0) {
            MimeMultipart htmlPartWithInlineImages = new MimeMultipart("multipart/related", boundaryGenerator.generateBoundary());
            htmlPartWithInlineImages.addBodyPart(htmlPart);
            addInlineAttachmentsToMessage(htmlPartWithInlineImages);
            composedMimeMessage.addBodyPart(MimeBodyPart.create(htmlPartWithInlineImages));
        } else {
            composedMimeMessage.addBodyPart(htmlPart);
        }
        if (hasAttachments) {
            // If we're HTML and have attachments, we have a MimeMultipart container to hold the
            // whole message (mp here), of which one part is a MimeMultipart container
            // (composedMimeMessage) with the user's composed messages, and subsequent parts for
            // the attachments.
            MimeMultipart mp = createMimeMultipart();
            mp.addBodyPart(MimeBodyPart.create(composedMimeMessage));
            addAttachmentsToMessage(mp);
            MimeMessageHelper.setBody(message, mp);
        } else {
            // If no attachments, our multipart/alternative part is the only one we need.
            MimeMessageHelper.setBody(message, composedMimeMessage);
        }
    } else if (messageFormat == SimpleMessageFormat.TEXT) {
        // Text-only message.
        if (hasAttachments) {
            MimeMultipart mp = createMimeMultipart();
            mp.addBodyPart(MimeBodyPart.create(body, "text/plain"));
            addAttachmentsToMessage(mp);
            MimeMessageHelper.setBody(message, mp);
        } else {
            // No attachments to include, just stick the text body in the message and call it good.
            MimeMessageHelper.setBody(message, body);
        }
    }
    // If this is a draft, add metadata for thawing.
    if (isDraft) {
        // Add the identity to the message.
        message.addHeader(K9.IDENTITY_HEADER, buildIdentityHeader(body, bodyPlain));
    }
}
Also used : TextBody(com.fsck.k9.mail.internet.TextBody) MimeMultipart(com.fsck.k9.mail.internet.MimeMultipart) MimeBodyPart(com.fsck.k9.mail.internet.MimeBodyPart)

Example 74 with Body

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

the class AttachmentInfoExtractor method extractAttachmentInfo.

@WorkerThread
public AttachmentViewInfo extractAttachmentInfo(Part part) throws MessagingException {
    Uri uri;
    long size;
    boolean isContentAvailable;
    if (part instanceof LocalPart) {
        LocalPart localPart = (LocalPart) part;
        String accountUuid = localPart.getAccountUuid();
        long messagePartId = localPart.getPartId();
        size = localPart.getSize();
        isContentAvailable = part.getBody() != null;
        uri = AttachmentProvider.getAttachmentUri(accountUuid, messagePartId);
    } else if (part instanceof LocalMessage) {
        LocalMessage localMessage = (LocalMessage) part;
        String accountUuid = localMessage.getAccount().getUuid();
        long messagePartId = localMessage.getMessagePartId();
        size = localMessage.getSize();
        isContentAvailable = part.getBody() != null;
        uri = AttachmentProvider.getAttachmentUri(accountUuid, messagePartId);
    } else {
        Body body = part.getBody();
        if (body instanceof DeferredFileBody) {
            DeferredFileBody decryptedTempFileBody = (DeferredFileBody) body;
            size = decryptedTempFileBody.getSize();
            uri = getDecryptedFileProviderUri(decryptedTempFileBody, part.getMimeType());
            isContentAvailable = true;
        } else {
            throw new IllegalArgumentException("Unsupported part type provided");
        }
    }
    return extractAttachmentInfo(part, uri, size, isContentAvailable);
}
Also used : LocalMessage(com.fsck.k9.mailstore.LocalMessage) LocalPart(com.fsck.k9.mailstore.LocalPart) Uri(android.net.Uri) Body(com.fsck.k9.mail.Body) DeferredFileBody(com.fsck.k9.mailstore.DeferredFileBody) DeferredFileBody(com.fsck.k9.mailstore.DeferredFileBody) WorkerThread(androidx.annotation.WorkerThread)

Example 75 with Body

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

the class TextPartFinder method findTextPartInMultipartAlternative.

private Part findTextPartInMultipartAlternative(Multipart multipart) {
    Part htmlPart = null;
    for (BodyPart bodyPart : multipart.getBodyParts()) {
        String mimeType = bodyPart.getMimeType();
        Body body = bodyPart.getBody();
        if (body instanceof Multipart) {
            Part candidatePart = findFirstTextPart(bodyPart);
            if (candidatePart != null) {
                if (isSameMimeType(candidatePart.getMimeType(), "text/html")) {
                    htmlPart = candidatePart;
                } else {
                    return candidatePart;
                }
            }
        } else if (isSameMimeType(mimeType, "text/plain")) {
            return bodyPart;
        } else if (isSameMimeType(mimeType, "text/html") && htmlPart == null) {
            htmlPart = bodyPart;
        }
    }
    if (htmlPart != null) {
        return htmlPart;
    }
    return null;
}
Also used : BodyPart(com.fsck.k9.mail.BodyPart) Multipart(com.fsck.k9.mail.Multipart) BodyPart(com.fsck.k9.mail.BodyPart) Part(com.fsck.k9.mail.Part) 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