Commit | Line | Data |
---|---|---|
0bec96d6 AE |
1 | #! /usr/bin/env python3 |
2 | ||
3 | """********************************************************************* | |
4 | * Edward is free software: you can redistribute it and/or modify * | |
5 | * it under the terms of the GNU Affero Public License as published by * | |
6 | * the Free Software Foundation, either version 3 of the License, or * | |
7 | * (at your option) any later version. * | |
8 | * * | |
9 | * Edward is distributed in the hope that it will be useful, * | |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * | |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * | |
12 | * GNU Affero Public License for more details. * | |
13 | * * | |
14 | * You should have received a copy of the GNU Affero Public License * | |
15 | * along with Edward. If not, see <http://www.gnu.org/licenses/>. * | |
16 | * * | |
17 | * Copyright (C) 2014-2015 Andrew Engelbrecht (AGPLv3+) * | |
18 | * Copyright (C) 2014 Josh Drake (AGPLv3+) * | |
19 | * Copyright (C) 2014 Lisa Marie Maginnis (AGPLv3+) * | |
20 | * * | |
21 | * Special thanks to Josh Drake for writing the original edward bot! :) * | |
22 | * * | |
23 | ************************************************************************ | |
24 | ||
25 | Code used from: | |
26 | ||
27 | * http://agpl.fsf.org/emailselfdefense.fsf.org/edward/CURRENT/edward.tar.gz | |
28 | ||
29 | """ | |
30 | ||
31 | import sys | |
32 | import email.parser | |
33 | import gpgme | |
34 | import re | |
35 | import io | |
36 | import time | |
37 | ||
c96f3837 | 38 | |
0bec96d6 AE |
39 | def main (): |
40 | ||
20f6e7c5 | 41 | handle_args() |
0bec96d6 | 42 | |
c96f3837 | 43 | email_text = sys.stdin.read() |
c96f3837 | 44 | |
65ed3800 AE |
45 | email_from, email_subject = email_from_subject(email_text) |
46 | ||
1da9b527 AE |
47 | plaintext, keys = email_decode_flatten (email_text) |
48 | encrypt_to_key = choose_reply_encryption_key(keys) | |
fafa21c3 | 49 | |
1da9b527 AE |
50 | reply_message = generate_reply(plaintext, email_from, \ |
51 | email_subject, encrypt_to_key) | |
c96f3837 | 52 | |
1da9b527 | 53 | print(reply_message) |
c96f3837 | 54 | |
0bec96d6 | 55 | |
7b06b980 | 56 | def email_decode_flatten (email_text): |
0bec96d6 | 57 | |
86663388 | 58 | body = "" |
394a1476 AE |
59 | keys = [] |
60 | ||
61 | email_struct = email.parser.Parser().parsestr(email_text) | |
62 | ||
63 | for subpart in email_struct.walk(): | |
8bb4b0d5 | 64 | |
394a1476 AE |
65 | payload, description, filename, content_type \ |
66 | = get_email_subpart_info(subpart) | |
0bec96d6 | 67 | |
394a1476 | 68 | if payload == "": |
8bb4b0d5 | 69 | continue |
0bec96d6 | 70 | |
394a1476 | 71 | if content_type == "multipart": |
d437f8b2 | 72 | continue |
0bec96d6 | 73 | |
394a1476 AE |
74 | if content_type == "application/pgp-encrypted": |
75 | if description == "PGP/MIME version identification": | |
8bb4b0d5 | 76 | if payload.strip() != "Version: 1": |
394a1476 AE |
77 | print(progname + ": Warning: unknown " \ |
78 | + description + ": " \ | |
20f6e7c5 | 79 | + payload.strip(), file=sys.stderr) |
8bb4b0d5 AE |
80 | continue |
81 | ||
d437f8b2 | 82 | |
394a1476 AE |
83 | if (filename == "encrypted.asc") or (content_type == "pgp/mime"): |
84 | plaintext, more_keys = decrypt_text(payload) | |
0bec96d6 | 85 | |
394a1476 AE |
86 | body += plaintext |
87 | keys += more_keys | |
88 | ||
89 | elif content_type == "text/plain": | |
86663388 | 90 | body += payload + "\n" |
0bec96d6 | 91 | |
8bb4b0d5 | 92 | else: |
86663388 AE |
93 | body += payload + "\n" |
94 | ||
394a1476 | 95 | return body, keys |
86663388 | 96 | |
c96f3837 | 97 | |
65ed3800 | 98 | def email_from_subject (email_text): |
d437f8b2 | 99 | |
394a1476 | 100 | email_struct = email.parser.Parser().parsestr(email_text) |
d437f8b2 | 101 | |
394a1476 AE |
102 | email_from = email_struct['From'] |
103 | email_subject = email_struct['Subject'] | |
d437f8b2 | 104 | |
394a1476 | 105 | return email_from, email_subject |
d437f8b2 | 106 | |
d437f8b2 | 107 | |
394a1476 | 108 | def get_email_subpart_info (part): |
d437f8b2 | 109 | |
394a1476 AE |
110 | charset = part.get_content_charset() |
111 | payload_bytes = part.get_payload(decode=True) | |
d437f8b2 | 112 | |
394a1476 AE |
113 | filename = part.get_filename() |
114 | content_type = part.get_content_type() | |
115 | description_list = part.get_params(header='content-description') | |
d437f8b2 | 116 | |
394a1476 AE |
117 | if charset == None: |
118 | charset = 'utf-8' | |
0bec96d6 | 119 | |
394a1476 AE |
120 | if payload_bytes != None: |
121 | payload = payload_bytes.decode(charset) | |
122 | else: | |
123 | payload = "" | |
0bec96d6 | 124 | |
394a1476 AE |
125 | if description_list != None: |
126 | description = description_list[0][0] | |
127 | else: | |
128 | description = "" | |
0bec96d6 | 129 | |
394a1476 | 130 | return payload, description, filename, content_type |
0bec96d6 | 131 | |
0bec96d6 | 132 | |
394a1476 | 133 | def decrypt_text (gpg_text): |
86663388 | 134 | |
394a1476 AE |
135 | body = "" |
136 | keys = [] | |
0bec96d6 | 137 | |
394a1476 | 138 | gpg_chunks = split_message(gpg_text) |
86663388 | 139 | |
394a1476 | 140 | plaintext_and_sigs_chunks = decrypt_chunks(gpg_chunks) |
0bec96d6 | 141 | |
394a1476 AE |
142 | for chunk in plaintext_and_sigs_chunks: |
143 | plaintext = chunk[0] | |
144 | sigs = chunk[1] | |
9eb78301 | 145 | |
394a1476 AE |
146 | for sig in sigs: |
147 | key = get_pub_key(sig) | |
148 | keys += [key] | |
0bec96d6 | 149 | |
394a1476 | 150 | # recursive for nested layers of mime and/or gpg |
7b06b980 | 151 | plaintext, more_keys = email_decode_flatten(plaintext) |
8bb4b0d5 | 152 | |
394a1476 AE |
153 | body += plaintext |
154 | keys += more_keys | |
8bb4b0d5 | 155 | |
394a1476 | 156 | return body, keys |
8bb4b0d5 | 157 | |
8bb4b0d5 | 158 | |
394a1476 | 159 | def get_pub_key (sig): |
16da8026 | 160 | |
394a1476 | 161 | gpgme_ctx = gpgme.Context() |
9eb78301 | 162 | |
7b06b980 AE |
163 | fingerprint = sig.fpr |
164 | key = gpgme_ctx.get_key(fingerprint) | |
0bec96d6 | 165 | |
394a1476 | 166 | return key |
0bec96d6 AE |
167 | |
168 | ||
169 | def split_message (text): | |
170 | ||
7b06b980 | 171 | gpg_matches = re.search( \ |
394a1476 AE |
172 | '(-----BEGIN PGP MESSAGE-----' + \ |
173 | '.*' + \ | |
0bec96d6 AE |
174 | '-----END PGP MESSAGE-----)', \ |
175 | text, \ | |
176 | re.DOTALL) | |
177 | ||
7b06b980 AE |
178 | if gpg_matches != None: |
179 | gpg_chunks = gpg_matches.groups() | |
0bec96d6 | 180 | else: |
7b06b980 AE |
181 | gpg_chunks = () |
182 | ||
183 | return gpg_chunks | |
0bec96d6 AE |
184 | |
185 | ||
394a1476 | 186 | def decrypt_chunks (gpg_chunks): |
0bec96d6 | 187 | |
394a1476 | 188 | plaintext_and_sigs_chunks = [] |
0bec96d6 | 189 | |
394a1476 AE |
190 | for gpg_chunk in gpg_chunks: |
191 | plaintext_and_sigs_chunks += [decrypt_chunk(gpg_chunk)] | |
0bec96d6 | 192 | |
394a1476 | 193 | return plaintext_and_sigs_chunks |
0bec96d6 | 194 | |
0bec96d6 | 195 | |
394a1476 | 196 | def decrypt_chunk (gpg_chunk): |
0bec96d6 | 197 | |
394a1476 | 198 | gpgme_ctx = gpgme.Context() |
0bec96d6 | 199 | |
394a1476 | 200 | chunk_b = io.BytesIO(gpg_chunk.encode('ASCII')) |
0bec96d6 AE |
201 | plain_b = io.BytesIO() |
202 | ||
394a1476 | 203 | sigs = gpgme_ctx.decrypt_verify(chunk_b, plain_b) |
0bec96d6 | 204 | |
394a1476 AE |
205 | plaintext = plain_b.getvalue().decode('ASCII') |
206 | return (plaintext, sigs) | |
0bec96d6 AE |
207 | |
208 | ||
fafa21c3 AE |
209 | def choose_reply_encryption_key (keys): |
210 | ||
211 | reply_key = None | |
212 | for key in keys: | |
213 | if (key.can_encrypt == True): | |
214 | reply_key = key | |
215 | break | |
216 | ||
217 | return key | |
218 | ||
219 | ||
1da9b527 AE |
220 | def generate_reply (plaintext, email_from, email_subject, encrypt_to_key): |
221 | ||
222 | plaintext_reply = "thanks for the message!\n\n\n" + plaintext | |
223 | ||
224 | encrypted_reply = encrypt_message(plaintext_reply, encrypt_to_key) | |
225 | ||
226 | reply = "To: " + email_from + "\n" | |
227 | reply += "Subject: " + email_subject + "\n" | |
228 | reply += encrypted_reply | |
229 | ||
230 | return reply | |
231 | ||
232 | ||
233 | def encrypt_message (plaintext, encrypt_to_key): | |
234 | ||
235 | gpgme_ctx = gpgme.Context() | |
236 | gpgme_ctx.armor = True | |
237 | ||
238 | plaintext_bytes = io.BytesIO(plaintext.encode('UTF-8')) | |
239 | encrypted_bytes = io.BytesIO() | |
240 | ||
241 | gpgme_ctx.encrypt([encrypt_to_key], gpgme.ENCRYPT_ALWAYS_TRUST, | |
242 | plaintext_bytes, encrypted_bytes) | |
243 | ||
244 | encrypted_txt = encrypted_bytes.getvalue().decode('ASCII') | |
245 | return encrypted_txt | |
246 | ||
247 | ||
20f6e7c5 AE |
248 | def handle_args (): |
249 | if __name__ == "__main__": | |
250 | ||
251 | global progname | |
252 | progname = sys.argv[0] | |
253 | ||
254 | if len(sys.argv) > 1: | |
255 | print(progname + ": error, this program doesn't " \ | |
256 | "need any arguments.", file=sys.stderr) | |
257 | exit(1) | |
258 | ||
259 | ||
0bec96d6 AE |
260 | main() |
261 |