diff options
-rwxr-xr-x | check_bogofilter.py | 16 | ||||
-rwxr-xr-x | train_bogofilter.py | 34 |
2 files changed, 38 insertions, 12 deletions
diff --git a/check_bogofilter.py b/check_bogofilter.py index cb292eb..7f992ee 100755 --- a/check_bogofilter.py +++ b/check_bogofilter.py @@ -29,7 +29,7 @@ def move_messages(ids, target): client.uid('STORE', ids_str, '+FLAGS', r'(\Deleted)') -def process_folder(proc_fld_name, target_fld, unsure_fld, +def process_folder(proc_fld_name, target_fld, unsure_fld, spam_fld=None, bogofilter_param=['-l']): ham_msgs = [] @@ -84,10 +84,18 @@ def process_folder(proc_fld_name, target_fld, unsure_fld, else: # 3 or something else I/O error raise IOError('Bogofilter failed with error %d', ret_code) - move_messages(spam_msgs, target_fld) move_messages(unsure_msgs, unsure_fld) + log.debug('ham_msgs = %s, spam_msgs = %s' % (ham_msgs, spam_msgs)) if ham_msgs: - client.uid('STORE', ','.join(ham_msgs), '-FLAGS', r'(\Seen)') + if spam_fld is None: + client.uid('STORE', ','.join(ham_msgs), '-FLAGS', r'(\Seen)') + if spam_msgs: + if spam_fld is not None: + move_messages(spam_msgs, spam_fld) + else: + client.uid('STORE', ','.join(spam_msgs), + '+FLAGS.SILENT', r'(\Deleted \Seen)') + client.uid('EXPUNGE', ','.join(spam_msgs)) client.close() @@ -104,7 +112,7 @@ server = config.get("imap-training", "server") client = imaplib.IMAP4_SSL(server) client.login(login, password) -processedCounter += process_folder('INBOX', '_suspects', '_unsure') +processedCounter += process_folder('INBOX', '_suspects', '_unsure', '_spam') client.logout() diff --git a/train_bogofilter.py b/train_bogofilter.py index 09489d8..8bde56b 100755 --- a/train_bogofilter.py +++ b/train_bogofilter.py @@ -1,19 +1,24 @@ #!/usr/bin/python +import datetime import email import imaplib import logging import os.path +import re import subprocess from ConfigParser import ConfigParser -logging.basicConfig(format='%(levelname)s:%(funcName)s:%(message)s', - level=logging.INFO) +# logging.basicConfig(format='%(levelname)s:%(funcName)s:%(message)s', +# level=logging.DEBUG) log = logging.getLogger('train_bogofilter') CMD_STR = "/usr/bin/bogofilter -%s" # imaplib.Debug = 4 +# Description in RFC2359 +# DEBUG:process_folder:newmsg = '[APPENDUID 1424278334 31178] Append completed.' +APPENDUID_RE = re.compile(r'\[APPENDUID (\d+) (\d+)\] Append completed\.') def process_folder(proc_fld_name, bogofilter_param, end_fld_name, mark_seen=True): client.select(proc_fld_name) @@ -25,8 +30,17 @@ def process_folder(proc_fld_name, bogofilter_param, end_fld_name, mark_seen=True for msgId in messages: log.debug('msgId = %s', msgId) typ, msg_data = client.fetch(msgId, '(RFC822)') + # log.debug('msg_data:\n%s\n%s', msg_data, '-' * 30) msg = hparser.parsestr(msg_data[0][1]) + date_str = msg['Date'] + log.debug("date_str = %s", date_str) + date_tuple=email.utils.parsedate_tz(date_str) + log.debug("date_tuple = %s", date_tuple) + # date_tulpe is 10-tuple (including time distance from GMT) + # but Time2Internaldate wants just 9-tuple + int_date = imaplib.Time2Internaldate(date_tuple[:-1]) + log.debug("int_date = %s", int_date) ret = subprocess.Popen(CMD_STR % bogofilter_param, stdin=subprocess.PIPE, @@ -36,12 +50,16 @@ def process_folder(proc_fld_name, bogofilter_param, end_fld_name, mark_seen=True log.debug("ret.returncode = %s", ret.returncode) if ret.returncode == 0: del msg['X-Bogosity'] - typ, newmsg = client.append(end_fld_name, '', '', + typ, newmsg = client.append(end_fld_name, None, + int_date, msg.as_string(True)) log.debug("typ = %s", typ) - log.debug("newmsg = %s", newmsg) - # if mark_seen: - # client.store(newmsg, '+FLAGS', r'(\Seen)') + newmsg = newmsg[0] + log.debug("newmsg = %s (%s)", newmsg, type(newmsg)) + if mark_seen: + typ, data = client.store(msgId, '+FLAGS', r'(\Seen)') + log.debug("typ = %s", typ) + log.debug("data = %s", data) if typ != 'OK': raise IOError("Cannot store a message to the folder %s" % end_fld_name) @@ -68,7 +86,7 @@ server = config.get("imap-training", "server") client = imaplib.IMAP4_SSL(server) client.login(login, password) -for box in [('Junk', 's', 'Trash', True), ('Ham', 'n', 'INBOX', False)]: +for box in [('Junk', 's', 'Trash', True), ('_ham', 'n', 'INBOX', False)]: log.debug('box = %s', box) # processedCounter += process_folder(box[0], box[1], box[2], box[3) processedCounter += process_folder(*box) @@ -76,4 +94,4 @@ for box in [('Junk', 's', 'Trash', True), ('Ham', 'n', 'INBOX', False)]: client.logout() if processedCounter > 0: - log.info("Processed %d spam messages.", processedCounter) + log.info("Processed %d messages.", processedCounter) |