import gpgme
import re
import io
+import os
import email.parser
import email.message
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 = []
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
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
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)
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()
def generate_reply (plaintext, email_from, email_subject, encrypt_to_key,
- sign_with_fingerprint):
+ sign_with_fingerprint, gpgme_ctx):
reply = "To: " + email_from + "\n"
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',
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]
--- /dev/null
+#! /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"
+