From 8666338857203b7a18d4d70a2197dab6b4c5b25c Mon Sep 17 00:00:00 2001 From: Andrew Engelbrecht Date: Sun, 27 Jul 2014 02:47:22 -0400 Subject: [PATCH] accumulate message before printing ...so i can transform and do other things with the text. this will allow me to compose another message quoting this one without a unix pipe. --- edward-bot | 57 +++++++++++++++++++++++++++++------------------------- 1 file changed, 31 insertions(+), 26 deletions(-) diff --git a/edward-bot b/edward-bot index 39d7f8f..7ad7d9e 100755 --- a/edward-bot +++ b/edward-bot @@ -42,15 +42,18 @@ def main (): txt = sys.stdin.read() msg = email.parser.Parser().parsestr(txt) - print("From: " + msg['From']) - print("Subject: " + msg['Subject']) - print() + message = "" + message += "From: " + msg['From'] + "\n" + message += "Subject: " + msg['Subject'] + "\n\n" - msg_walk(msg) + message += msg_walk(msg) + + print(message) def msg_walk (msg): + body = "" for part in msg.walk(): if part.get_content_type() == 'multipart': @@ -86,30 +89,39 @@ def msg_walk (msg): continue elif (filename == "encrypted.asc") or (conttype == "pgp/mime"): - dec_msg = decrypt_payload(payload) - print_decrypted(dec_msg) + payload_dec = decrypt_payload(payload) + body += payload_dec elif conttype == "text/plain": - print(payload) + body += payload + "\n" else: - print(payload) + body += payload + "\n" + + return body -def print_decrypted (message): +def decrypt_payload (payload): - if message == None: + blocks = split_message(payload) + decrypted_tree = decrypt_blocks(blocks) + + if decrypted_tree == None: return - for chunk in message: - msg = email.parser.Parser().parsestr(chunk[0]) - sigs = chunk[1] + body = "" + for node in decrypted_tree: + msg = email.parser.Parser().parsestr(node[0]) + sigs = node[1] - msg_walk(msg) + body += msg_walk(msg) for sig in sigs: - print_sig(sig) + body += format_sig(sig) + + return body + -def print_sig (sig): +def format_sig (sig): fprint = sig.fpr fprint_short = re.search("[0-9A-Fa-f]{32}([0-9A-Fa-f]{8})", fprint).groups()[0] @@ -143,17 +155,10 @@ def print_sig (sig): status = "BAD Signature " - print("Signature Made " + date + " using key ID " + fprint_short) - print(status + "from " + name + " (" + comment + ") <" \ - + e_addr + ">") + sig_str = "Signature Made " + date + " using key ID " + fprint_short + "\n" + sig_str += status + "from " + name + " (" + comment + ") <" + e_addr + ">" - -def decrypt_payload (payload): - - blocks = split_message(payload) - message = decrypt_blocks(blocks) - - return message + return sig_str def split_message (text): -- 2.25.1