Search in sources :

Example 26 with FetchProfile

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

the class ImapFolder method fetch.

@Override
public void fetch(List<ImapMessage> messages, FetchProfile fetchProfile, MessageRetrievalListener<ImapMessage> listener) throws MessagingException {
    if (messages == null || messages.isEmpty()) {
        return;
    }
    checkOpen();
    List<String> uids = new ArrayList<>(messages.size());
    HashMap<String, Message> messageMap = new HashMap<>();
    for (Message message : messages) {
        String uid = message.getUid();
        uids.add(uid);
        messageMap.put(uid, message);
    }
    Set<String> fetchFields = new LinkedHashSet<>();
    fetchFields.add("UID");
    if (fetchProfile.contains(FetchProfile.Item.FLAGS)) {
        fetchFields.add("FLAGS");
    }
    if (fetchProfile.contains(FetchProfile.Item.ENVELOPE)) {
        fetchFields.add("INTERNALDATE");
        fetchFields.add("RFC822.SIZE");
        fetchFields.add("BODY.PEEK[HEADER.FIELDS (date subject from content-type to cc " + "reply-to message-id references in-reply-to " + K9MailLib.IDENTITY_HEADER + ")]");
    }
    if (fetchProfile.contains(FetchProfile.Item.STRUCTURE)) {
        fetchFields.add("BODYSTRUCTURE");
    }
    if (fetchProfile.contains(FetchProfile.Item.BODY_SANE)) {
        int maximumAutoDownloadMessageSize = store.getStoreConfig().getMaximumAutoDownloadMessageSize();
        if (maximumAutoDownloadMessageSize > 0) {
            fetchFields.add(String.format(Locale.US, "BODY.PEEK[]<0.%d>", maximumAutoDownloadMessageSize));
        } else {
            fetchFields.add("BODY.PEEK[]");
        }
    }
    if (fetchProfile.contains(FetchProfile.Item.BODY)) {
        fetchFields.add("BODY.PEEK[]");
    }
    String spaceSeparatedFetchFields = combine(fetchFields.toArray(new String[fetchFields.size()]), ' ');
    for (int windowStart = 0; windowStart < messages.size(); windowStart += (FETCH_WINDOW_SIZE)) {
        int windowEnd = Math.min(windowStart + FETCH_WINDOW_SIZE, messages.size());
        List<String> uidWindow = uids.subList(windowStart, windowEnd);
        try {
            String commaSeparatedUids = combine(uidWindow.toArray(new String[uidWindow.size()]), ',');
            String command = String.format("UID FETCH %s (%s)", commaSeparatedUids, spaceSeparatedFetchFields);
            connection.sendCommand(command, false);
            ImapResponse response;
            int messageNumber = 0;
            ImapResponseCallback callback = null;
            if (fetchProfile.contains(FetchProfile.Item.BODY) || fetchProfile.contains(FetchProfile.Item.BODY_SANE)) {
                callback = new FetchBodyCallback(messageMap);
            }
            do {
                response = connection.readResponse(callback);
                if (response.getTag() == null && ImapResponseParser.equalsIgnoreCase(response.get(1), "FETCH")) {
                    ImapList fetchList = (ImapList) response.getKeyedValue("FETCH");
                    String uid = fetchList.getKeyedString("UID");
                    long msgSeq = response.getLong(0);
                    if (uid != null) {
                        try {
                            msgSeqUidMap.put(msgSeq, uid);
                            if (K9MailLib.isDebug()) {
                                Log.v(LOG_TAG, "Stored uid '" + uid + "' for msgSeq " + msgSeq + " into map");
                            }
                        } catch (Exception e) {
                            Log.e(LOG_TAG, "Unable to store uid '" + uid + "' for msgSeq " + msgSeq);
                        }
                    }
                    Message message = messageMap.get(uid);
                    if (message == null) {
                        if (K9MailLib.isDebug()) {
                            Log.d(LOG_TAG, "Do not have message in messageMap for UID " + uid + " for " + getLogId());
                        }
                        handleUntaggedResponse(response);
                        continue;
                    }
                    if (listener != null) {
                        listener.messageStarted(uid, messageNumber++, messageMap.size());
                    }
                    ImapMessage imapMessage = (ImapMessage) message;
                    Object literal = handleFetchResponse(imapMessage, fetchList);
                    if (literal != null) {
                        if (literal instanceof String) {
                            String bodyString = (String) literal;
                            InputStream bodyStream = new ByteArrayInputStream(bodyString.getBytes());
                            imapMessage.parse(bodyStream);
                        } else if (literal instanceof Integer) {
                        // All the work was done in FetchBodyCallback.foundLiteral()
                        } else {
                            // This shouldn't happen
                            throw new MessagingException("Got FETCH response with bogus parameters");
                        }
                    }
                    if (listener != null) {
                        listener.messageFinished(imapMessage, messageNumber, messageMap.size());
                    }
                } else {
                    handleUntaggedResponse(response);
                }
            } while (response.getTag() == null);
        } catch (IOException ioe) {
            throw ioExceptionHandler(connection, ioe);
        }
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Message(com.fsck.k9.mail.Message) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) MessagingException(com.fsck.k9.mail.MessagingException) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) IOException(java.io.IOException) IOException(java.io.IOException) MessagingException(com.fsck.k9.mail.MessagingException) ByteArrayInputStream(java.io.ByteArrayInputStream)

