From 0878c846bc2c1030ed01c8db34e20796c31ccd2d Mon Sep 17 00:00:00 2001
From: Aleksander Machniak <alec@alec.pl>
Date: Sun, 08 Mar 2015 05:54:28 -0400
Subject: [PATCH] Enigma: Larry support, finished PGP decryption, other fixes and improvements

---
 plugins/enigma/enigma.js |  218 +++++++++++++++++++++++++++++++++++++++++------------
 1 files changed, 167 insertions(+), 51 deletions(-)

diff --git a/plugins/enigma/enigma.js b/plugins/enigma/enigma.js
index 29c6482..a7bc43f 100644
--- a/plugins/enigma/enigma.js
+++ b/plugins/enigma/enigma.js
@@ -1,42 +1,51 @@
 /* Enigma Plugin */
 
-if (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);
+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)
-            {
-                var p = rcmail;
-                rcmail.keys_list = new rcube_list_widget(rcmail.gui_objects.keyslist,
-                    {multiselect:false, draggable:false, keyboard:false});
-                rcmail.keys_list.addEventListener('select', function(o){ p.enigma_key_select(o); });
-                rcmail.keys_list.init();
-                rcmail.keys_list.focus();
+        if (rcmail.gui_objects.keyslist) {
+            rcmail.keys_list = new rcube_list_widget(rcmail.gui_objects.keyslist,
+                {multiselect:false, draggable:false, keyboard:false});
+            rcmail.keys_list
+                .addEventListener('select', function(o) { rcmail.enigma_keylist_select(o); })
+                .addEventListener('keypress', function(o) { rcmail.enigma_keylist_keypress(o); })
+                .init()
+                .focus();
 
-                rcmail.enigma_list();
+            rcmail.enigma_list();
 
-                rcmail.register_command('firstpage', function(props) {return rcmail.enigma_list_page('first'); });
-                rcmail.register_command('previouspage', function(props) {return rcmail.enigma_list_page('previous'); });
-                rcmail.register_command('nextpage', function(props) {return rcmail.enigma_list_page('next'); });
-                rcmail.register_command('lastpage', function(props) {return rcmail.enigma_list_page('last'); });
-            }
+            rcmail.register_command('firstpage', function(props) { return rcmail.enigma_list_page('first'); });
+            rcmail.register_command('previouspage', function(props) { return rcmail.enigma_list_page('previous'); });
+            rcmail.register_command('nextpage', function(props) { return rcmail.enigma_list_page('next'); });
+            rcmail.register_command('lastpage', function(props) { return rcmail.enigma_list_page('last'); });
+        }
 
-            if (rcmail.env.action == 'edit-prefs') {
-                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);
-            }
-            else if (rcmail.env.action == 'plugin.enigma') {
-                rcmail.register_command('plugin.enigma-import', function() { rcmail.enigma_import() }, true);
-                rcmail.register_command('plugin.enigma-export', function() { rcmail.enigma_export() }, true);
+        if (rcmail.env.action == 'plugin.enigmakeys') {
+            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);
+        }
+    }
+    else if (rcmail.env.task == 'mail') {
+        if (rcmail.env.action == 'compose') {
+            $('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);
             }
         }
-    });
-}
+    }
+});
+
 
 /*********************************************************/
 /*********    Enigma Settings/Keys/Certs UI      *********/
@@ -45,47 +54,82 @@
 // Display key(s) import form
 rcube_webmail.prototype.enigma_key_import = function()
 {
-    this.enigma_loadframe(null, '&_a=keyimport');
+    this.enigma_loadframe('&_action=plugin.enigmakeys&_a=import');
 };
 
-// Submit key(s) form
+// Delete key(s)
+rcube_webmail.prototype.enigma_key_delete = function()
+{
+    var keys = this.keys_list.get_selection();
+
+    if (!keys.length || !confirm(this.get_label('enigma.keyremoveconfirm')))
+        return;
+
+    var lock = this.display_message(this.get_label('enigma.keyremoving'), 'loading'),
+        post = {_a: 'delete', _keys: keys};
+
+    // send request to server
+    this.http_post('plugin.enigmakeys', post, lock);
+};
+
+// Submit key(s) import form
 rcube_webmail.prototype.enigma_import = function()
 {
     var form, file;
+
     if (form = this.gui_objects.importform) {
         file = document.getElementById('rcmimportfile');
         if (file && !file.value) {
             alert(this.get_label('selectimportfile'));
             return;
         }
+
+        var lock = this.set_busy(true, 'importwait');
+
+        form.action = this.add_url(form.action, '_unlock', lock);
         form.submit();
-        this.set_busy(true, 'importwait');
+
         this.lock_form(form, true);
    }
 };
 
 // list row selection handler
-rcube_webmail.prototype.enigma_key_select = function(list)
+rcube_webmail.prototype.enigma_keylist_select = function(list)
 {
     var id;
     if (id = list.get_single_selection())
-        this.enigma_loadframe(id);
+        this.enigma_loadframe('&_action=plugin.enigmakeys&_a=info&_id=' + id);
+
+    this.enable_command('plugin.enigma-key-delete', list.selection.length > 0);
+};
+
+rcube_webmail.prototype.enigma_keylist_keypress = function(list)
+{
+    if (list.modkey == CONTROL_KEY)
+        return;
+
+    if (list.key_pressed == list.DELETE_KEY || list.key_pressed == list.BACKSPACE_KEY)
+        this.command('plugin.enigma-key-delete');
+    else if (list.key_pressed == 33)
+        this.command('previouspage');
+    else if (list.key_pressed == 34)
+        this.command('nextpage');
 };
 
 // load key frame
-rcube_webmail.prototype.enigma_loadframe = function(id, url)
+rcube_webmail.prototype.enigma_loadframe = function(url)
 {
     var frm, win;
+
     if (this.env.contentframe && window.frames && (frm = window.frames[this.env.contentframe])) {
-        if (!id && !url && (win = window.frames[this.env.contentframe])) {
-            if (win.location && win.location.href.indexOf(this.env.blankpage)<0)
+        if (!url && (win = window.frames[this.env.contentframe])) {
+            if (win.location && win.location.href.indexOf(this.env.blankpage) < 0)
                 win.location.href = this.env.blankpage;
             return;
         }
+
         this.set_busy(true);
-        if (!url)
-            url = '&_a=keyinfo&_id='+id;
-        frm.location.href = this.env.comm_path+'&_action=plugin.enigma&_framed=1' + url;
+        frm.location.href = this.env.comm_path + '&_framed=1' + url;
     }
 };
 
@@ -96,14 +140,14 @@
         props = this.gui_objects.qsearchbox.value;
 
     if (props || this.env.search_request) {
-        var params = {'_a': 'keysearch', '_q': urlencode(props)},
+        var params = {'_a': 'search', '_q': urlencode(props)},
           lock = this.set_busy(true, 'searching');
 //        if (this.gui_objects.search_filter)
   //          addurl += '&_filter=' + this.gui_objects.search_filter.value;
-        this.env.current_page = 1;  
+        this.env.current_page = 1;
         this.enigma_loadframe();
         this.enigma_clear_list();
-        this.http_post('plugin.enigma', params, lock);
+        this.http_post('plugin.enigmakeys', params, lock);
     }
 
     return false;
@@ -129,7 +173,7 @@
 // Keys/certs listing
 rcube_webmail.prototype.enigma_list = function(page)
 {
-    var params = {'_a': 'keylist'},
+    var params = {'_a': 'list'},
       lock = this.set_busy(true, 'loading');
 
     this.env.current_page = page ? page : 1;
@@ -140,7 +184,7 @@
         params._p = page;
 
     this.enigma_clear_list();
-    this.http_post('plugin.enigma', params, lock);
+    this.http_post('plugin.enigmakeys', params, lock);
 }
 
 // Change list page
@@ -197,10 +241,82 @@
 // Import attached keys/certs file
 rcube_webmail.prototype.enigma_import_attachment = function(mime_id)
 {
-    var lock = this.set_busy(true, 'loading');
-    this.http_post('plugin.enigmaimport', '_uid='+this.env.uid+'&_mbox='
-        +urlencode(this.env.mailbox)+'&_part='+urlencode(mime_id), lock);
+    var lock = this.set_busy(true, 'loading'),
+        post = {_uid: this.env.uid, _mbox: this.env.mailbox, _part: mime_id};
+
+    this.http_post('plugin.enigmaimport', post, lock);
 
     return false;
-};
+}
 
+rcube_webmail.prototype.enigma_password_request = function(data)
+{
+    if (!data || !data.keyid) {
+        return;
+    }
+
+    var ref = this,
+        msg = this.get_label('enigma.enterkeypass'),
+        myprompt = $('<div class="prompt">'),
+        myprompt_content = $('<div class="message">')
+            .appendTo(myprompt),
+        myprompt_input = $('<input>').attr({type: 'password', size: 30})
+            .keypress(function(e) {
+                if (e.which == 13)
+                    (ref.is_framed() ? window.parent.$ : $)('.ui-dialog-buttonpane button.mainaction:visible').click();
+            })
+            .appendTo(myprompt);
+
+    data.key = data.keyid;
+    data.keyid = data.keyid.substr(0, 8);
+
+    $.each(['keyid', 'user'], function() {
+        msg = msg.replace('$' + this, data[this]);
+    });
+
+    myprompt_content.text(msg);
+
+    this.show_popup_dialog(myprompt, this.get_label('enigma.enterkeypasstitle'),
+        [{
+            text: this.get_label('save'),
+            'class': 'mainaction',
+            click: function(e) {
+                e.stopPropagation();
+
+                var jq = ref.is_framed() ? window.parent.$ : $,
+                    pass = myprompt_input.val();
+
+                if (!pass) {
+                    myprompt_input.focus();
+                    return;
+                }
+
+                ref.enigma_password_submit(data.key, pass);
+                jq(this).remove();
+            }
+        },
+        {
+            text: this.get_label('cancel'),
+            click: function(e) {
+                var jq = ref.is_framed() ? window.parent.$ : $;
+                e.stopPropagation();
+                jq(this).remove();
+            }
+        }], {width: 400});
+
+    if (this.is_framed() && parent.rcmail.message_list) {
+        // this fixes bug when pressing Enter on "Save" button in the dialog
+        parent.rcmail.message_list.blur();
+    }
+}
+
+rcube_webmail.prototype.enigma_password_submit = function(keyid, password)
+{
+    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: '_token', value: this.env.request_token}))
+        .appendTo(document.body);
+
+    form.submit();
+}

--
Gitblit v1.9.1