-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubmitspam.py
executable file
·68 lines (47 loc) · 1.46 KB
/
submitspam.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
#!/usr/bin/env python
import email
import smtplib
import subprocess
import sys
notify_name = 'Spam Submit'
notify_email = '[email protected]'
msg = email.message_from_file(sys.stdin)
msg_from = msg['From']
if not msg.is_multipart():
print 'exit out here'
sys.exit(1)
parts = msg.get_payload()
results = []
for p in parts:
record = {}
if not p.get_content_type() == 'message/rfc822':
continue
subparts = p.get_payload()
if not len(subparts) == 1:
record['error'] = 'more subparts than expected'
results.append(record)
continue
submsg = subparts[0]
record['Subject'] = submsg['Subject']
proc = subprocess.Popen(['/usr/bin/sa-learn', '--spam', '--no-sync'],
stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.PIPE, cwd='/tmp/')
result_stdout, result_stderr = proc.communicate(input=submsg.as_string())
record['Result'] = result_stdout.strip()
results.append(record)
# sync the database and journal after processing all in the collection
proc = subprocess.Popen(['/usr/bin/sa-learn', '--sync'],
stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.PIPE, cwd='/tmp/')
headers = """From: %s <%s>
To: %s
Subject: Results from submission
Results are below!
""" % (notify_name, notify_email, msg_from)
output = ''
for i in results:
keys = i.keys()
keys.sort()
for k in keys:
output += '%10s: %s\n' % (k, i[k])
output += "\n\n"
smtp = smtplib.SMTP('localhost')
smtp.sendmail(notify_email, msg_from, '%s%s' % (headers,output))