1 #! /usr/bin/env python3
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. *
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. *
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/>. *
17 * Copyright (C) 2014-2015 Andrew Engelbrecht (AGPLv3+) *
18 * Copyright (C) 2014 Josh Drake (AGPLv3+) *
19 * Copyright (C) 2014 Lisa Marie Maginnis (AGPLv3+) *
21 * Special thanks to Josh Drake for writing the original edward bot! :) *
23 ************************************************************************
27 * http://agpl.fsf.org/emailselfdefense.fsf.org/edward/CURRENT/edward.tar.gz
43 email_text
= sys
.stdin
.read()
45 email_from
, email_subject
= email_from_subject(email_text
)
47 plaintext
, keys
= email_decode_flatten (email_text
)
48 encrypt_to_key
= choose_reply_encryption_key(keys
)
50 reply_message
= generate_reply(plaintext
, email_from
, \
51 email_subject
, encrypt_to_key
,
52 "DAB4F989E2788B8DF058E0EFEF1EC52039B36E58")
57 def email_decode_flatten (email_text
):
62 email_struct
= email
.parser
.Parser().parsestr(email_text
)
64 for subpart
in email_struct
.walk():
66 payload
, description
, filename
, content_type \
67 = get_email_subpart_info(subpart
)
72 if content_type
== "multipart":
75 if content_type
== "application/pgp-encrypted":
76 if description
== "PGP/MIME version identification":
77 if payload
.strip() != "Version: 1":
78 print(progname
+ ": Warning: unknown " \
79 + description
+ ": " \
80 + payload
.strip(), file=sys
.stderr
)
84 if (filename
== "encrypted.asc") or (content_type
== "pgp/mime"):
85 plaintext
, more_keys
= decrypt_text(payload
)
90 elif content_type
== "text/plain":
91 body
+= payload
+ "\n"
94 body
+= payload
+ "\n"
99 def email_from_subject (email_text
):
101 email_struct
= email
.parser
.Parser().parsestr(email_text
)
103 email_from
= email_struct
['From']
104 email_subject
= email_struct
['Subject']
106 return email_from
, email_subject
109 def get_email_subpart_info (part
):
111 charset
= part
.get_content_charset()
112 payload_bytes
= part
.get_payload(decode
=True)
114 filename
= part
.get_filename()
115 content_type
= part
.get_content_type()
116 description_list
= part
.get_params(header
='content-description')
121 if payload_bytes
!= None:
122 payload
= payload_bytes
.decode(charset
)
126 if description_list
!= None:
127 description
= description_list
[0][0]
131 return payload
, description
, filename
, content_type
134 def decrypt_text (gpg_text
):
139 gpg_chunks
= split_message(gpg_text
)
141 plaintext_and_sigs_chunks
= decrypt_chunks(gpg_chunks
)
143 for chunk
in plaintext_and_sigs_chunks
:
148 key
= get_pub_key(sig
)
151 # recursive for nested layers of mime and/or gpg
152 plaintext
, more_keys
= email_decode_flatten(plaintext
)
160 def get_pub_key (sig
):
162 gpgme_ctx
= gpgme
.Context()
164 fingerprint
= sig
.fpr
165 key
= gpgme_ctx
.get_key(fingerprint
)
170 def split_message (text
):
172 gpg_matches
= re
.search( \
173 '(-----BEGIN PGP MESSAGE-----' + \
175 '-----END PGP MESSAGE-----)', \
179 if gpg_matches
!= None:
180 gpg_chunks
= gpg_matches
.groups()
187 def decrypt_chunks (gpg_chunks
):
189 return map(decrypt_chunk
, gpg_chunks
)
192 def decrypt_chunk (gpg_chunk
):
194 gpgme_ctx
= gpgme
.Context()
196 chunk_b
= io
.BytesIO(gpg_chunk
.encode('ASCII'))
197 plain_b
= io
.BytesIO()
199 sigs
= gpgme_ctx
.decrypt_verify(chunk_b
, plain_b
)
201 plaintext
= plain_b
.getvalue().decode('ASCII')
202 return (plaintext
, sigs
)
205 def choose_reply_encryption_key (keys
):
209 if (key
.can_encrypt
== True):
216 def generate_reply (plaintext
, email_from
, email_subject
, encrypt_to_key
,
217 sign_with_fingerprint
):
219 reply
= "To: " + email_from
+ "\n"
220 reply
+= "Subject: " + email_subject
+ "\n"
223 if (encrypt_to_key
!= None):
224 plaintext_reply
= "thanks for the message!\n\n\n"
225 plaintext_reply
+= email_quote_text(plaintext
)
227 encrypted_reply
= encrypt_sign_message(plaintext_reply
, encrypt_to_key
,
228 sign_with_fingerprint
)
230 reply
+= encrypted_reply
233 reply
+= "Sorry, i couldn't find your key.\n"
234 reply
+= "I'll need that to encrypt a message to you."
239 def email_quote_text (text
):
241 quoted_message
= re
.sub(r
'^', r
'> ', text
, flags
=re
.MULTILINE
)
243 return quoted_message
246 def encrypt_sign_message (plaintext
, encrypt_to_key
, sign_with_fingerprint
):
248 gpgme_ctx
= gpgme
.Context()
249 gpgme_ctx
.armor
= True
251 sign_with_key
= gpgme_ctx
.get_key(sign_with_fingerprint
)
252 gpgme_ctx
.signers
= [sign_with_key
]
254 plaintext_bytes
= io
.BytesIO(plaintext
.encode('UTF-8'))
255 encrypted_bytes
= io
.BytesIO()
257 gpgme_ctx
.encrypt_sign([encrypt_to_key
], gpgme
.ENCRYPT_ALWAYS_TRUST
,
258 plaintext_bytes
, encrypted_bytes
)
260 encrypted_txt
= encrypted_bytes
.getvalue().decode('ASCII')
265 if __name__
== "__main__":
268 progname
= sys
.argv
[0]
270 if len(sys
.argv
) > 1:
271 print(progname
+ ": error, this program doesn't " \
272 "need any arguments.", file=sys
.stderr
)