Пример #1
0
  @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:[email protected]"));

    Assert.assertEquals("image/jpeg", msgBody.getBodyPart(1).getMimeType());
  }
Пример #2
0
  @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());
  }
Пример #3
0
  @Test
  public void migratePgpMimeEncryptedMessage() throws Exception {
    SQLiteDatabase db = createV50Database();
    insertPgpMimeEncryptedMessage(db);
    db.close();

    LocalStore localStore = LocalStore.getInstance(account, RuntimeEnvironment.application);

    LocalMessage msg = localStore.getFolder("dev").getMessage("6");
    FetchProfile fp = new FetchProfile();
    fp.add(FetchProfile.Item.BODY);
    localStore.getFolder("dev").fetch(Collections.singletonList(msg), fp, null);

    Assert.assertEquals(5, msg.getId());
    Assert.assertEquals(13, msg.getHeaderNames().size());
    Assert.assertEquals("multipart/encrypted", msg.getMimeType());
    Assert.assertEquals(2, msg.getAttachmentCount());

    Multipart body = (Multipart) msg.getBody();
    Assert.assertEquals(1, msg.getHeader(MimeHeader.HEADER_CONTENT_TYPE).length);
    Assert.assertEquals(
        "application/pgp-encrypted",
        MimeUtility.getHeaderParameter(
            msg.getHeader(MimeHeader.HEADER_CONTENT_TYPE)[0], "protocol"));
    Assert.assertEquals(
        "UoPmpPX/dBe4BELn",
        MimeUtility.getHeaderParameter(
            msg.getHeader(MimeHeader.HEADER_CONTENT_TYPE)[0], "boundary"));
    Assert.assertEquals("UoPmpPX/dBe4BELn", body.getBoundary());
    Assert.assertEquals(2, body.getCount());

    Assert.assertEquals("application/pgp-encrypted", body.getBodyPart(0).getMimeType());
    Assert.assertEquals("application/octet-stream", body.getBodyPart(1).getMimeType());
  }
Пример #4
0
  @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);
  }
Пример #5
0
  @Override
  public void fetch(
      List<WebDavMessage> messages,
      FetchProfile fp,
      MessageRetrievalListener<WebDavMessage> listener)
      throws MessagingException {
    if (messages == null || messages.isEmpty()) {
      return;
    }

    /** Fetch message envelope information for the array */
    if (fp.contains(FetchProfile.Item.ENVELOPE)) {
      fetchEnvelope(messages, listener);
    }
    /** Fetch message flag info for the array */
    if (fp.contains(FetchProfile.Item.FLAGS)) {
      fetchFlags(messages, listener);
    }

    if (fp.contains(FetchProfile.Item.BODY_SANE)) {
      int maximumAutoDownloadSize = store.getStoreConfig().getMaximumAutoDownloadMessageSize();
      if (maximumAutoDownloadSize > 0) {
        fetchMessages(messages, listener, (maximumAutoDownloadSize / 76));
      } else {
        fetchMessages(messages, listener, -1);
      }
    }
    if (fp.contains(FetchProfile.Item.BODY)) {
      fetchMessages(messages, listener, -1);
    }
  }
