Exemplo n.º 1
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);
  }
Exemplo n.º 2
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());
  }
Exemplo n.º 3
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());
  }
Exemplo n.º 4
0
  private String getType(String dbName, String id, String format) {
    String type;
    if (FORMAT_THUMBNAIL.equals(format)) {
      type = "image/png";
    } else {
      final Account account = Preferences.getPreferences(getContext()).getAccount(dbName);

      try {
        final LocalStore localStore = LocalStore.getLocalInstance(account, K9.app);

        AttachmentInfo attachmentInfo = localStore.getAttachmentInfo(id);
        if (FORMAT_VIEW.equals(format)) {
          type = MimeUtility.getMimeTypeForViewing(attachmentInfo.type, attachmentInfo.name);
        } else {
          // When accessing the "raw" message we deliver the original
          // MIME type.
          type = attachmentInfo.type;
        }
      } catch (MessagingException e) {
        Log.e(K9.LOG_TAG, "Unable to retrieve LocalStore for " + account, e);
        type = null;
      }
    }

    return type;
  }
Exemplo n.º 5
0
  @Override
  public void sendMessage(Message message) throws MessagingException {
    ArrayList<Address> addresses = new ArrayList<Address>();
    {
      addresses.addAll(Arrays.asList(message.getRecipients(RecipientType.TO)));
      addresses.addAll(Arrays.asList(message.getRecipients(RecipientType.CC)));
      addresses.addAll(Arrays.asList(message.getRecipients(RecipientType.BCC)));
    }
    message.setRecipients(RecipientType.BCC, null);

    HashMap<String, ArrayList<String>> charsetAddressesMap =
        new HashMap<String, ArrayList<String>>();
    for (Address address : addresses) {
      String addressString = address.getAddress();
      String charset = MimeUtility.getCharsetFromAddress(addressString);
      ArrayList<String> addressesOfCharset = charsetAddressesMap.get(charset);
      if (addressesOfCharset == null) {
        addressesOfCharset = new ArrayList<String>();
        charsetAddressesMap.put(charset, addressesOfCharset);
      }
      addressesOfCharset.add(addressString);
    }

    for (Map.Entry<String, ArrayList<String>> charsetAddressesMapEntry :
        charsetAddressesMap.entrySet()) {
      String charset = charsetAddressesMapEntry.getKey();
      ArrayList<String> addressesOfCharset = charsetAddressesMapEntry.getValue();
      message.setCharset(charset);
      sendMessageTo(addressesOfCharset, message);
    }
  }
Exemplo n.º 6
0
 /**
  * Set up the additional headers text view with the supplied header data.
  *
  * @param additionalHeaders List of header entries. Each entry consists of a header name and a
  *     header value. Header names may appear multiple times.
  *     <p>This method is always called from within the UI thread by {@link
  *     #showAdditionalHeaders()}.
  */
 private void populateAdditionalHeadersView(final List<HeaderEntry> additionalHeaders) {
   SpannableStringBuilder sb = new SpannableStringBuilder();
   boolean first = true;
   for (HeaderEntry additionalHeader : additionalHeaders) {
     if (!first) {
       sb.append("\n");
     } else {
       first = false;
     }
     StyleSpan boldSpan = new StyleSpan(Typeface.BOLD);
     SpannableString label = new SpannableString(additionalHeader.label + ": ");
     label.setSpan(boldSpan, 0, label.length(), 0);
     sb.append(label);
     sb.append(MimeUtility.unfoldAndDecode(additionalHeader.value));
   }
   mAdditionalHeadersView.setText(sb);
 }
Exemplo n.º 7
0
 private Bitmap createThumbnail(String type, InputStream data) {
   if (MimeUtility.mimeTypeMatches(type, "image/*")) {
     return createImageThumbnail(data);
   }
   return null;
 }