#!/bin/ksh # # $Id: runmail,v 1.6 2004/03/12 21:59:33 vogelke Exp $ # # filter all mail messages in current directory. PATH=/bin:/usr/bin:/usr/local/bin:/opt/sfw/bin:$HOME/bin; export PATH case "$DISPLAY" in "") DISPLAY="`uname -n`:0.0" ;; *) ;; esac export DISPLAY tag=`basename $0` tmp=$tag.$RANDOM.tmp good=$tag.$RANDOM.good die () { xnote "$*" logger -t "$tag" "$*" exit 1 } start=`date` week="`/bin/date +%Yw%W`" #-------------------------------------------------------------------------- # Run alert check for high-priority mail. ls ??? > /dev/null 2>&1 || die "no files" fgrep -il -f $HOME/.whitelist-alert ??? > $tmp set X `wc -l $tmp` case "$2" in 0) ;; *) xnote "you have $2 high-priority messages" ;; esac #-------------------------------------------------------------------------- # Run whitelist check before deleting or filing anything. ls ??? > /dev/null 2>&1 || die "no files after alert" fgrep -il -f $HOME/.whitelist ??? > $tmp set X `wc -l $tmp` case "$2" in 0) echo no whitelisted messages ;; *) echo saving $2 whitelisted messages cp /dev/null $good || die "can't write $good" for file in `cat $tmp` do if formail -D 655360 $HOME/mail/msgid.cache < $file then echo $file: already seen else echo $file: not seen formail -A 'X-Spam: whitelist' < $file >> $good (echo; formail -X "" < $file) >> $HOME/mail/HEADERS.$week fi done xargs rm < $tmp rm -f $tmp if lockfile -0 -r0 -ml then cat $good >> $MAIL || die "can't append $good to $MAIL" lockfile -mu rm $good else echo "could not lock $MAIL, see $good" fi ;; esac #-------------------------------------------------------------------------- # These messages are not spam but need procmail handling. ls ??? > /dev/null 2>&1 || die "no files after first whitelist check" fgrep -il -f $HOME/.whitelist2 ??? > $tmp set X `wc -l $tmp` case "$2" in 0) echo no whitelisted messages for procmail ;; *) echo filtering $2 whitelisted messages for file in `cat $tmp` do echo $file procmail < $file rm $file done ;; esac #-------------------------------------------------------------------------- # Check for 8-bit characters in body using hibits. # Sample test went through 556 messages in ~4 seconds. ls ??? > /dev/null 2>&1 || die "no more files" echo running hibits, 10 percent means fail hibits -m10 -ips ??? | grep 'spam' | cut -f1 -d' '> $tmp set X `wc -l $tmp` case "$2" in 0) echo hibits found no 8-bit messages ;; *) echo removing $2 8-bit messages sf="$HOME/mail/spam-8bit" xargs cat < $tmp >> $sf || die "$sf: can't append" xargs rm < $tmp cp /dev/null $tmp ;; esac #-------------------------------------------------------------------------- # Check for spam using ifile. ls ??? > /dev/null 2>&1 || die "no more files" echo running ifile ifile -c -q ??? | grep 'spam' | cut -f1 -d' '> $tmp set X `wc -l $tmp` case "$2" in 0) echo ifile found no spam messages ;; *) echo removing $2 spam messages sf="$HOME/mail/spam-folder" xargs cat < $tmp >> $sf || die "$sf: can't append" xargs rm < $tmp cp /dev/null $tmp ;; esac #-------------------------------------------------------------------------- # Send remaining messages through procmail. ls ??? > /dev/null 2>&1 || die "no more files" echo running procmail for file in ??? do echo $file procmail < $file done echo start: $start echo done: `date` #frm -Sq mb exit 0