~ubuntu-branches/ubuntu/dapper/dbacl/dapper

« back to all changes in this revision

Viewing changes to ts/spambayes

  • Committer: Bazaar Package Importer
  • Author(s): Clint Adams
  • Date: 2005-05-07 12:59:53 UTC
  • Revision ID: james.westby@ubuntu.com-20050507125953-xzy2bwkb2qamglwm
Tags: upstream-1.9
ImportĀ upstreamĀ versionĀ 1.9

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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 2 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
 
 
54
SB="sb_filter.py"
 
55
TRAIN="sb_mboxtrain.py"
 
56
[ -z "$TEMPDIR" ] && TEMPDIR=/tmp
 
57
 
 
58
case "$1" in
 
59
    filter)
 
60
        shift
 
61
        CATEGORY=`basename $1`
 
62
        DBPATH=`dirname $1`
 
63
        $SB -f -d "${DBPATH}/spambayes.db" | grep '^X-Spambayes-Classification:' | sed -e 's/^.*spam.*/spam/' -e 's/^[^s].*$/notspam/'
 
64
        ;;
 
65
    learn)
 
66
        shift
 
67
        CATEGORY=`basename $1`
 
68
        DBPATH=`dirname $1`
 
69
        if [ ! -e "${DBPATH}/spambayes.db" ]; then
 
70
            # bug: -n switch must be last
 
71
            $SB -d "${DBPATH}/spambayes.db" -n
 
72
        fi
 
73
        cat > "${TEMPDIR}/mbox.tmp"
 
74
        if [ "$CATEGORY" = "spam" ]; then
 
75
            $TRAIN -d "${DBPATH}/spambayes.db" -s "${TEMPDIR}/mbox.tmp"
 
76
        else
 
77
            $TRAIN -d "${DBPATH}/spambayes.db" -g "${TEMPDIR}/mbox.tmp"
 
78
        fi
 
79
        rm -f "${TEMPDIR}/mbox.tmp"
 
80
        ;;
 
81
    clean)
 
82
        shift
 
83
        find "$1" -name "spambayes.db" -exec rm {} \;
 
84
        find "$1" -name "*.tmp" -exec rm {} \;
 
85
        ;;
 
86
    describe)
 
87
        VER="(unavailable?)"
 
88
        if [ -n "`which $SB`" ] ; then
 
89
            VER="x"
 
90
        fi
 
91
        echo "SpamBayes $VER with default settings"
 
92
        ;;
 
93
    bootstrap)
 
94
        if [ -d "$2" ] ; then
 
95
            if [ -n "`which $SB`" -a -n "`which $TRAIN`" ] ; then
 
96
                echo "selecting $0"
 
97
                cp "$0" "$2"
 
98
                echo -e "\tspambayes is hard-coded for use only with exactly"
 
99
                echo -e "\ttwo categories named 'spam' and 'notspam'."
 
100
            else
 
101
                echo "spambayes appears to be missing"
 
102
            fi
 
103
        else
 
104
            echo "bad target directory $2"
 
105
        fi
 
106
        ;;
 
107
    toe)
 
108
        ME="$0"
 
109
        shift
 
110
        TRUECAT="$1"
 
111
        DBPATH=`dirname $1`
 
112
        shift
 
113
        cat > "$TEMPDIR/mailtoe.tmp"
 
114
        VERDICT=`cat $TEMPDIR/mailtoe.tmp | $ME filter "$@"`
 
115
        if [ "x$VERDICT" != "x`basename $TRUECAT`" ] ; then
 
116
            if [ "`basename $TRUECAT`" = "spam" ]; then
 
117
                cat "$TEMPDIR/mailtoe.tmp" | $SB -d "${DBPATH}/spambayes.db" -s > /dev/null
 
118
            else
 
119
                cat "$TEMPDIR/mailtoe.tmp" | $SB -d "${DBPATH}/spambayes.db" -g > /dev/null
 
120
            fi
 
121
        fi
 
122
        echo -ne "$VERDICT"
 
123
        ;;
 
124
 
 
125
    foot)
 
126
        ME="$0"
 
127
        shift
 
128
        TRUECAT="$1"
 
129
        DBPATH=`dirname $1`
 
130
        shift
 
131
        cat > "$TEMPDIR/mailfoot.tmp"
 
132
        VERDICT=`cat "$TEMPDIR/mailfoot.tmp" | $ME filter "$@"`
 
133
        if [ "`basename $TRUECAT`" = "spam" ]; then
 
134
            cat "$TEMPDIR/mailfoot.tmp" | $SB -d "${DBPATH}/spambayes.db" -s > /dev/null
 
135
        else
 
136
            cat "$TEMPDIR/mailfoot.tmp" | $SB -d "${DBPATH}/spambayes.db" -g > /dev/null
 
137
        fi
 
138
        echo -ne "$VERDICT"
 
139
        ;;
 
140
 
 
141
esac