mirror of
https://github.com/moparisthebest/k-9
synced 2024-11-24 02:12:15 -05:00
Move "magic" from Part.setBody() implementations to MimeMessageHelper.setBody()
Now adding message bodies during parsing won't set/modify headers.
This commit is contained in:
parent
51a60b5ad3
commit
d32d6eed0e
@ -104,6 +104,7 @@ import com.fsck.k9.mail.Part;
|
|||||||
import com.fsck.k9.mail.internet.MimeBodyPart;
|
import com.fsck.k9.mail.internet.MimeBodyPart;
|
||||||
import com.fsck.k9.mail.internet.MimeHeader;
|
import com.fsck.k9.mail.internet.MimeHeader;
|
||||||
import com.fsck.k9.mail.internet.MimeMessage;
|
import com.fsck.k9.mail.internet.MimeMessage;
|
||||||
|
import com.fsck.k9.mail.internet.MimeMessageHelper;
|
||||||
import com.fsck.k9.mail.internet.MimeMultipart;
|
import com.fsck.k9.mail.internet.MimeMultipart;
|
||||||
import com.fsck.k9.mail.internet.MimeUtility;
|
import com.fsck.k9.mail.internet.MimeUtility;
|
||||||
import com.fsck.k9.mail.internet.TextBody;
|
import com.fsck.k9.mail.internet.TextBody;
|
||||||
@ -1404,10 +1405,10 @@ public class MessageCompose extends K9Activity implements OnClickListener,
|
|||||||
MimeMultipart mp = new MimeMultipart();
|
MimeMultipart mp = new MimeMultipart();
|
||||||
mp.addBodyPart(new MimeBodyPart(composedMimeMessage));
|
mp.addBodyPart(new MimeBodyPart(composedMimeMessage));
|
||||||
addAttachmentsToMessage(mp);
|
addAttachmentsToMessage(mp);
|
||||||
message.setBody(mp);
|
MimeMessageHelper.setBody(message, mp);
|
||||||
} else {
|
} else {
|
||||||
// If no attachments, our multipart/alternative part is the only one we need.
|
// If no attachments, our multipart/alternative part is the only one we need.
|
||||||
message.setBody(composedMimeMessage);
|
MimeMessageHelper.setBody(message, composedMimeMessage);
|
||||||
}
|
}
|
||||||
} else if (mMessageFormat == SimpleMessageFormat.TEXT) {
|
} else if (mMessageFormat == SimpleMessageFormat.TEXT) {
|
||||||
// Text-only message.
|
// Text-only message.
|
||||||
@ -1415,10 +1416,10 @@ public class MessageCompose extends K9Activity implements OnClickListener,
|
|||||||
MimeMultipart mp = new MimeMultipart();
|
MimeMultipart mp = new MimeMultipart();
|
||||||
mp.addBodyPart(new MimeBodyPart(body, "text/plain"));
|
mp.addBodyPart(new MimeBodyPart(body, "text/plain"));
|
||||||
addAttachmentsToMessage(mp);
|
addAttachmentsToMessage(mp);
|
||||||
message.setBody(mp);
|
MimeMessageHelper.setBody(message, mp);
|
||||||
} else {
|
} else {
|
||||||
// No attachments to include, just stick the text body in the message and call it good.
|
// No attachments to include, just stick the text body in the message and call it good.
|
||||||
message.setBody(body);
|
MimeMessageHelper.setBody(message, body);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -79,6 +79,7 @@ import com.fsck.k9.mail.Pusher;
|
|||||||
import com.fsck.k9.mail.Store;
|
import com.fsck.k9.mail.Store;
|
||||||
import com.fsck.k9.mail.Transport;
|
import com.fsck.k9.mail.Transport;
|
||||||
import com.fsck.k9.mail.internet.MimeMessage;
|
import com.fsck.k9.mail.internet.MimeMessage;
|
||||||
|
import com.fsck.k9.mail.internet.MimeMessageHelper;
|
||||||
import com.fsck.k9.mail.internet.MimeUtility;
|
import com.fsck.k9.mail.internet.MimeUtility;
|
||||||
import com.fsck.k9.mail.internet.TextBody;
|
import com.fsck.k9.mail.internet.TextBody;
|
||||||
import com.fsck.k9.mail.store.local.LocalFolder;
|
import com.fsck.k9.mail.store.local.LocalFolder;
|
||||||
@ -2712,7 +2713,7 @@ public class MessagingController implements Runnable {
|
|||||||
LocalFolder localFolder = (LocalFolder)localStore.getFolder(account.getErrorFolderName());
|
LocalFolder localFolder = (LocalFolder)localStore.getFolder(account.getErrorFolderName());
|
||||||
MimeMessage message = new MimeMessage();
|
MimeMessage message = new MimeMessage();
|
||||||
|
|
||||||
message.setBody(new TextBody(body));
|
MimeMessageHelper.setBody(message, new TextBody(body));
|
||||||
message.setFlag(Flag.X_DOWNLOADED_FULL, true);
|
message.setFlag(Flag.X_DOWNLOADED_FULL, true);
|
||||||
message.setSubject(subject);
|
message.setSubject(subject);
|
||||||
|
|
||||||
@ -3205,7 +3206,7 @@ public class MessagingController implements Runnable {
|
|||||||
|
|
||||||
//FIXME: This is an ugly hack that won't be needed once the Message objects have been united.
|
//FIXME: This is an ugly hack that won't be needed once the Message objects have been united.
|
||||||
Message remoteMessage = remoteFolder.getMessage(message.getUid());
|
Message remoteMessage = remoteFolder.getMessage(message.getUid());
|
||||||
remoteMessage.setBody(message.getBody());
|
MimeMessageHelper.setBody(remoteMessage, message.getBody());
|
||||||
remoteFolder.fetchPart(remoteMessage, part, null);
|
remoteFolder.fetchPart(remoteMessage, part, null);
|
||||||
|
|
||||||
localFolder.updateMessage((LocalMessage)message);
|
localFolder.updateMessage((LocalMessage)message);
|
||||||
|
@ -35,7 +35,7 @@ public class MimeBodyPart extends BodyPart {
|
|||||||
if (mimeType != null) {
|
if (mimeType != null) {
|
||||||
addHeader(MimeHeader.HEADER_CONTENT_TYPE, mimeType);
|
addHeader(MimeHeader.HEADER_CONTENT_TYPE, mimeType);
|
||||||
}
|
}
|
||||||
setBody(body);
|
MimeMessageHelper.setBody(this, body);
|
||||||
}
|
}
|
||||||
|
|
||||||
private String getFirstHeader(String name) {
|
private String getFirstHeader(String name) {
|
||||||
@ -75,25 +75,6 @@ public class MimeBodyPart extends BodyPart {
|
|||||||
@Override
|
@Override
|
||||||
public void setBody(Body body) throws MessagingException {
|
public void setBody(Body body) throws MessagingException {
|
||||||
this.mBody = body;
|
this.mBody = body;
|
||||||
if (body instanceof Multipart) {
|
|
||||||
Multipart multipart = ((Multipart)body);
|
|
||||||
multipart.setParent(this);
|
|
||||||
String type = multipart.getContentType();
|
|
||||||
setHeader(MimeHeader.HEADER_CONTENT_TYPE, type);
|
|
||||||
if ("multipart/signed".equalsIgnoreCase(type)) {
|
|
||||||
setEncoding(MimeUtil.ENC_7BIT);
|
|
||||||
} else {
|
|
||||||
setEncoding(MimeUtil.ENC_8BIT);
|
|
||||||
}
|
|
||||||
} else if (body instanceof TextBody) {
|
|
||||||
String contentType = String.format("%s;\r\n charset=utf-8", getMimeType());
|
|
||||||
String name = MimeUtility.getHeaderParameter(getContentType(), "name");
|
|
||||||
if (name != null) {
|
|
||||||
contentType += String.format(";\r\n name=\"%s\"", name);
|
|
||||||
}
|
|
||||||
setHeader(MimeHeader.HEADER_CONTENT_TYPE, contentType);
|
|
||||||
setEncoding(MimeUtil.ENC_8BIT);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -396,22 +396,6 @@ public class MimeMessage extends Message {
|
|||||||
@Override
|
@Override
|
||||||
public void setBody(Body body) throws MessagingException {
|
public void setBody(Body body) throws MessagingException {
|
||||||
this.mBody = body;
|
this.mBody = body;
|
||||||
setHeader("MIME-Version", "1.0");
|
|
||||||
if (body instanceof Multipart) {
|
|
||||||
Multipart multipart = ((Multipart)body);
|
|
||||||
multipart.setParent(this);
|
|
||||||
String type = multipart.getContentType();
|
|
||||||
setHeader(MimeHeader.HEADER_CONTENT_TYPE, type);
|
|
||||||
if ("multipart/signed".equalsIgnoreCase(type)) {
|
|
||||||
setEncoding(MimeUtil.ENC_7BIT);
|
|
||||||
} else {
|
|
||||||
setEncoding(MimeUtil.ENC_8BIT);
|
|
||||||
}
|
|
||||||
} else if (body instanceof TextBody) {
|
|
||||||
setHeader(MimeHeader.HEADER_CONTENT_TYPE, String.format("%s;\r\n charset=utf-8",
|
|
||||||
getMimeType()));
|
|
||||||
setEncoding(MimeUtil.ENC_8BIT);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private String getFirstHeader(String name) {
|
private String getFirstHeader(String name) {
|
||||||
|
52
src/com/fsck/k9/mail/internet/MimeMessageHelper.java
Normal file
52
src/com/fsck/k9/mail/internet/MimeMessageHelper.java
Normal file
@ -0,0 +1,52 @@
|
|||||||
|
package com.fsck.k9.mail.internet;
|
||||||
|
|
||||||
|
|
||||||
|
import com.fsck.k9.mail.Body;
|
||||||
|
import com.fsck.k9.mail.Message;
|
||||||
|
import com.fsck.k9.mail.MessagingException;
|
||||||
|
import com.fsck.k9.mail.Multipart;
|
||||||
|
import com.fsck.k9.mail.Part;
|
||||||
|
import org.apache.james.mime4j.util.MimeUtil;
|
||||||
|
|
||||||
|
|
||||||
|
public class MimeMessageHelper {
|
||||||
|
private MimeMessageHelper() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void setBody(Part part, Body body) throws MessagingException {
|
||||||
|
part.setBody(body);
|
||||||
|
|
||||||
|
if (part instanceof Message) {
|
||||||
|
part.setHeader("MIME-Version", "1.0");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (body instanceof Multipart) {
|
||||||
|
Multipart multipart = ((Multipart) body);
|
||||||
|
multipart.setParent(part);
|
||||||
|
String type = multipart.getContentType();
|
||||||
|
part.setHeader(MimeHeader.HEADER_CONTENT_TYPE, type);
|
||||||
|
if ("multipart/signed".equalsIgnoreCase(type)) {
|
||||||
|
setEncoding(part, MimeUtil.ENC_7BIT);
|
||||||
|
} else {
|
||||||
|
setEncoding(part, MimeUtil.ENC_8BIT);
|
||||||
|
}
|
||||||
|
} else if (body instanceof TextBody) {
|
||||||
|
String contentType = String.format("%s;\r\n charset=utf-8", part.getMimeType());
|
||||||
|
String name = MimeUtility.getHeaderParameter(part.getContentType(), "name");
|
||||||
|
if (name != null) {
|
||||||
|
contentType += String.format(";\r\n name=\"%s\"", name);
|
||||||
|
}
|
||||||
|
part.setHeader(MimeHeader.HEADER_CONTENT_TYPE, contentType);
|
||||||
|
|
||||||
|
setEncoding(part, MimeUtil.ENC_8BIT);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void setEncoding(Part part, String encoding) throws MessagingException {
|
||||||
|
Body body = part.getBody();
|
||||||
|
if (body != null) {
|
||||||
|
body.setEncoding(encoding);
|
||||||
|
}
|
||||||
|
part.setHeader(MimeHeader.HEADER_CONTENT_TRANSFER_ENCODING, encoding);
|
||||||
|
}
|
||||||
|
}
|
@ -1067,7 +1067,7 @@ public class MimeUtility {
|
|||||||
String text = readToString(in, charset);
|
String text = readToString(in, charset);
|
||||||
|
|
||||||
// Replace the body with a TextBody that already contains the decoded text
|
// Replace the body with a TextBody that already contains the decoded text
|
||||||
part.setBody(new TextBody(text));
|
MimeMessageHelper.setBody(part, new TextBody(text));
|
||||||
|
|
||||||
return text;
|
return text;
|
||||||
} finally {
|
} finally {
|
||||||
|
@ -48,6 +48,7 @@ import java.util.regex.Pattern;
|
|||||||
import java.util.zip.Inflater;
|
import java.util.zip.Inflater;
|
||||||
import java.util.zip.InflaterInputStream;
|
import java.util.zip.InflaterInputStream;
|
||||||
|
|
||||||
|
import com.fsck.k9.mail.internet.MimeMessageHelper;
|
||||||
import javax.net.ssl.SSLException;
|
import javax.net.ssl.SSLException;
|
||||||
|
|
||||||
import org.apache.commons.io.IOUtils;
|
import org.apache.commons.io.IOUtils;
|
||||||
@ -1615,7 +1616,7 @@ public class ImapStore extends Store {
|
|||||||
if (literal != null) {
|
if (literal != null) {
|
||||||
if (literal instanceof Body) {
|
if (literal instanceof Body) {
|
||||||
// Most of the work was done in FetchAttchmentCallback.foundLiteral()
|
// Most of the work was done in FetchAttchmentCallback.foundLiteral()
|
||||||
part.setBody((Body)literal);
|
MimeMessageHelper.setBody(part, (Body) literal);
|
||||||
} else if (literal instanceof String) {
|
} else if (literal instanceof String) {
|
||||||
String bodyString = (String)literal;
|
String bodyString = (String)literal;
|
||||||
InputStream bodyStream = new ByteArrayInputStream(bodyString.getBytes());
|
InputStream bodyStream = new ByteArrayInputStream(bodyString.getBytes());
|
||||||
@ -1624,7 +1625,7 @@ public class ImapStore extends Store {
|
|||||||
.getHeader(MimeHeader.HEADER_CONTENT_TRANSFER_ENCODING)[0];
|
.getHeader(MimeHeader.HEADER_CONTENT_TRANSFER_ENCODING)[0];
|
||||||
String contentType = part
|
String contentType = part
|
||||||
.getHeader(MimeHeader.HEADER_CONTENT_TYPE)[0];
|
.getHeader(MimeHeader.HEADER_CONTENT_TYPE)[0];
|
||||||
part.setBody(MimeUtility.decodeBody(bodyStream,
|
MimeMessageHelper.setBody(part, MimeUtility.decodeBody(bodyStream,
|
||||||
contentTransferEncoding, contentType));
|
contentTransferEncoding, contentType));
|
||||||
} else {
|
} else {
|
||||||
// This shouldn't happen
|
// This shouldn't happen
|
||||||
|
@ -19,6 +19,7 @@ import java.util.Set;
|
|||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
|
import com.fsck.k9.mail.internet.MimeMessageHelper;
|
||||||
import org.apache.commons.io.IOUtils;
|
import org.apache.commons.io.IOUtils;
|
||||||
import org.apache.james.mime4j.util.MimeUtil;
|
import org.apache.james.mime4j.util.MimeUtil;
|
||||||
|
|
||||||
@ -774,17 +775,17 @@ public class LocalFolder extends Folder implements Serializable {
|
|||||||
// triggering T_MIME_NO_TEXT and T_TVD_MIME_NO_HEADERS
|
// triggering T_MIME_NO_TEXT and T_TVD_MIME_NO_HEADERS
|
||||||
// SpamAssassin rules.
|
// SpamAssassin rules.
|
||||||
localMessage.setHeader(MimeHeader.HEADER_CONTENT_TYPE, "text/plain");
|
localMessage.setHeader(MimeHeader.HEADER_CONTENT_TYPE, "text/plain");
|
||||||
localMessage.setBody(new TextBody(""));
|
MimeMessageHelper.setBody(localMessage, new TextBody(""));
|
||||||
} else if (mp.getCount() == 1 && (mp.getBodyPart(0) instanceof LocalAttachmentBodyPart) == false)
|
} else if (mp.getCount() == 1 && (mp.getBodyPart(0) instanceof LocalAttachmentBodyPart) == false)
|
||||||
|
|
||||||
{
|
{
|
||||||
// If we have only one part, drop the MimeMultipart container.
|
// If we have only one part, drop the MimeMultipart container.
|
||||||
BodyPart part = mp.getBodyPart(0);
|
BodyPart part = mp.getBodyPart(0);
|
||||||
localMessage.setHeader(MimeHeader.HEADER_CONTENT_TYPE, part.getContentType());
|
localMessage.setHeader(MimeHeader.HEADER_CONTENT_TYPE, part.getContentType());
|
||||||
localMessage.setBody(part.getBody());
|
MimeMessageHelper.setBody(localMessage, part.getBody());
|
||||||
} else {
|
} else {
|
||||||
// Otherwise, attach the MimeMultipart to the message.
|
// Otherwise, attach the MimeMultipart to the message.
|
||||||
localMessage.setBody(mp);
|
MimeMessageHelper.setBody(localMessage, mp);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1642,11 +1643,13 @@ public class LocalFolder extends Folder implements Serializable {
|
|||||||
mAccount,
|
mAccount,
|
||||||
attachmentId);
|
attachmentId);
|
||||||
if (MimeUtil.isMessage(attachment.getMimeType())) {
|
if (MimeUtil.isMessage(attachment.getMimeType())) {
|
||||||
attachment.setBody(new LocalAttachmentMessageBody(
|
LocalAttachmentMessageBody body = new LocalAttachmentMessageBody(
|
||||||
contentUri, LocalFolder.this.localStore.mApplication));
|
contentUri, LocalFolder.this.localStore.mApplication);
|
||||||
|
MimeMessageHelper.setBody(attachment, body);
|
||||||
} else {
|
} else {
|
||||||
attachment.setBody(new LocalAttachmentBody(
|
LocalAttachmentBody body = new LocalAttachmentBody(
|
||||||
contentUri, LocalFolder.this.localStore.mApplication));
|
contentUri, LocalFolder.this.localStore.mApplication);
|
||||||
|
MimeMessageHelper.setBody(attachment, body);
|
||||||
}
|
}
|
||||||
ContentValues cv = new ContentValues();
|
ContentValues cv = new ContentValues();
|
||||||
cv.put("content_uri", contentUri != null ? contentUri.toString() : null);
|
cv.put("content_uri", contentUri != null ? contentUri.toString() : null);
|
||||||
|
@ -5,6 +5,8 @@ import java.io.ByteArrayOutputStream;
|
|||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
|
|
||||||
|
import com.fsck.k9.mail.internet.MimeMessageHelper;
|
||||||
import org.apache.commons.io.IOUtils;
|
import org.apache.commons.io.IOUtils;
|
||||||
import org.apache.james.mime4j.codec.Base64InputStream;
|
import org.apache.james.mime4j.codec.Base64InputStream;
|
||||||
import org.apache.james.mime4j.util.MimeUtil;
|
import org.apache.james.mime4j.util.MimeUtil;
|
||||||
@ -322,7 +324,7 @@ public class MessageTest extends AndroidTestCase {
|
|||||||
multipartBody.addBodyPart(textBodyPart(MimeUtil.ENC_8BIT));
|
multipartBody.addBodyPart(textBodyPart(MimeUtil.ENC_8BIT));
|
||||||
multipartBody.addBodyPart(textBodyPart(MimeUtil.ENC_QUOTED_PRINTABLE));
|
multipartBody.addBodyPart(textBodyPart(MimeUtil.ENC_QUOTED_PRINTABLE));
|
||||||
multipartBody.addBodyPart(binaryBodyPart());
|
multipartBody.addBodyPart(binaryBodyPart());
|
||||||
message.setBody(multipartBody);
|
MimeMessageHelper.setBody(message, multipartBody);
|
||||||
|
|
||||||
return message;
|
return message;
|
||||||
}
|
}
|
||||||
|
@ -21,7 +21,7 @@ public class ViewablesTest extends AndroidTestCase {
|
|||||||
|
|
||||||
// Create message
|
// Create message
|
||||||
MimeMessage message = new MimeMessage();
|
MimeMessage message = new MimeMessage();
|
||||||
message.setBody(body);
|
MimeMessageHelper.setBody(message, body);
|
||||||
|
|
||||||
// Extract text
|
// Extract text
|
||||||
ViewableContainer container = MimeUtility.extractTextAndAttachments(getContext(), message);
|
ViewableContainer container = MimeUtility.extractTextAndAttachments(getContext(), message);
|
||||||
@ -45,7 +45,7 @@ public class ViewablesTest extends AndroidTestCase {
|
|||||||
// Create message
|
// Create message
|
||||||
MimeMessage message = new MimeMessage();
|
MimeMessage message = new MimeMessage();
|
||||||
message.setHeader("Content-Type", "text/html");
|
message.setHeader("Content-Type", "text/html");
|
||||||
message.setBody(body);
|
MimeMessageHelper.setBody(message, body);
|
||||||
|
|
||||||
// Extract text
|
// Extract text
|
||||||
ViewableContainer container = MimeUtility.extractTextAndAttachments(getContext(), message);
|
ViewableContainer container = MimeUtility.extractTextAndAttachments(getContext(), message);
|
||||||
@ -75,7 +75,7 @@ public class ViewablesTest extends AndroidTestCase {
|
|||||||
|
|
||||||
// Create message
|
// Create message
|
||||||
MimeMessage message = new MimeMessage();
|
MimeMessage message = new MimeMessage();
|
||||||
message.setBody(multipart);
|
MimeMessageHelper.setBody(message, multipart);
|
||||||
|
|
||||||
// Extract text
|
// Extract text
|
||||||
ViewableContainer container = MimeUtility.extractTextAndAttachments(getContext(), message);
|
ViewableContainer container = MimeUtility.extractTextAndAttachments(getContext(), message);
|
||||||
@ -119,7 +119,7 @@ public class ViewablesTest extends AndroidTestCase {
|
|||||||
innerMessage.setRecipients(RecipientType.TO, new Address[] { new Address("to@example.com") });
|
innerMessage.setRecipients(RecipientType.TO, new Address[] { new Address("to@example.com") });
|
||||||
innerMessage.setSubject("Subject");
|
innerMessage.setSubject("Subject");
|
||||||
innerMessage.setFrom(new Address("from@example.com"));
|
innerMessage.setFrom(new Address("from@example.com"));
|
||||||
innerMessage.setBody(innerBody);
|
MimeMessageHelper.setBody(innerMessage, innerBody);
|
||||||
|
|
||||||
// Create multipart/mixed part
|
// Create multipart/mixed part
|
||||||
MimeMultipart multipart = new MimeMultipart();
|
MimeMultipart multipart = new MimeMultipart();
|
||||||
@ -131,7 +131,7 @@ public class ViewablesTest extends AndroidTestCase {
|
|||||||
|
|
||||||
// Create message
|
// Create message
|
||||||
MimeMessage message = new MimeMessage();
|
MimeMessage message = new MimeMessage();
|
||||||
message.setBody(multipart);
|
MimeMessageHelper.setBody(message, multipart);
|
||||||
|
|
||||||
// Extract text
|
// Extract text
|
||||||
ViewableContainer container = MimeUtility.extractTextAndAttachments(getContext(), message);
|
ViewableContainer container = MimeUtility.extractTextAndAttachments(getContext(), message);
|
||||||
|
Loading…
Reference in New Issue
Block a user