Search in sources :

Example 51 with Body

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

the class ImapFolder method handleFetchResponse.

// Returns value of body field
private Object handleFetchResponse(ImapMessage message, ImapList fetchList) throws MessagingException {
    Object result = null;
    if (fetchList.containsKey("FLAGS")) {
        ImapList flags = fetchList.getKeyedList("FLAGS");
        if (flags != null) {
            for (int i = 0, count = flags.size(); i < count; i++) {
                String flag = flags.getString(i);
                if (flag.equalsIgnoreCase("\\Deleted")) {
                    message.setFlagInternal(Flag.DELETED, true);
                } else if (flag.equalsIgnoreCase("\\Answered")) {
                    message.setFlagInternal(Flag.ANSWERED, true);
                } else if (flag.equalsIgnoreCase("\\Seen")) {
                    message.setFlagInternal(Flag.SEEN, true);
                } else if (flag.equalsIgnoreCase("\\Flagged")) {
                    message.setFlagInternal(Flag.FLAGGED, true);
                } else if (flag.equalsIgnoreCase("$Forwarded")) {
                    message.setFlagInternal(Flag.FORWARDED, true);
                    /* a message contains FORWARDED FLAG -> so we can also create them */
                    store.getPermanentFlagsIndex().add(Flag.FORWARDED);
                }
            }
        }
    }
    if (fetchList.containsKey("INTERNALDATE")) {
        Date internalDate = fetchList.getKeyedDate("INTERNALDATE");
        message.setInternalDate(internalDate);
    }
    if (fetchList.containsKey("RFC822.SIZE")) {
        int size = fetchList.getKeyedNumber("RFC822.SIZE");
        message.setSize(size);
    }
    if (fetchList.containsKey("BODYSTRUCTURE")) {
        ImapList bs = fetchList.getKeyedList("BODYSTRUCTURE");
        if (bs != null) {
            try {
                parseBodyStructure(bs, message, "TEXT");
            } catch (MessagingException e) {
                if (K9MailLib.isDebug()) {
                    Log.d(LOG_TAG, "Error handling message for " + getLogId(), e);
                }
                message.setBody(null);
            }
        }
    }
    if (fetchList.containsKey("BODY")) {
        int index = fetchList.getKeyIndex("BODY") + 2;
        int size = fetchList.size();
        if (index < size) {
            result = fetchList.getObject(index);
            // Check if there's an origin octet
            if (result instanceof String) {
                String originOctet = (String) result;
                if (originOctet.startsWith("<") && (index + 1) < size) {
                    result = fetchList.getObject(index + 1);
                }
            }
        }
    }
    return result;
}
Also used : MessagingException(com.fsck.k9.mail.MessagingException) Date(java.util.Date)

Example 52 with Body

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

the class Multipart method setCharset.

public void setCharset(String charset) throws MessagingException {
    if (mParts.isEmpty())
        return;
    BodyPart part = mParts.get(0);
    Body body = part.getBody();
    if (body instanceof TextBody) {
        CharsetSupport.setCharset(charset, part);
        ((TextBody) body).setCharset(charset);
    }
}
Also used : TextBody(com.fsck.k9.mail.internet.TextBody) TextBody(com.fsck.k9.mail.internet.TextBody)

Example 53 with Body

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

the class EncryptionDetector method containsPartWithMimeType.

private boolean containsPartWithMimeType(Part part, String... wantedMimeTypes) {
    String mimeType = part.getMimeType();
    if (isMimeTypeAnyOf(mimeType, wantedMimeTypes)) {
        return true;
    }
    Body body = part.getBody();
    if (body instanceof Multipart) {
        Multipart multipart = (Multipart) body;
        for (BodyPart bodyPart : multipart.getBodyParts()) {
            if (containsPartWithMimeType(bodyPart, wantedMimeTypes)) {
                return true;
            }
        }
    }
    return false;
}
Also used : BodyPart(com.fsck.k9.mail.BodyPart) Multipart(com.fsck.k9.mail.Multipart) Body(com.fsck.k9.mail.Body)

Example 54 with Body

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

