]> 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:

make distro@fd list undelayed and pass justme
[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   LOCKFILE="$maildir/.retrain.lock"
53   trap "rm -f $LOCKFILE" 1 2 3 4 5 6 7 8 10 11 12 13 14 15
54   if ! lockfile -0 -r0 -l 3600 "$LOCKFILE" 2>/dev/null; then
55     echo "E: another retrain process is already running over that Maildir." >&2
56     exit 1
57   fi
58
59   files="$files $(find $MAILDIR/cur $MAILDIR/new -type f)"
60 fi
61
62 for msg in $files; do
63   $NICE ${0%/*}/train $ARG "$msg" | $RESUBMIT || ret=$?
64   case "${ret:-0}" in
65     0|75) :;;
66     *)
67       echo "E: resubmission returned with exit code $ret" >&2
68       continue
69       ;;
70   esac
71   rm -f "$msg"
72   sleep $PAUSETIME
73 done
74
75 if [ -n "${LOCKFILE:-}" ]; then
76   rm -f "$LOCKFILE"
77   trap - 1 2 3 4 5 6 7 8 10 11 12 13 14 15
78 fi
79
80 exit 0