Search in sources :

Example 21 with Html

use of com.fsck.k9.mail.internet.Viewable.Html in project k-9 by k9mail.

the class TextPartFinderTest method findFirstTextPart_withMultipartAlternativeContainingOnlyTextHtmlPart.

@Test
public void findFirstTextPart_withMultipartAlternativeContainingOnlyTextHtmlPart() throws Exception {
    BodyPart expected = createTextPart("text/html");
    Part part = createMultipart("multipart/alternative", createPart("image/gif"), expected, createTextPart("text/html"));
    Part result = textPartFinder.findFirstTextPart(part);
    assertEquals(expected, result);
}
Also used : BodyPart(com.fsck.k9.mail.BodyPart) BodyPart(com.fsck.k9.mail.BodyPart) MessageCreationHelper.createEmptyPart(com.fsck.k9.message.MessageCreationHelper.createEmptyPart) Part(com.fsck.k9.mail.Part) MessageCreationHelper.createPart(com.fsck.k9.message.MessageCreationHelper.createPart) MessageCreationHelper.createTextPart(com.fsck.k9.message.MessageCreationHelper.createTextPart) Test(org.junit.Test)

Example 22 with Html

use of com.fsck.k9.mail.internet.Viewable.Html in project k-9 by k9mail.

the class MessageViewInfoExtractorTest method testMultipartDigestWithMessages.

@Test
public void testMultipartDigestWithMessages() throws Exception {
    String data = "Content-Type: multipart/digest; boundary=\"bndry\"\r\n" + "\r\n" + "--bndry\r\n" + "\r\n" + "Content-Type: text/plain\r\n" + "\r\n" + "text body of first message\r\n" + "\r\n" + "--bndry\r\n" + "\r\n" + "Subject: subject of second message\r\n" + "Content-Type: multipart/alternative; boundary=\"bndry2\"\r\n" + "\r\n" + "--bndry2\r\n" + "Content-Type: text/plain\r\n" + "\r\n" + "text part of second message\r\n" + "\r\n" + "--bndry2\r\n" + "Content-Type: text/html\"\r\n" + "\r\n" + "html part of second message\r\n" + "\r\n" + "--bndry2--\r\n" + "\r\n" + "--bndry--\r\n";
    MimeMessage message = MimeMessage.parseMimeMessage(new ByteArrayInputStream(data.getBytes()), false);
    // Extract text
    List<Part> outputNonViewableParts = new ArrayList<>();
    ArrayList<Viewable> outputViewableParts = new ArrayList<>();
    MessageExtractor.findViewablesAndAttachments(message, outputViewableParts, outputNonViewableParts);
    String expectedExtractedText = "Subject: (No subject)\r\n" + "\r\n" + "text body of first message\r\n" + "\r\n" + "\r\n" + "------------------------------------------------------------------------\r\n" + "\r\n" + "Subject: subject of second message\r\n" + "\r\n" + "text part of second message\r\n";
    String expectedHtmlText = "<table style=\"border: 0\">" + "<tr><th style=\"text-align: left; vertical-align: top;\">Subject:</th><td>(No subject)</td></tr>" + "</table>" + "<pre dir=\"auto\" class=\"k9mail\">text body of first message<br></pre>" + "<p style=\"margin-top: 2.5em; margin-bottom: 1em; border-bottom: 1px solid #000\"></p>" + "<table style=\"border: 0\">" + "<tr><th style=\"text-align: left; vertical-align: top;\">Subject:</th><td>subject of second message</td></tr>" + "</table>" + "<pre dir=\"auto\" class=\"k9mail\">text part of second message<br></pre>";
    assertEquals(4, outputViewableParts.size());
    assertEquals("subject of second message", ((MessageHeader) outputViewableParts.get(2)).getMessage().getSubject());
    ViewableExtractedText firstMessageExtractedText = messageViewInfoExtractor.extractTextFromViewables(outputViewableParts);
    assertEquals(expectedExtractedText, firstMessageExtractedText.text);
    assertEquals(expectedHtmlText, firstMessageExtractedText.html);
}
Also used : MimeMessage(com.fsck.k9.mail.internet.MimeMessage) ByteArrayInputStream(java.io.ByteArrayInputStream) MimeBodyPart(com.fsck.k9.mail.internet.MimeBodyPart) BodyPart(com.fsck.k9.mail.BodyPart) Part(com.fsck.k9.mail.Part) ArrayList(java.util.ArrayList) Viewable(com.fsck.k9.mail.internet.Viewable) ViewableExtractedText(com.fsck.k9.mailstore.MessageViewInfoExtractor.ViewableExtractedText) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) MessageHeader(com.fsck.k9.mail.internet.Viewable.MessageHeader) K9RobolectricTest(com.fsck.k9.K9RobolectricTest) Test(org.junit.Test)

