sa_teacher (version 1.11)
index
/home/ewalstad/tmp/utils/sa_teacher.py

I needed a script to automate the task of teaching Spamassassin's Bayesian
filter about user's spam and ham.  This script searches the /etc/passwd file
for users with user id's between UID_MIN and UID_MAX.  For each user entry it
finds, it will check within the user's home directory for an email directory
(default is "Maildir").  It will then look for a ".spam" and ".ham" directory
under the email directory.  Assuming it finds the spam and ham directories, it
will run sa-learn on the cur, new and tmp subdirectories to teach Spamassassin
about the spam and ham.
 
** Requires Maildir format mailboxes. **
** Be sure to set the constants in the User class before running this script. **
 
If run from the command line, the script will instantiate a Users object and
call its trainBoth() method.  This results in training Spamassassin on both
the user's spam and ham folders.
 
Here's an example crontab entry for running this script every Monday,
Wednesday and Friday at 5:00pm and an entry for sending a all users (all_users
is an alias to a list of users on the machine) a reminder
to move their email into appropriate folders for training:
----- start crontab entry -----
45 16 * * Mon,Wed,Fri echo "Spam training at 5:00pm today!  Remember to move or copy your spam and ham to the appropriate folders." | mail -s "Spam Training Reminder" all_users
0 17 * * Mon,Wed,Fri /usr/local/sa_teacher/sa_teacher.py
-----  end crontab entry  -----

 
Modules
       
commands
os

 
Classes
       
User
Users

 
class User
    Container for users info including user name, user id and
path to their home directory.  This class includes methods for
teaching Spamassassin on this user's spam and ham folders.
 
  Methods defined here:
__init__(self, delete_spam_after=1)
getUserFromPasswdFileLine(self, line)
Parses a line from the /etc/passwd file to initialize itself.
Upon success, it will return itself.  On failure it will return zero.
Failure is defined as:
 - user id is not between UID_MIN and UID_MAX
 - HOME_PATH/MAIL_DIR directory must exist
pathsToHam(self)
Returns a list of ham paths
pathsToSpam(self)
Returns a list of spam paths
trainHam(self)
Trains spamassassin on the user's ham email folders
trainSpam(self)
Trains spamassassin on the user's spam email folders

Data and other attributes defined here:
HAM_DIR = '.ham'
HOME_PATH = 5
MAIL_DIR = 'Maildir'
MAIL_DIR_SUB_DIRS = ['cur', 'new', 'tmp']
SA_LEARN = '/usr/bin/sa-learn %s --local %s'
SPAM_DIR = '.spam'
UID = 2
UID_MAX = 1999
UID_MIN = 1000
USER_NAME = 0

 
class Users
    Container of User objects
 
  Methods defined here:
__getitem__(self, key)
Makes the object work like a dictionary:
users['guido'].home_dir -> '/home/guido'
__init__(self, delete_spam_after=1)
Populates itself with User objects.  See the User class for details
on how users are found
__setitem__(self, key, value)
Makes the object work like a dictionary:
users['guido'] = User()
keys(self)
Mimics the standard Python dictionary keys() method,
returning a list of keys (user names, in this case).
trainBoth(self)
Trains spamassassin on each user's spam and ham email folders
trainHam(self)
Trains spamassassin on each user's ham email folders
trainSpam(self)
Trains spamassassin on each user's spam email folders
values(self)
Mimics the standard Python dictionary values() method,
returning a list of values (User class objects, in this case).

 
Data
        DELETE_SPAM_AFTER = 1
__version__ = '$Revision: 1.11 $'