fixed potential string decoding bug
[edward.git] / edward-bot
index 94c49722ce6a48545e54857d723c1e715307599f..e7163bb9b4529356039bfe32a74f58d799922e78 100755 (executable)
@@ -33,7 +33,6 @@ import email.parser
 import gpgme
 import re
 import io
-import time
 
 
 def main ():
@@ -44,11 +43,12 @@ def main ():
 
     email_from, email_subject = email_from_subject(email_text)
 
-    plaintext, keys = email_decode_flatten (email_text)
+    plaintext, keys = email_decode_flatten(email_text)
     encrypt_to_key = choose_reply_encryption_key(keys)
 
     reply_message = generate_reply(plaintext, email_from, \
-                                   email_subject, encrypt_to_key)
+                                   email_subject, encrypt_to_key,
+                                   "DAB4F989E2788B8DF058E0EFEF1EC52039B36E58")
 
     print(reply_message)
 
@@ -173,7 +173,7 @@ def split_message (text):
             '.*' + \
             '-----END PGP MESSAGE-----)', \
             text, \
-            re.DOTALL)
+            flags=re.DOTALL)
 
     if gpg_matches != None:
         gpg_chunks = gpg_matches.groups()
@@ -185,12 +185,7 @@ def split_message (text):
 
 def decrypt_chunks (gpg_chunks):
 
-    plaintext_and_sigs_chunks = []
-
-    for gpg_chunk in gpg_chunks:
-        plaintext_and_sigs_chunks += [decrypt_chunk(gpg_chunk)]
-
-    return plaintext_and_sigs_chunks
+    return map(decrypt_chunk, gpg_chunks)
 
 
 def decrypt_chunk (gpg_chunk):
@@ -202,7 +197,7 @@ def decrypt_chunk (gpg_chunk):
 
     sigs = gpgme_ctx.decrypt_verify(chunk_b, plain_b)
 
-    plaintext = plain_b.getvalue().decode('ASCII')
+    plaintext = plain_b.getvalue().decode('UTF-8')
     return (plaintext, sigs)
 
 
@@ -214,31 +209,51 @@ def choose_reply_encryption_key (keys):
             reply_key = key
             break
 
-    return key
-
+    return reply_key
 
-def generate_reply (plaintext, email_from, email_subject, encrypt_to_key):
 
-    plaintext_reply = "thanks for the message!\n\n\n" + plaintext
-
-    encrypted_reply = encrypt_message(plaintext_reply, encrypt_to_key)
+def generate_reply (plaintext, email_from, email_subject, encrypt_to_key,
+                    sign_with_fingerprint):
 
     reply  = "To: " + email_from + "\n"
     reply += "Subject: " + email_subject + "\n"
-    reply += encrypted_reply
+    reply += "\n"
+
+    if (encrypt_to_key != None):
+        plaintext_reply  = "thanks for the message!\n\n\n"
+        plaintext_reply += email_quote_text(plaintext)
+
+        encrypted_reply = encrypt_sign_message(plaintext_reply, encrypt_to_key,
+                                               sign_with_fingerprint)
+
+        reply += encrypted_reply
+
+    else:
+        reply += "Sorry, i couldn't find your key.\n"
+        reply += "I'll need that to encrypt a message to you."
 
     return reply
 
 
-def encrypt_message (plaintext, encrypt_to_key):
+def email_quote_text (text):
+
+    quoted_message = re.sub(r'^', r'> ', text, flags=re.MULTILINE)
+
+    return quoted_message
+
+
+def encrypt_sign_message (plaintext, encrypt_to_key, sign_with_fingerprint):
 
     gpgme_ctx = gpgme.Context()
     gpgme_ctx.armor = True
 
+    sign_with_key = gpgme_ctx.get_key(sign_with_fingerprint)
+    gpgme_ctx.signers = [sign_with_key]
+
     plaintext_bytes = io.BytesIO(plaintext.encode('UTF-8'))
     encrypted_bytes = io.BytesIO()
 
-    gpgme_ctx.encrypt([encrypt_to_key], gpgme.ENCRYPT_ALWAYS_TRUST,
+    gpgme_ctx.encrypt_sign([encrypt_to_key], gpgme.ENCRYPT_ALWAYS_TRUST,
             plaintext_bytes, encrypted_bytes)
 
     encrypted_txt = encrypted_bytes.getvalue().decode('ASCII')