use a settings file to manage configurations
authorAndrew Engelbrecht <sudoman@ninthfloor.org>
Sun, 19 Jul 2015 20:13:19 +0000 (16:13 -0400)
committerAndrew Engelbrecht <sudoman@ninthfloor.org>
Mon, 7 Dec 2015 18:05:42 +0000 (13:05 -0500)
edward
edward_config.py [new file with mode: 0644]

diff --git a/edward b/edward
index 3b828276245bd7339612ac6f6ce28004cbe666d8..f235265b83319f9b44f8d929565a42c04cd0ac2d 100755 (executable)
--- a/edward
+++ b/edward
@@ -36,6 +36,7 @@ import sys
 import gpgme
 import re
 import io
+import os
 
 import email.parser
 import email.message
@@ -45,26 +46,31 @@ from email.mime.multipart       import MIMEMultipart
 from email.mime.application     import MIMEApplication
 from email.mime.nonmultipart    import MIMENonMultipart
 
+import edward_config
 
 def main ():
 
     handle_args()
 
     email_text = sys.stdin.read()
-
     email_from, email_subject = email_from_subject(email_text)
 
-    plaintext, keys = email_decode_flatten(email_text)
+    os.environ['GNUPGHOME'] = edward_config.gnupghome
+    gpgme_ctx = gpgme.Context()
+    gpgme_ctx.armor = True
+
+    plaintext, keys = email_decode_flatten(email_text, gpgme_ctx)
     encrypt_to_key = choose_reply_encryption_key(keys)
 
     reply_message = generate_reply(plaintext, email_from, \
                                    email_subject, encrypt_to_key,
-                                   "DAB4F989E2788B8DF058E0EFEF1EC52039B36E58")
+                                   edward_config.sign_with_key,
+                                   gpgme_ctx)
 
     print(reply_message)
 
 
-def email_decode_flatten (email_text):
+def email_decode_flatten (email_text, gpgme_ctx):
 
     body = ""
     keys = []
@@ -92,7 +98,7 @@ def email_decode_flatten (email_text):
 
 
         if (filename == "encrypted.asc") or (content_type == "pgp/mime"):
-            plaintext, more_keys = decrypt_text(payload)
+            plaintext, more_keys = decrypt_text(payload, gpgme_ctx)
 
             body += plaintext
             keys += more_keys
@@ -141,25 +147,25 @@ def get_email_subpart_info (part):
     return payload, description, filename, content_type
 
 
-def decrypt_text (gpg_text):
+def decrypt_text (gpg_text, gpgme_ctx):
 
     body = ""
     keys = []
 
     gpg_chunks = split_message(gpg_text)
 
-    plaintext_and_sigs_chunks = decrypt_chunks(gpg_chunks)
+    plaintext_and_sigs_chunks = decrypt_chunks(gpg_chunks, gpgme_ctx)
 
     for chunk in plaintext_and_sigs_chunks:
         plaintext   = chunk[0]
         sigs        = chunk[1]
 
         for sig in sigs:
-            key = get_pub_key(sig)
+            key = get_pub_key(sig, gpgme_ctx)
             keys += [key]
 
         # recursive for nested layers of mime and/or gpg
-        plaintext, more_keys = email_decode_flatten(plaintext)
+        plaintext, more_keys = email_decode_flatten(plaintext, gpgme_ctx)
 
         body += plaintext
         keys += more_keys
@@ -167,9 +173,7 @@ def decrypt_text (gpg_text):
     return body, keys
 
 
-def get_pub_key (sig):
-
-    gpgme_ctx = gpgme.Context()
+def get_pub_key (sig, gpgme_ctx):
 
     fingerprint = sig.fpr
     key = gpgme_ctx.get_key(fingerprint)
@@ -194,14 +198,12 @@ def split_message (text):
     return gpg_chunks
 
 
-def decrypt_chunks (gpg_chunks):
+def decrypt_chunks (gpg_chunks, gpgme_ctx):
 
-    return map(decrypt_chunk, gpg_chunks)
+    return [decrypt_chunk(chunk, gpgme_ctx) for chunk in gpg_chunks]
 
 
-def decrypt_chunk (gpg_chunk):
-
-    gpgme_ctx = gpgme.Context()
+def decrypt_chunk (gpg_chunk, gpgme_ctx):
 
     chunk_b = io.BytesIO(gpg_chunk.encode('ascii'))
     plain_b = io.BytesIO()
@@ -224,7 +226,7 @@ def choose_reply_encryption_key (keys):
 
 
 def generate_reply (plaintext, email_from, email_subject, encrypt_to_key,
-                    sign_with_fingerprint):
+                    sign_with_fingerprint, gpgme_ctx):
 
 
     reply  = "To: " + email_from + "\n"
@@ -246,7 +248,8 @@ def generate_reply (plaintext, email_from, email_subject, encrypt_to_key,
 
         encrypted_text = encrypt_sign_message(plaintext_mime.as_string(),
                                               encrypt_to_key,
-                                              sign_with_fingerprint)
+                                              sign_with_fingerprint,
+                                              gpgme_ctx)
 
         control_mime = MIMEApplication("Version: 1",
                                        _subtype='pgp-encrypted',
@@ -283,10 +286,7 @@ def email_quote_text (text):
     return quoted_message
 
 
-def encrypt_sign_message (plaintext, encrypt_to_key, sign_with_fingerprint):
-
-    gpgme_ctx = gpgme.Context()
-    gpgme_ctx.armor = True
+def encrypt_sign_message (plaintext, encrypt_to_key, sign_with_fingerprint, gpgme_ctx):
 
     sign_with_key = gpgme_ctx.get_key(sign_with_fingerprint)
     gpgme_ctx.signers = [sign_with_key]
diff --git a/edward_config.py b/edward_config.py
new file mode 100644 (file)
index 0000000..37ca1e3
--- /dev/null
@@ -0,0 +1,30 @@
+#! /usr/bin/env python3
+# -*- coding: utf-8 -*-
+"""*********************************************************************
+* Edward is free software: you can redistribute it and/or modify       *
+* it under the terms of the GNU Affero Public License as published by  *
+* the Free Software Foundation, either version 3 of the License, or    *
+* (at your option) any later version.                                  *
+*                                                                      *
+* Edward is distributed in the hope that it will be useful,            *
+* but WITHOUT ANY WARRANTY; without even the implied warranty of       *
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
+* GNU Affero Public License for more details.                          *
+*                                                                      *
+* You should have received a copy of the GNU Affero Public License     *
+* along with Edward.  If not, see <http://www.gnu.org/licenses/>.      *
+*                                                                      *
+* Copyright (C) 2015 Andrew Engelbrecht                     (AGPLv3+)  *
+* Copyright (C) 2014 Josh Drake                             (AGPLv3+)  *
+* Copyright (C) 2014 Lisa Marie Maginnis                    (AGPLv3+)  *
+************************************************************************
+
+Code sourced from these projects:
+
+  * http://agpl.fsf.org/emailselfdefense.fsf.org/edward/CURRENT/edward.tar.gz
+
+"""
+
+gnupghome = "/home/e/edward/.gnupg/"
+sign_with_key = "F357AA1A5B1FA42CFD9FE52A9FF2194CC09A61E8"
+