snappymail/dev/View/Popup/OpenPgpImport.js

78 lines
1.7 KiB
JavaScript
Raw Normal View History

import { addObservablesTo } from 'External/ko';
import { GnuPGUserStore } from 'Stores/User/GnuPG';
import { OpenPGPUserStore } from 'Stores/User/OpenPGP';
2014-08-21 23:08:34 +08:00
import { AbstractViewPopup } from 'Knoin/AbstractViews';
export class OpenPgpImportPopupView extends AbstractViewPopup {
constructor() {
super('OpenPgpImport');
2014-08-21 23:08:34 +08:00
addObservablesTo(this, {
key: '',
keyError: false,
2022-01-20 23:38:27 +08:00
keyErrorMessage: '',
saveGnuPG: true,
2022-02-09 22:43:14 +08:00
saveServer: false
});
2014-08-25 15:10:51 +08:00
this.canGnuPG = GnuPGUserStore.isSupported();
2022-01-20 23:38:27 +08:00
this.key.subscribe(() => {
this.keyError(false);
this.keyErrorMessage('');
});
2016-09-10 06:38:16 +08:00
}
2016-06-30 08:02:45 +08:00
submitForm() {
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) {
2022-01-29 00:56:44 +08:00
return;
2016-09-10 06:38:16 +08:00
}
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;
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]) {
this.saveGnuPG() && GnuPGUserStore.isSupported() && GnuPGUserStore.importKey(this.key(), (iError, oData) => {
iError && alert(oData.ErrorMessage);
});
OpenPGPUserStore.isSupported() && OpenPGPUserStore.importKey(this.key());
}
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);
if (this.keyError()) {
2022-01-29 00:56:44 +08:00
return;
2016-09-10 06:38:16 +08:00
}
2016-06-30 08:02:45 +08:00
this.close();
}
onShow(key) {
this.key(key || '');
this.keyError(false);
this.keyErrorMessage('');
}
}