#!/bin/sh

# spammer
# Andrew Davison (ad@ratree.psu.ac.th), August 1997

# Get a list of the message numbers of new mail by
# calling "new_msgs" and reading its $ml.mno.
#
# Process each new message in $mspool, separating out the
# messages which have a spam count ($wnum) >= $minspam.
#
# The spam messages are appended to $ml.spam and deleted
# from $mspool.
#
# Spam words are stored in $ml.spamwds.


mspool="/var/spool/mail/ad"
ml="/home/ad/coding/mailer/mail"
minspam=3

count=0                                # count of spam messages
/home/ad/coding/mailer/new_msgs        # what are the new message numbers?
for msg in `cat $ml.mno`
do
  wnum=`readmsg -n $msg |  \
         tr ' ' '\012' |    \
         grep -Fwic -f $ml.spamwds`    # count the spam words in the mesg
  if [ $wnum -ge $minspam ]
  then                                 # store spam message in $ml.spam
    readmsg -h $msg >> $ml.spam 
    echo  >> $ml.spam
    count=`expr $count + 1`
    dmsgs="$dmsgs $msg"                # record spam message number
  fi
done

if [ $count -gt 0 ]                    # there were some spams found
then
  echo "$count spam mail at `date`" 
  cp -f $mspool $ml.bak
#  echo Backed up all mail to $ml.bak 

  echo "d $dmsgs" | mail > /dev/null    # delete all spam mail from $mspool
  echo Moved spam mail to $ml.spam 
  echo -n "Press return to continue: "
  read
fi

