summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatěj Cepl <mcepl@redhat.com>2014-03-24 17:28:54 +0100
committerMatěj Cepl <mcepl@redhat.com>2014-03-24 17:46:07 +0100
commitfa31ad3b3b16a2d5439071787055ccfc65a24451 (patch)
tree3fd440b25d91ba31d44c9d0d8570290e279f70c9
parent2204de5201c339935c72cc23490f8ed6f47ac6d9 (diff)
downloadimap-folder-training-fa31ad3b3b16a2d5439071787055ccfc65a24451.tar.gz
Hopefully working version of train_bogofilter.
-rwxr-xr-xtrain_bogofilter68
1 files 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()