Example 27 with FetchProfile

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

the class MigrationTest method migrateTextPlain.

@Test
public void migrateTextPlain() throws Exception {
    SQLiteDatabase db = createV50Database();
    insertSimplePlaintextMessage(db);
    db.close();
    LocalStore localStore = LocalStore.getInstance(account, RuntimeEnvironment.application);
    LocalMessage msg = localStore.getFolder("dev").getMessage("3");
    FetchProfile fp = new FetchProfile();
    fp.add(FetchProfile.Item.BODY);
    localStore.getFolder("dev").fetch(Collections.singletonList(msg), fp, null);
    Assert.assertEquals("text/plain", msg.getMimeType());
    Assert.assertEquals(2, msg.getId());
    Assert.assertEquals(13, msg.getHeaderNames().size());
    Assert.assertEquals(0, msg.getAttachmentCount());
    Assert.assertEquals(1, msg.getHeader("User-Agent").length);
    Assert.assertEquals("Mutt/1.5.24 (2015-08-30)", msg.getHeader("User-Agent")[0]);
    Assert.assertEquals(1, msg.getHeader(MimeHeader.HEADER_CONTENT_TYPE).length);
    Assert.assertEquals("text/plain", MimeUtility.getHeaderParameter(msg.getHeader(MimeHeader.HEADER_CONTENT_TYPE)[0], null));
    Assert.assertEquals("utf-8", MimeUtility.getHeaderParameter(msg.getHeader(MimeHeader.HEADER_CONTENT_TYPE)[0], "charset"));
    Assert.assertTrue(msg.getBody() instanceof BinaryMemoryBody);
    String msgTextContent = MessageExtractor.getTextFromPart(msg);
    Assert.assertEquals("nothing special here.\r\n", msgTextContent);
}
Also used : FetchProfile(com.fsck.k9.mail.FetchProfile) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) Test(org.junit.Test)

Example 28 with FetchProfile

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

the class MigrationTest method migrateHtmlWithRelatedMessage.

@Test
public void migrateHtmlWithRelatedMessage() throws Exception {
    SQLiteDatabase db = createV50Database();
    insertHtmlWithRelatedMessage(db);
    db.close();
    LocalStore localStore = LocalStore.getInstance(account, RuntimeEnvironment.application);
    LocalMessage msg = localStore.getFolder("dev").getMessage("10");
    FetchProfile fp = new FetchProfile();
    fp.add(FetchProfile.Item.BODY);
    localStore.getFolder("dev").fetch(Collections.singletonList(msg), fp, null);
    Assert.assertEquals(9, msg.getId());
    Assert.assertEquals(11, msg.getHeaderNames().size());
    Assert.assertEquals("multipart/mixed", msg.getMimeType());
    Assert.assertEquals(1, msg.getAttachmentCount());
    Multipart msgBody = (Multipart) msg.getBody();
    Assert.assertEquals("------------050707070308090509030605", msgBody.getBoundary());
    Multipart multipartAlternativePart = (Multipart) msgBody.getBodyPart(0).getBody();
    BodyPart htmlPart = multipartAlternativePart.getBodyPart(1);
    String msgTextContent = MessageExtractor.getTextFromPart(htmlPart);
    Assert.assertNotNull(msgTextContent);
    Assert.assertTrue(msgTextContent.contains("cid:part1.07090108.09020601@example.org"));
    Assert.assertEquals("image/jpeg", msgBody.getBodyPart(1).getMimeType());
}
Also used : BodyPart(com.fsck.k9.mail.BodyPart) FetchProfile(com.fsck.k9.mail.FetchProfile) Multipart(com.fsck.k9.mail.Multipart) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) Test(org.junit.Test)

Example 29 with FetchProfile

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

the class MigrationTest method migrateMixedWithAttachments.

@Test
public void migrateMixedWithAttachments() throws Exception {
    SQLiteDatabase db = createV50Database();
    insertMixedWithAttachments(db);
    db.close();
    LocalStore localStore = LocalStore.getInstance(account, RuntimeEnvironment.application);
    LocalMessage msg = localStore.getFolder("dev").getMessage("4");
    FetchProfile fp = new FetchProfile();
    fp.add(FetchProfile.Item.BODY);
    localStore.getFolder("dev").fetch(Collections.singletonList(msg), fp, null);
    Assert.assertEquals(3, msg.getId());
    Assert.assertEquals(8, msg.getHeaderNames().size());
    Assert.assertEquals("multipart/mixed", msg.getMimeType());
    Assert.assertEquals(1, msg.getHeader(MimeHeader.HEADER_CONTENT_TYPE).length);
    Assert.assertEquals("multipart/mixed", MimeUtility.getHeaderParameter(msg.getHeader(MimeHeader.HEADER_CONTENT_TYPE)[0], null));
    Assert.assertEquals("----5D6OUTIYLNN2X63O0R2M0V53TOUAQP", MimeUtility.getHeaderParameter(msg.getHeader(MimeHeader.HEADER_CONTENT_TYPE)[0], "boundary"));
    Assert.assertEquals(2, msg.getAttachmentCount());
    Multipart body = (Multipart) msg.getBody();
    Assert.assertEquals(3, body.getCount());
    Assert.assertEquals("multipart/alternative", body.getBodyPart(0).getMimeType());
    LocalBodyPart attachmentPart = (LocalBodyPart) body.getBodyPart(1);
    Assert.assertEquals("image/png", attachmentPart.getMimeType());
    Assert.assertEquals("2", attachmentPart.getServerExtra());
    Assert.assertEquals("attachment", MimeUtility.getHeaderParameter(attachmentPart.getDisposition(), null));
    Assert.assertEquals("k9small.png", MimeUtility.getHeaderParameter(attachmentPart.getDisposition(), "filename"));
    Assert.assertEquals("2250", MimeUtility.getHeaderParameter(attachmentPart.getDisposition(), "size"));
    FileBackedBody attachmentBody = (FileBackedBody) attachmentPart.getBody();
    Assert.assertEquals(2250, attachmentBody.getSize());
    Assert.assertEquals(MimeUtil.ENC_BINARY, attachmentBody.getEncoding());
    Assert.assertEquals("application/whatevs", body.getBodyPart(2).getMimeType());
    Assert.assertNull(body.getBodyPart(2).getBody());
}
Also used : FetchProfile(com.fsck.k9.mail.FetchProfile) Multipart(com.fsck.k9.mail.Multipart) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) Test(org.junit.Test)

Example 30 with FetchProfile

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

the class MigrationTest method migratePgpMimeSignedMessage.

@Test
public void migratePgpMimeSignedMessage() throws Exception {
    SQLiteDatabase db = createV50Database();
    insertPgpMimeSignedMessage(db);
    db.close();
    LocalStore localStore = LocalStore.getInstance(account, RuntimeEnvironment.application);
    LocalMessage msg = localStore.getFolder("dev").getMessage("5");
    FetchProfile fp = new FetchProfile();
    fp.add(FetchProfile.Item.BODY);
    localStore.getFolder("dev").fetch(Collections.singletonList(msg), fp, null);
    Assert.assertEquals(4, msg.getId());
    Assert.assertEquals(8, msg.getHeaderNames().size());
    Assert.assertEquals("multipart/mixed", msg.getMimeType());
    Assert.assertEquals(2, msg.getAttachmentCount());
    Multipart body = (Multipart) msg.getBody();
    Assert.assertEquals(3, body.getCount());
    Assert.assertEquals("multipart/alternative", body.getBodyPart(0).getMimeType());
    Assert.assertEquals("image/png", body.getBodyPart(1).getMimeType());
    Assert.assertEquals("application/pgp-signature", body.getBodyPart(2).getMimeType());
}
Also used : FetchProfile(com.fsck.k9.mail.FetchProfile) Multipart(com.fsck.k9.mail.Multipart) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) Test(org.junit.Test)

Aggregations

FetchProfile (com.fsck.k9.mail.FetchProfile)49 Test (org.junit.Test)27 LocalMessage (com.fsck.k9.mailstore.LocalMessage)15 MessagingException (com.fsck.k9.mail.MessagingException)12 ArrayList (java.util.ArrayList)10 LocalFolder (com.fsck.k9.mailstore.LocalFolder)9 LocalStore (com.fsck.k9.mailstore.LocalStore)9 SQLiteDatabase (android.database.sqlite.SQLiteDatabase)8 Date (java.util.Date)8 Message (com.fsck.k9.mail.Message)7 SuppressLint (android.annotation.SuppressLint)6 MimeMessage (com.fsck.k9.mail.internet.MimeMessage)6 AuthenticationFailedException (com.fsck.k9.mail.AuthenticationFailedException)5 Multipart (com.fsck.k9.mail.Multipart)5 ByteArrayInputStream (java.io.ByteArrayInputStream)5 IOException (java.io.IOException)5 WebDavMessage (com.fsck.k9.mail.store.webdav.WebDavMessage)4 LinkedList (java.util.LinkedList)4 HttpResponse (org.apache.http.HttpResponse)4 StatusLine (org.apache.http.StatusLine)4