home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 2 / 2033 < prev    next >
Internet Message Format  |  1990-12-28  |  6KB

  1. From: falk@peregrine.Eng.Sun.COM (Ed Falk)
  2. Newsgroups: alt.sources
  3. Subject: Kill file for mail.
  4. Message-ID: <2258@exodus.Eng.Sun.COM>
  5. Date: 7 Nov 90 00:18:01 GMT
  6.  
  7. In article <1990Nov06.052117.26136@ddsw1.MCS.COM>, arf@ddsw1.MCS.COM (Jack Schmidling) writes:
  8. >  
  9. >                    HATE MAIL FROM A FOOL
  10. >  
  11. >      [complaint about hate mail]
  12.  
  13. Here's a handy shell script I use all the time.  If you're running
  14. unix, you can use this program to sort your incoming mail based on
  15. anything in the header.  You can send your mail to different folders,
  16. or even /dev/null.  It's also possible to send mail through a filter,
  17. say to reformat it or something.  It would even be easy to take mail
  18. from a particular user, and bounce it back to that person, or
  19. Postmaster at that person's site....
  20.  
  21. Make this script executable, name it "sortmail", put it where the
  22. mailer daemon can access it (watch those permissions), and create a
  23. .forward file in your home directory to divert your mail through the
  24. script.
  25.  
  26. CAVEAT:  Whenever you install any script or program to filter
  27. your mail, test it thoroughly by sending yourself mail from other
  28. accounts and from other machines.
  29.  
  30. This script works on Suns, I haven't tried it on other machines.
  31.  
  32. #!/bin/csh -f
  33. #
  34. #
  35. #
  36. #   create this $HOME/.forward file:
  37. #
  38. #    "| /home/myname/bin/sortmail myname"
  39. #
  40. # (exact path depends on where you put this script.  You must use a
  41. # full path)
  42. #
  43. # mail is classified according to the "$list" variable.
  44. # Classifiable mail goes to the corresponding mailbox entry.
  45. #
  46. # unclasifiable mail drops out at the bottom and goes to "$default"
  47. #
  48. # Don't forget to make sortmail executable.
  49. #
  50. # To classify mail according to who it was directed
  51. # to, use the variable "${TO}" rather than the string
  52. # "To:.*"  Look at the definition of "${TO}" to see why.
  53. #
  54. # leading '+' in a filename expands to $maildir/
  55. #
  56. # if the filename is '|', then the next item in the list is the full
  57. # pathname of a program to run with the message as stdin.  No arguments
  58. # may be passed, so your program will probably be a short shell script.
  59. #
  60. # Setting the "$vacation" variable also activates vacation for all mail sent
  61. # directly to you.  If you set 'vacation', make sure there's a
  62. # .vacation.msg file as described in vacation(1).  Also run "vacation -I"
  63. # as per the vacation(1) man page.
  64. #
  65. #
  66. # variables you may want to customize:
  67. #
  68. #    list        list of test lines and folder names.
  69. #    user        username.  Normally provided in .forward
  70. #    mailbox        where user's mail would normally be stored.
  71. #    maildir        where user's mail folders are stored.
  72. #    vacation    set to turn on vacation program.
  73. #    default        Where unclassifiable mail goes.
  74.  
  75. set user=$1
  76. set mailbox=/usr/spool/mail/$user
  77. set maildir=~$user/Mail
  78. #set vacation
  79. set noglob
  80.  
  81. set TO="^(To|Cc|Apparently-To):.*"
  82.  
  83. # pairs of test lines and mailbox names
  84. #
  85. # In this example, mail from MAILER-DAEMON  goes to +bounces
  86. #    mail from joe with the 132-character terminal
  87. #        gets passed through a script to reformat it.
  88. #    mail from isaac@goanna goes to /dev/null.
  89. #    mail from realjerk@psuvm.edu goes through a script that
  90. #        sends it to Postmaster@psuvm.edu.
  91. #    mail to me specificly goes to my mailbox
  92. #    mail to the scuba mailing list goes to the +scuba folder.
  93. #    mail with a "Precedence: junk" line goes to +other.
  94. #    everything else goes to +other.
  95.  
  96. set list=( \
  97.     "^From:.*MAILER-DAEMON" +bounces \
  98.     "^From:.*joe@shmoe" | /home/myname/bin/fixjoe \
  99.     "^From:.*isaac@goanna.cs.rmit.OZ.AU" /dev/null \
  100.     "^From:.*realjerk@psuvm.edu" /dev/null \
  101.     "${TO}$user" $mailbox \
  102.     "${TO}scuba" +scuba \
  103.     "^Precedence: junk" +other \
  104.     )
  105.  
  106. #set default=$mailbox
  107. set default=+other
  108.  
  109. set logfile = /dev/console
  110. #set logfile = ~$user/msglog
  111.  
  112. # end of user-customized variables
  113.  
  114.  
  115.  
  116.  
  117. set path=(/usr/ucb /usr/bin /bin)
  118. set HOST=`/bin/hostname`
  119. set SCRIPT=`basename $0`
  120. set SYNTAX="$SCRIPT username [param-file]"
  121. set MSGTMP=/usr/tmp/sortmail-tmp_$$
  122. set HDRTMP=/usr/tmp/sortmail-hdr_$$
  123.  
  124. rm -f $MSGTMP $HDRTMP        # Make sure temp files clear
  125.  
  126. sed -e '2,$s/^From />From /' -e '1s/>From /From /' > $MSGTMP
  127. sed -e '/^$/,$d' < $MSGTMP > $HDRTMP
  128.  
  129. #echo ===================== >> $logfile
  130. #cat $HDRTMP >> $logfile
  131. #echo ===================== >> $logfile
  132. #cat $MSGTMP >> $logfile
  133. #echo ===================== >> $logfile
  134.  
  135. #egrep '^From:' $HDRTMP >> $logfile
  136. #egrep '^To:' $HDRTMP >> $logfile
  137. #egrep '^Subject:' $HDRTMP >> $logfile
  138.  
  139. if ( $status != 0 ) then
  140.   echo ${SCRIPT}: Sorry - error while receiving your message to $user.
  141.   echo ${SCRIPT}: /usr/tmp filesystem on $HOST probably full
  142.   cat $MSGTMP >> $mailbox
  143.   /bin/rm -f $MSGTMP $HDRTMP
  144.   echo `date` $SCRIPT lost a mail message >> /dev/console
  145.   exit 1
  146. endif
  147.  
  148. echo "" >> $MSGTMP
  149.  
  150. while ( $#list > 1  &&  $?FILED == 0 )
  151.   egrep -i -s "$list[1]" $HDRTMP
  152.   if ( $status == 0 ) then
  153.     if ( $list[2] == '|' ) then
  154. #      echo piped through $list[3] >> $logfile
  155.       $list[3] < $MSGTMP
  156.     else
  157.       set box=`echo $list[2] | sed "s.^+.$maildir/."`
  158.       cat $MSGTMP >> $box
  159. #      echo filed to $box >> $logfile
  160.     endif
  161.     if ( $status == 0 ) then
  162.       set FILED
  163.       break
  164.     endif
  165.   endif
  166.   if ( $list[2] == '|' ) shift list
  167.   shift list
  168.   shift list
  169. end
  170.  
  171.  
  172.  
  173. if ( $?FILED == 0 ) then
  174.   set box=`echo $default | sed "s.^+.$maildir/."`
  175.   cat $MSGTMP >> $box
  176. #  echo not classified, filed to $box >> $logfile
  177. endif
  178.  
  179.  
  180.  
  181. if ( $?vacation ) then
  182.   egrep -i -s "$(TO)$user" $MSGTMP
  183.   if ( $status == 0 ) /usr/ucb/vacation $user < $MSGTMP
  184. endif
  185.  
  186.  
  187. /bin/rm -f $MSGTMP $HDRTMP
  188. exit 0
  189. --
  190.     -ed falk, sun microsystems -- sun!falk, falk@sun.com
  191.     "What are politicians going to tell people when the
  192.     Constitution is gone and we still have a drug problem?"
  193.             -- William Simpson, A.C.L.U.
  194.