From 4c7ce7bfc917ca76dede8ae29ad4ab96a67968fd Mon Sep 17 00:00:00 2001
From: Aleksander Machniak <alec@alec.pl>
Date: Sat, 02 Apr 2016 03:40:17 -0400
Subject: [PATCH] Merge branch 'guitarmanusa-attach-pgp-key'

---
 plugins/enigma/enigma.php                  |   20 ++++++++++
 plugins/enigma/lib/enigma_driver_gnupg.php |   11 +++++
 plugins/enigma/lib/enigma_ui.php           |   29 ++++++++++++++
 plugins/enigma/localization/en_US.inc      |    2 +
 plugins/enigma/lib/enigma_engine.php       |   32 ++++++++++++++++
 plugins/enigma/config.inc.php.dist         |    3 +
 6 files changed, 96 insertions(+), 1 deletions(-)

diff --git a/plugins/enigma/config.inc.php.dist b/plugins/enigma/config.inc.php.dist
index 17e3265..57dfcad 100644
--- a/plugins/enigma/config.inc.php.dist
+++ b/plugins/enigma/config.inc.php.dist
@@ -28,6 +28,9 @@
 // Enable encrypting all messages by default
 $config['enigma_encrypt_all'] = false;
 
+// Enable signing all messages by default
+$config['enigma_attach_pubkey'] = false;
+
 // Default for how long to store private key passwords (in minutes).
 // When set to 0 passwords will be stored for the whole session.
 $config['enigma_password_time'] = 5;
diff --git a/plugins/enigma/enigma.php b/plugins/enigma/enigma.php
index 8e8ded2..c9471ac 100644
--- a/plugins/enigma/enigma.php
+++ b/plugins/enigma/enigma.php
@@ -339,6 +339,25 @@
             );
         }
 
+        if (!isset($no_override['enigma_attach_pubkey'])) {
+            if (!$p['current']) {
+                $p['blocks']['main']['content'] = true;
+                return $p;
+            }
+
+            $field_id = 'rcmfd_enigma_attach_pubkey';
+            $input    = new html_checkbox(array(
+                    'name'  => '_enigma_attach_pubkey',
+                    'id'    => $field_id,
+                    'value' => 1,
+            ));
+
+            $p['blocks']['main']['options']['enigma_encrypt_all'] = array(
+                'title'   => html::label($field_id, $this->gettext('attachpubkeydefault')),
+                'content' => $input->show($this->rc->config->get('enigma_attach_pubkey') ? 1 : 0),
+            );
+        }
+
         if (!isset($no_override['enigma_password_time'])) {
             if (!$p['current']) {
                 $p['blocks']['main']['content'] = true;
@@ -380,6 +399,7 @@
                 'enigma_encryption'    => (bool) rcube_utils::get_input_value('_enigma_encryption', rcube_utils::INPUT_POST),
                 'enigma_sign_all'      => (bool) rcube_utils::get_input_value('_enigma_sign_all', rcube_utils::INPUT_POST),
                 'enigma_encrypt_all'   => (bool) rcube_utils::get_input_value('_enigma_encrypt_all', rcube_utils::INPUT_POST),
+                'enigma_attach_pubkey' => (bool) rcube_utils::get_input_value('_enigma_attach_pubkey', rcube_utils::INPUT_POST),
                 'enigma_password_time' => intval(rcube_utils::get_input_value('_enigma_password_time', rcube_utils::INPUT_POST)),
             );
         }
diff --git a/plugins/enigma/lib/enigma_driver_gnupg.php b/plugins/enigma/lib/enigma_driver_gnupg.php
index 5ddf724..ab4aa45 100644
--- a/plugins/enigma/lib/enigma_driver_gnupg.php
+++ b/plugins/enigma/lib/enigma_driver_gnupg.php
@@ -339,6 +339,17 @@
         }
     }
 
