When constructing new messages check header values

This commit is contained in:
cketti 2020-10-10 22:05:08 +02:00
parent 8663bbb5b9
commit 5bc7ac3901
5 changed files with 43 additions and 12 deletions

View file

@ -20,8 +20,8 @@ class AutocryptTransferMessageCreator(private val stringProvider: AutocryptStrin
val subjectText = stringProvider.transferMessageSubject()
val messageText = stringProvider.transferMessageBody()
val textBodyPart = MimeBodyPart(TextBody(messageText))
val dataBodyPart = MimeBodyPart(BinaryMemoryBody(data, "7bit"))
val textBodyPart = MimeBodyPart.create(TextBody(messageText))
val dataBodyPart = MimeBodyPart.create(BinaryMemoryBody(data, "7bit"))
dataBodyPart.setHeader(MimeHeader.HEADER_CONTENT_TYPE, "application/autocrypt-setup")
dataBodyPart.setHeader(MimeHeader.HEADER_CONTENT_DISPOSITION, "attachment; filename=\"autocrypt-setup-message\"")
@ -29,7 +29,7 @@ class AutocryptTransferMessageCreator(private val stringProvider: AutocryptStrin
messageBody.addBodyPart(textBodyPart)
messageBody.addBodyPart(dataBodyPart)
val message = MimeMessage()
val message = MimeMessage.create()
MimeMessageHelper.setBody(message, messageBody)
val nowDate = Date()

View file

@ -84,7 +84,7 @@ public abstract class MessageBuilder {
protected MimeMessage build() throws MessagingException {
//FIXME: check arguments
MimeMessage message = new MimeMessage();
MimeMessage message = MimeMessage.create();
buildHeader(message);
buildBody(message);
@ -168,8 +168,8 @@ public abstract class MessageBuilder {
composedMimeMessage.setSubType("alternative");
// Let the receiver select either the text or the HTML part.
bodyPlain = buildText(isDraft, SimpleMessageFormat.TEXT);
composedMimeMessage.addBodyPart(new MimeBodyPart(bodyPlain, "text/plain"));
composedMimeMessage.addBodyPart(new MimeBodyPart(body, "text/html"));
composedMimeMessage.addBodyPart(MimeBodyPart.create(bodyPlain, "text/plain"));
composedMimeMessage.addBodyPart(MimeBodyPart.create(body, "text/html"));
if (hasAttachments) {
// If we're HTML and have attachments, we have a MimeMultipart container to hold the
@ -177,7 +177,7 @@ public abstract class MessageBuilder {
// (composedMimeMessage) with the user's composed messages, and subsequent parts for
// the attachments.
MimeMultipart mp = createMimeMultipart();
mp.addBodyPart(new MimeBodyPart(composedMimeMessage));
mp.addBodyPart(MimeBodyPart.create(composedMimeMessage));
addAttachmentsToMessage(mp);
MimeMessageHelper.setBody(message, mp);
} else {
@ -188,7 +188,7 @@ public abstract class MessageBuilder {
// Text-only message.
if (hasAttachments) {
MimeMultipart mp = createMimeMultipart();
mp.addBodyPart(new MimeBodyPart(body, "text/plain"));
mp.addBodyPart(MimeBodyPart.create(body, "text/plain"));
addAttachmentsToMessage(mp);
MimeMessageHelper.setBody(message, mp);
} else {
@ -233,7 +233,7 @@ public abstract class MessageBuilder {
}
Body body = new TempFileBody(attachment.getFileName());
MimeBodyPart bp = new MimeBodyPart(body);
MimeBodyPart bp = MimeBodyPart.create(body);
addContentType(bp, attachment.getContentType(), attachment.getName());
addContentDisposition(bp, attachment.getName(), attachment.getSize());

View file

@ -395,7 +395,7 @@ public class PgpMessageBuilder extends MessageBuilder {
multipartSigned.setSubType("signed");
multipartSigned.addBodyPart(signedBodyPart);
multipartSigned.addBodyPart(
new MimeBodyPart(new BinaryMemoryBody(signedData, MimeUtil.ENC_7BIT),
MimeBodyPart.create(new BinaryMemoryBody(signedData, MimeUtil.ENC_7BIT),
"application/pgp-signature; name=\"signature.asc\""));
MimeMessageHelper.setBody(currentProcessedMimeMessage, multipartSigned);
@ -414,8 +414,8 @@ public class PgpMessageBuilder extends MessageBuilder {
private void mimeBuildEncryptedMessage(@NonNull Body encryptedBodyPart) throws MessagingException {
MimeMultipart multipartEncrypted = createMimeMultipart();
multipartEncrypted.setSubType("encrypted");
multipartEncrypted.addBodyPart(new MimeBodyPart(new TextBody("Version: 1"), "application/pgp-encrypted"));
MimeBodyPart encryptedPart = new MimeBodyPart(encryptedBodyPart, "application/octet-stream; name=\"encrypted.asc\"");
multipartEncrypted.addBodyPart(MimeBodyPart.create(new TextBody("Version: 1"), "application/pgp-encrypted"));
MimeBodyPart encryptedPart = MimeBodyPart.create(encryptedBodyPart, "application/octet-stream; name=\"encrypted.asc\"");
encryptedPart.addHeader(MimeHeader.HEADER_CONTENT_DISPOSITION, "inline; filename=\"encrypted.asc\"");
multipartEncrypted.addBodyPart(encryptedPart);
MimeMessageHelper.setBody(currentProcessedMimeMessage, multipartEncrypted);

View file

@ -27,6 +27,20 @@ public class MimeBodyPart extends BodyPart {
private final MimeHeader mHeader;
private Body mBody;
/**
* Creates an instance that will check the header field syntax when adding headers.
*/
public static MimeBodyPart create(Body body) throws MessagingException {
return new MimeBodyPart(body, null, true);
}
/**
* Creates an instance that will check the header field syntax when adding headers.
*/
public static MimeBodyPart create(Body body, String contentType) throws MessagingException {
return new MimeBodyPart(body, contentType, true);
}
public MimeBodyPart() throws MessagingException {
this(null);
}
@ -36,7 +50,12 @@ public class MimeBodyPart extends BodyPart {
}
public MimeBodyPart(Body body, String contentType) throws MessagingException {
this(body, contentType, false);
}
private MimeBodyPart(Body body, String contentType, boolean checkHeaders) throws MessagingException {
mHeader = new MimeHeader();
mHeader.setCheckHeaders(checkHeaders);
if (contentType != null) {
addHeader(MimeHeader.HEADER_CONTENT_TYPE, contentType);
}

View file

@ -75,7 +75,19 @@ public class MimeMessage extends Message {
return mimeMessage;
}
/**
* Creates an instance that will check the header field syntax when adding headers.
*/
public static MimeMessage create() {
return new MimeMessage(true);
}
public MimeMessage() {
this(false);
}
private MimeMessage(boolean checkHeaders) {
mHeader.setCheckHeaders(checkHeaders);
}
/**