Screening mail

Gary Weimer 588-0953 weimer at garden.kodak.COM
Tue Feb 26 08:01:33 AEST 1991


In article <1991Feb21.164534.5413 at ux1.cso.uiuc.edu>,
ssaroff at newton.ncsa.uiuc.edu (Stephen Saroff) writes:
|> Is there an easy way to screen in coming mail?  I would like to take all
|> messages with a given subject, and automatically resend them to a
|> mailing list. 

I don't remember where I posted this last week, so I'll repost it here.
This works in SunOS:

=================== Start .forward file ============================
"|<path>/mailsort.csh <login id> ~/MAIL"
==================== End .forward file =============================

for debugging, you may wish to use:

=================== Start .forward file ============================
<login id>,"|<path>/mailsort.csh <login id> ~/MAIL"
==================== End .forward file =============================

=================== Start mailsort.csh file ============================
#!/bin/csh -fb

# login id of person using this script
# environment vars take precidence over defaults
if (!($?USER)) set USER=weimer

# mail directory of person using this script
# environment vars take precidence over defaults
#    NOTE: USER will not be set when prog called by mail daemon
if (!($?MAILDIR)) then
  if (-e ~$USER/MAIL) then
    set MAILDIR=~$USER/MAIL
  else
    set MAILDIR=~$USER/mail
  endif
endif

###################################################################
# NOTE: mail message will be saved in the file $MSG,
#       the mail header only will be saved in the file $HEADER
#       the mail body only will be saved in the file $BODY
###################################################################

# mail header lines to skip when saving message
#    $MSG will still have these, $HEADER will not
set HDRSKIP='$1=="Received:" || $1=="id" || $1=="Message-Id:"'
#set HDRSKIP=""

# arguments take precidence over defaults and environmen vars
#    NOTE: this is not important part of code logic (you can skip it)
set PARAMS=($*)
if ($#PARAMS > 1) then
  set USER=$PARAMS[1]
  set MAILDIR=$PARAMS[2]
  set TMP=`echo $MAILDIR|awk '{print index($1,"//")}'`
  if ($TMP == 1) set MAILDIR=~$USER`echo $MAILDIR|awk '{print substr($1,2)}'`
else if ("$PARAMS" != "") then
  set $USER=$PARAMS
  if (-e ~$USER/MAIL) then
    set MAILDIR=~$USER/MAIL
  else
    set MAILDIR=~$USER/mail
  endif
endif

#######################################################################
#
# generic setup stuff
#
#######################################################################
# date in case I want it for file names
set DATE    = `date +"%y-%m-%d"`

# awk script for separating mail header from mail body
set PARSER=('{if (b) {print $0>>bf;next}};{if (NF==0) {b=1;next}};{if
(""'$HDRSKIP'){next}};{print $0>>hf};END{print>>bf;print>>hf}')

# files for msg, header, & body
set MSG="/tmp/$USER.mail.$$"
set HEADER=$MSG.head
set BODY=$MSG.body
/bin/rm -f $HEADER $BODY $MSG    # just in case we've found a duplicate name...

# our mail server uses /usr/spool/mail, other machines mount this in /var
if (-e /usr/spool/mail) then
  set MAILBOX=/usr/spool/mail/$USER
else
  set MAILBOX=/var/spool/mail/$USER
endif

# stdin is the mail message being sent, put it in $MSG
cat > $MSG
# separate header and body
awk "$PARSER" hf=$HEADER bf=$BODY $MSG

while (1)	#we really only go through loop once
                # this allows us to use break instead of goto

#######################################################################
#
# BEGIN SORTING
#
#######################################################################

#######################################################################
# if you do not want a message to be sent to yourself, and you do not
# want it to match any other conditions, 'break' after sending message--
# DO NOT 'exit'--temporary files will not get removed
#######################################################################
# when concatinating to a mailbox, you should always add a blank line
# to the end of what you are concatinating (hence all the 'echo ""'s
# below)
#######################################################################

#
# extra stuff just for me :-)
#
# intro to prepend to forwarded msg's (stating this has been auto-forwarded)
set AUTOHDR=$MAILDIR/auto.header
# flag for who a copy was sent to (put at end my copy of files auto-forwarded)
set COPYHDR=$MAILDIR/copy.header

#
# template
#
#set SUBJECT=`grep '^Subject:.<subj to check for>' $HEADER`
#set TO=`grep '^To:.<id you expect forwarded mail from>' $HEADER`
#set OTHER=`grep '<text to look for in body of msg>' $BODY`
#if ("$<SUBJECT, TO, or OTHER>" != "") then # we found a match
#    # do any other calculations/parsing here
#
#    # save to a file
#    (cat $<MSG, HEADER, and/or BODY>; echo "") >> $MAILDIR/<file>
#    # put in your own mailbox (as if we never did this script)
#    (cat $MSG; echo "") >> $MAILBOX
#    # forward to someone else
#    cat $<MSG, HEADER, and/or BODY> | /usr/ucb/Mail -s "<subject>" <userid>
#
#    # run any programs, edit files, etc. that you want to here
#    break
#endif

#
# Budtool Report
#
#    simply write $MSG to appropriate file
#
set SUBJECT=`grep '^Subject:.Budtool.Report$' $HEADER`
if ("$SUBJECT" != "") then
    set MACH=`grep '^rsh' $BODY | awk '{print $2; exit}'`
    (cat $MSG; echo "") >> $MAILDIR/backups/$MACH/$DATE
    break
endif

#
# warehouse submissions -- cc: edj
#
set SUBJECT=`grep '^Subject:.wh.submission$' $HEADER`
if ("$SUBJECT" != "") then
    (cat $AUTOHDR $HEADER $BODY) | /usr/ucb/Mail -s "wh submission" edj
    (cat $MSG $COPYHDR;echo "edj";echo "") >> $MAILBOX
    break
endif

#
# warehouse requests -- cc: cwhite
#
set SUBJECT=`grep '^Subject:.*\<[rR]equest' $HEADER`
set TO=`egrep '^To: wareh' $HEADER`
if ("$SUBJECT" != "" && "$TO" != "") then
    (cat $AUTOHDR $HEADER $BODY) | /usr/ucb/Mail -s "wh request" cwhite
    (cat $MSG $COPYHDR;echo "cwhite";echo "") >> $MAILBOX
    break
endif

#
# test (see if any of this works...)
#
#set SUBJECT=`grep '^Subject:.test$' $HEADER`
#if ("$SUBJECT" != "" ) then
#    (cat $HEADER; echo "test successful"; echo "") >> $MAILBOX
#    break
#endif

#######################################################################
#
# No matches--Put in mailbox
#
#######################################################################

cat $MSG >> $MAILBOX
echo "" >> $MAILBOX
break  #don't repeat while loop
end
/bin/rm -f $HEADER $BODY $MSG
exit
==================== End mailsort.csh file =============================

e-mail me if you have questions--I don't always have time to read this
newsgroup.

weimer at ssd.kodak.com ( Gary Weimer )



More information about the Comp.unix.questions mailing list