[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: automating sa-learn via cyrus mailbox?



On Saturday 28 August 2004 10:31 pm, Will Trillich wrote:

> we're running cyrus21 and exim4 for email services, and would
> like to automate the "sa-learn" feature system-wide.

Here's what I'm using for that purpose:


###############################
#!/usr/bin/env python

# Copyright 2004 by Kirk Strauser.  BSD licence, etc.

import os
import re

mailboxes = {
    'mailserver.example.com':
    { 'ham' : 'INBOX.spam.train.ham',
      'spam': 'INBOX.spam.train.spam', }
    }

learncmd = 'fetchmail %(options)s --silent --folder %(folder)s --mda "sa-learn --%(type)s" %(server)s'
printed = None
resre = re.compile(r'Learned from (\d+) message\(s\) \((\d+) message\(s\) examined\)\.')
totalexamined = totallearned = 0

for server in mailboxes.keys():
    for mailtype in mailboxes[server].keys():
        # print 'Fetching', mailtype
        execcmd = learncmd % {
            'folder' : mailboxes[server][mailtype],
            'server' : server,
            'type'   : mailtype,
            'options': '',
            }
        # print execcmd
        cmdoutput = os.popen(execcmd).readlines()
        if not cmdoutput:
            continue
        learned = examined = 0
        for line in cmdoutput:
            result = resre.match(line)
            if result:
                learned += int(result.group(1))
                examined += int(result.group(2))
        if not printed:
            print 'sa-learn results:'
            print
            printed = 1
        print '  Type:', mailtype
        print '    Examined:', examined
        print '    Learned :', learned
###############################

I preconfigured fetchmail with my username and password like so:

###############################
poll mailserver.example.com with proto IMAP
       user 'myusername' there with password 'password' is 'myusername' here options fetchall batchlimit 100
###############################

Then, I run the script as a cron job every 15 minutes.  It downloads mail
from 'INBOX.spam.train.ham' and uses that for ham training, then repeats
the process for .spam.  This is a single-user setup, but you should be
able to extend it pretty easily.

I read my mail with Kmail and drag false positives and negatives into the
appropriate folders as I go.
-- 
Kirk Strauser



Reply to: