From 82dcbb7488ce1625ba4f41fbdc8e6319d3da9691 Mon Sep 17 00:00:00 2001
From: Aleksander Machniak <alec@alec.pl>
Date: Sun, 20 Sep 2015 11:45:53 -0400
Subject: [PATCH] Fix various issues in Mailvelope integration (#1490533)

---
 program/steps/mail/compose.inc          |    5 +-
 program/localization/en_US/messages.inc |    2 +
 program/steps/mail/func.inc             |   11 +++--
 skins/larry/styles.css                  |    4 ++
 program/js/app.js                       |   56 ++++++++++++++++++++--------
 skins/larry/mail.css                    |    4 --
 skins/larry/templates/compose.html      |    2 
 skins/larry/ui.js                       |    1 
 8 files changed, 57 insertions(+), 28 deletions(-)

diff --git a/program/js/app.js b/program/js/app.js
index 19c5f9b..8a9712f 100644
--- a/program/js/app.js
+++ b/program/js/app.js
@@ -325,7 +325,9 @@
         else if (this.env.action == 'get')
           this.enable_command('download', 'print', true);
         // show printing dialog
-        else if (this.env.action == 'print' && this.env.uid) {
+        else if (this.env.action == 'print' && this.env.uid
+          && !this.env.is_pgp_content && !this.env.pgp_mime_part
+        ) {
           this.print_dialog();
         }
 
@@ -3361,13 +3363,13 @@
     }
   };
 
-  // 
+  // Load Mailvelope functionality (and initialize keyring if needed)
   this.mailvelope_load = function(action)
   {
     if (this.env.browser_capabilities)
       this.env.browser_capabilities['pgpmime'] = 1;
 
-    var keyring = this.get_local_storage_prefix();
+    var keyring = this.env.user_id;
 
     mailvelope.getKeyring(keyring).then(function(kr) {
       ref.mailvelope_keyring = kr;
@@ -3383,17 +3385,20 @@
     });
   };
 
