summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatěj Cepl <mcepl@cepl.eu>2017-10-23 20:38:35 +0200
committerMatěj Cepl <mcepl@cepl.eu>2017-10-23 20:40:47 +0200
commitcc52a775a514b30311c1f8f0ee17095a2088c8a0 (patch)
tree5a1f6d7caf71b0af5507bf132d0d29cb2e65822b
parentb3dd3baf5dd4540ffd9adef15432960e3708ce7d (diff)
downloadimap-folder-training-cc52a775a514b30311c1f8f0ee17095a2088c8a0.tar.gz
Use logging better.
-rwxr-xr-xcheck_bogofilter.py2
-rwxr-xr-xtrain_bogofilter.py21
-rwxr-xr-xtrain_dspam_from_folder49
3 files changed, 14 insertions, 58 deletions
diff --git a/check_bogofilter.py b/check_bogofilter.py
index f75c696..cd0fb72 100755
--- a/check_bogofilter.py
+++ b/check_bogofilter.py
@@ -11,7 +11,7 @@ logging.basicConfig(format='%(levelname)s:%(funcName)s:%(message)s',
level=logging.INFO)
log = logging.getLogger('check_bogofilter')
-if log.getEffectiveLevel() == logging.DEBUG:
+if log.getEffectiveLevel() >= logging.DEBUG:
imaplib.Debug = 4
pattern_uid = re.compile('\d+ \(UID (?P<uid>\d+)\)')
diff --git a/train_bogofilter.py b/train_bogofilter.py
index 48f20bc..beadacf 100755
--- a/train_bogofilter.py
+++ b/train_bogofilter.py
@@ -5,21 +5,26 @@ import logging
import os.path
import subprocess
from ConfigParser import ConfigParser
+
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
+
+if log.getEffectiveLevel() >= logging.DEBUG:
+ imaplib.Debug = 4
+
def process_folder(proc_fld_name, bogofilter_param, end_fld_name, mark_seen=True):
client.select(proc_fld_name)
_, resp = client.search(None, "ALL")
messages = resp[0].split()
- logging.debug('messages = %s', messages)
+ log.debug('messages = %s', messages)
proc_msg_count = 0
for msgId in messages:
- logging.debug('msgId = %s', msgId)
+ log.debug('msgId = %s', msgId)
typ, msg_data = client.fetch(msgId, '(RFC822)')
msg = hparser.parsestr(msg_data[0][1])
@@ -29,13 +34,13 @@ def process_folder(proc_fld_name, bogofilter_param, end_fld_name, mark_seen=True
shell=True)
ret.communicate(input=msg_data[0][1])
- logging.debug("ret.returncode = %s", ret.returncode)
+ log.debug("ret.returncode = %s", ret.returncode)
if ret.returncode == 0:
del msg['X-Bogosity']
typ, newmsg = client.append(end_fld_name, '', '',
msg.as_string(True))
- logging.debug("typ = %s", typ)
- logging.debug("newmsg = %s", newmsg)
+ log.debug("typ = %s", typ)
+ log.debug("newmsg = %s", newmsg)
# if mark_seen:
# client.store(newmsg, '+FLAGS', r'(\Seen)')
if typ != 'OK':
@@ -65,11 +70,11 @@ client = imaplib.IMAP4_SSL(server)
client.login(login, password)
for box in [('Junk', 's', 'Trash', True), ('Ham', 'n', 'INBOX', False)]:
- logging.debug('box = %s', box)
+ log.debug('box = %s', box)
# processedCounter += process_folder(box[0], box[1], box[2], box[3)
processedCounter += process_folder(*box)
client.logout()
if processedCounter > 0:
- logging.info("Processed %d spam messages.", processedCounter)
+ log.info("Processed %d spam messages.", processedCounter)
diff --git a/train_dspam_from_folder b/train_dspam_from_folder
deleted file mode 100755
index 6cc9323..0000000
--- a/train_dspam_from_folder
+++ /dev/null
@@ -1,49 +0,0 @@
-#!/usr/bin/python
-# Mind you, we have only python 2.4.3 on RHEL-5
-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")
-
-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)