Aleksander Machniak
2015-08-17 72a3b29fc5a07b4976dbbd1924da662e3d83d44f
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,20 +25,26 @@
            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') {
        if (rcmail.env.action == 'compose') {
            rcmail.addEventListener('beforesend', function(props) { rcmail.enigma_beforesend_handler(props); })
                .addEventListener('beforesavedraft', function(props) { rcmail.enigma_beforesavedraft_handler(props); });
            $('input,label', $('#enigmamenu')).mouseup(function(e) {
                // don't close the menu on mouse click inside
                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);
        }
    }
});
@@ -55,6 +58,65 @@
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
            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 {
        // @TODO
    }
};
// Action executed after successful key generation and import
rcube_webmail.prototype.enigma_key_create_success = function()
{
    parent.rcmail.enigma_list(1);
};
// Delete key(s)
@@ -128,8 +190,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;
    }
};
@@ -234,9 +296,45 @@
    list.insert_row(row);
}
/*********************************************************/
/*********        Enigma Message methods         *********/
/*********************************************************/
// handle message send/save action
rcube_webmail.prototype.enigma_beforesend_handler = function(props)
{
    this.env.last_action = 'send';
    this.enigma_compose_handler(props);
}
rcube_webmail.prototype.enigma_beforesavedraft_handler = function(props)
{
    this.env.last_action = 'savedraft';
    this.enigma_compose_handler(props);
}
rcube_webmail.prototype.enigma_compose_handler = function(props)
{
    var form = this.gui_objects.messageform;
    // copy inputs from enigma menu to the form
    $('#enigmamenu input').each(function() {
        var id = this.id + '_cpy', input = $('#' + id);
        if (!input.length) {
            input = $(this).clone();
            input.prop({id: id, type: 'hidden'}).appendTo(form);
        }
        input.val(this.checked ? '1' : '');
    });
    // disable signing when saving drafts
    if (this.env.last_action == 'savedraft') {
        $('input[name="_enigma_sign"]', form).val(0);
    }
}
// Import attached keys/certs file
rcube_webmail.prototype.enigma_import_attachment = function(mime_id)
@@ -249,6 +347,7 @@
    return false;
}
// password request popup
rcube_webmail.prototype.enigma_password_request = function(data)
{
    if (!data || !data.keyid) {
@@ -268,7 +367,8 @@
            .appendTo(myprompt);
    data.key = data.keyid;
    data.keyid = data.keyid.substr(0, 8);
    if (data.keyid.length > 8)
        data.keyid = data.keyid.substr(data.keyid.length - 8);
    $.each(['keyid', 'user'], function() {
        msg = msg.replace('$' + this, data[this]);
@@ -283,15 +383,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();
            }
        },
@@ -310,13 +411,39 @@
    }
}
rcube_webmail.prototype.enigma_password_submit = function(keyid, password)
// submit entered password
rcube_webmail.prototype.enigma_password_submit = function(data)
{
    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(data)
{
    var form = this.gui_objects.messageform;
    if (!$('input[name="_keyid"]', form).length) {
        $(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(data.key);
        $('input[name="_passwd"]', form).val(data.password);
    }
    this.submit_messageform(this.env.last_action == 'savedraft');
}