the class PgpMessageBuilderTest method buildSign__withDetachedSignatureInResult__shouldSucceed.

@Test
public void buildSign__withDetachedSignatureInResult__shouldSucceed() throws MessagingException {
    cryptoStatusBuilder.setCryptoMode(CryptoMode.SIGN_ONLY);
    pgpMessageBuilder.setCryptoStatus(cryptoStatusBuilder.build());
    ArgumentCaptor<Intent> capturedApiIntent = ArgumentCaptor.forClass(Intent.class);
    Intent returnIntent = new Intent();
    returnIntent.putExtra(OpenPgpApi.RESULT_CODE, OpenPgpApi.RESULT_CODE_SUCCESS);
    returnIntent.putExtra(OpenPgpApi.RESULT_DETACHED_SIGNATURE, new byte[] { 1, 2, 3 });
    when(openPgpApi.executeApi(capturedApiIntent.capture(), any(OpenPgpDataSource.class), any(OutputStream.class))).thenReturn(returnIntent);
    Callback mockCallback = mock(Callback.class);
    pgpMessageBuilder.buildAsync(mockCallback);
    Intent expectedIntent = new Intent(OpenPgpApi.ACTION_DETACHED_SIGN);
    expectedIntent.putExtra(OpenPgpApi.EXTRA_SIGN_KEY_ID, TEST_SIGN_KEY_ID);
    expectedIntent.putExtra(OpenPgpApi.EXTRA_REQUEST_ASCII_ARMOR, true);
    assertIntentEqualsActionAndExtras(expectedIntent, capturedApiIntent.getValue());
    ArgumentCaptor<MimeMessage> captor = ArgumentCaptor.forClass(MimeMessage.class);
    verify(mockCallback).onMessageBuildSuccess(captor.capture(), eq(false));
    verifyNoMoreInteractions(mockCallback);
    MimeMessage message = captor.getValue();
    Assert.assertEquals("message must be multipart/signed", "multipart/signed", message.getMimeType());
    MimeMultipart multipart = (MimeMultipart) message.getBody();
    Assert.assertEquals("multipart/signed must consist of two parts", 2, multipart.getCount());
    BodyPart contentBodyPart = multipart.getBodyPart(0);
    Assert.assertEquals("first part must have content type text/plain", "text/plain", MimeUtility.getHeaderParameter(contentBodyPart.getContentType(), null));
    Assert.assertTrue("signed message body must be TextBody", contentBodyPart.getBody() instanceof TextBody);
    Assert.assertEquals(MimeUtil.ENC_QUOTED_PRINTABLE, ((TextBody) contentBodyPart.getBody()).getEncoding());
    assertContentOfBodyPartEquals("content must match the message text", contentBodyPart, TEST_MESSAGE_TEXT);
    BodyPart signatureBodyPart = multipart.getBodyPart(1);
    String contentType = signatureBodyPart.getContentType();
    Assert.assertEquals("second part must be pgp signature", "application/pgp-signature", MimeUtility.getHeaderParameter(contentType, null));
    Assert.assertEquals("second part must be called signature.asc", "signature.asc", MimeUtility.getHeaderParameter(contentType, "name"));
    assertContentOfBodyPartEquals("content must match the supplied detached signature", signatureBodyPart, new byte[] { 1, 2, 3 });
}
Also used : TextBody(com.fsck.k9.mail.internet.TextBody) BodyPart(com.fsck.k9.mail.BodyPart) Callback(com.fsck.k9.message.MessageBuilder.Callback) MimeMessage(com.fsck.k9.mail.internet.MimeMessage) MimeMultipart(com.fsck.k9.mail.internet.MimeMultipart) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) PendingIntent(android.app.PendingIntent) Intent(android.content.Intent) OpenPgpDataSource(org.openintents.openpgp.util.OpenPgpApi.OpenPgpDataSource) Test(org.junit.Test)

Example 55 with Body

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

the class PgpMessageBuilderTest method buildEncrypt__shouldSucceed.

