Aleksander Machniak
2015-08-29 a63f14ec4045e82f47b237663bcf09939a0eadc5
plugins/enigma/enigma.js
@@ -3,9 +3,6 @@
window.rcmail && rcmail.addEventListener('init', function(evt) {
    if (rcmail.env.task == 'settings') {
        rcmail.register_command('plugin.enigma', function() { rcmail.goto_url('plugin.enigma') }, true);
        rcmail.register_command('plugin.enigma-key-import', function() { rcmail.enigma_key_import() }, true);
//        rcmail.register_command('plugin.enigma-key-export', function() { rcmail.enigma_key_export() }, true);
        rcmail.register_command('plugin.enigma-key-delete', function(props) { return rcmail.enigma_key_delete(); });
        if (rcmail.gui_objects.keyslist) {
            rcmail.keys_list = new rcube_list_widget(rcmail.gui_objects.keyslist,
@@ -28,7 +25,11 @@
            rcmail.register_command('search', function(props) {return rcmail.enigma_search(props); }, true);
            rcmail.register_command('reset-search', function(props) {return rcmail.enigma_search_reset(props); }, true);
            rcmail.register_command('plugin.enigma-import', function() { rcmail.enigma_import(); }, true);
//            rcmail.register_command('plugin.enigma-export', function() { rcmail.enigma_export(); }, true);
//            rcmail.register_command('plugin.enigma-export', function() { rcmail.enigma_key_export(); }, true);
            rcmail.register_command('plugin.enigma-key-import', function() { rcmail.enigma_key_import() }, true);
            rcmail.register_command('plugin.enigma-key-delete', function(props) { return rcmail.enigma_key_delete(); });
            rcmail.register_command('plugin.enigma-key-create', function(props) { return rcmail.enigma_key_create(); }, true);
            rcmail.register_command('plugin.enigma-key-save', function(props) { return rcmail.enigma_key_create_save(); }, true);
        }
    }
    else if (rcmail.env.task == 'mail') {
@@ -41,10 +42,9 @@
                e.stopPropagation();
            });
        }
        else if (rcmail.env.action == 'show' || rcmail.env.action == 'preview') {
            if (rcmail.env.enigma_password_request) {
                rcmail.enigma_password_request(rcmail.env.enigma_password_request);
            }
        if (rcmail.env.enigma_password_request) {
            rcmail.enigma_password_request(rcmail.env.enigma_password_request);
        }
    }
});
@@ -58,6 +58,70 @@
rcube_webmail.prototype.enigma_key_import = function()
{
    this.enigma_loadframe('&_action=plugin.enigmakeys&_a=import');
};
// Display key(s) generation form
rcube_webmail.prototype.enigma_key_create = function()
{
    this.enigma_loadframe('&_action=plugin.enigmakeys&_a=create');
};
// Generate key(s) and submit them
rcube_webmail.prototype.enigma_key_create_save = function()
{
    var options, lock,
        user = $('#key-ident > option').filter(':selected').text(),
        password = $('#key-pass').val(),
        confirm = $('#key-pass-confirm').val(),
        size = $('#key-size').val();
    // validate the form
    if (!password || !confirm)
        return alert(this.gettext('enigma.formerror'));
    if (password != confirm)
        return alert(this.gettext('enigma.passwordsdiffer'));
    if (user.match(/^<[^>]+>$/))
        return alert(this.gettext('enigma.nonameident'));
    // generate keys
    // use OpenPGP.js if browser supports required features
    if (window.openpgp && window.crypto && (window.crypto.getRandomValues || window.crypto.subtle)) {
        lock = this.set_busy(true, 'enigma.keygenerating');
        options = {
            numBits: size,
            userId: user,
            passphrase: password
        };
        openpgp.generateKeyPair(options).then(function(keypair) {
            // success
            var post = {_a: 'import', _keys: keypair.privateKeyArmored};
            // send request to server
            rcmail.http_post('plugin.enigmakeys', post, lock);
        }).catch(function(error) {
            // failure
            rcmail.set_busy(false, null, lock);
            rcmail.display_message(rcmail.gettext('enigma.keygenerateerror'), 'error');
        });
    }
    // generate keys on the server
    else if (rcmail.env.enigma_keygen_server) {
        lock = this.set_busy(true, 'enigma.keygenerating');
        options = {_a: 'generate', _user: user, _password: password, _size: size};
        rcmail.http_post('plugin.enigmakeys', options, lock);
    }
    else {
        rcmail.display_message(rcmail.gettext('enigma.keygennosupport'), 'error');
    }
};
// Action executed after successful key generation and import
rcube_webmail.prototype.enigma_key_create_success = function()
{
    parent.rcmail.enigma_list(1);
};
// Delete key(s)
@@ -131,8 +195,8 @@
            return;
        }
        this.set_busy(true);
        frm.location.href = this.env.comm_path + '&_framed=1' + url;
        this.env.frame_lock = this.set_busy(true, 'loading');
        frm.location.href = this.env.comm_path + '&_framed=1&' + url;
    }
};
@@ -324,15 +388,16 @@
            click: function(e) {
                e.stopPropagation();
                var jq = ref.is_framed() ? window.parent.$ : $,
                    pass = myprompt_input.val();
                var jq = ref.is_framed() ? window.parent.$ : $;
                if (!pass) {
                data.password = myprompt_input.val();
                if (!data.password) {
                    myprompt_input.focus();
                    return;
                }
                ref.enigma_password_submit(data.key, pass);
                ref.enigma_password_submit(data);
                jq(this).remove();
            }
        },
