snappymail/dev/View/Popup/AddOpenPgpKey.js

109 lines
2.2 KiB
JavaScript
Raw Normal View History

2016-06-30 08:02:45 +08:00
var
_ = require('_'),
ko = require('ko'),
2014-08-25 23:49:01 +08:00
2016-06-30 08:02:45 +08:00
Utils = require('Common/Utils'),
2014-08-21 23:08:34 +08:00
2016-06-30 08:02:45 +08:00
PgpStore = require('Stores/User/Pgp'),
2014-08-25 15:10:51 +08:00
2016-06-30 08:02:45 +08:00
kn = require('Knoin/Knoin'),
AbstractView = require('Knoin/AbstractView');
2014-08-21 23:08:34 +08:00
2016-06-30 08:02:45 +08:00
/**
* @constructor
* @extends AbstractView
*/
function AddOpenPgpKeyPopupView()
{
AbstractView.call(this, 'Popups', 'PopupsAddOpenPgpKey');
2014-08-21 23:08:34 +08:00
2016-06-30 08:02:45 +08:00
this.key = ko.observable('');
this.key.error = ko.observable(false);
this.key.focus = ko.observable(false);
2014-08-25 15:10:51 +08:00
2016-06-30 08:02:45 +08:00
this.key.subscribe(function() {
this.key.error(false);
}, this);
this.addOpenPgpKeyCommand = Utils.createCommand(this, function() {
var
count = 30,
2016-08-10 03:52:30 +08:00
keyTrimmed = Utils.trim(this.key()),
2016-06-30 08:02:45 +08:00
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
openpgpKeyring = PgpStore.openpgpKeyring;
2016-06-30 08:02:45 +08:00
2016-08-10 03:52:30 +08:00
if (/[\n]/.test(keyTrimmed))
2016-06-30 08:02:45 +08:00
{
2016-08-10 03:52:30 +08:00
keyTrimmed = keyTrimmed.replace(/[\r]+/g, '').replace(/[\n]{2,}/g, '\n\n');
2016-06-30 08:02:45 +08:00
}
2016-08-10 03:52:30 +08:00
this.key.error('' === keyTrimmed);
2016-06-30 08:02:45 +08:00
if (!openpgpKeyring || this.key.error())
{
return false;
}
var done = false;
2016-08-10 03:52:30 +08:00
2016-06-30 08:02:45 +08:00
do
{
var match = reg.exec(keyTrimmed);
if (match && 0 < count)
2014-04-27 00:09:38 +08:00
{
2016-06-30 08:02:45 +08:00
if (match[0] && match[1] && match[2] && match[1] === match[2])
2014-04-27 00:09:38 +08:00
{
2016-06-30 08:02:45 +08:00
if ('PRIVATE' === match[1])
2014-08-21 23:08:34 +08:00
{
2016-06-30 08:02:45 +08:00
openpgpKeyring.privateKeys.importKey(match[0]);
}
else if ('PUBLIC' === match[1])
{
openpgpKeyring.publicKeys.importKey(match[0]);
2014-08-21 23:08:34 +08:00
}
2014-04-27 00:09:38 +08:00
}
2014-08-21 23:08:34 +08:00
2016-06-30 08:02:45 +08:00
count -= 1;
done = false;
}
else
{
done = true;
2014-04-27 00:09:38 +08:00
}
2016-06-30 08:02:45 +08:00
}
while (!done);
2016-06-30 08:02:45 +08:00
openpgpKeyring.store();
2014-07-12 02:57:22 +08:00
2016-06-30 08:02:45 +08:00
require('App/User').default.reloadOpenPgpKeys();
Utils.delegateRun(this, 'cancelCommand');
2016-06-30 08:02:45 +08:00
return true;
});
2014-08-21 23:08:34 +08:00
2016-06-30 08:02:45 +08:00
kn.constructorEnd(this);
}
kn.extendAsViewModel(['View/Popup/AddOpenPgpKey', 'PopupsAddOpenPgpKeyViewModel'], AddOpenPgpKeyPopupView);
_.extend(AddOpenPgpKeyPopupView.prototype, AbstractView.prototype);
2016-06-30 08:02:45 +08:00
AddOpenPgpKeyPopupView.prototype.clearPopup = function()
{
this.key('');
this.key.error(false);
};
2016-06-30 08:02:45 +08:00
AddOpenPgpKeyPopupView.prototype.onShow = function()
{
this.clearPopup();
};
2016-06-30 08:02:45 +08:00
AddOpenPgpKeyPopupView.prototype.onShowWithDelay = function()
{
this.key.focus(true);
};
2016-06-30 08:02:45 +08:00
module.exports = AddOpenPgpKeyPopupView;