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
|
#!/usr/bin/python
# Mind you, we have only python 2.4.3 on RHEL-5
import imaplib, subprocess, email
import email.Parser
cmd_string = "/usr/bin/dspam --user dspam --debug --class=spam "\
+ "--source=error --deliver=summary --stdout --signature='%s'"
hparser = email.Parser.Parser()
client = imaplib.IMAP4_SSL("luther.ceplovi.cz")
client.login("dspam","hnus")
client.select("Public folders/Junk")
status, resp = client.search(None, "ALL")
messages = resp[0].split()
for msgId in messages:
# or no .PEEK ... do I want to mark a message as Seen?
typ, msg_data = client.fetch(msgId, '(BODY.PEEK[HEADER.FIELDS (SUBJECT FROM X-DSPAM-SIGNATURE)])')
headers = hparser.parsestr(msg_data[0][1],headersonly=True)
print headers.keys()
if 'X-Dspam-Signature' in headers.keys():
print headers['X-Dspam-Signature']
ret = subprocess.Popen(cmd_string \
% headers['X-Dspam-Signature'], shell=True).wait()
if ret == 0:
typ, response = client.store(msgId, '+FLAGS', r'(\Deleted)')
else:
raise OSError, "dspam finished with failure code: %d" % ret
client.expunge()
client.close()
client.logout()
|