snappymail/dev/ViewModels/Popups/PopupsAddOpenPgpKeyViewModel.js

91 lines
1.9 KiB
JavaScript
Raw Normal View History

/* RainLoop Webmail (c) RainLoop Team | Licensed under CC BY-NC-SA 3.0 */
/**
* @constructor
* @extends KnoinAbstractViewModel
*/
function PopupsAddOpenPgpKeyViewModel()
{
KnoinAbstractViewModel.call(this, 'Popups', 'PopupsAddOpenPgpKey');
this.key = ko.observable('');
this.key.error = ko.observable(false);
this.key.focus = ko.observable(false);
this.key.subscribe(function () {
this.key.error(false);
}, this);
this.addOpenPgpKeyCommand = Utils.createCommand(this, function () {
var
2014-04-27 00:09:38 +08:00
iCount = 30,
aMatch = null,
sKey = Utils.trim(this.key()),
2014-07-12 02:57:22 +08:00
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,
oOpenpgpKeyring = RL.data().openpgpKeyring
;
2014-07-12 02:57:22 +08:00
sKey = sKey.replace(/[\r\n]([a-zA-Z0-9]{2,}:[^\r\n]+)[\r\n]+([a-zA-Z0-9\/\\+=]{10,})/g, '\n$1!-!N!-!$2')
.replace(/[\n\r]+/g, '\n').replace(/!-!N!-!/g, '\n\n');
this.key.error('' === sKey);
2014-07-12 02:57:22 +08:00
if (!oOpenpgpKeyring || this.key.error())
{
return false;
}
2014-04-27 00:09:38 +08:00
do
{
aMatch = oReg.exec(sKey);
if (!aMatch || 0 > iCount)
{
break;
}
if (aMatch[0] && aMatch[1] && aMatch[2] && aMatch[1] === aMatch[2])
{
if ('PRIVATE' === aMatch[1])
{
oOpenpgpKeyring.privateKeys.importKey(aMatch[0]);
}
else if ('PUBLIC' === aMatch[1])
{
oOpenpgpKeyring.publicKeys.importKey(aMatch[0]);
}
}
iCount--;
}
while (true);
oOpenpgpKeyring.store();
RL.reloadOpenPgpKeys();
Utils.delegateRun(this, 'cancelCommand');
2014-07-12 02:57:22 +08:00
return true;
});
Knoin.constructorEnd(this);
}
Utils.extendAsViewModel('PopupsAddOpenPgpKeyViewModel', PopupsAddOpenPgpKeyViewModel);
PopupsAddOpenPgpKeyViewModel.prototype.clearPopup = function ()
{
this.key('');
this.key.error(false);
};
PopupsAddOpenPgpKeyViewModel.prototype.onShow = function ()
{
this.clearPopup();
};
PopupsAddOpenPgpKeyViewModel.prototype.onFocus = function ()
{
this.key.focus(true);
};