1
0
Fork 0
mirror of https://github.com/M66B/FairEmail.git synced 2024-12-27 18:27:43 +00:00

Send singed-only messages

This commit is contained in:
M66B 2019-12-01 09:51:24 +01:00
parent f256e870bb
commit 06519366e8
2 changed files with 45 additions and 54 deletions

View file

@ -110,6 +110,7 @@ import org.openintents.openpgp.util.OpenPgpApi;
import org.openintents.openpgp.util.OpenPgpServiceConnection; import org.openintents.openpgp.util.OpenPgpServiceConnection;
import java.io.BufferedOutputStream; import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileOutputStream; import java.io.FileOutputStream;
@ -133,6 +134,7 @@ import java.util.regex.Pattern;
import javax.mail.Address; import javax.mail.Address;
import javax.mail.BodyPart; import javax.mail.BodyPart;
import javax.mail.MessageRemovedException; import javax.mail.MessageRemovedException;
import javax.mail.MessagingException;
import javax.mail.Multipart; import javax.mail.Multipart;
import javax.mail.Part; import javax.mail.Part;
import javax.mail.Session; import javax.mail.Session;
@ -141,7 +143,6 @@ import javax.mail.internet.ContentType;
import javax.mail.internet.InternetAddress; import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart; import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.ParseException; import javax.mail.internet.ParseException;
import static android.app.Activity.RESULT_CANCELED; import static android.app.Activity.RESULT_CANCELED;
@ -209,8 +210,6 @@ public class FragmentCompose extends FragmentBase {
private String[] pgpUserIds; private String[] pgpUserIds;
private long[] pgpKeyIds; private long[] pgpKeyIds;
private long pgpSignKeyId; private long pgpSignKeyId;
private String pgpContent;
private String pgpContentType;
static final int REDUCED_IMAGE_SIZE = 1440; // pixels static final int REDUCED_IMAGE_SIZE = 1440; // pixels
static final int REDUCED_IMAGE_QUALITY = 90; // percent static final int REDUCED_IMAGE_QUALITY = 90; // percent
@ -1537,32 +1536,35 @@ public class FragmentCompose extends FragmentBase {
if (OpenPgpApi.ACTION_GET_SIGN_KEY_ID.equals(data.getAction())) { if (OpenPgpApi.ACTION_GET_SIGN_KEY_ID.equals(data.getAction())) {
// Serialize content // Serialize content
imessage.saveChanges(); imessage.saveChanges();
Object content = imessage.getContent(); BodyPart bpContent = new MimeBodyPart() {
if (content instanceof String) { @Override
pgpContent = (String) content; public void setContent(Object content, String type) throws MessagingException {
pgpContentType = imessage.getContentType(); super.setContent(content, type);
// Build plain text part with headers // https://javaee.github.io/javamail/FAQ#howencode
BodyPart plainPart = new MimeBodyPart(); updateHeaders();
plainPart.setContent(pgpContent, pgpContentType); if (content instanceof Multipart) {
Multipart plainMultiPart = new MimeMultipart(); try {
plainMultiPart.addBodyPart(plainPart); MessageHelper.overrideContentTransferEncoding((Multipart) content);
MimeMessage m = new MimeMessage(isession); } catch (IOException ex) {
m.setContent(plainMultiPart); Log.e(ex);
m.saveChanges();
try (OutputStream out = new FileOutputStream(input)) {
plainPart.writeTo(out);
}
} else if (content instanceof Multipart) {
pgpContent = null;
pgpContentType = ((MimeMultipart) content).getContentType();
try (OutputStream out = new FileOutputStream(input)) {
((MimeMultipart) content).writeTo(out);
} }
} else } else
throw new ParseException(content.getClass().getName()); setHeader("Content-Transfer-Encoding", "base64");
}
};
bpContent.setContent(imessage.getContent(), imessage.getContentType());
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bpContent.writeTo(bos);
String raw = new String(bos.toByteArray());
raw.replaceAll(" +$", "") // trim trailing spaces
.replace("\\r?\\n", "\\r\\n"); // normalize new lines
try (OutputStream out = new FileOutputStream(input)) {
out.write(raw.getBytes());
}
} else { } else {
// Serialize message // Serialize message
try (OutputStream out = new FileOutputStream(input)) { try (OutputStream out = new FileOutputStream(input)) {
@ -1710,19 +1712,12 @@ public class FragmentCompose extends FragmentBase {
EntityAttachment attachment = new EntityAttachment(); EntityAttachment attachment = new EntityAttachment();
attachment.message = id; attachment.message = id;
attachment.sequence = db.attachment().getAttachmentSequence(id) + 1; attachment.sequence = db.attachment().getAttachmentSequence(id) + 1;
attachment.name = "content.txt"; attachment.name = "content.asc";
attachment.type = pgpContentType; attachment.type = "text/plain";
attachment.disposition = Part.INLINE; attachment.disposition = Part.INLINE;
attachment.encryption = EntityAttachment.PGP_CONTENT; attachment.encryption = EntityAttachment.PGP_CONTENT;
attachment.id = db.attachment().insertAttachment(attachment); attachment.id = db.attachment().insertAttachment(attachment);
// Restore plain text without headers
ContentType ct = new ContentType(pgpContentType);
if (!"multipart".equals(ct.getPrimaryType()))
try (OutputStream out = new FileOutputStream(input)) {
out.write(pgpContent.getBytes());
}
File file = attachment.getFile(context); File file = attachment.getFile(context);
input.renameTo(file); input.renameTo(file);

View file

@ -252,26 +252,10 @@ public class MessageHelper {
for (final EntityAttachment content : attachments) for (final EntityAttachment content : attachments)
if (EntityAttachment.PGP_CONTENT.equals(content.encryption)) { if (EntityAttachment.PGP_CONTENT.equals(content.encryption)) {
final ContentType ct = new ContentType(content.type); BodyPart bpContent = new MimeBodyPart(new FileInputStream(content.getFile(context)));
final ContentType cts = new ContentType(attachment.type);
// Build content
FileDataSource dsContent = new FileDataSource(content.getFile(context));
dsContent.setFileTypeMap(new FileTypeMap() {
@Override
public String getContentType(File file) {
return ct.toString();
}
@Override
public String getContentType(String filename) {
return ct.toString();
}
});
BodyPart bpContent = new MimeBodyPart();
bpContent.setDataHandler(new DataHandler(dsContent));
// Build signature // Build signature
final ContentType cts = new ContentType(attachment.type);
BodyPart bpSignature = new MimeBodyPart(); BodyPart bpSignature = new MimeBodyPart();
bpSignature.setFileName(attachment.name); bpSignature.setFileName(attachment.name);
FileDataSource dsSignature = new FileDataSource(attachment.getFile(context)); FileDataSource dsSignature = new FileDataSource(attachment.getFile(context));
@ -483,6 +467,18 @@ public class MessageHelper {
} }
} }
static void overrideContentTransferEncoding(Multipart mp) throws MessagingException, IOException {
for (int i = 0; i < mp.getCount(); i++) {
Part part = mp.getBodyPart(i);
Object content = part.getContent();
if (content instanceof Multipart) {
part.setHeader("Content-Transfer-Encoding", "7bit");
overrideContentTransferEncoding((Multipart) content);
} else
part.setHeader("Content-Transfer-Encoding", "base64");
}
}
MessageHelper(MimeMessage message) { MessageHelper(MimeMessage message) {
this.imessage = message; this.imessage = message;
} }
@ -1295,7 +1291,7 @@ public class MessageHelper {
apart.attachment = new EntityAttachment(); apart.attachment = new EntityAttachment();
apart.attachment.disposition = apart.disposition; apart.attachment.disposition = apart.disposition;
apart.attachment.name = apart.filename; apart.attachment.name = apart.filename;
apart.attachment.type = ct.getBaseType().toLowerCase(Locale.ROOT); apart.attachment.type = "text/plain";
apart.attachment.size = getSize(); apart.attachment.size = getSize();
apart.attachment.encryption = apart.encrypt; apart.attachment.encryption = apart.encrypt;