From 96c3d84cddb861956cfbc719d694eb972343f1c3 Mon Sep 17 00:00:00 2001
From: Aleksander Machniak <alec@alec.pl>
Date: Sat, 02 Apr 2016 04:35:17 -0400
Subject: [PATCH] Enigma: Varius fixes and cleanup in code for attaching pubkeys

---
 plugins/enigma/enigma.php                  |    2 
 plugins/enigma/lib/enigma_driver_gnupg.php |   11 -----
 plugins/enigma/lib/enigma_ui.php           |   21 +---------
 plugins/enigma/README                      |    2 
 plugins/enigma/composer.json               |    6 +-
 plugins/enigma/lib/enigma_engine.php       |   59 +++++++++++++----------------
 plugins/enigma/config.inc.php.dist         |    2 
 7 files changed, 35 insertions(+), 68 deletions(-)

diff --git a/plugins/enigma/README b/plugins/enigma/README
index ac20b79..d9fc379 100644
--- a/plugins/enigma/README
+++ b/plugins/enigma/README
@@ -18,6 +18,7 @@
 + PGP: key generation (client- or server-side)
 + Handling of PGP keys attached to incoming messages
 + User preferences to disable plugin features
++ Attaching public keys to email
 
 
 TODO:
@@ -33,7 +34,6 @@
 - Generate revocation certs
 - Search filter to see invalid/expired keys
 - Key server(s) support (search, import, upload, refresh)
-- Attaching public keys to email
 - Mark keys as trusted/untrasted, display appropriate message in verify/decrypt status
 - Change attachment icon on messages list for encrypted messages (like vcard_attachment plugin does)
 - Support for multi-server installations (store keys in sql database?)
diff --git a/plugins/enigma/composer.json b/plugins/enigma/composer.json
index 3245ee7..4cfc44d 100644
--- a/plugins/enigma/composer.json
+++ b/plugins/enigma/composer.json
@@ -3,7 +3,7 @@
     "type": "roundcube-plugin",
     "description": "PGP Encryption for Roundcube",
     "license": "GPLv3+",
-    "version": "0.4",
+    "version": "0.5",
     "authors": [
         {
             "name": "Aleksander Machniak",
@@ -23,7 +23,7 @@
     ],
     "require": {
         "php": ">=5.3.0",
-        "roundcube/plugin-installer": ">=0.1.3",
-        "pear-pear.php.net/crypt_gpg": "*"
+        "roundcube/plugin-installer": "~0.1.6",
+        "pear-pear.php.net/crypt_gpg": "~1.4.0"
     }
 }
diff --git a/plugins/enigma/config.inc.php.dist b/plugins/enigma/config.inc.php.dist
index 57dfcad..2cce5ee 100644
--- a/plugins/enigma/config.inc.php.dist
+++ b/plugins/enigma/config.inc.php.dist
@@ -28,7 +28,7 @@
 // Enable encrypting all messages by default
 $config['enigma_encrypt_all'] = false;
 
-// Enable signing all messages by default
+// Enable attaching a public key to all messages by default
 $config['enigma_attach_pubkey'] = false;
 
 // Default for how long to store private key passwords (in minutes).
diff --git a/plugins/enigma/enigma.php b/plugins/enigma/enigma.php
index c9471ac..2ebe2cc 100644
--- a/plugins/enigma/enigma.php
+++ b/plugins/enigma/enigma.php
@@ -352,7 +352,7 @@
                     'value' => 1,
             ));
 
-            $p['blocks']['main']['options']['enigma_encrypt_all'] = array(
+            $p['blocks']['main']['options']['enigma_attach_pubkey'] = array(
                 'title'   => html::label($field_id, $this->gettext('attachpubkeydefault')),
                 'content' => $input->show($this->rc->config->get('enigma_attach_pubkey') ? 1 : 0),
             );
diff --git a/plugins/enigma/lib/enigma_driver_gnupg.php b/plugins/enigma/lib/enigma_driver_gnupg.php
index ab4aa45..5ddf724 100644
--- a/plugins/enigma/lib/enigma_driver_gnupg.php
+++ b/plugins/enigma/lib/enigma_driver_gnupg.php
@@ -339,17 +339,6 @@
         }
     }
 
-    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 d2f3972..c970933 100644
--- a/plugins/enigma/lib/enigma_engine.php
+++ b/plugins/enigma/lib/enigma_engine.php
@@ -313,6 +313,33 @@
     }
 
     /**
+     * Handler for attaching public key to a message
+     *
+     * @param Mail_mime Original message
+     *
+     * @return bool True on success, False on failure
+     */
+    function attach_public_key(&$message)
+    {
+        $headers = $message->headers();
+        $from    = rcube_mime::decode_address_list($headers['From'], 1, false, null, true);
+        $from    = $from[1];
+
+        // find my key
+        if ($from && ($key = $this->find_key($from))) {
+            $pubkey_armor = $this->export_key($key->id);
+
+            if (!$pubkey_armor instanceof enigma_error) {
+                $pubkey_name = '0x' . enigma_key::format_id($key->id) . '.asc';
+                $message->addAttachment($pubkey_armor, 'application/pgp-keys', $pubkey_name, false, '7bit');
+                return true;
+            }
+        }
+
+        return false;
+    }
+
+    /**
      * Handler for message_part_structure hook.
      * Called for every part of the message.
      *
@@ -909,38 +936,6 @@
     {
         $this->load_pgp_driver();
         $result = $this->pgp_driver->list_keys($pattern);
-
-        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_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(
diff --git a/plugins/enigma/lib/enigma_ui.php b/plugins/enigma/lib/enigma_ui.php
index 3ccacd4..37c1d41 100644
--- a/plugins/enigma/lib/enigma_ui.php
+++ b/plugins/enigma/lib/enigma_ui.php
@@ -941,7 +941,8 @@
         $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);
+            $this->enigma->load_engine();
+            $this->enigma->engine->attach_public_key($p['message']);
         }
 
         if (!$savedraft && rcube_utils::get_input_value('_enigma_sign', rcube_utils::INPUT_POST)) {
@@ -975,24 +976,6 @@
             }
 
             $this->rc->output->send('iframe');
-        }
-
-        return $p;
-    }
-
-    /**
-     * 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;

--
Gitblit v1.9.1