-
Notifications
You must be signed in to change notification settings - Fork 2
/
received.py
50 lines (42 loc) · 1.53 KB
/
received.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
import os
import re
import email
BY_POSTFIX = r'by pulerau\.scitechtinget\.dk \(Postfix\) '
SMTP_ID = r'with E?SMTPS? id [+A-Za-z0-9-]+'
FOR = r'(( |\n\t)for <[^@]+@(taagekammeret|TAAGEKAMMERET)\.(dk|DK)>)?;( |\n\t)'
TIME = r'[A-Za-z0-9, :+]+ \(CES?T\)$'
patterns = [
r'^from localhost \(localhost\.localdomain \[127\.0\.0\.1\]\)\n\t' +
BY_POSTFIX +
SMTP_ID + FOR + TIME,
r'^from pulerau\.scitechtinget\.dk \(\[127\.0\.0\.1\]\)\n\t' +
r'by localhost \(pulerau\.scitechtinget\.dk \[127\.0\.0\.1\]\) ' +
r'\(amavisd-new, port 10024\)\n\t' +
SMTP_ID + FOR + TIME,
r'^from (?P<helo_name>\S+) ' +
r'\((?P<name>[A-Za-z0-9_.-]+) \[(?P<ip>[0-9.]+)\]\)\n\t' +
BY_POSTFIX +
SMTP_ID + FOR + TIME,
]
for f in os.listdir('errorarchive'):
if not f.endswith('.mail'):
continue
with open('errorarchive/%s' % f, 'rb') as fp:
message = email.message_from_binary_file(fp)
received = message.get_all('Received')
if received is None:
continue # Probably a test email
if type(received) != list:
raise TypeError(received)
if 'emailtunnel' in str(received[0]):
continue
if '[email protected]' in str(message.get('From')):
continue
assert len(received) >= len(patterns), (received, f)
for r, p in zip(received, patterns):
if not re.match(p, str(r)):
print(str(r))
print(p)
raise ValueError(f)
mo = re.match(patterns[2], str(received[2]))
print(*mo.group('helo_name', 'name', 'ip'))