Kill file for mail.

Ed Falk falk at peregrine.Eng.Sun.COM
Wed Nov 7 11:18:01 AEST 1990


In article <1990Nov06.052117.26136 at ddsw1.MCS.COM>, arf at ddsw1.MCS.COM (Jack Schmidling) writes:
> 
>  
>                    HATE MAIL FROM A FOOL
>  
>  	[complaint about hate mail]

Here's a handy shell script I use all the time.  If you're running
unix, you can use this program to sort your incoming mail based on
anything in the header.  You can send your mail to different folders,
or even /dev/null.  It's also possible to send mail through a filter,
say to reformat it or something.  It would even be easy to take mail
from a particular user, and bounce it back to that person, or
Postmaster at that person's site....

Make this script executable, name it "sortmail", put it where the
mailer daemon can access it (watch those permissions), and create a
.forward file in your home directory to divert your mail through the
script.

CAVEAT:  Whenever you install any script or program to filter
your mail, test it thoroughly by sending yourself mail from other
accounts and from other machines.

This script works on Suns, I haven't tried it on other machines.

#!/bin/csh -f
#
#
#
#   create this $HOME/.forward file:
#
#	"| /home/myname/bin/sortmail myname"
#
# (exact path depends on where you put this script.  You must use a
# full path)
#
# mail is classified according to the "$list" variable.
# Classifiable mail goes to the corresponding mailbox entry.
#
# unclasifiable mail drops out at the bottom and goes to "$default"
#
# Don't forget to make sortmail executable.
#
# To classify mail according to who it was directed
# to, use the variable "${TO}" rather than the string
# "To:.*"  Look at the definition of "${TO}" to see why.
#
# leading '+' in a filename expands to $maildir/
#
# if the filename is '|', then the next item in the list is the full
# pathname of a program to run with the message as stdin.  No arguments
# may be passed, so your program will probably be a short shell script.
#
# Setting the "$vacation" variable also activates vacation for all mail sent
# directly to you.  If you set 'vacation', make sure there's a
# .vacation.msg file as described in vacation(1).  Also run "vacation -I"
# as per the vacation(1) man page.
#
#
# variables you may want to customize:
#
#	list		list of test lines and folder names.
#	user		username.  Normally provided in .forward
#	mailbox		where user's mail would normally be stored.
#	maildir		where user's mail folders are stored.
#	vacation	set to turn on vacation program.
#	default		Where unclassifiable mail goes.

set user=$1
set mailbox=/usr/spool/mail/$user
set maildir=~$user/Mail
#set vacation
set noglob

set TO="^(To|Cc|Apparently-To):.*"

# pairs of test lines and mailbox names
#
# In this example, mail from MAILER-DAEMON  goes to +bounces
#	mail from joe with the 132-character terminal
#		gets passed through a script to reformat it.
#	mail from isaac at goanna goes to /dev/null.
#	mail from realjerk at psuvm.edu goes through a script that
#		sends it to Postmaster at psuvm.edu.
#	mail to me specificly goes to my mailbox
#	mail to the scuba mailing list goes to the +scuba folder.
#	mail with a "Precedence: junk" line goes to +other.
#	everything else goes to +other.

set list=( \
	"^From:.*MAILER-DAEMON" +bounces \
	"^From:.*joe at shmoe" | /home/myname/bin/fixjoe \
	"^From:.*isaac at goanna.cs.rmit.OZ.AU" /dev/null \
	"^From:.*realjerk at psuvm.edu" /dev/null \
	"${TO}$user" $mailbox \
	"${TO}scuba" +scuba \
	"^Precedence: junk" +other \
	)

#set default=$mailbox
set default=+other

set logfile = /dev/console
#set logfile = ~$user/msglog

# end of user-customized variables




set path=(/usr/ucb /usr/bin /bin)
set HOST=`/bin/hostname`
set SCRIPT=`basename $0`
set SYNTAX="$SCRIPT username [param-file]"
set MSGTMP=/usr/tmp/sortmail-tmp_$$
set HDRTMP=/usr/tmp/sortmail-hdr_$$

rm -f $MSGTMP $HDRTMP		# Make sure temp files clear

sed -e '2,$s/^From />From /' -e '1s/>From /From /' > $MSGTMP
sed -e '/^$/,$d' < $MSGTMP > $HDRTMP

#echo ===================== >> $logfile
#cat $HDRTMP >> $logfile
#echo ===================== >> $logfile
#cat $MSGTMP >> $logfile
#echo ===================== >> $logfile

#egrep '^From:' $HDRTMP >> $logfile
#egrep '^To:' $HDRTMP >> $logfile
#egrep '^Subject:' $HDRTMP >> $logfile

if ( $status != 0 ) then
  echo ${SCRIPT}: Sorry - error while receiving your message to $user.
  echo ${SCRIPT}: /usr/tmp filesystem on $HOST probably full
  cat $MSGTMP >> $mailbox
  /bin/rm -f $MSGTMP $HDRTMP
  echo `date` $SCRIPT lost a mail message >> /dev/console
  exit 1
endif

echo "" >> $MSGTMP

while ( $#list > 1  &&  $?FILED == 0 )
  egrep -i -s "$list[1]" $HDRTMP
  if ( $status == 0 ) then
    if ( $list[2] == '|' ) then
#      echo piped through $list[3] >> $logfile
      $list[3] < $MSGTMP
    else
      set box=`echo $list[2] | sed "s.^+.$maildir/."`
      cat $MSGTMP >> $box
#      echo filed to $box >> $logfile
    endif
    if ( $status == 0 ) then
      set FILED
      break
    endif
  endif
  if ( $list[2] == '|' ) shift list
  shift list
  shift list
end



if ( $?FILED == 0 ) then
  set box=`echo $default | sed "s.^+.$maildir/."`
  cat $MSGTMP >> $box
#  echo not classified, filed to $box >> $logfile
endif



if ( $?vacation ) then
  egrep -i -s "$(TO)$user" $MSGTMP
  if ( $status == 0 ) /usr/ucb/vacation $user < $MSGTMP
endif


/bin/rm -f $MSGTMP $HDRTMP
exit 0
--
	-ed falk, sun microsystems -- sun!falk, falk at sun.com
	"What are politicians going to tell people when the
	Constitution is gone and we still have a drug problem?"
			-- William Simpson, A.C.L.U.



More information about the Alt.sources mailing list