@Test
public void buildEncrypt__shouldSucceed() throws MessagingException {
    ComposeCryptoStatus cryptoStatus = cryptoStatusBuilder.setCryptoMode(CryptoMode.PRIVATE).setRecipients(Collections.singletonList(new Recipient("test", "test@example.org", "labru", -1, "key"))).build();
    pgpMessageBuilder.setCryptoStatus(cryptoStatus);
    ArgumentCaptor<Intent> capturedApiIntent = ArgumentCaptor.forClass(Intent.class);
    Intent returnIntent = new Intent();
    returnIntent.putExtra(OpenPgpApi.RESULT_CODE, OpenPgpApi.RESULT_CODE_SUCCESS);
    when(openPgpApi.executeApi(capturedApiIntent.capture(), any(OpenPgpDataSource.class), any(OutputStream.class))).thenReturn(returnIntent);
    Callback mockCallback = mock(Callback.class);
    pgpMessageBuilder.buildAsync(mockCallback);
    Intent expectedApiIntent = new Intent(OpenPgpApi.ACTION_SIGN_AND_ENCRYPT);
    expectedApiIntent.putExtra(OpenPgpApi.EXTRA_SIGN_KEY_ID, TEST_SIGN_KEY_ID);
    expectedApiIntent.putExtra(OpenPgpApi.EXTRA_KEY_IDS, new long[] { TEST_SELF_ENCRYPT_KEY_ID });
    expectedApiIntent.putExtra(OpenPgpApi.EXTRA_REQUEST_ASCII_ARMOR, true);
    expectedApiIntent.putExtra(OpenPgpApi.EXTRA_ENCRYPT_OPPORTUNISTIC, false);
    expectedApiIntent.putExtra(OpenPgpApi.EXTRA_USER_IDS, cryptoStatus.getRecipientAddresses());
    assertIntentEqualsActionAndExtras(expectedApiIntent, capturedApiIntent.getValue());
    ArgumentCaptor<MimeMessage> captor = ArgumentCaptor.forClass(MimeMessage.class);
    verify(mockCallback).onMessageBuildSuccess(captor.capture(), eq(false));
    verifyNoMoreInteractions(mockCallback);
    MimeMessage message = captor.getValue();
    Assert.assertEquals("message must be multipart/encrypted", "multipart/encrypted", message.getMimeType());
    MimeMultipart multipart = (MimeMultipart) message.getBody();
    Assert.assertEquals("multipart/encrypted must consist of two parts", 2, multipart.getCount());
    BodyPart dummyBodyPart = multipart.getBodyPart(0);
    Assert.assertEquals("first part must be pgp encrypted dummy part", "application/pgp-encrypted", dummyBodyPart.getContentType());
    assertContentOfBodyPartEquals("content must match the supplied detached signature", dummyBodyPart, "Version: 1");
    BodyPart encryptedBodyPart = multipart.getBodyPart(1);
    Assert.assertEquals("second part must be octet-stream of encrypted data", "application/octet-stream; name=\"encrypted.asc\"", encryptedBodyPart.getContentType());
    Assert.assertTrue("message body must be BinaryTempFileBody", encryptedBodyPart.getBody() instanceof BinaryTempFileBody);
    Assert.assertEquals(MimeUtil.ENC_7BIT, ((BinaryTempFileBody) encryptedBodyPart.getBody()).getEncoding());
}
Also used : BodyPart(com.fsck.k9.mail.BodyPart) Callback(com.fsck.k9.message.MessageBuilder.Callback) BinaryTempFileBody(com.fsck.k9.mail.internet.BinaryTempFileBody) MimeMessage(com.fsck.k9.mail.internet.MimeMessage) MimeMultipart(com.fsck.k9.mail.internet.MimeMultipart) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) ComposeCryptoStatus(com.fsck.k9.activity.compose.ComposeCryptoStatus) Recipient(com.fsck.k9.view.RecipientSelectView.Recipient) PendingIntent(android.app.PendingIntent) Intent(android.content.Intent) OpenPgpDataSource(org.openintents.openpgp.util.OpenPgpApi.OpenPgpDataSource) Test(org.junit.Test)

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