Пример #6
0
    public List<Message> getMessages(final int max, final boolean flagged, final Date since)
        throws MessagingException {
      if (LOCAL_LOGV)
        Log.v(TAG, String.format(Locale.ENGLISH, "getMessages(%d, %b, %s)", max, flagged, since));

      final List<Message> messages;
      final ImapSearcher searcher =
          new ImapSearcher() {
            @Override
            public List<ImapResponse> search() throws IOException, MessagingException {
              final StringBuilder sb =
                  new StringBuilder("UID SEARCH 1:*")
                      .append(' ')
                      .append(getQuery())
                      .append(" UNDELETED");
              if (since != null) sb.append(" SENTSINCE ").append(RFC3501_DATE.format(since));
              if (flagged) sb.append(" FLAGGED");

              return executeSimpleCommand(sb.toString().trim());
            }
          };

      final Message[] msgs = search(searcher, null);

      Log.i(
          TAG, "Found " + msgs.length + " msgs" + (since == null ? "" : " (since " + since + ")"));
      if (max > 0 && msgs.length > max) {
        if (LOCAL_LOGV) Log.v(TAG, "Fetching envelopes");

        FetchProfile fp = new FetchProfile();
        fp.add(FetchProfile.Item.DATE);
        fetch(msgs, fp, null);

        if (LOCAL_LOGV) Log.v(TAG, "Sorting");
        // Debug.startMethodTracing("sorting");
        Arrays.sort(msgs, MessageComparator.INSTANCE);
        // Debug.stopMethodTracing();
        if (LOCAL_LOGV) Log.v(TAG, "Sorting done");

        messages = new ArrayList<Message>(max);
        // messages.addAll(Arrays.asList(msgs).subList(0, max));
        int aSize = Arrays.asList(msgs).size();
        messages.addAll(Arrays.asList(msgs).subList(aSize - max, aSize));
      } else {
        messages = new ArrayList<Message>(msgs.length);
        Collections.addAll(messages, msgs);
      }

      // Collections.reverse(messages);

      return messages;
    }
Пример #7
0
  @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("k9small.png", attachmentPart.getDisplayName());
    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"));
    Assert.assertTrue(attachmentPart.isFirstClassAttachment());

    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());
  }
Пример #8
0
    public Message[] getMessagesSince(final Date since, final int max, final boolean flagged)
        throws MessagingException {
      ImapSearcher searcher =
          new ImapSearcher() {
            public List<ImapResponse> search() throws IOException, MessagingException {
              String sentSince = since != null ? " SENTSINCE " + RFC3501_DATE.format(since) : "";
              return executeSimpleCommand(
                  "UID SEARCH 1:* NOT DELETED" + sentSince + (flagged ? " FLAGGED" : ""));
            }
          };

      Message[] msgs = search(searcher, null);

      Log.i(
          TAG, "Found " + msgs.length + " msgs" + (since == null ? "" : " (since " + since + ")"));
      if (max > 0 && msgs.length > max) {
        if (LOCAL_LOGV) Log.v(TAG, "Fetching envelopes");

        FetchProfile fp = new FetchProfile();
        fp.add(FetchProfile.Item.DATE);
        fetch(msgs, fp, null);

        if (LOCAL_LOGV) Log.v(TAG, "Sorting");
        // Debug.startMethodTracing("sorting");
        Arrays.sort(
            msgs,
            new Comparator<Message>() {
              public int compare(Message m1, Message m2) {
                return (m2 != null
                        && m2.getSentDate() != null
                        && m1 != null
                        && m1.getSentDate() != null)
                    ? m2.getSentDate().compareTo(m1.getSentDate())
                    : -1;
              }
            });
        // Debug.stopMethodTracing();
        if (LOCAL_LOGV) Log.v(TAG, "Sorting done");

        Message[] recent = new Message[max];
        System.arraycopy(msgs, 0, recent, 0, max);

        return recent;
      }
      return msgs;
    }
Пример #9
0
  @Test
  public void migrateTextHtml() throws Exception {
    SQLiteDatabase db = createV50Database();
    insertMultipartAlternativeMessage(db);
    db.close();

    LocalStore localStore = LocalStore.getInstance(account, RuntimeEnvironment.application);

    LocalMessage msg = localStore.getFolder("dev").getMessage("9");
    FetchProfile fp = new FetchProfile();
    fp.add(FetchProfile.Item.BODY);
    localStore.getFolder("dev").fetch(Collections.singletonList(msg), fp, null);

    Assert.assertEquals(8, msg.getId());
    Assert.assertEquals(9, msg.getHeaderNames().size());
    Assert.assertEquals("multipart/alternative", msg.getMimeType());
    Assert.assertEquals(0, msg.getAttachmentCount());

    Multipart msgBody = (Multipart) msg.getBody();
    Assert.assertEquals("------------060200010509000000040004", msgBody.getBoundary());
  }
