From 2be0c87e00e363f1bc6eea5d26fa4148ba21b2d0 Mon Sep 17 00:00:00 2001 From: Matěj Cepl Date: Sat, 8 Mar 2014 00:30:05 +0100 Subject: Let train_dspam_from_folder generate debug messages. --- train_bogofilter | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ train_dspam_from_folder | 13 +++++++++++-- 2 files changed, 60 insertions(+), 2 deletions(-) create mode 100755 train_bogofilter 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) diff --git a/train_dspam_from_folder b/train_dspam_from_folder index 2aed4b7..6cc9323 100755 --- a/train_dspam_from_folder +++ b/train_dspam_from_folder @@ -1,12 +1,18 @@ #!/usr/bin/python # Mind you, we have only python 2.4.3 on RHEL-5 -import imaplib, subprocess, email +import imaplib, subprocess, email, sys 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") @@ -19,11 +25,14 @@ 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() @@ -37,4 +46,4 @@ client.expunge() client.close() client.logout() if processedCounter > 0: - print "Processed %d spam messages." % processedCounter + debug("Processed %d spam messages." % processedCounter) -- cgit