snappymail/dev/View/Popup/AddOpenPgpKey.js

113 lines
2.2 KiB
JavaScript
Raw Normal View History

2014-09-05 06:49:03 +08:00
(function () {
2014-08-25 23:49:01 +08:00
'use strict';
2014-08-21 23:08:34 +08:00
var
_ = require('_'),
2014-08-25 23:49:01 +08:00
ko = require('ko'),
2014-08-25 15:10:51 +08:00
2014-09-05 06:49:03 +08:00
Utils = require('Common/Utils'),
2014-08-21 23:08:34 +08:00
2015-02-03 07:58:58 +08:00
PgpStore = require('Stores/User/Pgp'),
2014-08-21 23:08:34 +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 AddOpenPgpKeyPopupView()
2014-08-21 23:08:34 +08:00
{
AbstractView.call(this, 'Popups', 'PopupsAddOpenPgpKey');
2014-08-21 23:08:34 +08:00
this.key = ko.observable('');
this.key.error = ko.observable(false);
this.key.focus = ko.observable(false);
2014-08-21 23:08:34 +08:00
this.key.subscribe(function () {
this.key.error(false);
}, this);
2014-08-21 23:08:34 +08:00
this.addOpenPgpKeyCommand = Utils.createCommand(this, function () {
2014-07-12 02:57:22 +08:00
2014-08-21 23:08:34 +08:00
var
iCount = 30,
aMatch = null,
sKey = Utils.trim(this.key()),
oReg = /[\-]{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,
2015-02-03 07:58:58 +08:00
oOpenpgpKeyring = PgpStore.openpgpKeyring
2014-08-21 23:08:34 +08:00
;
2014-07-12 02:57:22 +08:00
2015-03-26 20:46:17 +08:00
if (/[\n]/.test(sKey))
{
sKey = sKey.replace(/[\r]+/g, '')
.replace(/[\n]{2,}/g, '\n\n');
}
2014-08-21 23:08:34 +08:00
this.key.error('' === sKey);
if (!oOpenpgpKeyring || this.key.error())
2014-04-27 00:09:38 +08:00
{
2014-08-21 23:08:34 +08:00
return false;
2014-04-27 00:09:38 +08:00
}
2014-08-21 23:08:34 +08:00
do
2014-04-27 00:09:38 +08:00
{
2014-08-21 23:08:34 +08:00
aMatch = oReg.exec(sKey);
if (!aMatch || 0 > iCount)
2014-04-27 00:09:38 +08:00
{
2014-08-21 23:08:34 +08:00
break;
2014-04-27 00:09:38 +08:00
}
2014-08-21 23:08:34 +08:00
if (aMatch[0] && aMatch[1] && aMatch[2] && aMatch[1] === aMatch[2])
2014-04-27 00:09:38 +08:00
{
2014-08-21 23:08:34 +08:00
if ('PRIVATE' === aMatch[1])
{
oOpenpgpKeyring.privateKeys.importKey(aMatch[0]);
}
else if ('PUBLIC' === aMatch[1])
{
oOpenpgpKeyring.publicKeys.importKey(aMatch[0]);
}
2014-04-27 00:09:38 +08:00
}
2014-08-21 23:08:34 +08:00
iCount--;
2014-04-27 00:09:38 +08:00
}
2014-08-21 23:08:34 +08:00
while (true);
2014-04-27 00:09:38 +08:00
2014-08-21 23:08:34 +08:00
oOpenpgpKeyring.store();
2014-04-27 00:09:38 +08:00
2014-10-18 21:43:44 +08:00
require('App/User').reloadOpenPgpKeys();
2014-08-21 23:08:34 +08:00
Utils.delegateRun(this, 'cancelCommand');
2014-08-21 23:08:34 +08:00
return true;
});
2014-07-12 02:57:22 +08:00
2014-08-21 23:08:34 +08:00
kn.constructorEnd(this);
}
kn.extendAsViewModel(['View/Popup/AddOpenPgpKey', 'PopupsAddOpenPgpKeyViewModel'], AddOpenPgpKeyPopupView);
_.extend(AddOpenPgpKeyPopupView.prototype, AbstractView.prototype);
2014-08-21 23:08:34 +08:00
AddOpenPgpKeyPopupView.prototype.clearPopup = function ()
2014-08-21 23:08:34 +08:00
{
this.key('');
this.key.error(false);
};
AddOpenPgpKeyPopupView.prototype.onShow = function ()
2014-08-21 23:08:34 +08:00
{
this.clearPopup();
};
2015-02-16 05:55:59 +08:00
AddOpenPgpKeyPopupView.prototype.onShowWithDelay = function ()
2014-08-21 23:08:34 +08:00
{
this.key.focus(true);
};
module.exports = AddOpenPgpKeyPopupView;
2014-09-05 06:49:03 +08:00
}());