Пример #10
0
  @Test
  public void migratePgpInlineClearsignedMessage() throws Exception {
    SQLiteDatabase db = createV50Database();
    insertPgpInlineClearsignedMessage(db);
    db.close();

    LocalStore localStore = LocalStore.getInstance(account, RuntimeEnvironment.application);

    LocalMessage msg = localStore.getFolder("dev").getMessage("8");
    FetchProfile fp = new FetchProfile();
    fp.add(FetchProfile.Item.BODY);
    localStore.getFolder("dev").fetch(Collections.singletonList(msg), fp, null);

    Assert.assertEquals(7, msg.getId());
    Assert.assertEquals(12, msg.getHeaderNames().size());
    Assert.assertEquals("text/plain", msg.getMimeType());
    Assert.assertEquals(0, msg.getAttachmentCount());
    Assert.assertTrue(msg.getBody() instanceof BinaryMemoryBody);

    String msgTextContent = MessageExtractor.getTextFromPart(msg);
    Assert.assertEquals(
        OpenPgpUtils.PARSE_RESULT_SIGNED_MESSAGE, OpenPgpUtils.parseMessage(msgTextContent));
  }
Пример #11
0
 /**
  * Fetch the items contained in the FetchProfile into the given set of Messages in as efficient
  * a manner as possible.
  *
  * @param messages
  * @param fp
  * @throws MessagingException
  */
 @Override
 public void fetch(
     List<Pop3Message> messages, FetchProfile fp, MessageRetrievalListener<Pop3Message> listener)
     throws MessagingException {
   if (messages == null || messages.isEmpty()) {
     return;
   }
   List<String> uids = new ArrayList<String>();
   for (Message message : messages) {
     uids.add(message.getUid());
   }
   try {
     indexUids(uids);
   } catch (IOException ioe) {
     throw new MessagingException("fetch", ioe);
   }
   try {
     if (fp.contains(FetchProfile.Item.ENVELOPE)) {
       /*
        * We pass the listener only if there are other things to do in the
        * FetchProfile. Since fetchEnvelop works in bulk and eveything else
        * works one at a time if we let fetchEnvelope send events the
        * event would get sent twice.
        */
       fetchEnvelope(messages, fp.size() == 1 ? listener : null);
     }
   } catch (IOException ioe) {
     throw new MessagingException("fetch", ioe);
   }
   for (int i = 0, count = messages.size(); i < count; i++) {
     Pop3Message pop3Message = messages.get(i);
     try {
       if (listener != null && !fp.contains(FetchProfile.Item.ENVELOPE)) {
         listener.messageStarted(pop3Message.getUid(), i, count);
       }
       if (fp.contains(FetchProfile.Item.BODY)) {
         fetchBody(pop3Message, -1);
       } else if (fp.contains(FetchProfile.Item.BODY_SANE)) {
         /*
          * To convert the suggested download size we take the size
          * divided by the maximum line size (76).
          */
         if (mStoreConfig.getMaximumAutoDownloadMessageSize() > 0) {
           fetchBody(pop3Message, (mStoreConfig.getMaximumAutoDownloadMessageSize() / 76));
         } else {
           fetchBody(pop3Message, -1);
         }
       } else if (fp.contains(FetchProfile.Item.STRUCTURE)) {
         /*
          * If the user is requesting STRUCTURE we are required to set the body
          * to null since we do not support the function.
          */
         pop3Message.setBody(null);
       }
       if (listener != null && !(fp.contains(FetchProfile.Item.ENVELOPE) && fp.size() == 1)) {
         listener.messageFinished(pop3Message, i, count);
       }
     } catch (IOException ioe) {
       throw new MessagingException("Unable to fetch message", ioe);
     }
   }
 }