Composer changes for OpenPGP.js

This commit is contained in:
the-djmaze 2022-01-20 09:59:56 +01:00
parent e731343093
commit 3cc3a76b23
3 changed files with 92 additions and 47 deletions

View file

@ -29,7 +29,13 @@ export class OpenPgpKeyModel extends AbstractModel {
this.armor = armor; this.armor = armor;
this.isPrivate = !!isPrivate; this.isPrivate = !!isPrivate;
this.selectUser(userID); if (this.users) {
const index = this.users.indexOf(userID);
if (-1 !== index) {
this.user = this.users[index];
this.email = this.emails[index];
}
}
this.deleteAccess = ko.observable(false); this.deleteAccess = ko.observable(false);
} }
@ -48,22 +54,4 @@ export class OpenPgpKeyModel extends AbstractModel {
} }
return null; return null;
} }
select(pattern, property) {
if (this[property]) {
const index = this[property].indexOf(pattern);
if (-1 !== index) {
this.user = this.users[index];
this.email = this.emails[index];
}
}
}
selectUser(user) {
this.select(user, 'users');
}
selectEmail(email) {
this.select(email, 'emails');
}
} }

View file

@ -338,7 +338,7 @@ class ComposePopupView extends AbstractViewPopup {
getMessageRequestParams(sSaveFolder) getMessageRequestParams(sSaveFolder)
{ {
let TextIsHtml = this.oEditor.isHtml() ? 1 : 0, let TextIsHtml = this.oEditor.isHtml(),
Text = this.oEditor.getData(true); Text = this.oEditor.getData(true);
if (TextIsHtml) { if (TextIsHtml) {
let l; let l;
@ -361,13 +361,13 @@ class ComposePopupView extends AbstractViewPopup {
Bcc: this.bcc(), Bcc: this.bcc(),
ReplyTo: this.replyTo(), ReplyTo: this.replyTo(),
Subject: this.subject(), Subject: this.subject(),
TextIsHtml: TextIsHtml, TextIsHtml: TextIsHtml ? 1 : 0,
Text: Text, Text: Text,
DraftInfo: this.aDraftInfo, DraftInfo: this.aDraftInfo,
InReplyTo: this.sInReplyTo, InReplyTo: this.sInReplyTo,
References: this.sReferences, References: this.sReferences,
MarkAsImportant: this.markAsImportant() ? 1 : 0, MarkAsImportant: this.markAsImportant() ? 1 : 0,
Attachments: this.prepearAttachmentsForSendOrSave(), Attachments: this.prepareAttachmentsForSendOrSave(),
// Only used at send, not at save: // Only used at send, not at save:
Dsn: this.requestDsn() ? 1 : 0, Dsn: this.requestDsn() ? 1 : 0,
ReadReceiptRequest: this.requestReadReceipt() ? 1 : 0 ReadReceiptRequest: this.requestReadReceipt() ? 1 : 0
@ -425,28 +425,81 @@ class ComposePopupView extends AbstractViewPopup {
setFolderHash(this.draftsFolder(), ''); setFolderHash(this.draftsFolder(), '');
setFolderHash(sSentFolder, ''); setFolderHash(sSentFolder, '');
Remote.request('SendMessage', const
(iError, data) => { params = this.getMessageRequestParams(sSentFolder),
this.sending(false); sign = this.pgpSign() && this.canPgpSign(),
if (this.modalVisibility()) { encrypt = this.pgpEncrypt() && this.canPgpEncrypt(),
if (iError) { send = () =>
if (Notification.CantSaveMessage === iError) { Remote.request('SendMessage',
this.sendSuccessButSaveError(true); (iError, data) => {
this.savedErrorDesc(i18n('COMPOSE/SAVED_ERROR_ON_SEND').trim()); this.sending(false);
} else { if (this.modalVisibility()) {
this.sendError(true); if (iError) {
this.sendErrorDesc(getNotification(iError, data && data.ErrorMessage) if (Notification.CantSaveMessage === iError) {
|| getNotification(Notification.CantSendMessage)); this.sendSuccessButSaveError(true);
this.savedErrorDesc(i18n('COMPOSE/SAVED_ERROR_ON_SEND').trim());
} else {
this.sendError(true);
this.sendErrorDesc(getNotification(iError, data && data.ErrorMessage)
|| getNotification(Notification.CantSendMessage));
}
} else {
this.closeCommand();
}
} }
} else { this.reloadDraftFolder();
this.closeCommand(); },
} params,
30000
);
let pgpPromise = null,
cfg = {
data: params.Text,
};
if ('openpgp' == sign) {
let privateKey;
try {
const keys = PgpUserStore.openpgpKeyring.privateKeys.getForAddress(this.currentIdentity().email());
if (keys[0]) {
keys[0].decrypt(window.prompt('Password', ''));
privateKey = keys[0];
cfg.privateKeys = [privateKey];
} }
this.reloadDraftFolder(); } catch (e) {
}, console.error(e);
this.getMessageRequestParams(sSentFolder), privateKey = null;
30000 }
); if (!privateKey) {
this.sendError(true);
this.sendErrorDesc(i18n('PGP_NOTIFICATIONS/NO_PRIVATE_KEY_FOUND'));
return;
}
}
if (encrypt && sign && encrypt != sign) {
// error 'sign and encrypt must be same engine';
} else if ('openpgp' == encrypt) {
this.allRecipients().forEach(recEmail => {
cfg.publicKeys = cfg.publicKeys.concat(PgpUserStore.openpgpKeyring.publicKeys.getForAddress(recEmail));
});
pgpPromise = openpgp.encrypt(cfg);
} else if ('openpgp' == sign) {
pgpPromise = openpgp.sign(cfg);
} else {
params.Sign = sign;
params.Encrypt = encrypt;
}
pgpPromise
? pgpPromise
.then(mData => {
params.Text = mData.data;
send();
})
.catch(e => {
this.sendError(true);
this.sendErrorDesc(i18n('PGP_NOTIFICATIONS/PGP_ERROR', { ERROR: '' + e }));
})
: send();
} }
} }
} }
@ -1278,7 +1331,7 @@ class ComposePopupView extends AbstractViewPopup {
/** /**
* @returns {Object} * @returns {Object}
*/ */
prepearAttachmentsForSendOrSave() { prepareAttachmentsForSendOrSave() {
const result = {}; const result = {};
this.attachments.forEach(item => { this.attachments.forEach(item => {
if (item && item.complete() && item.tempName() && item.enabled()) { if (item && item.complete() && item.tempName() && item.enabled()) {
@ -1448,9 +1501,9 @@ class ComposePopupView extends AbstractViewPopup {
); );
} }
initPgpEncrypt() { allRecipients() {
const email = new EmailModel(); const email = new EmailModel();
return PgpUserStore.hasPublicKeyForEmails([ return [
// this.currentIdentity.email(), // this.currentIdentity.email(),
this.to(), this.to(),
this.cc(), this.cc(),
@ -1459,7 +1512,11 @@ class ComposePopupView extends AbstractViewPopup {
email.clear(); email.clear();
email.parse(value.trim()); email.parse(value.trim());
return email.email || false; return email.email || false;
}).filter(v => v), 1).then(result => { }).filter(v => v);
}
initPgpEncrypt() {
return PgpUserStore.hasPublicKeyForEmails(this.allRecipients(), 1).then(result => {
console.log({canPgpEncrypt:result}); console.log({canPgpEncrypt:result});
this.canPgpEncrypt(result); this.canPgpEncrypt(result);
}); });

View file

@ -28,7 +28,7 @@
<td> <td>
<span class="open-pgp-key-img fontastic" data-i18n="[title]SETTINGS_OPEN_PGP/TITLE_PRIVATE">🔒</span> <span class="open-pgp-key-img fontastic" data-i18n="[title]SETTINGS_OPEN_PGP/TITLE_PRIVATE">🔒</span>
<span class="open-pgp-key-user" data-bind="foreach: users"> <span class="open-pgp-key-user" data-bind="foreach: users">
<div class="open-pgp-key-user-address" data-bind="text: $data"></div> <span class="open-pgp-key-user-address" data-bind="text: $data" style="display:inline-block"></span>
</span> </span>
</td> </td>
<td> <td>