@@ -352,34 +417,37 @@
}
// submit entered password
rcube_webmail.prototype.enigma_password_submit = function(keyid, password)
rcube_webmail.prototype.enigma_password_submit = function(data)
{
    if (this.env.action == 'compose') {
        return this.enigma_password_compose_submit(keyid, password);
    if (this.env.action == 'compose' && !data['compose-init']) {
        return this.enigma_password_compose_submit(data);
    }
    var lock = this.set_busy(true, 'loading');
    // message preview
    var form = $('<form>').attr({method: 'post', action: location.href, style: 'display:none'})
        .append($('<input>').attr({type: 'hidden', name: '_keyid', value: keyid}))
        .append($('<input>').attr({type: 'hidden', name: '_passwd', value: password}))
        .append($('<input>').attr({type: 'hidden', name: '_keyid', value: data.key}))
        .append($('<input>').attr({type: 'hidden', name: '_passwd', value: data.password}))
        .append($('<input>').attr({type: 'hidden', name: '_token', value: this.env.request_token}))
        .append($('<input>').attr({type: 'hidden', name: '_unlock', value: lock}))
        .appendTo(document.body);
    form.submit();
}
// submit entered password - in mail compose page
rcube_webmail.prototype.enigma_password_compose_submit = function(keyid, password)
rcube_webmail.prototype.enigma_password_compose_submit = function(data)
{
    var form = this.gui_objects.messageform;
    if (!$('input[name="_keyid"]', form).length) {
        $(form).append($('<input>').attr({type: 'hidden', name: '_keyid', value: keyid}))
            .append($('<input>').attr({type: 'hidden', name: '_passwd', value: password}));
        $(form).append($('<input>').attr({type: 'hidden', name: '_keyid', value: data.key}))
            .append($('<input>').attr({type: 'hidden', name: '_passwd', value: data.password}));
    }
    else {
        $('input[name="_keyid"]', form).val(keyid);
        $('input[name="_passwd"]', form).val(password);
        $('input[name="_keyid"]', form).val(data.key);
        $('input[name="_passwd"]', form).val(data.password);
    }
    this.submit_messageform(this.env.last_action == 'savedraft');