Search in sources :

Example 31 with Body

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

Example 32 with Body

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

the class LocalFolder method updateOrInsertMessagePart.

private long updateOrInsertMessagePart(SQLiteDatabase db, ContentValues cv, Part part, long existingMessagePartId) throws IOException, MessagingException {
    byte[] headerBytes = getHeaderBytes(part);
    cv.put("mime_type", part.getMimeType());
    cv.put("header", headerBytes);
    cv.put("type", MessagePartType.UNKNOWN);
    File file = null;
    Body body = part.getBody();
    if (body instanceof Multipart) {
        multipartToContentValues(cv, (Multipart) body);
    } else if (body == null) {
        missingPartToContentValues(cv, part);
    } else if (body instanceof Message) {
        messageMarkerToContentValues(cv);
    } else {
        file = leafPartToContentValues(cv, part, body);
    }
    long messagePartId;
    if (existingMessagePartId != INVALID_MESSAGE_PART_ID) {
        messagePartId = existingMessagePartId;
        db.update("message_parts", cv, "id = ?", new String[] { Long.toString(messagePartId) });
    } else {
        messagePartId = db.insertOrThrow("message_parts", null, cv);
    }
    if (file != null) {
        moveTemporaryFile(file, Long.toString(messagePartId));
    }
    return messagePartId;
}
Also used : Multipart(com.fsck.k9.mail.Multipart) MimeMultipart(com.fsck.k9.mail.internet.MimeMultipart) Message(com.fsck.k9.mail.Message) File(java.io.File) Body(com.fsck.k9.mail.Body) BinaryTempFileBody(com.fsck.k9.mail.internet.BinaryTempFileBody)

Example 33 with Body

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

the class LocalFolder method writeBodyToDisk.

private File writeBodyToDisk(Body body) throws IOException, MessagingException {
    File file = File.createTempFile("body", null, BinaryTempFileBody.getTempDirectory());
    OutputStream out = new FileOutputStream(file);
    try {
        body.writeTo(out);
    } finally {
        out.close();
    }
    return file;
}
Also used : CountingOutputStream(com.fsck.k9.mail.filter.CountingOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) File(java.io.File)

Example 34 with Body

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

the class LocalFolder method leafPartToContentValues.

private File leafPartToContentValues(ContentValues cv, Part part, Body body) throws MessagingException, IOException {
    AttachmentViewInfo attachment = attachmentInfoExtractor.extractAttachmentInfoForDatabase(part);
    cv.put("display_name", attachment.displayName);
    String encoding = getTransferEncoding(part);
    if (!(body instanceof SizeAware)) {
        throw new IllegalStateException("Body needs to implement SizeAware");
    }
    SizeAware sizeAwareBody = (SizeAware) body;
    long fileSize = sizeAwareBody.getSize();
    File file = null;
    int dataLocation;
    if (fileSize > MAX_BODY_SIZE_FOR_DATABASE) {
        dataLocation = DataLocation.ON_DISK;
        file = writeBodyToDiskIfNecessary(part);
        long size = decodeAndCountBytes(file, encoding, fileSize);
        cv.put("decoded_body_size", size);
    } else {
        dataLocation = DataLocation.IN_DATABASE;
        byte[] bodyData = getBodyBytes(body);
        cv.put("data", bodyData);
        long size = decodeAndCountBytes(bodyData, encoding, bodyData.length);
        cv.put("decoded_body_size", size);
    }
    cv.put("data_location", dataLocation);
    cv.put("encoding", encoding);
    cv.put("content_id", part.getContentId());
    return file;
}
Also used : SizeAware(com.fsck.k9.mail.internet.SizeAware) File(java.io.File)

Example 35 with Body

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

the class MessageCryptoStructureDetector method findPgpInlineParts.

public static List<Part> findPgpInlineParts(Part startPart) {
    List<Part> inlineParts = new ArrayList<>();
    Stack<Part> partsToCheck = new Stack<>();
    partsToCheck.push(startPart);
    while (!partsToCheck.isEmpty()) {
        Part part = partsToCheck.pop();
        Body body = part.getBody();
        if (isPartPgpInlineEncryptedOrSigned(part)) {
            inlineParts.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 inlineParts;
}
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)

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