From a9d399155d205ae41015d7d205c6dacd7ecfc0d2 Mon Sep 17 00:00:00 2001
From: Kyle Francis <franck6@rpi.edu>
Date: Wed, 30 Mar 2016 09:33:53 -0400
Subject: [PATCH] Implemented attaching pub PGP key to outgoing messages.

---
 plugins/enigma/lib/enigma_driver_gnupg.php |   11 +++++
 plugins/enigma/lib/enigma_ui.php           |   24 +++++++++++
 plugins/enigma/lib/enigma_engine.php       |   32 ++++++++++++++++
 3 files changed, 66 insertions(+), 1 deletions(-)

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..f0963dc 100644
--- a/plugins/enigma/lib/enigma_ui.php
+++ b/plugins/enigma/lib/enigma_ui.php
@@ -929,11 +929,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 +976,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.

--
Gitblit v1.9.1