snappymail/dev/View/Popup/ComposeOpenPgp.js

421 lines
8.8 KiB
JavaScript
Raw Normal View History

2014-03-20 06:39:36 +08:00
2014-09-05 06:49:03 +08:00
(function () {
2014-03-20 06:39:36 +08:00
2014-08-25 23:49:01 +08:00
'use strict';
2014-08-21 23:08:34 +08:00
var
2014-08-25 23:49:01 +08:00
_ = require('_'),
ko = require('ko'),
key = require('key'),
2014-08-25 15:10:51 +08:00
2014-09-05 06:49:03 +08:00
Utils = require('Common/Utils'),
Enums = require('Common/Enums'),
Translator = require('Common/Translator'),
2014-03-20 06:39:36 +08:00
2015-02-04 01:18:08 +08:00
PgpStore = require('Stores/User/Pgp'),
2014-03-21 07:47:13 +08:00
EmailModel = require('Model/Email'),
2014-08-25 15:10:51 +08:00
kn = require('Knoin/Knoin'),
AbstractView = require('Knoin/AbstractView')
2014-08-21 23:08:34 +08:00
;
2014-08-25 15:10:51 +08:00
2014-08-21 23:08:34 +08:00
/**
* @constructor
* @extends AbstractView
2014-08-21 23:08:34 +08:00
*/
function ComposeOpenPgpPopupView()
2014-08-21 23:08:34 +08:00
{
AbstractView.call(this, 'Popups', 'PopupsComposeOpenPgp');
2014-03-21 07:47:13 +08:00
2015-07-30 01:21:24 +08:00
var self = this;
2015-07-30 02:13:49 +08:00
this.optionsCaption = Translator.i18n('PGP_NOTIFICATIONS/ADD_A_PUBLICK_KEY');
this.addOptionClass = function (oDomOption, oItem)
{
self.defautOptionsAfterRender(oDomOption, oItem);
if (oItem) {
oDomOption.classList.add(oItem['class']);
}
};
2015-07-30 01:21:24 +08:00
2014-08-21 23:08:34 +08:00
this.notification = ko.observable('');
2014-03-21 07:47:13 +08:00
2015-07-30 01:21:24 +08:00
this.sign = ko.observable(false);
this.encrypt = ko.observable(false);
2014-03-21 07:47:13 +08:00
2014-08-21 23:08:34 +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
2014-08-21 23:08:34 +08:00
this.text = ko.observable('');
2015-07-30 01:21:24 +08:00
this.selectedPublicKey = ko.observable(null);
this.signKey = ko.observable(null);
this.encryptKeys = ko.observableArray([]);
this.encryptKeysView = ko.computed(function () {
return _.compact(_.map(this.encryptKeys(), function (oKey) {
return oKey ? oKey.key : null;
}));
}, this);
this.publicKeysOptions = ko.computed(function () {
return _.compact(_.flatten(_.map(PgpStore.openpgpkeysPublic(), function (oKey, iIndex) {
return -1 < Utils.inArray(oKey, self.encryptKeysView()) ? null :
_.map(oKey.users, function (sUser) {
return {
'id': oKey.guid,
'name': '(' + oKey.id.substr(-8).toUpperCase() + ') ' + sUser,
'key': oKey,
'class': iIndex % 2 ? 'odd' : 'even'
};
});
}), true));
2015-07-30 01:21:24 +08:00
});
2014-08-21 23:08:34 +08:00
this.submitRequest = ko.observable(false);
2015-07-30 01:21:24 +08:00
this.resultCallback = null;
2014-08-21 23:08:34 +08:00
// commands
this.doCommand = Utils.createCommand(this, function () {
var
bResult = true,
oPrivateKey = null,
2015-07-30 01:21:24 +08:00
aPrivateKeys = [],
2014-08-21 23:08:34 +08:00
aPublicKeys = []
;
this.submitRequest(true);
if (bResult && this.sign())
{
2015-07-30 01:21:24 +08:00
if (!this.signKey())
{
2015-09-02 03:50:30 +08:00
this.notification(Translator.i18n('PGP_NOTIFICATIONS/NO_PRIVATE_KEY_FOUND'));
2015-07-30 01:21:24 +08:00
bResult = false;
}
else if (!this.signKey().key)
2014-03-21 07:47:13 +08:00
{
this.notification(Translator.i18n('PGP_NOTIFICATIONS/NO_PRIVATE_KEY_FOUND_FOR', {
2015-07-30 01:21:24 +08:00
'EMAIL': this.signKey().email
2014-04-18 06:42:30 +08:00
}));
2014-08-21 23:08:34 +08:00
2014-03-21 07:47:13 +08:00
bResult = false;
}
2015-07-30 01:21:24 +08:00
if (bResult)
{
aPrivateKeys = this.signKey().key.getNativeKeys();
oPrivateKey = aPrivateKeys[0] || null;
2014-03-21 07:47:13 +08:00
2015-07-30 01:21:24 +08:00
try
2014-03-21 07:47:13 +08:00
{
2015-07-30 01:21:24 +08:00
if (oPrivateKey)
{
oPrivateKey.decrypt(Utils.pString(this.password()));
}
}
catch (e)
{
oPrivateKey = null;
}
2014-08-21 23:08:34 +08:00
2015-07-30 01:21:24 +08:00
if (!oPrivateKey)
{
this.notification(Translator.i18n('PGP_NOTIFICATIONS/NO_PRIVATE_KEY_FOUND'));
2014-08-21 23:08:34 +08:00
bResult = false;
2014-03-21 07:47:13 +08:00
}
2015-07-30 01:21:24 +08:00
}
}
2014-04-18 06:42:30 +08:00
2015-07-30 01:21:24 +08:00
if (bResult && this.encrypt())
{
if (0 === this.encryptKeys().length)
2014-08-21 23:08:34 +08:00
{
2015-07-30 01:21:24 +08:00
this.notification(Translator.i18n('PGP_NOTIFICATIONS/NO_PUBLIC_KEYS_FOUND'));
2014-03-21 07:47:13 +08:00
bResult = false;
}
2015-07-30 01:21:24 +08:00
else if (this.encryptKeys())
{
aPublicKeys = [];
_.each(this.encryptKeys(), function (oKey) {
if (oKey && oKey.key)
{
aPublicKeys = aPublicKeys.concat(_.compact(_.flatten(oKey.key.getNativeKeys())));
}
else if (oKey && oKey.email)
{
self.notification(Translator.i18n('PGP_NOTIFICATIONS/NO_PUBLIC_KEYS_FOUND_FOR', {
'EMAIL': oKey.email
}));
bResult = false;
}
});
if (bResult && (0 === aPublicKeys.length || this.encryptKeys().length !== aPublicKeys.length))
{
bResult = false;
}
}
2014-03-21 07:47:13 +08:00
}
2015-07-30 01:21:24 +08:00
if (bResult && self.resultCallback)
{
_.delay(function () {
2014-03-21 07:47:13 +08:00
var oPromise = null;
2015-07-30 01:21:24 +08:00
try
{
2014-08-21 23:08:34 +08:00
if (oPrivateKey && 0 === aPublicKeys.length)
{
oPromise = PgpStore.openpgp.signClearMessage([oPrivateKey], self.text());
2014-08-21 23:08:34 +08:00
}
else if (oPrivateKey && 0 < aPublicKeys.length)
{
oPromise = PgpStore.openpgp.signAndEncryptMessage(aPublicKeys, oPrivateKey, self.text());
2014-08-21 23:08:34 +08:00
}
else if (!oPrivateKey && 0 < aPublicKeys.length)
{
oPromise = PgpStore.openpgp.encryptMessage(aPublicKeys, self.text());
2014-08-21 23:08:34 +08:00
}
}
catch (e)
{
Utils.log(e);
self.notification(Translator.i18n('PGP_NOTIFICATIONS/PGP_ERROR', {
2014-08-21 23:08:34 +08:00
'ERROR': '' + e
}));
}
2014-03-20 06:39:36 +08:00
if (oPromise)
{
try
{
oPromise.then(function (mData) {
self.resultCallback(mData);
self.cancelCommand();
})['catch'](function (e) {
self.notification(Translator.i18n('PGP_NOTIFICATIONS/PGP_ERROR', {
'ERROR': '' + e
}));
});
}
catch (e)
{
self.notification(Translator.i18n('PGP_NOTIFICATIONS/PGP_ERROR', {
'ERROR': '' + e
}));
}
}
2015-07-30 01:21:24 +08:00
self.submitRequest(false);
}, 10);
}
2015-07-30 02:13:49 +08:00
else
{
self.submitRequest(false);
}
2014-03-20 06:39:36 +08:00
2015-07-30 01:21:24 +08:00
return bResult;
2014-03-20 06:39:36 +08:00
2014-08-21 23:08:34 +08:00
}, function () {
return !this.submitRequest() && (this.sign() || this.encrypt());
});
2014-03-20 06:39:36 +08:00
2015-07-30 01:21:24 +08:00
this.addCommand = Utils.createCommand(this, function () {
var
sKeyId = this.selectedPublicKey(),
aKeys = this.encryptKeys(),
oOption = sKeyId ? _.find(this.publicKeysOptions(), function (oItem) {
return oItem && sKeyId === oItem.id;
}) : null
;
if (oOption)
{
aKeys.push({
'empty': !oOption.key,
'selected': ko.observable(!!oOption.key),
2016-04-07 20:07:19 +08:00
'removable': this.signKey().id !== oOption.key.id,
'users': oOption.key.users,
2015-09-02 03:50:30 +08:00
'hash': oOption.key.id.substr(-8).toUpperCase(),
2015-07-30 01:21:24 +08:00
'key': oOption.key
});
this.encryptKeys(aKeys);
}
});
this.selectedPublicKey.subscribe(function (sValue) {
if (sValue)
{
this.addCommand();
}
}, this);
2014-08-21 23:08:34 +08:00
this.sDefaultKeyScope = Enums.KeyState.PopupComposeOpenPGP;
2015-07-30 01:21:24 +08:00
this.defautOptionsAfterRender = Utils.defautOptionsAfterRender;
this.deletePublickKey = _.bind(this.deletePublickKey, this);
2014-03-21 07:47:13 +08:00
2014-08-21 23:08:34 +08:00
kn.constructorEnd(this);
}
2014-03-21 07:47:13 +08:00
kn.extendAsViewModel(['View/Popup/ComposeOpenPgp', 'PopupsComposeOpenPgpViewModel'], ComposeOpenPgpPopupView);
_.extend(ComposeOpenPgpPopupView.prototype, AbstractView.prototype);
2014-03-21 07:47:13 +08:00
2015-07-30 01:21:24 +08:00
ComposeOpenPgpPopupView.prototype.deletePublickKey = function (oKey)
{
this.encryptKeys.remove(oKey);
};
ComposeOpenPgpPopupView.prototype.clearPopup = function ()
2014-08-21 23:08:34 +08:00
{
this.notification('');
2014-03-20 06:39:36 +08:00
2015-07-30 01:21:24 +08:00
this.sign(false);
this.encrypt(false);
2014-08-21 23:08:34 +08:00
this.password('');
this.password.focus(false);
this.buttonFocus(false);
2015-07-30 01:21:24 +08:00
this.signKey(null);
this.encryptKeys([]);
2014-08-21 23:08:34 +08:00
this.text('');
2014-08-21 23:08:34 +08:00
this.resultCallback = null;
};
2014-03-20 06:39:36 +08:00
ComposeOpenPgpPopupView.prototype.onBuild = function ()
{
2014-08-21 23:08:34 +08:00
key('tab,shift+tab', Enums.KeyState.PopupComposeOpenPGP, _.bind(function () {
2014-08-21 23:08:34 +08:00
switch (true)
{
case this.password.focus():
this.buttonFocus(true);
break;
case this.buttonFocus():
this.password.focus(true);
break;
}
2014-03-20 06:39:36 +08:00
2014-08-21 23:08:34 +08:00
return false;
2014-03-21 00:05:35 +08:00
2014-08-21 23:08:34 +08:00
}, this));
};
2014-03-21 00:05:35 +08:00
2015-02-16 05:55:59 +08:00
ComposeOpenPgpPopupView.prototype.onHideWithDelay = function ()
2014-03-21 00:05:35 +08:00
{
2014-08-21 23:08:34 +08:00
this.clearPopup();
};
2014-03-20 06:39:36 +08:00
2015-02-16 05:55:59 +08:00
ComposeOpenPgpPopupView.prototype.onShowWithDelay = function ()
2014-03-21 00:05:35 +08:00
{
2014-08-21 23:08:34 +08:00
if (this.sign())
{
this.password.focus(true);
}
else
{
this.buttonFocus(true);
}
};
2014-03-21 00:05:35 +08:00
ComposeOpenPgpPopupView.prototype.onShow = function (fCallback, sText, oIdentity, sTo, sCc, sBcc)
2014-03-21 00:05:35 +08:00
{
2014-08-21 23:08:34 +08:00
this.clearPopup();
var
aRec = [],
2015-07-30 01:21:24 +08:00
sEmail = '',
oKey = null,
oEmail = new EmailModel()
2014-08-21 23:08:34 +08:00
;
this.resultCallback = fCallback;
2014-03-21 00:05:35 +08:00
2014-08-21 23:08:34 +08:00
if ('' !== sTo)
{
aRec.push(sTo);
}
if ('' !== sCc)
{
aRec.push(sCc);
}
if ('' !== sBcc)
{
aRec.push(sBcc);
}
aRec = aRec.join(', ').split(',');
aRec = _.compact(_.map(aRec, function (sValue) {
oEmail.clear();
oEmail.mailsoParse(Utils.trim(sValue));
return '' === oEmail.email ? false : oEmail.email;
}));
2015-07-30 01:21:24 +08:00
if (oIdentity && oIdentity.email())
{
sEmail = oIdentity.email();
2016-04-07 20:07:19 +08:00
aRec.unshift(sEmail);
2015-07-30 01:21:24 +08:00
oKey = PgpStore.findPrivateKeyByEmailNotNative(sEmail);
if (oKey)
{
this.signKey({
'users': oKey.users || [sEmail],
2015-09-02 03:50:30 +08:00
'hash': oKey.id.substr(-8).toUpperCase(),
2015-07-30 01:21:24 +08:00
'key': oKey
});
}
}
if (this.signKey())
{
this.sign(true);
}
if (aRec && 0 < aRec.length)
{
2016-04-07 20:07:19 +08:00
this.encryptKeys(_.uniq(_.compact(_.map(aRec, function (sEmail) {
2015-07-30 01:21:24 +08:00
var oKey = PgpStore.findPublicKeyByEmailNotNative(sEmail) || null;
return {
'empty': !oKey,
'selected': ko.observable(!!oKey),
2016-04-07 20:07:19 +08:00
'removable': oIdentity && oIdentity.email() && oIdentity.email() !== sEmail,
'users': oKey ? (oKey.users || [sEmail]) : [sEmail],
2015-09-02 03:50:30 +08:00
'hash': oKey ? oKey.id.substr(-8).toUpperCase() : '',
2015-07-30 01:21:24 +08:00
'key': oKey
};
2016-04-07 20:07:19 +08:00
})), function (oEncryptKey) {
return oEncryptKey.hash;
}));
2015-07-30 01:21:24 +08:00
if (0 < this.encryptKeys().length)
{
this.encrypt(true);
}
}
2014-08-21 23:08:34 +08:00
this.text(sText);
};
module.exports = ComposeOpenPgpPopupView;
2014-08-21 23:08:34 +08:00
}());