Example 23 with Html

use of com.fsck.k9.mail.internet.Viewable.Html in project k-9 by k9mail.

the class TextPartFinderTest method findFirstTextPart_withMultipartAlternativeContainingMultipartRelatedContainingTextHtmlFirst.

@Test
public void findFirstTextPart_withMultipartAlternativeContainingMultipartRelatedContainingTextHtmlFirst() throws Exception {
    BodyPart expected = createTextPart("text/plain");
    Part part = createMultipart("multipart/alternative", createMultipart("multipart/related", createTextPart("text/html"), createPart("image/jpeg")), expected);
    Part result = textPartFinder.findFirstTextPart(part);
    assertEquals(expected, result);
}
Also used : BodyPart(com.fsck.k9.mail.BodyPart) BodyPart(com.fsck.k9.mail.BodyPart) MessageCreationHelper.createEmptyPart(com.fsck.k9.message.MessageCreationHelper.createEmptyPart) Part(com.fsck.k9.mail.Part) MessageCreationHelper.createPart(com.fsck.k9.message.MessageCreationHelper.createPart) MessageCreationHelper.createTextPart(com.fsck.k9.message.MessageCreationHelper.createTextPart) Test(org.junit.Test)

Example 24 with Html

use of com.fsck.k9.mail.internet.Viewable.Html in project k-9 by k9mail.

the class MessagingController method sendAlternate.

public void sendAlternate(Context context, Account account, LocalMessage message) {
    Timber.d("Got message %s:%s:%s for sendAlternate", account.getDescription(), message.getFolder(), message.getUid());
    Intent msg = new Intent(Intent.ACTION_SEND);
    String quotedText = null;
    Part part = MimeUtility.findFirstPartByMimeType(message, "text/plain");
    if (part == null) {
        part = MimeUtility.findFirstPartByMimeType(message, "text/html");
    }
    if (part != null) {
        quotedText = MessageExtractor.getTextFromPart(part);
    }
    if (quotedText != null) {
        msg.putExtra(Intent.EXTRA_TEXT, quotedText);
    }
    msg.putExtra(Intent.EXTRA_SUBJECT, message.getSubject());
    Address[] from = message.getFrom();
    String[] senders = new String[from.length];
    for (int i = 0; i < from.length; i++) {
        senders[i] = from[i].toString();
    }
    msg.putExtra(Intents.Share.EXTRA_FROM, senders);
    Address[] to = message.getRecipients(RecipientType.TO);
    String[] recipientsTo = new String[to.length];
    for (int i = 0; i < to.length; i++) {
        recipientsTo[i] = to[i].toString();
    }
    msg.putExtra(Intent.EXTRA_EMAIL, recipientsTo);
    Address[] cc = message.getRecipients(RecipientType.CC);
    String[] recipientsCc = new String[cc.length];
    for (int i = 0; i < cc.length; i++) {
        recipientsCc[i] = cc[i].toString();
    }
    msg.putExtra(Intent.EXTRA_CC, recipientsCc);
    msg.setType("text/plain");
    context.startActivity(Intent.createChooser(msg, context.getString(R.string.send_alternate_chooser_title)));
}
Also used : Address(com.fsck.k9.mail.Address) Part(com.fsck.k9.mail.Part) Intent(android.content.Intent) SuppressLint(android.annotation.SuppressLint)

Example 25 with Html

use of com.fsck.k9.mail.internet.Viewable.Html in project k-9 by k9mail.

the class MigrationTo51 method db51MigrateMessageFormat.

/**
     * This method converts from the old message table structure to the new one.
     *
     * This is a complex migration, and ultimately we do not have enough
     * information to recreate the mime structure of the original mails.
     * What we have:
     *  - general mail info
     *  - html_content and text_content data, which is the squashed readable content of the mail
     *  - a table with message headers
     *  - attachments
     *
     * What we need to do:
     *  - migrate general mail info as-is
     *  - flag mails as migrated for re-download
     *  - for each message, recreate a mime structure from its message content and attachments:
     *    + insert one or both of textContent and htmlContent, depending on mimeType
     *    + if mimeType is text/plain, text/html or multipart/alternative and no
     *      attachments are present, just insert that.
     *    + otherwise, use multipart/mixed, adding attachments after textual content
     *    + revert content:// URIs in htmlContent to original cid: URIs.
     */
