[go: up one dir, main page]

Menu

[0ec8fa]: / ts / spambayes  Maximize  Restore  History

Download this file

142 lines (137 with data), 4.1 kB

#!/bin/bash
# 
# Copyright (C) 2002 Laird Breyer
#  
# 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 3 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., 675 Mass Ave, Cambridge, MA 02139, USA.
# 
# Author:   Laird Breyer <laird@lbreyer.com>
#
# IMPLEMENTATION NOTES
#
# This script follows the mailcross testsuite interface
# requirements. Type man mailcross for details.
#
# The script accepts one of more commands on the command line,
# and may read STDIN and write STDOUT as follows:
#
# If $1 == "filter":
# In this case, a single email is expected on STDIN,
# and a list of category filenames is expected in $2, $3, etc.
# The script writes the category name corresponding to the 
# input email on STDOUT.
#
# If $1 == "learn":
# In this case, a standard mbox stream is expected on STDIN,
# while a suitable category file name is expected in $2. No output
# is written to STDOUT.
#
# If $1 == "clean":
# In this case, a directory is expected in $2, which is examined
# for old database information. If any old databases are found, they
# are purged or reset. No output is written to STDOUT.
#
# If $1 == "describe":
# In this case, STDIN and the command line are ignored. A single
# line is written on STDOUT, describing the filter functionality.
#
# If $1 == "bootstrap":
# In this case, the current script is copied to the directory $2,
# provided the classifier we're wrapping exists on the system.
#

SB="sb_filter.py"
TRAIN="sb_mboxtrain.py"
[ -z "$TEMPDIR" ] && TEMPDIR=/tmp

case "$1" in
    filter)
	shift
	CATEGORY=`basename $1`
	DBPATH=`dirname $1`
	$SB -f -d "${DBPATH}/spambayes.db" | grep '^X-Spambayes-Classification:' | sed -e 's/^.*spam.*/spam/' -e 's/^[^s].*$/notspam/'
	;;
    learn)
	shift
	CATEGORY=`basename $1`
	DBPATH=`dirname $1`
	if [ ! -e "${DBPATH}/spambayes.db" ]; then
	    # bug: -n switch must be last
	    $SB -d "${DBPATH}/spambayes.db" -n
	fi
	cat > "${TEMPDIR}/mbox.tmp"
	if [ "$CATEGORY" = "spam" ]; then
	    $TRAIN -d "${DBPATH}/spambayes.db" -s "${TEMPDIR}/mbox.tmp"
	else
	    $TRAIN -d "${DBPATH}/spambayes.db" -g "${TEMPDIR}/mbox.tmp"
	fi
	rm -f "${TEMPDIR}/mbox.tmp"
	;;
    clean)
	shift
	find "$1" -name "spambayes.db" -exec rm {} \;
	find "$1" -name "*.tmp" -exec rm {} \;
	;;
    describe)
	VER="(unavailable?)"
	if [ -n "`which $SB`" ] ; then
	    VER="x"
	fi
	echo "SpamBayes $VER with default settings"
	;;
    bootstrap)
	if [ -d "$2" ] ; then
            if [ -n "`which $SB`" -a -n "`which $TRAIN`" ] ; then
		echo "selecting $0"
		cp "$0" "$2"
		echo -e "\tspambayes is hard-coded for use only with exactly"
		echo -e "\ttwo categories named 'spam' and 'notspam'."
	    else
		echo "spambayes appears to be missing"
            fi
	else
	    echo "bad target directory $2"
	fi
	;;
    toe)
	ME="$0"
	shift
	TRUECAT="$1"
	DBPATH=`dirname $1`
	shift
	cat > "$TEMPDIR/mailtoe.tmp"
	VERDICT=`cat $TEMPDIR/mailtoe.tmp | $ME filter "$@"`
	if [ "x$VERDICT" != "x`basename $TRUECAT`" ] ; then
	    if [ "`basename $TRUECAT`" = "spam" ]; then
		cat "$TEMPDIR/mailtoe.tmp" | $SB -d "${DBPATH}/spambayes.db" -s > /dev/null
	    else
		cat "$TEMPDIR/mailtoe.tmp" | $SB -d "${DBPATH}/spambayes.db" -g > /dev/null
	    fi
	fi
	echo -ne "$VERDICT"
	;;

    foot)
	ME="$0"
	shift
	TRUECAT="$1"
	DBPATH=`dirname $1`
	shift
	cat > "$TEMPDIR/mailfoot.tmp"
	VERDICT=`cat "$TEMPDIR/mailfoot.tmp" | $ME filter "$@"`
	if [ "`basename $TRUECAT`" = "spam" ]; then
	    cat "$TEMPDIR/mailfoot.tmp" | $SB -d "${DBPATH}/spambayes.db" -s > /dev/null
	else
	    cat "$TEMPDIR/mailfoot.tmp" | $SB -d "${DBPATH}/spambayes.db" -g > /dev/null
	fi
	echo -ne "$VERDICT"
	;;

esac