+    public function pubkey_for_attach($email)
+    {
+        try {
+            $pubkey = $this->gpg->exportPublicKey($email, true);
+            return $pubkey;
+        }
+        catch (Exception $e) {
+            return $this->get_error_from_exception($e);
+        }
+    }
+
     /**
      * Converts Crypt_GPG exception into Enigma's error object
      *
diff --git a/plugins/enigma/lib/enigma_engine.php b/plugins/enigma/lib/enigma_engine.php
index 96f792d..d2f3972 100644
--- a/plugins/enigma/lib/enigma_engine.php
+++ b/plugins/enigma/lib/enigma_engine.php
@@ -921,6 +921,38 @@
         return $result;
     }
 
+    function get_gpg_pubkey_for_attach($email)
+    {
+        $this->load_pgp_driver();
+        $result = $this->pgp_driver->pubkey_for_attach($email);
+
+        if ($result instanceof enigma_error) {
+            rcube::raise_error(array(
+                'code' => 600, 'type' => 'php',
+                'file' => __FILE__, 'line' => __LINE__,
+                'message' => "Enigma plugin: " . $result->getMessage()
+                ), true, false);
+        }
+
+        return $result;
+    }
+
+    function get_keyID($email)
+    {
+        $this->load_pgp_driver();
+        $result = $this->pgp_driver->get_keyID($email);
+
+        if ($result instanceof enigma_error) {
+            rcube::raise_error(array(
+                'code' => 600, 'type' => 'php',
+                'file' => __FILE__, 'line' => __LINE__,
+                'message' => "Enigma plugin: " . $result->getMessage()
+                ), true, false);
+        }
+
+        return $result;
+    }
+
     /**
      * Find PGP private/public key
      *
diff --git a/plugins/enigma/lib/enigma_ui.php b/plugins/enigma/lib/enigma_ui.php
index 6ae69da..3ccacd4 100644
--- a/plugins/enigma/lib/enigma_ui.php
+++ b/plugins/enigma/lib/enigma_ui.php
@@ -730,6 +730,11 @@
         $menu->add(null, $chbox->show($this->rc->config->get('enigma_encrypt_all') ? 1 : 0,
             array('name' => '_enigma_encrypt', 'id' => 'enigmaencryptopt')));
 
+        $menu->add(null, html::label(array('for' => 'enigmaattachpubkeyopt'),
+            rcube::Q($this->enigma->gettext('attachpubkeymsg'))));
+        $menu->add(null, $chbox->show($this->rc->config->get('enigma_attach_pubkey') ? 1 : 0, 
+            array('name' => '_enigma_attachpubkey', 'id' => 'enigmaattachpubkeyopt')));
+
         $menu = html::div(array('id' => 'enigmamenu', 'class' => 'popupmenu'), $menu->show());
 
         // Options menu contents
@@ -929,11 +934,15 @@
     }
 
     /**
-     * Handle message_ready hook (encryption/signing)
+     * Handle message_ready hook (encryption/signing/attach public key)
      */
     function message_ready($p)
     {
         $savedraft = !empty($_POST['_draft']) && empty($_GET['_saveonly']);
+
+        if (!$savedraft && rcube_utils::get_input_value('_enigma_attachpubkey', rcube_utils::INPUT_POST)) {
+            $p = $this->attach_public($p);
+        }
 
         if (!$savedraft && rcube_utils::get_input_value('_enigma_sign', rcube_utils::INPUT_POST)) {
             $this->enigma->load_engine();
@@ -972,6 +981,24 @@
     }
 
     /**
+     * Add sender's public key (PGP).
+     */
+    function attach_public($p)
+    {
+        // get sender's PGP pubkey for attachment
+        $this->enigma->load_engine();
+        $key = $this->enigma->engine->list_keys($p['message']->headers()['From']);
+        $keyID = $key[0]->subkeys[0]->get_short_id();
+        $pubkey_armor = $this->enigma->engine->get_gpg_pubkey_for_attach($p['message']->headers()['From']);
+
+        if(!$pubkey_armor instanceof enigma_error) {
+            $p['message']->addAttachment($pubkey_armor, 'application/pgp-keys', "0x$keyID.asc", false);
+        }
+
+        return $p;
+    }
+
+   /**
      * Handler for message_compose_body hook
      * Display error when the message cannot be encrypted
      * and provide a way to try again with a password.
diff --git a/plugins/enigma/localization/en_US.inc b/plugins/enigma/localization/en_US.inc
index 0e4d2b4..5224ca5 100644
--- a/plugins/enigma/localization/en_US.inc
+++ b/plugins/enigma/localization/en_US.inc
@@ -53,6 +53,7 @@
 $labels['supportdecryption'] = 'Enable message decryption';
 $labels['signdefault'] = 'Sign all messages by default';
 $labels['encryptdefault'] = 'Encrypt all messages by default';
+$labels['attachpubkeydefault'] = 'Attach my public PGP key by default';
 $labels['passwordtime'] = 'Keep private key passwords for';
 $labels['nminutes'] = '$m minute(s)';
 $labels['wholesession'] = 'the whole session';
@@ -82,6 +83,7 @@
 $labels['enterkeypasstitle'] = 'Enter key passphrase';
 $labels['enterkeypass'] = 'A passphrase is needed to unlock the secret key ($keyid) for user: $user.';
 $labels['arialabelkeyexportoptions'] = 'Keys export options';
+$labels['attachpubkeymsg'] = 'Attach my public key';
 
 $messages = array();
 $messages['sigvalid'] = 'Verified signature from $sender.';

--
Gitblit v1.9.1