1#!/bin/bash
2#
3# Copyright (C) 2002 Laird Breyer
4#
5# This program is free software; you can redistribute it and/or modify
6# it under the terms of the GNU General Public License as published by
7# the Free Software Foundation; either version 3 of the License, or
8# (at your option) any later version.
9#
10# This program is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13# GNU General Public License for more details.
14#
15# You should have received a copy of the GNU General Public License
16# along with this program; if not, write to the Free Software
17# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18#
19# Author:   Laird Breyer <laird@lbreyer.com>
20#
21# IMPLEMENTATION NOTES
22#
23# This script follows the mailcross testsuite interface
24# requirements. Type man mailcross for details.
25#
26# The script accepts one of more commands on the command line,
27# and may read STDIN and write STDOUT as follows:
28#
29# If $1 == "filter":
30# In this case, a single email is expected on STDIN,
31# and a list of category filenames is expected in $2, $3, etc.
32# The script writes the category name corresponding to the
33# input email on STDOUT.
34#
35# If $1 == "learn":
36# In this case, a standard mbox stream is expected on STDIN,
37# while a suitable category file name is expected in $2. No output
38# is written to STDOUT.
39#
40# If $1 == "clean":
41# In this case, a directory is expected in $2, which is examined
42# for old database information. If any old databases are found, they
43# are purged or reset. No output is written to STDOUT.
44#
45# If $1 == "describe":
46# In this case, STDIN and the command line are ignored. A single
47# line is written on STDOUT, describing the filter functionality.
48#
49# If $1 == "bootstrap":
50# In this case, the current script is copied to the directory $2,
51# provided the classifier we're wrapping exists on the system.
52#
53# If $1 == "toe":
54# In this case, a single email is expected on STDIN,
55# and a list of category filenames is expected in $2, $3, etc.
56# The category name in $2 represents the "true" category, and $3 $4 etc
57# are a complete list of possible categories.
58# The script writes the classified category name corresponding to the
59# input email on STDOUT, and if this differs from the true category $2,
60# then, and only then, the email is learned.
61#
62# If $1 == "foot":
63# Like "toe", but the input email is always learned.
64#
65
66BFIL="bogofilter -f"
67[ -z "$TEMPDIR" ] && TEMPDIR=/tmp
68
69case "$1" in
70    filter)
71	shift
72	CATEGORY=`basename $1`
73	DBPATH=`dirname $1`
74	$BFIL -d "$DBPATH"
75	if [ "$?" = "0" ]; then
76	    echo "spam"
77	else
78	    echo "notspam"
79	fi
80	;;
81    learn)
82	shift
83	CATEGORY=`basename $1`
84	DBPATH=`dirname $1`
85	if [ "$CATEGORY" = "spam" ]; then
86	    $BFIL -M -d "$DBPATH" -s
87	else
88	    $BFIL -M -d "$DBPATH" -n
89	fi
90	;;
91    clean)
92	shift
93	find "$1" -name "wordlist.db" -exec rm {} \;
94	find "$1" -name "*.tmp" -exec rm {} \;
95	;;
96    describe)
97	VER="(unavailable?)"
98	if [ -n "`which bogofilter`" ] ; then
99	    VER=`bogofilter -V 2>&1 | head -1 | sed -e 's/.*version //'`
100	fi
101	echo "bogofilter $VER with Robinson-Fischer algorithm"
102	;;
103    bootstrap)
104	if [ -d "$2" ] ; then
105	    if [ -n "`which bogofilter`" ] ; then
106		echo "selecting $0"
107		cp "$0" "$2"
108		echo -e "\tbogofilterF is hard-coded for use only with exactly"
109		echo -e "\ttwo categories named 'spam' and 'notspam'."
110	    else
111		echo "bogofilter appears to be missing"
112            fi
113	else
114	    echo "bad target directory $2"
115	fi
116	;;
117    toe)
118	ME="$0"
119	shift
120	TRUECAT="$1"
121	DBPATH=`dirname $1`
122	shift
123	cat > "$TEMPDIR/mailtoe.tmp"
124	VERDICT=`cat "$TEMPDIR/mailtoe.tmp" | "$ME" filter "$@"`
125	if [ "x$VERDICT" != "x`basename $TRUECAT`" ] ; then
126	    if [ "x`basename $TRUECAT`" = "xspam" ]; then
127		cat "$TEMPDIR/mailtoe.tmp" | $BFIL -d "$DBPATH" -s
128	    else
129		cat "$TEMPDIR/mailtoe.tmp" | $BFIL -d "$DBPATH" -n
130	    fi
131	fi
132	echo -ne "$VERDICT"
133	;;
134    foot)
135	ME="$0"
136	shift
137	TRUECAT="$1"
138	DBPATH=`dirname $1`
139	shift
140	cat > "$TEMPDIR/mailfoot.tmp"
141	VERDICT=`cat "$TEMPDIR/mailfoot.tmp" | "$ME" filter "$@"`
142	if [ "x`basename $TRUECAT`" = "xspam" ]; then
143	    cat "$TEMPDIR/mailfoot.tmp" | $BFIL -d "$DBPATH" -s
144	else
145	    cat "$TEMPDIR/mailfoot.tmp" | $BFIL -d "$DBPATH" -n
146	fi
147	echo -ne "$VERDICT"
148	;;
149esac
150