snappymail/dev/View/Popup/AddOpenPgpKey.js

108 lines
2.3 KiB
JavaScript
Raw Normal View History

import { PgpUserStore } from 'Stores/User/Pgp';
2014-08-21 23:08:34 +08:00
import { decorateKoCommands } from 'Knoin/Knoin';
import { AbstractViewPopup } from 'Knoin/AbstractViews';
2022-01-20 23:38:27 +08:00
import { Capa } from 'Common/Enums';
import { Settings } from 'Common/Globals';
class AddOpenPgpKeyPopupView extends AbstractViewPopup {
constructor() {
super('AddOpenPgpKey');
2014-08-21 23:08:34 +08:00
this.addObservables({
key: '',
keyError: false,
2022-01-20 23:38:27 +08:00
keyErrorMessage: '',
saveGnuPG: true,
saveOpenPGP: true
});
2014-08-25 15:10:51 +08:00
2022-01-20 23:38:27 +08:00
this.canGnuPG = Settings.capa(Capa.GnuPG);
this.canOpenPGP = Settings.capa(Capa.OpenPGP);
this.key.subscribe(() => {
this.keyError(false);
this.keyErrorMessage('');
});
decorateKoCommands(this, {
addOpenPgpKeyCommand: 1
});
2016-09-10 06:38:16 +08:00
}
2016-06-30 08:02:45 +08:00
2016-09-10 06:38:16 +08:00
addOpenPgpKeyCommand() {
let keyTrimmed = this.key().trim();
2016-06-30 08:02:45 +08:00
2022-01-20 23:38:27 +08:00
if (/\n/.test(keyTrimmed)) {
keyTrimmed = keyTrimmed.replace(/\r+/g, '').replace(/\n{2,}/g, '\n\n');
2016-09-10 06:38:16 +08:00
}
2016-06-30 08:02:45 +08:00
this.keyError(!keyTrimmed);
this.keyErrorMessage('');
2016-06-30 08:02:45 +08:00
2022-01-20 23:38:27 +08:00
if (!keyTrimmed) {
2016-09-10 06:38:16 +08:00
return false;
}
2019-07-05 03:19:24 +08:00
let match = null,
2016-09-10 06:38:16 +08:00
count = 30,
done = false;
2022-01-20 23:38:27 +08:00
// eslint-disable-next-line max-len
const reg = /[-]{3,6}BEGIN[\s]PGP[\s](PRIVATE|PUBLIC)[\s]KEY[\s]BLOCK[-]{3,6}[\s\S]+?[-]{3,6}END[\s]PGP[\s](PRIVATE|PUBLIC)[\s]KEY[\s]BLOCK[-]{3,6}/gi,
keyring = PgpUserStore.openpgpKeyring;
2016-08-10 03:52:30 +08:00
2019-07-05 03:19:24 +08:00
do {
2016-09-10 06:38:16 +08:00
match = reg.exec(keyTrimmed);
2019-07-05 03:19:24 +08:00
if (match && 0 < count) {
if (match[0] && match[1] && match[2] && match[1] === match[2]) {
2016-09-10 06:38:16 +08:00
let err = null;
2022-01-20 23:38:27 +08:00
if (this.saveGnuPG()) {
PgpUserStore.gnupgImportKey(this.key());
}
if (this.canOpenPGP && this.saveOpenPGP()) {
if ('PRIVATE' === match[1]) {
err = keyring.privateKeys.importKey(match[0]);
} else if ('PUBLIC' === match[1]) {
err = keyring.publicKeys.importKey(match[0]);
}
2016-06-30 08:02:45 +08:00
}
2014-08-21 23:08:34 +08:00
2019-07-05 03:19:24 +08:00
if (err) {
this.keyError(true);
this.keyErrorMessage(err && err[0] ? '' + err[0] : '');
console.log(err);
2016-09-10 06:38:16 +08:00
}
}
2016-09-10 06:38:16 +08:00
--count;
2016-09-10 06:38:16 +08:00
done = false;
2019-07-05 03:19:24 +08:00
} else {
2016-09-10 06:38:16 +08:00
done = true;
}
2019-07-05 03:19:24 +08:00
} while (!done);
2022-01-20 23:38:27 +08:00
if (this.canOpenPGP && this.saveOpenPGP()) {
keyring.store();
}
2014-07-12 02:57:22 +08:00
2022-01-20 23:38:27 +08:00
// PgpUserStore.reloadOpenPgpKeys();
if (this.keyError()) {
2016-09-10 06:38:16 +08:00
return false;
}
2016-06-30 08:02:45 +08:00
this.cancelCommand();
2016-09-10 06:38:16 +08:00
return true;
}
2021-11-03 21:58:34 +08:00
onShow() {
this.key('');
this.keyError(false);
this.keyErrorMessage('');
}
}
2019-07-05 03:19:24 +08:00
export { AddOpenPgpKeyPopupView, AddOpenPgpKeyPopupView as default };