From 4eb70c3f0635425dad40058eba5e229cb29198a1 Mon Sep 17 00:00:00 2001 From: the-djmaze <> Date: Mon, 31 Jan 2022 17:29:41 +0100 Subject: [PATCH] #89 show decrypted HTML with inline attachments --- dev/Model/Message.js | 6 +--- dev/View/User/MailBox/MessageView.js | 44 ++++++++++++++++++---------- 2 files changed, 30 insertions(+), 20 deletions(-) diff --git a/dev/Model/Message.js b/dev/Model/Message.js index c17cd24bb..10cdda500 100644 --- a/dev/Model/Message.js +++ b/dev/Model/Message.js @@ -95,7 +95,6 @@ export class MessageModel extends AbstractModel { focused: false, selected: false, checked: false, - hasAttachments: false, isHtml: false, hasImages: false, @@ -174,7 +173,6 @@ export class MessageModel extends AbstractModel { this.selected(false); this.checked(false); - this.hasAttachments(false); this.isHtml(false); this.hasImages(false); @@ -229,7 +227,6 @@ export class MessageModel extends AbstractModel { json.Priority = MessagePriority.Normal; } if (super.revivePropertiesFromJson(json)) { - this.hasAttachments(!!this.attachments.length); // this.foundCIDs = isArray(json.FoundCIDs) ? json.FoundCIDs : []; // this.attachments(AttachmentCollectionModel.reviveFromJson(json.Attachments, this.foundCIDs)); @@ -324,7 +321,7 @@ export class MessageModel extends AbstractModel { forwarded: this.isForwarded(), focused: this.focused(), important: this.isImportant(), - withAttachments: this.hasAttachments(), + withAttachments: !!this.attachments().length, emptySubject: !this.subject(), // hasChildrenMessage: 1 < this.threadsLen(), hasUnseenSubMessage: this.hasUnseenSubMessage(), @@ -577,7 +574,6 @@ export class MessageModel extends AbstractModel { this.selected(message.selected()); this.checked(message.checked()); - this.hasAttachments(message.hasAttachments()); this.attachments(message.attachments()); this.threads(message.threads()); diff --git a/dev/View/User/MailBox/MessageView.js b/dev/View/User/MailBox/MessageView.js index 9f25a3e55..5c7dd7802 100644 --- a/dev/View/User/MailBox/MessageView.js +++ b/dev/View/User/MailBox/MessageView.js @@ -46,6 +46,7 @@ import { OpenPGPUserStore } from 'Stores/User/OpenPGP'; const currentMessage = () => MessageUserStore.message(); import PostalMime from '../../../../vendors/postal-mime/src/postal-mime.js'; +import { AttachmentModel } from 'Model/Attachment'; const mimeToMessage = (data, message) => { // TODO: Check multipart/signed @@ -53,23 +54,36 @@ const mimeToMessage = (data, message) => { if (/Content-Type:.+; boundary=/.test(headers)) { // https://github.com/postalsys/postal-mime (new PostalMime).parse(data).then(result => { - /* - result.attachments[0] = { - content: ArrayBuffer - disposition: null - filename: "signature.asc" - mimeType: "application/pgp-signature" - }; - result.headers; - */ - // TODO: parse inline attachments from result.attachments + let html = result.html, + regex = /^<+|>+$/g; + result.attachments.forEach(data => { + let attachment = new AttachmentModel; + attachment.mimeType = data.mimeType; + attachment.fileName = data.filename; + attachment.content = data.content; // ArrayBuffer + attachment.cid = data.contentId || ''; + // Parse inline attachments from result.attachments + if (attachment.cid) { + if (html) { + let cid = 'cid:' + attachment.cid.replace(regex, ''), + b64 = 'data:' + data.mimeType + ';base64,' + btoa(String.fromCharCode(...new Uint8Array(data.content))); + html = html + .replace('src="' + cid + '"', 'src="' + b64 + '"') + .replace("src='" + cid + "'", "src='" + b64 + "'"); + } + } +// data.disposition +// data.related = true; + message.attachments.push(attachment); + }); +// result.headers; // TODO: strip script tags and all other security that PHP also does - message.html(result.html || ''); message.plain(result.text || ''); - if (result.text) { - message.viewPlain(); - } else { + if (html) { + message.html(html.replace(/<\/?script[\s\S]*?>/gi, '') || ''); message.viewHtml(); + } else { + message.viewPlain(); } }); return; @@ -722,7 +736,7 @@ export class MailMessageView extends AbstractViewRight { if (params.Passphrase) { rl.app.Remote.post('GnupgDecrypt', null, params) .then(data => { - // TODO + // TODO mimeToMessage(data, message) console.dir(data); }) .catch(error => {