OpenPGP (Compose) (#53) UNSTABLE

This commit is contained in:
RainLoop Team 2014-03-21 03:47:13 +04:00
parent b330f77bc0
commit 1c4ce1dfb2
14 changed files with 631 additions and 321 deletions

View file

@ -16,13 +16,168 @@ function PopupsComposeOpenPgpViewModel()
this.password = ko.observable('');
this.password.focus = ko.observable(true);
this.from = ko.observable('');
this.to = ko.observableArray([]);
this.text = ko.observable('');
this.resultCallback = null;
this.submitRequest = ko.observable(false);
// commands
this.doCommand = Utils.createCommand(this, function () {
this.cancelCommand();
var
self = this,
bResult = true,
aOpenpgpkeysPublic = RL.data().openpgpkeysPublic(),
oKey = null,
oPrivateKey = null,
aPublicKeys = [],
fFindPublicKey = function (sEmail) {
var
oResult = null,
oKey = _.find(aOpenpgpkeysPublic, function (oItem) {
return oItem && sEmail === oItem.email;
})
;
if (oKey)
{
try
{
oResult = window.openpgp.key.readArmored(oKey.armor);
if (oResult && !oResult.err && oResult.keys && oResult.keys[0])
{
oResult = oResult.keys[0];
}
else
{
oResult = null;
}
}
catch (e)
{
oResult = null;
}
}
return oResult;
}
;
this.submitRequest(true);
if (bResult && this.sign() && '' === this.from())
{
this.notification('Please specify From email address');
bResult = false;
}
if (bResult && this.sign())
{
oKey = _.find(RL.data().openpgpkeysPrivate(), function (oItem) {
return oItem && self.from() === oItem.email;
});
if (oKey)
{
try
{
oPrivateKey = window.openpgp.key.readArmored(oKey.armor);
if (oPrivateKey && !oPrivateKey.err && oPrivateKey.keys && oPrivateKey.keys[0])
{
oPrivateKey = oPrivateKey.keys[0];
oPrivateKey.decrypt(this.password());
}
else
{
oPrivateKey = null;
}
}
catch (e)
{
oPrivateKey = null;
}
}
if (!oPrivateKey)
{
this.notification('No private key found for "' + this.from() + '" email');
bResult = false;
}
}
if (bResult && this.encrypt() && 0 === this.to().length)
{
this.notification('Please specify at least one recipient');
bResult = false;
}
if (bResult && this.encrypt())
{
aPublicKeys = _.compact(_.map(this.to(), function (sEmail) {
var oKey = fFindPublicKey(sEmail);
if (!oKey && bResult)
{
self.notification('No public key found for "' + sEmail + '" email');
bResult = false;
}
return oKey;
}));
if (0 === aPublicKeys.length || this.to().length !== aPublicKeys.length)
{
bResult = false;
}
}
_.delay(function () {
if (self.resultCallback && bResult)
{
try {
if (oPrivateKey && 0 === aPublicKeys.length)
{
self.resultCallback(
window.openpgp.signClearMessage([oPrivateKey], self.text())
);
}
else if (oPrivateKey && 0 < aPublicKeys.length)
{
self.resultCallback(
window.openpgp.signAndEncryptMessage(aPublicKeys, oPrivateKey, self.text())
);
}
else if (!oPrivateKey && 0 < aPublicKeys.length)
{
self.resultCallback(
window.openpgp.encryptMessage(aPublicKeys, self.text())
);
}
}
catch (e)
{
self.notification('OpenPGP error: ' + e);
bResult = false;
}
}
if (bResult)
{
self.cancelCommand();
}
self.submitRequest(false);
}, 10);
}, function () {
return '' === this.notification();
return !this.submitRequest() && (this.sign() || this.encrypt());
});
Knoin.constructorEnd(this);
@ -36,6 +191,14 @@ PopupsComposeOpenPgpViewModel.prototype.clearPopup = function ()
this.password('');
this.password.focus(false);
this.from('');
this.to([]);
this.text('');
this.submitRequest(false);
this.resultCallback = null;
};
PopupsComposeOpenPgpViewModel.prototype.onHide = function ()
@ -53,20 +216,11 @@ PopupsComposeOpenPgpViewModel.prototype.onShow = function (fCallback, sText, sFr
aRec = []
;
if ('' === sTo + sCc + sBcc)
{
this.notification('Please specify at least one recipient');
return false;
}
this.resultCallback = fCallback;
oEmail.clear();
oEmail.mailsoParse(sFromEmail);
if ('' === oEmail.email)
{
this.notification('Please specify From email address');
return false;
}
else
if ('' !== oEmail.email)
{
sResultFromEmail = oEmail.email;
}
@ -93,22 +247,7 @@ PopupsComposeOpenPgpViewModel.prototype.onShow = function (fCallback, sText, sFr
return '' === oEmail.email ? false : oEmail.email;
}));
if (0 === aRec.length)
{
this.notification('Please specify at least one recipient');
return false;
}
window.console.log(sResultFromEmail);
window.console.log(aRec);
// TODO
};
PopupsComposeOpenPgpViewModel.prototype.onFocus = function ()
{
if (this.sign())
{
this.password.focus(true);
}
this.from(sResultFromEmail);
this.to(aRec);
this.text(sText);
};