From 40c37ab30be7fab2c12efeadceabc709ce3dcb50 Mon Sep 17 00:00:00 2001 From: Andrew Engelbrecht <sudoman@ninthfloor.org> Date: Sun, 19 Jul 2015 16:13:19 -0400 Subject: [PATCH] use a settings file to manage configurations --- edward | 46 +++++++++++++++++++++++----------------------- edward_config.py | 30 ++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 23 deletions(-) create mode 100644 edward_config.py diff --git a/edward b/edward index 3b82827..f235265 100755 --- 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 index 0000000..37ca1e3 --- /dev/null +++ b/edward_config.py @@ -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" + -- 2.25.1