From fa31ad3b3b16a2d5439071787055ccfc65a24451 Mon Sep 17 00:00:00 2001 From: Matěj Cepl Date: Mon, 24 Mar 2014 17:28:54 +0100 Subject: Hopefully working version of train_bogofilter. --- train_bogofilter | 68 ++++++++++++++++++++++++++------------------------------ 1 file changed, 32 insertions(+), 36 deletions(-) diff --git a/train_bogofilter b/train_bogofilter index 56d1086..00f444d 100755 --- a/train_bogofilter +++ b/train_bogofilter @@ -1,55 +1,49 @@ #!/usr/bin/python -import imaplib -import subprocess import email +import imaplib import logging -import email.Parser +import os.path +import subprocess from ConfigParser import ConfigParser logging.basicConfig(format='%(levelname)s:%(funcName)s:%(message)s', level=logging.DEBUG) CMD_STR = "/usr/bin/bogofilter -%s" +imaplib.Debug = 4 def process_folder(proc_fld_name, bogofilter_param, end_fld_name): client.select(proc_fld_name) _, resp = client.search(None, "ALL") messages = resp[0].split() + logging.debug('messages = {0}'.format(messages)) proc_msg_count = 0 - logging.debug(messages) for msgId in messages: - # FIXME or no .PEEK ... do I want to mark a message as Seen? - # If not, I have to unset \Seen flag - # fetch(msgId, '(RFC822)') gets whole message as it is - _, msg_data = client.fetch(msgId, - '(BODY.PEEK[HEADER.FIELDS ' + - '(SUBJECT FROM X-DSPAM-SIGNATURE)])') - logging.debug("msgId = %s", msgId) - headers = hparser.parsestr(msg_data[0][1], headersonly=True) - logging.debug("headers:\n%s", headers) - if 'X-Dspam-Signature' in headers.keys(): - ret = subprocess.Popen(CMD_STR % bogofilter_param, - stdin=subprocess.Popen, - shell=True) - ret.communicate(input=msg_data) # FIXME Whole message + logging.debug('msgId = {0}'.format(msgId)) + typ, msg_data = client.fetch(msgId, '(RFC822)') + + msg = hparser.parsestr(msg_data[0][1]) + + ret = subprocess.Popen(CMD_STR % bogofilter_param, + stdin=subprocess.PIPE, + shell=True) + ret.communicate(input=msg_data[0][1]) - if ret.returncode == 0: - typ, _ = client.copy(msgId, end_fld_name) - # Also - # http://bytes.com/topic/python/answers\ - # /41900-copying-moving-mail-message-using-imaplib - # Also RFC 2060, section 6.4.8, UID command - # Also http://pymotw.com/2/imaplib/ - # folder.copy(msg_ids, where_folder) - if typ != 'OK': - raise IOError("Cannot store a message to the folder %s" - % end_fld_name) - else: - client.store(msgId, '+FLAGS', r'(\Deleted)') - proc_msg_count += 1 + logging.debug("ret.returncode = %s", ret.returncode) + if ret.returncode == 0: + del msg['X-Bogosity'] + typ, _ = client.append(end_fld_name, '', '', + msg.as_string(True)) + logging.debug("typ = %s", typ) + if typ != 'OK': + raise IOError("Cannot store a message to the folder %s" + % end_fld_name) else: - raise OSError("bogofilter finished with the returncode: %d" - % ret) + client.store(msgId, '+FLAGS', r'(\Deleted)') + proc_msg_count += 1 + else: + raise OSError("bogofilter finished with the returncode: %d" + % ret) client.expunge() client.close() @@ -59,14 +53,16 @@ def process_folder(proc_fld_name, bogofilter_param, end_fld_name): processedCounter = 0 hparser = email.Parser.Parser() config = ConfigParser() -config.read("/etc/bogofilter-imap-train.cfg") +config.read(os.path.expanduser("~/.bogofilter-imap-train-rc")) login = config.get("imap-training", "login") password = config.get("imap-training", "password") -client = imaplib.IMAP4_SSL("luther.ceplovi.cz") +server = config.get("imap-training", "server") +client = imaplib.IMAP4_SSL(server) client.login(login, password) for box in [('Spam', 's', 'Deleted Items'), ('Ham', 'n', 'INBOX')]: + logging.debug('box = {0}'.format(box)) processedCounter += process_folder(box[0], box[1], box[2]) client.logout() -- cgit