snappymail/dev/ViewModels/Popups/PopupsAddOpenPgpKeyViewModel.js

109 lines
2.3 KiB
JavaScript
Raw Normal View History

/* RainLoop Webmail (c) RainLoop Team | Licensed under CC BY-NC-SA 3.0 */
2014-08-25 23:49:01 +08:00
(function (module, require) {
'use strict';
2014-08-21 23:08:34 +08:00
var
2014-08-25 23:49:01 +08:00
ko = require('ko'),
2014-08-25 15:10:51 +08:00
2014-08-25 23:49:01 +08:00
Utils = require('Utils'),
2014-08-21 23:08:34 +08:00
2014-08-27 23:59:44 +08:00
Data = require('Storage:RainLoop:Data'),
2014-08-21 23:08:34 +08:00
2014-08-27 23:59:44 +08:00
kn = require('App:Knoin'),
KnoinAbstractViewModel = require('Knoin:AbstractViewModel')
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 KnoinAbstractViewModel
*/
function PopupsAddOpenPgpKeyViewModel()
{
KnoinAbstractViewModel.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,
oOpenpgpKeyring = Data.openpgpKeyring
;
2014-07-12 02:57:22 +08:00
2014-08-21 23:08:34 +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');
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-08-27 23:59:44 +08:00
require('App:RainLoop').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);
}
2014-08-21 23:08:34 +08:00
kn.extendAsViewModel('PopupsAddOpenPgpKeyViewModel', PopupsAddOpenPgpKeyViewModel);
PopupsAddOpenPgpKeyViewModel.prototype.clearPopup = function ()
{
this.key('');
this.key.error(false);
};
2014-08-21 23:08:34 +08:00
PopupsAddOpenPgpKeyViewModel.prototype.onShow = function ()
{
this.clearPopup();
};
2014-08-21 23:08:34 +08:00
PopupsAddOpenPgpKeyViewModel.prototype.onFocus = function ()
{
this.key.focus(true);
};
2014-08-22 23:08:56 +08:00
module.exports = PopupsAddOpenPgpKeyViewModel;
2014-08-25 23:49:01 +08:00
}(module, require));