public static void db51MigrateMessageFormat(SQLiteDatabase db, MigrationsHelper migrationsHelper) {
    renameOldMessagesTableAndCreateNew(db);
    copyMessageMetadataToNewTable(db);
    File attachmentDirNew, attachmentDirOld;
    Account account = migrationsHelper.getAccount();
    attachmentDirNew = StorageManager.getInstance(K9.app).getAttachmentDirectory(account.getUuid(), account.getLocalStorageProviderId());
    attachmentDirOld = renameOldAttachmentDirAndCreateNew(account, attachmentDirNew);
    Cursor msgCursor = db.query("messages_old", new String[] { "id", "flags", "html_content", "text_content", "mime_type", "attachment_count" }, null, null, null, null, null);
    try {
        Timber.d("migrating %d messages", msgCursor.getCount());
        ContentValues cv = new ContentValues();
        while (msgCursor.moveToNext()) {
            long messageId = msgCursor.getLong(0);
            String messageFlags = msgCursor.getString(1);
            String htmlContent = msgCursor.getString(2);
            String textContent = msgCursor.getString(3);
            String mimeType = msgCursor.getString(4);
            int attachmentCount = msgCursor.getInt(5);
            try {
                updateFlagsForMessage(db, messageId, messageFlags, migrationsHelper);
                MimeHeader mimeHeader = loadHeaderFromHeadersTable(db, messageId);
                MimeStructureState structureState = MimeStructureState.getNewRootState();
                boolean messageHadSpecialFormat = false;
                // we do not rely on the protocol parameter here but guess by the multipart structure
                boolean isMaybePgpMimeEncrypted = attachmentCount == 2 && MimeUtil.isSameMimeType(mimeType, "multipart/encrypted");
                if (isMaybePgpMimeEncrypted) {
                    MimeStructureState maybeStructureState = migratePgpMimeEncryptedContent(db, messageId, attachmentDirOld, attachmentDirNew, mimeHeader, structureState);
                    if (maybeStructureState != null) {
                        structureState = maybeStructureState;
                        messageHadSpecialFormat = true;
                    }
                }
                if (!messageHadSpecialFormat) {
                    boolean isSimpleStructured = attachmentCount == 0 && Utility.isAnyMimeType(mimeType, "text/plain", "text/html", "multipart/alternative");
                    if (isSimpleStructured) {
                        structureState = migrateSimpleMailContent(db, htmlContent, textContent, mimeType, mimeHeader, structureState);
                    } else {
                        mimeType = "multipart/mixed";
                        structureState = migrateComplexMailContent(db, attachmentDirOld, attachmentDirNew, messageId, htmlContent, textContent, mimeHeader, structureState);
                    }
                }
                cv.clear();
                cv.put("mime_type", mimeType);
                cv.put("message_part_id", structureState.rootPartId);
                cv.put("attachment_count", attachmentCount);
                db.update("messages", cv, "id = ?", new String[] { Long.toString(messageId) });
            } catch (IOException e) {
                Timber.e(e, "error inserting into database");
            }
        }
    } finally {
        msgCursor.close();
    }
    cleanUpOldAttachmentDirectory(attachmentDirOld);
    dropOldMessagesTable(db);
}
Also used : ContentValues(android.content.ContentValues) Account(com.fsck.k9.Account) MimeHeader(com.fsck.k9.mail.internet.MimeHeader) IOException(java.io.IOException) Cursor(android.database.Cursor) File(java.io.File)

Aggregations

Part (com.fsck.k9.mail.Part)31 BodyPart (com.fsck.k9.mail.BodyPart)24 Test (org.junit.Test)24 MessageCreationHelper.createTextPart (com.fsck.k9.message.MessageCreationHelper.createTextPart)12 MessageCreationHelper.createEmptyPart (com.fsck.k9.message.MessageCreationHelper.createEmptyPart)11 MessageCreationHelper.createPart (com.fsck.k9.message.MessageCreationHelper.createPart)11 ArrayList (java.util.ArrayList)11 MimeMessage (com.fsck.k9.mail.internet.MimeMessage)9 Body (com.fsck.k9.mail.Body)7 MimeBodyPart (com.fsck.k9.mail.internet.MimeBodyPart)7 Message (com.fsck.k9.mail.Message)6 Multipart (com.fsck.k9.mail.Multipart)6 TextBody (com.fsck.k9.mail.internet.TextBody)6 Viewable (com.fsck.k9.mail.internet.Viewable)6 Alternative (com.fsck.k9.mail.internet.Viewable.Alternative)4 Html (com.fsck.k9.mail.internet.Viewable.Html)4 Text (com.fsck.k9.mail.internet.Viewable.Text)4 MessageFormat (com.fsck.k9.Account.MessageFormat)3 K9RobolectricTest (com.fsck.k9.K9RobolectricTest)3 MessagingException (com.fsck.k9.mail.MessagingException)3