From 1da9b52706deefc7b844404b6a85301f0ceafe1f Mon Sep 17 00:00:00 2001 From: Andrew Engelbrecht Date: Sat, 18 Jul 2015 17:51:20 -0400 Subject: [PATCH] added simple message reply generation the reply is encrypted to the first encryption-capable key used to sign the message. --- edward-bot | 39 +++++++++++++++++++++++++++++++++------ 1 file changed, 33 insertions(+), 6 deletions(-) diff --git a/edward-bot b/edward-bot index ce55408..94c4972 100755 --- a/edward-bot +++ b/edward-bot @@ -42,16 +42,15 @@ def main (): email_text = sys.stdin.read() - plaintext, keys = email_decode_flatten (email_text) email_from, email_subject = email_from_subject(email_text) - reply_encrypt_to_key = choose_reply_encryption_key(keys) + plaintext, keys = email_decode_flatten (email_text) + encrypt_to_key = choose_reply_encryption_key(keys) - print("From: " + email_from) - print("Subject: " + email_subject) - print(plaintext) + reply_message = generate_reply(plaintext, email_from, \ + email_subject, encrypt_to_key) - print(reply_encrypt_to_key.subkeys[0].fpr) + print(reply_message) def email_decode_flatten (email_text): @@ -218,6 +217,34 @@ def choose_reply_encryption_key (keys): return 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) + + reply = "To: " + email_from + "\n" + reply += "Subject: " + email_subject + "\n" + reply += encrypted_reply + + return reply + + +def encrypt_message (plaintext, encrypt_to_key): + + gpgme_ctx = gpgme.Context() + gpgme_ctx.armor = True + + plaintext_bytes = io.BytesIO(plaintext.encode('UTF-8')) + encrypted_bytes = io.BytesIO() + + gpgme_ctx.encrypt([encrypt_to_key], gpgme.ENCRYPT_ALWAYS_TRUST, + plaintext_bytes, encrypted_bytes) + + encrypted_txt = encrypted_bytes.getvalue().decode('ASCII') + return encrypted_txt + + def handle_args (): if __name__ == "__main__": -- 2.25.1