]> git.madduck.net Git - etc/mailfilter.git/blob - bin/retrain

madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

All patches and comments are welcome. Please squash your changes to logical commits before using git-format-patch and git-send-email to patches@git.madduck.net. If you'd read over the Git project's submission guidelines and adhered to them, I'd be especially grateful.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

allow certain senders to pass undelayed, and add penny
[etc/mailfilter.git] / bin / retrain
1 #!/bin/sh
2 set -eu
3
4 NICE='/usr/bin/nice -20'
5 NICE=''
6
7 RESUBMIT="$NICE $HOME/.etc/mailfilter/bin/resubmit"
8
9 PAUSETIME=10
10
11 HAM=0
12 SPAM=0
13
14 maildir=
15 files=
16 for i in "$@"; do
17   case "$i" in
18     --ham) HAM=1;;
19     --spam) SPAM=1;;
20     *)
21       if [ -f "$i" ] && [ -r "$i" ]; then
22         files="$files $i"
23       elif [ -d "$i"/cur ] && [ -d "$i"/new ] && [ -d "$i"/tmp ]; then
24         if [ -n "$maildir" ]; then
25           echo "E: Maildir already specified: $i" >&2
26           exit 1
27         fi
28         maildir="$i"
29       else
30         echo "E: unknown argument: $i" >&2
31         exit 255
32       fi
33       ;;
34   esac
35 done
36
37 ARG=
38 case "$HAM/$SPAM" in
39   0/0)
40     echo "E: you need to specify either --ham or --spam." >&2
41     exit 255
42     ;;
43   1/1)
44     echo "E: you cannot specify both --ham and --spam." >&2
45     exit 255
46     ;;
47   0/1) ARG=--spam;;
48   1/0) ARG=--ham;;
49 esac
50
51 if [ -n "$maildir" ]; then
52   ME="${0##*/}"
53   LOCKFILE="$maildir/.$ME.lock"
54   trap "rm -f $LOCKFILE" 1 2 3 4 5 6 7 8 10 11 12 13 14 15
55   if ! lockfile -0 -r0 -l 3600 "$LOCKFILE" 2>/dev/null; then
56     echo "E: another $ME process is already running over that Maildir." >&2
57     exit 1
58   fi
59
60   files="$files $(find $MAILDIR/cur $MAILDIR/new -type f)"
61 fi
62
63 for msg in $files; do
64   $NICE ${0%/*}/train $ARG "$msg" | $RESUBMIT || ret=$?
65   case "${ret:-0}" in
66     0|75) :;;
67     *)
68       echo "E: resubmission returned with exit code $ret" >&2
69       continue
70       ;;
71   esac
72   rm -f "$msg"
73   sleep $PAUSETIME
74 done
75
76 if [ -n "${LOCKFILE:-}" ]; then
77   rm -f "$LOCKFILE"
78   trap - 1 2 3 4 5 6 7 8 10 11 12 13 14 15
79 fi
80
81 exit 0