This repository has been archived by the owner on May 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
detach.py
executable file
·346 lines (277 loc) · 10.4 KB
/
detach.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
#!/usr/bin/env python3
########################################################################
# File name: detach.py
# This file is part of: None
#
# LICENSE
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
#
# This program 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
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
########################################################################
import base64
import quopri
import configparser
import smtplib
import email.header
import email.mime.multipart
import email.mime.text
import email.parser
import os
import re
from datetime import datetime
PREFIXTEXT = """\
NOTE: This is detach.py, sorry to interrupt you. I have taken
the attachments and put them into
{destdir}
which can be accessed via
{desturl}
for your convenience.
"""
def get_mails(maildir):
for filename in os.listdir(maildir):
parts = filename.split(",")
if len(parts) == 1:
# not a maildir file
continue
yield os.path.join(maildir, filename)
def exclude_seen_mails(mails):
for filename in mails:
parts = filename.split(",")
if "S" in parts[-1]:
# seen
continue
yield filename
def parse_mails(filenames):
parser = email.parser.BytesParser()
for filename in filenames:
with open(filename, "rb") as fp:
yield parser.parse(fp)
def filter_list_admin_mails(mails):
for mail in mails:
if not mail["X-List-Administrivia"]:
continue
yield mail
def filter_and_extract_nested_mails(mails):
for mail in mails:
payload = mail.get_payload()
if not isinstance(payload, list):
continue
for part in payload:
if part["Content-Type"] == "message/rfc822":
yield mail, part.get_payload()[0]
def decode_header_string(hs):
result = []
for data, encoding in email.header.decode_header(hs):
if isinstance(data, str):
result.append(data)
else:
result.append(data.decode(encoding or "ascii"))
return "".join(result)
def ask(prompt, options):
options = list(map(str.lower, options))
options_parts = [options[0].upper()]
options_parts.extend(options[1:])
options_str = "/".join(options_parts)
while True:
result = input(prompt.format(options_str))
if not result:
return options[0]
if result.lower() in options:
return result
print("Incorrect choice")
def ask_nonexisting_dir(prompt, dirfmt, urlfmt):
while True:
destdir = input(prompt)
full = dirfmt.format(destdir)
fullurl = urlfmt.format(destdir)
try:
os.makedirs(full)
return (full, fullurl)
except FileExistsError:
print("File exists, use a different path")
def find_attachments(parts):
for part in parts:
content_dispo = part["Content-Disposition"]
if (content_dispo is not None
and content_dispo.lower().startswith("attachment")):
yield part
def extract_attachment_filename(part):
return part.get_filename()
def decode_attachment(part):
encoding = part["Content-Transfer-Encoding"]
if encoding is None:
return part.get_payload()
encoding = encoding.strip()
if encoding == "base64":
return base64.b64decode(part.get_payload().encode("ascii"))
elif encoding == "quoted-printable":
return quopri.decodestring(part.get_payload().encode("ascii"))
else:
raise ValueError("Unknown transfer encoding: {}".format(encoding))
def process_mail(outer,
inner,
dir_pattern,
url_pattern,
recipient="FSR Informatik <[email protected]>",
user="fsr-request"):
TEXT_CONTENT_TYPES = {"text/html", "text/plain", "application/html"}
HEADERS_TO_TRANSFER = [
"From",
"Date",
"Subject",
]
attachments = []
textual_data = []
for attachment in find_attachments(inner.walk()):
name = extract_attachment_filename(attachment)
data = decode_attachment(attachment)
attachments.append((name, data))
for part in inner.walk():
if part["Content-Type"] is None:
continue
ct = part["Content-Type"].split(";", 1)[0].strip()
if ct.lower() in TEXT_CONTENT_TYPES:
textual_data.append(part)
new_message = email.mime.multipart.MIMEMultipart("mixed")
if attachments:
print("attachments have been found:")
for i, (name, _) in enumerate(attachments):
print(" [{}]: {}".format(i + 1, name))
dirfmt = datetime.utcnow().strftime(dir_pattern)
urlfmt = datetime.utcnow().strftime(url_pattern)
(destdir, desturl) = ask_nonexisting_dir(
"attachment directory name (only suffix): ", dirfmt, urlfmt)
os.chmod(destdir, 0o775)
for name, data in attachments:
path = os.path.join(destdir, name)
with open(path, "wb") as f:
f.write(data)
os.chmod(path, 0o664)
note = email.mime.text.MIMEText(
PREFIXTEXT.format(destdir=destdir, desturl=desturl))
new_message.attach(note)
textual_part = email.mime.multipart.MIMEMultipart("alternative")
for data in textual_data:
# hack to work around weird muas adding empty plaintext parts
if data.get_payload().strip():
textual_part.attach(data)
new_message.attach(textual_part)
for headername in HEADERS_TO_TRANSFER:
if user != "fsr-request" and headername == "From":
new_message[headername] = user + "@ifsr.de"
else:
new_message[headername] = inner[headername]
new_message["To"] = recipient
new_message["User-Agent"] = "detach.py/0.1"
old_message_id = inner["Message-ID"].strip("<>")
timestamp = datetime.utcnow().strftime("%Y%m%d%H%M%S%f")
new_message["Message-ID"] = "<detached_at_{}_from_{}>".format(
timestamp, old_message_id)
return new_message
def get_smtp_conn(host, port, verbose):
if verbose:
print("connecting to {}:{}".format(host, port))
conn = smtplib.SMTP(host, port)
conn.starttls()
conn.ehlo_or_helo_if_needed()
return conn
def run(user, maildir, smtp_conn, exclude_seen, dir_pattern, url_pattern):
mails = get_mails(maildir)
if exclude_seen:
mails = exclude_seen_mails(mails)
parsed_mails = parse_mails(mails)
list_admin_mails = filter_list_admin_mails(parsed_mails)
mails_with_nested_mails = filter_and_extract_nested_mails(list_admin_mails)
options = ["y", "n"]
# confirm id pattern
pattern = re.compile("confirm\s\w{40}")
for parsed, nested in mails_with_nested_mails:
if not pattern.match(decode_header_string(nested["Subject"])):
print("found matching mail:")
print(" Subject: {}".format(
decode_header_string(parsed["Subject"])))
print(" nested Subject: {}".format(
decode_header_string(nested["Subject"])))
action = ask("Process mail? (Y = yes, n = no) [{}]", options)
if action == "y":
mail_to_send = process_mail(
parsed,
nested,
dir_pattern,
url_pattern,
)
else:
print()
action = ask("Reject mail for mailman? (Y = yes, n = no) [{}]",
["y", "n"])
if action == "y":
mail_to_send = process_mail(parsed, nested, dir_pattern,
url_pattern, "[email protected]",
user)
if action == "y":
try:
smtp_conn.send_message(mail_to_send)
except smtplib.SMTPSenderRefused:
smtp_conn = get_smtp_conn(smtp_host, smtp_port, args.verbose)
smtp_conn.send_message(mail_to_send)
if __name__ == "__main__":
import argparse
import sys
parser = argparse.ArgumentParser()
parser.add_argument("--with-read",
dest="exclude_seen",
action="store_false",
default=True,
help="Exclude messages marked as read (seen) "
"(overrides value obtained from configuration)")
parser.add_argument(
"-m",
"--maildir",
help="Path to maildir (overrides value obtained from configuration)",
default=None,
)
parser.add_argument("-v",
"--verbose",
action="store_true",
dest="verbose",
help="Be more verbose")
args = parser.parse_args()
cfg = configparser.RawConfigParser()
config_path = os.path.expanduser('~/.config/detach.ini')
cfg.read(config_path)
if args.maildir:
cfg.set("detach", "maildir", args.maildir)
if not args.exclude_seen:
cfg.set("detach", "exclude-seen", "false")
try:
user = cfg.get("detach", "user")
maildir = cfg.get("detach", "maildir")
exclude_seen = cfg.get("detach", "exclude-seen", fallback=True)
smtp_host = cfg.get("smtp", "host", fallback="localhost")
smtp_port = cfg.getint("smtp", "port", fallback=25)
pattern = cfg.get("detach", "pattern")
dir_pattern = cfg.get("detach", "dir") + pattern
url_pattern = cfg.get("detach", "url") + pattern
except (configparser.NoOptionError, configparser.NoSectionError,
ValueError) as e:
print("configuration error:", str(e))
sys.exit(2)
if args.verbose:
print("looking in mailbox: {}".format(maildir))
print("attachment directory pattern: {}".format(dir_pattern))
conn = get_smtp_conn(smtp_host, smtp_port, args.verbose)
try:
run(user, maildir, conn, exclude_seen, dir_pattern, url_pattern)
finally:
conn.close()