diff options
Diffstat (limited to 'train_bogofilter')
-rwxr-xr-x | train_bogofilter | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/train_bogofilter b/train_bogofilter new file mode 100755 index 0000000..d4e5bb2 --- /dev/null +++ b/train_bogofilter @@ -0,0 +1,49 @@ +#!/usr/bin/python +import imaplib, subprocess, email, sys +import logging +import email.Parser +from ConfigParser import ConfigParser + +cmd_string = "/usr/bin/dspam --user dspam --class=spam "\ + + "--source=error --deliver=summary --stdout --signature='%s'" + +DEBUG=False + +def debug(msg): + if DEBUG: + print >>sys.stderr,msg + +hparser = email.Parser.Parser() +config = ConfigParser() +config.read("/etc/dspam-imap-train.cfg") + +login = config.get("imap-training","login") +password = config.get("imap-training","password") +client = imaplib.IMAP4_SSL("luther.ceplovi.cz") +client.login(login, password) +client.select("Public folders/Junk") +status, resp = client.search(None, "ALL") +messages = resp[0].split() +processedCounter = 0 +debug(messages) + +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)])') + debug("msgId = %s" % msgId) + headers = hparser.parsestr(msg_data[0][1],headersonly=True) + debug("headers:\n%s" % headers) + if 'X-Dspam-Signature' in headers.keys(): + ret = subprocess.Popen(cmd_string \ + % headers['X-Dspam-Signature'], shell=True).wait() + if ret == 0: + typ, response = client.store(msgId, '+FLAGS', r'(\Deleted)') + processedCounter += 1 + else: + raise OSError, "dspam finished with failure code: %d" % ret + +client.expunge() +client.close() +client.logout() +if processedCounter > 0: + debug("Processed %d spam messages." % processedCounter) |