Search in sources :

Example 46 with Multipart

use of com.fsck.k9.mail.Multipart 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 47 with Multipart

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

Example 48 with Multipart

use of com.fsck.k9.mail.Multipart 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.getId() == 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 49 with Multipart

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

the class MessageDecryptVerifier method findPrimaryPartInMixed.

@Nullable
private static Part findPrimaryPartInMixed(Part part, List<Part> outputExtraParts) {
    Body body = part.getBody();
    boolean isMultipartMixed = part.isMimeType("multipart/mixed") && body instanceof Multipart;
    if (!isMultipartMixed) {
        return null;
    }
    Multipart multipart = (Multipart) body;
    if (multipart.getCount() == 0) {
        return null;
    }
    BodyPart firstBodyPart = multipart.getBodyPart(0);
    Part foundPart;
    if (isPartEncryptedOrSigned(firstBodyPart)) {
        foundPart = firstBodyPart;
    } else {
        foundPart = findPrimaryPartInAlternative(firstBodyPart);
    }
    if (foundPart != null && outputExtraParts != null) {
        for (int i = 1; i < multipart.getCount(); i++) {
            outputExtraParts.add(multipart.getBodyPart(i));
        }
    }
    return foundPart;
}
Also used : BodyPart(com.fsck.k9.mail.BodyPart) MimeBodyPart(com.fsck.k9.mail.internet.MimeBodyPart) 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) Body(com.fsck.k9.mail.Body) Nullable(android.support.annotation.Nullable)

Example 50 with Multipart

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

the class MessageDecryptVerifier method findSignedParts.

public static List<Part> findSignedParts(Part startPart, MessageCryptoAnnotations messageCryptoAnnotations) {
    List<Part> signedParts = new ArrayList<>();
    Stack<Part> partsToCheck = new Stack<>();
    partsToCheck.push(startPart);
    while (!partsToCheck.isEmpty()) {
        Part part = partsToCheck.pop();
        if (messageCryptoAnnotations.has(part)) {
            CryptoResultAnnotation resultAnnotation = messageCryptoAnnotations.get(part);
            MimeBodyPart replacementData = resultAnnotation.getReplacementData();
            if (replacementData != null) {
                part = replacementData;
            }
        }
        Body body = part.getBody();
        if (isPartMultipartSigned(part)) {
            signedParts.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 signedParts;
}
Also used : BodyPart(com.fsck.k9.mail.BodyPart) MimeBodyPart(com.fsck.k9.mail.internet.MimeBodyPart) 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) CryptoResultAnnotation(com.fsck.k9.mailstore.CryptoResultAnnotation) ArrayList(java.util.ArrayList) MimeBodyPart(com.fsck.k9.mail.internet.MimeBodyPart) Body(com.fsck.k9.mail.Body) Stack(java.util.Stack)

Aggregations

BodyPart (com.fsck.k9.mail.BodyPart)57 Part (com.fsck.k9.mail.Part)51 Test (org.junit.Test)47 MimeBodyPart (com.fsck.k9.mail.internet.MimeBodyPart)38 Multipart (com.fsck.k9.mail.Multipart)33 MimeMessage (com.fsck.k9.mail.internet.MimeMessage)29 Message (com.fsck.k9.mail.Message)25 Body (com.fsck.k9.mail.Body)22 MimeMultipart (com.fsck.k9.mail.internet.MimeMultipart)21 ArrayList (java.util.ArrayList)14 MessageCreationHelper.createEmptyPart (com.fsck.k9.message.MessageCreationHelper.createEmptyPart)13 MessageCreationHelper.createPart (com.fsck.k9.message.MessageCreationHelper.createPart)13 MessageCreationHelper.createTextPart (com.fsck.k9.message.MessageCreationHelper.createTextPart)13 ByteArrayOutputStream (java.io.ByteArrayOutputStream)8 TextBody (com.fsck.k9.mail.internet.TextBody)7 MessagingException (com.fsck.k9.mail.MessagingException)6 Stack (java.util.Stack)6 SQLiteDatabase (android.database.sqlite.SQLiteDatabase)5 FetchProfile (com.fsck.k9.mail.FetchProfile)5 BinaryTempFileBody (com.fsck.k9.mail.internet.BinaryTempFileBody)5