snappymail/dev/View/Popup/ComposeOpenPgp.js

462 lines
9.6 KiB
JavaScript
Raw Normal View History

2014-03-20 06:39:36 +08:00
import _ from '_';
import $ from '$';
import ko from 'ko';
import key from 'key';
2014-03-20 06:39:36 +08:00
import {
2016-09-10 06:38:16 +08:00
inArray, pString, log, isUnd, trim,
defautOptionsAfterRender
} from 'Common/Utils';
import {Magics, KeyState} from 'Common/Enums';
import {i18n} from 'Common/Translator';
2014-03-21 07:47:13 +08:00
import PgpStore from 'Stores/User/Pgp';
2014-08-25 15:10:51 +08:00
import {EmailModel} from 'Model/Email';
2014-08-25 15:10:51 +08:00
2016-09-10 06:38:16 +08:00
import {popup, command} from 'Knoin/Knoin';
import {AbstractViewNext} from 'Knoin/AbstractViewNext';
2014-03-21 07:47:13 +08:00
2016-08-24 06:17:50 +08:00
const KEY_NAME_SUBSTR = -8;
2016-09-10 06:38:16 +08:00
@popup({
name: 'View/Popup/ComposeOpenPgp',
templateID: 'PopupsComposeOpenPgp'
})
class ComposeOpenPgpPopupView extends AbstractViewNext
{
constructor() {
super();
2015-07-30 01:21:24 +08:00
this.publicKeysOptionsCaption = i18n('PGP_NOTIFICATIONS/ADD_A_PUBLICK_KEY');
this.privateKeysOptionsCaption = i18n('PGP_NOTIFICATIONS/SELECT_A_PRIVATE_KEY');
2015-07-30 01:21:24 +08:00
this.notification = ko.observable('');
2014-03-21 07:47:13 +08:00
this.sign = ko.observable(false);
this.encrypt = ko.observable(false);
2014-03-21 07:47:13 +08:00
this.password = ko.observable('');
this.password.focus = ko.observable(false);
this.buttonFocus = ko.observable(false);
2014-03-21 07:47:13 +08:00
this.text = ko.observable('');
this.selectedPrivateKey = ko.observable(null);
this.selectedPublicKey = ko.observable(null);
2015-07-30 01:21:24 +08:00
this.signKey = ko.observable(null);
this.encryptKeys = ko.observableArray([]);
2015-07-30 01:21:24 +08:00
this.encryptKeysView = ko.computed(
() => _.compact(_.map(this.encryptKeys(), (oKey) => (oKey ? oKey.key : null)))
);
2016-06-30 08:02:45 +08:00
this.privateKeysOptions = ko.computed(() => {
const opts = _.map(PgpStore.openpgpkeysPrivate(), (oKey, iIndex) => {
if (this.signKey() && this.signKey().key.id === oKey.id)
{
return null;
}
return _.map(oKey.users, (user) => ({
2016-06-30 08:02:45 +08:00
'id': oKey.guid,
2016-08-24 06:17:50 +08:00
'name': '(' + oKey.id.substr(KEY_NAME_SUBSTR).toUpperCase() + ') ' + user,
2016-06-30 08:02:45 +08:00
'key': oKey,
'class': iIndex % 2 ? 'odd' : 'even'
}));
2016-06-30 08:02:45 +08:00
});
return _.compact(_.flatten(opts, true));
});
this.publicKeysOptions = ko.computed(() => {
const opts = _.map(PgpStore.openpgpkeysPublic(), (oKey, index) => {
if (-1 < inArray(oKey, this.encryptKeysView()))
{
return null;
}
return _.map(oKey.users, (user) => ({
2016-06-30 08:02:45 +08:00
'id': oKey.guid,
2016-08-24 06:17:50 +08:00
'name': '(' + oKey.id.substr(KEY_NAME_SUBSTR).toUpperCase() + ') ' + user,
2016-06-30 08:02:45 +08:00
'key': oKey,
'class': index % 2 ? 'odd' : 'even'
}));
2016-06-30 08:02:45 +08:00
});
return _.compact(_.flatten(opts, true));
});
2014-08-21 23:08:34 +08:00
this.submitRequest = ko.observable(false);
2014-08-21 23:08:34 +08:00
this.resultCallback = null;
2015-07-30 01:21:24 +08:00
2016-09-10 06:38:16 +08:00
this.selectedPrivateKey.subscribe((value) => {
if (value)
{
this.selectCommand();
this.updateCommand();
}
});
2014-08-21 23:08:34 +08:00
2016-09-10 06:38:16 +08:00
this.selectedPublicKey.subscribe((value) => {
if (value)
{
this.addCommand();
}
});
this.sDefaultKeyScope = KeyState.PopupComposeOpenPGP;
this.defautOptionsAfterRender = defautOptionsAfterRender;
this.addOptionClass = (domOption, item) => {
this.defautOptionsAfterRender(domOption, item);
if (item && !isUnd(item.class) && domOption)
{
$(domOption).addClass(item.class);
}
};
this.deletePublickKey = _.bind(this.deletePublickKey, this);
}
@command((self) => !self.submitRequest() && (self.sign() || self.encrypt()))
doCommand() {
let
result = true,
privateKey = null,
aPublicKeys = [];
this.submitRequest(true);
if (result && this.sign())
{
if (!this.signKey())
{
this.notification(i18n('PGP_NOTIFICATIONS/NO_PRIVATE_KEY_FOUND'));
result = false;
}
else if (!this.signKey().key)
{
this.notification(i18n('PGP_NOTIFICATIONS/NO_PRIVATE_KEY_FOUND_FOR', {
'EMAIL': this.signKey().email
}));
2014-08-21 23:08:34 +08:00
2016-09-10 06:38:16 +08:00
result = false;
}
2014-08-21 23:08:34 +08:00
2016-09-10 06:38:16 +08:00
if (result)
2014-08-21 23:08:34 +08:00
{
2016-09-10 06:38:16 +08:00
const privateKeys = this.signKey().key.getNativeKeys();
privateKey = privateKeys[0] || null;
try
2015-07-30 01:21:24 +08:00
{
2016-09-10 06:38:16 +08:00
if (privateKey)
{
privateKey.decrypt(pString(this.password()));
}
2015-07-30 01:21:24 +08:00
}
2016-09-10 06:38:16 +08:00
catch (e)
2014-03-21 07:47:13 +08:00
{
2016-09-10 06:38:16 +08:00
privateKey = null;
}
2014-08-21 23:08:34 +08:00
2016-09-10 06:38:16 +08:00
if (!privateKey)
{
this.notification(i18n('PGP_NOTIFICATIONS/NO_PRIVATE_KEY_FOUND'));
result = false;
2014-03-21 07:47:13 +08:00
}
2016-09-10 06:38:16 +08:00
}
}
2014-03-21 07:47:13 +08:00
2016-09-10 06:38:16 +08:00
if (result && this.encrypt())
{
if (0 === this.encryptKeys().length)
{
this.notification(i18n('PGP_NOTIFICATIONS/NO_PUBLIC_KEYS_FOUND'));
result = false;
}
else if (this.encryptKeys())
{
aPublicKeys = [];
2014-03-21 07:47:13 +08:00
2016-09-10 06:38:16 +08:00
_.each(this.encryptKeys(), (oKey) => {
if (oKey && oKey.key)
2014-03-21 07:47:13 +08:00
{
2016-09-10 06:38:16 +08:00
aPublicKeys = aPublicKeys.concat(_.compact(_.flatten(oKey.key.getNativeKeys())));
2015-07-30 01:21:24 +08:00
}
2016-09-10 06:38:16 +08:00
else if (oKey && oKey.email)
2015-07-30 01:21:24 +08:00
{
2016-09-10 06:38:16 +08:00
this.notification(i18n('PGP_NOTIFICATIONS/NO_PUBLIC_KEYS_FOUND_FOR', {
'EMAIL': oKey.email
}));
2014-04-18 06:42:30 +08:00
result = false;
}
2016-09-10 06:38:16 +08:00
});
2015-07-30 01:21:24 +08:00
2016-09-10 06:38:16 +08:00
if (result && (0 === aPublicKeys.length || this.encryptKeys().length !== aPublicKeys.length))
{
result = false;
}
2016-09-10 06:38:16 +08:00
}
}
if (result && this.resultCallback)
{
_.delay(() => {
2015-07-30 01:21:24 +08:00
2016-09-10 06:38:16 +08:00
let pgpPromise = null;
2016-06-30 08:02:45 +08:00
2016-09-10 06:38:16 +08:00
try
{
if (privateKey && 0 === aPublicKeys.length)
2016-06-30 08:02:45 +08:00
{
2016-09-10 06:38:16 +08:00
pgpPromise = PgpStore.openpgp.sign({
data: this.text(),
privateKeys: [privateKey]
});
}
else if (privateKey && 0 < aPublicKeys.length)
{
pgpPromise = PgpStore.openpgp.encrypt({
data: this.text(),
publicKeys: aPublicKeys,
privateKeys: [privateKey]
});
}
else if (!privateKey && 0 < aPublicKeys.length)
{
pgpPromise = PgpStore.openpgp.encrypt({
data: this.text(),
publicKeys: aPublicKeys
});
2015-07-30 01:21:24 +08:00
}
}
2016-09-10 06:38:16 +08:00
catch (e)
{
log(e);
2016-09-10 06:38:16 +08:00
this.notification(i18n('PGP_NOTIFICATIONS/PGP_ERROR', {
'ERROR': '' + e
}));
}
2015-07-30 01:21:24 +08:00
2016-09-10 06:38:16 +08:00
if (pgpPromise)
{
try
{
2016-09-10 06:38:16 +08:00
pgpPromise.then((mData) => {
this.resultCallback(mData.data);
this.cancelCommand();
}).catch((e) => {
this.notification(i18n('PGP_NOTIFICATIONS/PGP_ERROR', {
'ERROR': '' + e
}));
});
2014-08-21 23:08:34 +08:00
}
catch (e)
{
this.notification(i18n('PGP_NOTIFICATIONS/PGP_ERROR', {
2014-08-21 23:08:34 +08:00
'ERROR': '' + e
}));
}
2016-09-10 06:38:16 +08:00
}
2014-03-20 06:39:36 +08:00
this.submitRequest(false);
2014-03-20 06:39:36 +08:00
2016-09-10 06:38:16 +08:00
}, Magics.Time20ms);
}
else
{
this.submitRequest(false);
}
2016-09-10 06:38:16 +08:00
return result;
}
2016-09-10 06:38:16 +08:00
@command()
selectCommand() {
const
keyId = this.selectedPrivateKey(),
option = keyId ? _.find(this.privateKeysOptions(), (item) => item && keyId === item.id) : null;
2016-06-30 08:02:45 +08:00
2016-09-10 06:38:16 +08:00
if (option)
{
this.signKey({
'empty': !option.key,
'selected': ko.observable(!!option.key),
'users': option.key.users,
'hash': option.key.id.substr(KEY_NAME_SUBSTR).toUpperCase(),
'key': option.key
});
}
}
2016-06-30 08:02:45 +08:00
2016-09-10 06:38:16 +08:00
@command()
addCommand() {
2016-06-30 08:02:45 +08:00
2016-09-10 06:38:16 +08:00
const
keyId = this.selectedPublicKey(),
keys = this.encryptKeys(),
option = keyId ? _.find(this.publicKeysOptions(), (item) => (item && keyId === item.id)) : null;
2016-09-10 06:38:16 +08:00
if (option)
{
keys.push({
'empty': !option.key,
'selected': ko.observable(!!option.key),
'removable': ko.observable(!this.sign() || !this.signKey() || this.signKey().key.id !== option.key.id),
'users': option.key.users,
'hash': option.key.id.substr(KEY_NAME_SUBSTR).toUpperCase(),
'key': option.key
});
2015-07-30 01:21:24 +08:00
2016-09-10 06:38:16 +08:00
this.encryptKeys(keys);
}
}
2015-07-30 01:21:24 +08:00
2016-09-10 06:38:16 +08:00
@command()
updateCommand() {
_.each(this.encryptKeys(), (oKey) => {
oKey.removable(!this.sign() || !this.signKey() || this.signKey().key.id !== oKey.key.id);
});
}
2015-07-30 01:21:24 +08:00
deletePublickKey(publicKey) {
this.encryptKeys.remove(publicKey);
}
2016-04-21 06:42:16 +08:00
clearPopup() {
this.notification('');
2016-04-21 06:42:16 +08:00
this.sign(false);
this.encrypt(false);
2016-04-21 06:42:16 +08:00
this.password('');
this.password.focus(false);
this.buttonFocus(false);
2014-03-21 07:47:13 +08:00
this.signKey(null);
this.encryptKeys([]);
this.text('');
2014-03-21 07:47:13 +08:00
this.resultCallback = null;
}
2014-03-21 07:47:13 +08:00
onBuild() {
key('tab,shift+tab', KeyState.PopupComposeOpenPGP, () => {
switch (true)
{
case this.password.focus():
this.buttonFocus(true);
break;
case this.buttonFocus():
this.password.focus(true);
break;
// no default
}
return false;
});
}
2015-07-30 01:21:24 +08:00
onHideWithDelay() {
this.clearPopup();
}
2014-03-20 06:39:36 +08:00
onShowWithDelay() {
if (this.sign())
2016-06-30 08:02:45 +08:00
{
this.password.focus(true);
2016-06-30 08:02:45 +08:00
}
else
{
this.buttonFocus(true);
}
}
2015-07-30 01:21:24 +08:00
onShow(fCallback, sText, identity, sTo, sCc, sBcc) {
this.clearPopup();
2014-03-20 06:39:36 +08:00
let
rec = [],
emailLine = '';
const email = new EmailModel();
2014-03-20 06:39:36 +08:00
this.resultCallback = fCallback;
2014-03-21 00:05:35 +08:00
if ('' !== sTo)
{
rec.push(sTo);
}
2014-03-21 00:05:35 +08:00
if ('' !== sCc)
{
rec.push(sCc);
}
2014-03-20 06:39:36 +08:00
if ('' !== sBcc)
{
rec.push(sBcc);
}
2014-03-21 00:05:35 +08:00
rec = rec.join(', ').split(',');
rec = _.compact(_.map(rec, (value) => {
email.clear();
2017-09-28 01:58:15 +08:00
email.parse(trim(value));
return '' === email.email ? false : email.email;
}));
2014-03-21 00:05:35 +08:00
if (identity && identity.email())
{
emailLine = identity.email();
rec.unshift(emailLine);
2014-08-21 23:08:34 +08:00
const keys = PgpStore.findAllPrivateKeysByEmailNotNative(emailLine);
if (keys && keys[0])
{
this.signKey({
'users': keys[0].users || [emailLine],
2016-08-24 06:17:50 +08:00
'hash': keys[0].id.substr(KEY_NAME_SUBSTR).toUpperCase(),
'key': keys[0]
});
}
}
2016-08-10 03:52:30 +08:00
if (this.signKey())
2014-08-21 23:08:34 +08:00
{
this.sign(true);
2014-08-21 23:08:34 +08:00
}
if (rec && 0 < rec.length)
{
this.encryptKeys(_.uniq(_.compact(_.flatten(_.map(rec, (recEmail) => {
const keys = PgpStore.findAllPublicKeysByEmailNotNative(recEmail);
2016-08-24 06:17:50 +08:00
return keys ? _.map(keys, (publicKey) => ({
'empty': !publicKey,
'selected': ko.observable(!!publicKey),
'removable': ko.observable(!this.sign() || !this.signKey() || this.signKey().key.id !== publicKey.id),
'users': publicKey ? (publicKey.users || [recEmail]) : [recEmail],
'hash': publicKey ? publicKey.id.substr(KEY_NAME_SUBSTR).toUpperCase() : '',
'key': publicKey
})) : [];
}), true)), (encryptKey) => encryptKey.hash));
2014-08-21 23:08:34 +08:00
if (0 < this.encryptKeys().length)
{
this.encrypt(true);
}
2015-07-30 01:21:24 +08:00
}
this.text(sText);
}
}
2014-08-21 23:08:34 +08:00
2016-09-10 06:38:16 +08:00
export {ComposeOpenPgpPopupView, ComposeOpenPgpPopupView as default};