-  // 
+  // Initializes Mailvelope editor or display container
   this.mailvelope_init = function(action, keyring)
   {
-    if (action == 'show' || action == 'preview') {
+    if (!window.mailvelope)
+      return;
+
+    if (action == 'show' || action == 'preview' || action == 'print') {
       // decrypt text body
-      if (this.env.is_pgp_content && window.mailvelope) {
+      if (this.env.is_pgp_content) {
         var data = $(this.env.is_pgp_content).text();
         ref.mailvelope_display_container(this.env.is_pgp_content, data, keyring);
       }
       // load pgp/mime message and pass it to the mailvelope display container
-      else if (this.env.pgp_mime_part && window.mailvelope) {
+      else if (this.env.pgp_mime_part) {
         var msgid = this.display_message(this.get_label('loadingdata'), 'loading'),
           selector = this.env.pgp_mime_container;
 
@@ -3409,21 +3414,32 @@
         });
       }
     }
-    else if (action == 'compose' && window.mailvelope) {
+    else if (action == 'compose') {
       this.env.compose_commands.push('compose-encrypted');
+      // display the toolbar button
+      $('#' + this.buttons['compose-encrypted'][0].id).show();
+
+      var is_html = $('input[name="_is_html"]').val() > 0;
 
       if (this.env.pgp_mime_message) {
         // fetch PGP/Mime part and open load into Mailvelope editor
         var lock = this.set_busy(true, this.get_label('loadingdata'));
+
         $.ajax({
           type: 'GET',
           url: this.url('get', this.env.pgp_mime_message),
           error: function(o, status, err) {
             ref.http_error(o, status, err, lock);
-            ref.enable_command('compose-encrypted', true);
+            ref.enable_command('compose-encrypted', !is_html);
           },
           success: function(data) {
             ref.set_busy(false, null, lock);
+
+            if (is_html) {
+              ref.command('toggle-editor', {html: false, noconvert: true});
+              $('#' + ref.env.composebody).val('');
+            }
+
             ref.compose_encrypted({ quotedMail: data });
             ref.enable_command('compose-encrypted', true);
           }
@@ -3431,7 +3447,7 @@
       }
       else {
         // enable encrypted compose toggle
-        this.enable_command('compose-encrypted', true);
+        this.enable_command('compose-encrypted', !is_html);
       }
     }
   };
@@ -3439,7 +3455,7 @@
   // handler for the 'compose-encrypted' command
   this.compose_encrypted = function(props)
   {
-    var container = $('#' + this.env.composebody).parent();
+    var options, container = $('#' + this.env.composebody).parent();
 
     // remove Mailvelope editor if active
     if (ref.mailvelope_editor) {
@@ -3458,10 +3474,16 @@
     }
     // embed Mailvelope editor container
     else {
-      var options = { predefinedText: $('#' + this.env.composebody).val() };
+      if (this.spellcheck_state())
+        this.editor.spellcheck_stop();
+
       if (props.quotedMail) {
         options = { quotedMail: props.quotedMail, quotedMailIndent: false };
       }
+      else {
+        options = { predefinedText: $('#' + this.env.composebody).val() };
+      }
+
       if (this.env.compose_mode == 'reply') {
         options.quotedMailIndent = true;
         options.quotedMailHeader = this.env.compose_reply_header;
@@ -3629,7 +3651,7 @@
   this.mailvelope_display_container = function(selector, data, keyring, msgid)
   {
     mailvelope.createDisplayContainer(selector, data, keyring, { showExternalContent: this.env.safemode }).then(function() {
-      $(selector).addClass('mailvelope').find('.message-part, .part-notice').hide();
+      $(selector).addClass('mailvelope').children().not('iframe').hide();
       ref.hide_message(msgid);
       setTimeout(function() { $(window).resize(); }, 10);
     }).catch(function(err) {
@@ -3772,7 +3794,7 @@
           ref.hide_message(lock);
 
           if (errorCode) {
-            ref.display_message('Failed to get key from keyserver', 'error');
+            ref.display_message(ref.get_label('keyservererror'), 'error');
             return;
           }
 
@@ -3782,9 +3804,9 @@
               // alert(ref.get_label('Key import was rejected'));
             }
             else {
+              var $key = keyid.substr(-8).toUpperCase();
               btn.closest('.key').fadeOut();
-              ref.display_message(ref.get_label('Public key $key successfully imported into your key ring')
-                .replace('$key', keyid.substr(-8).toUpperCase()), 'confirmation');
+              ref.display_message(ref.get_label('keyimportsuccess').replace('$key', $key), 'confirmation');
             }
           }).catch(function(err) {
             console.log(err);
@@ -4244,6 +4266,8 @@
     if (result) {
       // update internal format flag
       $("input[name='_is_html']").val(props.html ? 1 : 0);
+      // enable encrypted compose toggle
+      this.enable_command('compose-encrypted', !props.html);
     }
 
     return result;
diff --git a/program/localization/en_US/messages.inc b/program/localization/en_US/messages.inc
index d7bef0e..cc8d5ea 100644
--- a/program/localization/en_US/messages.inc
+++ b/program/localization/en_US/messages.inc
@@ -62,6 +62,8 @@
 $messages['encryptnoattachments'] = 'Already uploaded attachments cannot be encrypted. Please re-add them in the encryption editor.';
 $messages['searchpubkeyservers'] = 'Do you want to search public key servers for the missing keys?';
 $messages['encryptpubkeysfound'] = 'The following public keys have been found:';
+$messages['keyservererror'] = 'Failed to get key from keyserver';
+$messages['keyimportsuccess'] = 'Public key $key successfully imported into your key ring';
 $messages['nocontactsfound'] = 'No contacts found.';
 $messages['contactnotfound'] = 'The requested contact was not found.';
 $messages['contactsearchonly'] = 'Enter some search terms to find contacts';
diff --git a/program/steps/mail/compose.inc b/program/steps/mail/compose.inc
index fd87266..0b047d4 100644
--- a/program/steps/mail/compose.inc
+++ b/program/steps/mail/compose.inc
@@ -88,7 +88,7 @@
     'selectimportfile', 'messageissent', 'loadingdata', 'nopubkeyfor', 'nopubkeyforsender',
     'encryptnoattachments','encryptedsendialog','searchpubkeyservers', 'importpubkeys',
     'encryptpubkeysfound',  'search', 'close', 'import', 'keyid', 'keylength', 'keyexpired',
-    'keyrevoked');
+    'keyrevoked', 'keyimportsuccess', 'keyservererror');
 
 $OUTPUT->set_pagetitle($RCMAIL->gettext('compose'));
 
@@ -788,7 +788,6 @@
                             '_uid'  => $MESSAGE->uid,
                             '_part' => $pgp_mime_part->mime_id,
                         ));
-                        $RCMAIL->output->set_env('compose_mode', $compose_mode);
                     }
                     continue;
                 }
@@ -876,7 +875,7 @@
     }
 
     // register this part as pgp encrypted
-    if (strpos($body, 'BEGIN PGP MESSAGE') !== false) {
+    if (strpos($body, '-----BEGIN PGP MESSAGE-----') !== false) {
         $MESSAGE->pgp_mime = true;
         $RCMAIL->output->set_env('pgp_mime_message', array(
             '_mbox' => $RCMAIL->storage->get_folder(), '_uid' => $MESSAGE->uid, '_part' => $part->mime_id,
diff --git a/program/steps/mail/func.inc b/program/steps/mail/func.inc
index 920eb80..f456945 100644
--- a/program/steps/mail/func.inc
+++ b/program/steps/mail/func.inc
@@ -1237,8 +1237,8 @@
                 }
 
                 // check if the message body is PGP encrypted
-                if (strpos($body, 'BEGIN PGP MESSAGE') !== false) {
-                    $OUTPUT->set_env('is_pgp_content', '#' . $attrib['id']);
+                if (strpos($body, '-----BEGIN PGP MESSAGE-----') !== false) {
+                    $OUTPUT->set_env('is_pgp_content', '#message-part' . ($part_no + 1));
                 }
 
                 $plugin = $RCMAIL->plugins->exec_hook('message_body_prefix',
@@ -1261,8 +1261,11 @@
 
                     $out .= html::div($div_attr, $plugin['prefix'] . $body);
                 }
-                else
-                    $out .= html::div('message-part', $plugin['prefix'] . $body);
+                else {
+                    $container_id = 'message-part' . (++$part_no);
+                    $div_attr     = array('class' => 'message-part', 'id' => $container_id);
+                    $out .= html::div($div_attr, $plugin['prefix'] . $body);
+                }
             }
         }
     }
diff --git a/skins/larry/mail.css b/skins/larry/mail.css
index cac86e5..80e2109 100644
--- a/skins/larry/mail.css
+++ b/skins/larry/mail.css
@@ -879,10 +879,6 @@
 	margin: 8px;
 }
 
-#messagebody.mailvelope > iframe {
-	width: 99% !important;
-}
-
 #message-objects div,
 #messagebody span.part-notice {
 	margin: 8px;
diff --git a/skins/larry/styles.css b/skins/larry/styles.css
index 0896a9e..88459af 100644
--- a/skins/larry/styles.css
+++ b/skins/larry/styles.css
@@ -2021,6 +2021,10 @@
 	color: #1978a1;
 }
 
