Hello all...
I figure I've asked enough questions of this list that it's about time I
gave something back... You may not want it,but here it is anyway :)

I've written a bash script that takes will run sa-learn against the
administrator specified False-Postive and False-Negative folders.

Run this script from cron, and have your users drag n' drop emails that
get misclassified by SA to the appropriate folders.  The script will act
in 2 ways:

1.) Run it as root, and it will parse the administrator specified
USERLIST and run the internally defined autoLearn() function as each
user.
2.) Run it as an ordinary user and it will only learn from that user's
email.

I wrote it this way so that I could have a wrapper around sa-learn that
would make sure that the directories exist, create them if they don't
using maildirmake++, and not try to learn from directories with no
messages in them.

This is written to work with Courier IMAP and Maildir; I have not tried
it with anything else.

Someday I may get around to rewriting it in php and using php-imap to do
the moving around etc, but as a dirty hack this works ok.  It also
doesn't need passwords etc. in config files...

I hope this benefits someone out there... if there's enough interest,
I'll put it on my website and do a proper CVS for it.

If anyone has ideas for making it better (or suck less), let me know. 
Patches are always welcome...
-- 
Rubin Bennett <[EMAIL PROTECTED]>
RB Technologies
#!/bin/bash

# Copyright (c) 2004 by Rubin Bennett <[EMAIL PROTECTED]>
# All Rights reserved.

#This program is free software; you can redistribute it and/or
#modify it under the terms of the GNU General Public License
#as published by the Free Software Foundation; either version 2
#of the License, or (at your option) any later version.
#
#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#GNU General Public License for more details.
#
#You should have received a copy of the GNU General Public License
#along with this program; if not, write to the Free Software
#Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.


# Usage: IMAP users can move misclassified emails into the "False Negative"
# or "Flase Positive" folders, and this script will learn from them and put
# them where they belong.
# Spam will be moved to the designated Spam folder, and Ham will be moved to
# the user's Inbox.

# This script should be called by CRON or a similar scheduler.


# Requires:
#	 Maildir style email storage (i.e. Courier IMAP) and IMAP server

# Settings - tweak as necessary.
MAILDIR="/home/$USER/Maildir"
FALSE_NEG_FOLDER="Undetected Spam"
FALSE_POS_FOLDER="Not Spam"
SPAMFOLDER="Spam"

# List of users to run the autoLearn funtcion as (space separated)...
USERLIST=""



autoLearn() {
	# Checks to see if the specified FALSE_NEG_FOLDER and FALSE_POS_FOLDER exist,
	# and creates them if necessary.
	[ -d "${MAILDIR}/.${FALSE_NEG_FOLDER}" ] || /usr/bin/maildirmake++ -f "${FALSE_NEG_FOLDER}" "${MAILDIR}"
	[ -d "${MAILDIR}/.${FALSE_POS_FOLDER}" ] || /usr/bin/maildirmake++ -f "${FALSE_POS_FOLDER}" "${MAILDIR}"
	# Parses the designated Ham folder and then moves it's contents to the Inbox
	hamCount=`find "${MAILDIR}/.${FALSE_POS_FOLDER}/cur" | wc -l`
	if [ $hamCount -gt 2 ]
	then
	  echo "Learning from $hamCount HAM's"
  	  sa-learn --ham "${MAILDIR}/.${FALSE_POS_FOLDER}/cur/*"
  	  mv "${MAILDIR}/.${FALSE_POS_FOLDER}/cur/"* ${MAILDIR}/cur/
	fi
	
	# Parses the "Undetected Spam" folder and then moved it's contents to Spam
	spamCount=`find "${MAILDIR}/.${FALSE_NEG_FOLDER}/cur" | wc -l`
	if [ $spamCount -gt 2 ]
	then
	  echo "Learning from $spamCount SPAM's"
  	  sa-learn --spam "${MAILDIR}/.${FALSE_NEG_FOLDER}/cur/*"
  	  mv "${MAILDIR}/.${FALSE_NEG_FOLDER}/cur/"* ${MAILDIR}/.${SPAMFOLDER}/cur/
	fi
}

############### End of function declaration ###############
if [ "${USER}" == "root" ]
then
  for USER in $USERLIST;
  do
	echo "learning for $USER"
      	su - $USER -c sa-autolearn
  done
else
  autoLearn
fi

Attachment: signature.asc
Description: This is a digitally signed message part

Reply via email to