+.toolbar a.button.selected:focus {
+	color: #fff;
+}
+
 .toolbar a.button.hidden {
 	display: none;
 }
diff --git a/skins/larry/templates/compose.html b/skins/larry/templates/compose.html
index 77b9cfd..262a89d 100644
--- a/skins/larry/templates/compose.html
+++ b/skins/larry/templates/compose.html
@@ -33,7 +33,7 @@
 	<roundcube:button name="addattachment" type="link" class="button attach" label="attach" title="addattachment" onclick="UI.show_uploadform(event);return false" aria-haspopup="true" aria-expanded="false"tabindex="2" />
 	<roundcube:button command="insert-sig" type="link" class="button insertsig disabled" classAct="button insertsig" label="signature" title="insertsignature" tabindex="2" />
 	<a href="#responses" class="button responses" label="responses" title="<roundcube:label name='insertresponse' />" id="responsesmenulink" unselectable="on" onmousedown="return false" onclick="UI.toggle_popup('responsesmenu',event);return false" tabindex="2" aria-haspopup="true" aria-expanded="false" aria-owns="textresponsesmenu"><roundcube:label name="responses" /></a>
-	<roundcube:button command="compose-encrypted" type="link" class="button encrypt hidden" classAct="button encrypt" classSel="button encrypt selected" label="encrypt" title="encryptmessagemailvelope" tabindex="2" />
+	<roundcube:button command="compose-encrypted" type="link" class="button encrypt disabled" classAct="button encrypt" classSel="button encrypt selected" label="encrypt" title="encryptmessagemailvelope" tabindex="2" style="display:none" />
 	<roundcube:container name="toolbar" id="compose-toolbar" />
 </div>
 
diff --git a/skins/larry/ui.js b/skins/larry/ui.js
index 926f039..b3c6abd 100644
--- a/skins/larry/ui.js
+++ b/skins/larry/ui.js
@@ -182,6 +182,7 @@
           .addEventListener('compose-encrypted', function(e) {
             $("select[name='editorSelector']").prop('disabled', e.active);
             $('a.button.attach, a.button.responses')[(e.active?'addClass':'removeClass')]('disabled');
+            $('#responseslist a.insertresponse')[(e.active?'removeClass':'addClass')]('active');
           });
 
         // Show input elements with non-empty value

--
Gitblit v1.9.1