british-cars
[Top] [All Lists]

Some Mail Help

To: british-cars@Alliant.COM
Subject: Some Mail Help
From: mit-eddie!EBay.Sun.COM!mutchler@EDDIE.MIT.EDU (Dan Mutchler)
Date: Mon, 21 May 90 08:40:12 PDT
I've noticed that a lot of people are dropping off this alias due
to the volume of mail. I'm posting the mailsort utility that
may make this volume a little easier for some. "mailsort" sorts
incoming mail based on who the mail is sent to. It is very
useful for sending mail from aliases to mail folders that can
later be reviewed when it is a more appropriate time.

If you already have this utility, sorry to clutter your mailbox
with yet another useless message.

----------Cut Here------------------------...And at the end...
#!/bin/sh -u
#       @(#)mailsort 1.27 88/06/23 SMI
#
# Mailsort - Automatic filer of messages.
#
#
# Mailsort - Split mail input stream.  Mailsort divides your
#            input mail stream into two sub-streams based on the
#            "To:" and "Cc:" lines.  One substream is
#            handled through the normal mechanism (typically mailtool).
#            The other substream is directed to a file which can later
#            be read with "mail -f file".  The user
#            may select which mail goes to which stream.  For example
#            the user may select that mail directed to him/her by
#            name (e.g. joe@bigtime), or important aliases (gossip@sun)
#            go through the mailtool interface, while mail from 
#            less important mailing lists (e.g. tstech) go direct 
#            to a file.
#
# Nov. 16, 1988         Ross Lavender   sun!suncan!sunvcr!ross
#       added ability to file selected messages (from FOLDLIST) into specified
#       folders (FOLDFILES). All folder file names are prefixed by an underscore
#       for grouping and for easy visability.
#
#  ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#
#             I N S T A L L A T I O N    I N S T R U C T I O N S
#
#       1) Move this file to an appropriate location (e.g. your private
#          'bin' directory if you have one).
#
#       2) MAKE SURE THAT THIS SCRIPT IS SET TO BE EXECUTABLE (755)!
#          E.g.  chmod +x mailsort
#
#       3) Type "mailsort -i", which assists you in creating the
#          files ".forward" and ".mailsort" in your home directory.
#
#       4) Make sure that mailsort was installed properly by sending
#          mail to yourself!
#
#  +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#
# There are some environment variables that are loaded from a file
# in the user's home directory called ".mailsort".  The
# "mailsort -i" command will assist you in setting up a .mailsort
# file.  The variables it sets up are:
#
# NAMELIST defines the list of aliases and username variations that
# may appear in the "To:" and "Cc:" lines.  When it finds a match,
# that mail is directed to the 'normal' (mailtool) interface.
# FOLDLIST is the list of aliases and usernames (taken from NAMELIST)
# that correspond to folder files (taken from FOLDFILES) that the message 
# is optionally stored in.
# All other mail is placed directly in the file specified by
# MLIST.  The syntax of NAMELIST is an egrep-style regular
# expression.  That is, each alias is separated by a vertical
# bar "|" and the whole expression must be in quotes, for example,
# "john|jsmith|swatteam".
#
# PERSONAL is the filename to place personal messages.  Typically 
# it should be /usr/spool/mail/<you> if you want the normal mail alert
# mechanism to work.
#
# MLIST is the file to file all other messages.
#
# An example .mailsort file appears below:
#
#     NAMELIST='rfrank|roger|directors|salessupport|hqsalesmgr'
#     FOLDLIST='directors|salessupport'
#     FOLDFILES='directors|sales'
#     PERSONAL=/usr/spool/mail/rfrank
#     MLIST=/usr/u/rfrank/deferred_mail 
#
# How it works:
#
# The "mailsort -i" installation procedure creates both a ".forward"
# and a ".mailsort" file.  The ".forward" file will cause mail
# being sent to you, to automatically be scanned by the mailsort
# script, and sort your mail.  The ".forward" file will look
# something like this:
#
#       | /usr/local/bin/mailsort <the path to your home directory>
# E.G.
#       | /usr/local/bin/mailsort /usr/sunwest1/rfrank
#
# Note that if you have a very short alias in your NAMELIST, this
# very short alias may match with other addresses.  For example,
# if you have 'rr' in your NAMELIST, it will also match addresses
# in the 'To:' or 'Cc:' line if they contain 'rr' anywhere on those
# lines, such as 'To: rburridge'.
#
# Most of this was adapted from Beau James' answermail script by
# Sales Tactical Engineering (Jeff McMeekin's group).
#
# NOTE that this script is, sadly, incompatible with the
# vacation mailers and biff.  Someday....
#
#HOME=$HOME/src/mailsort
BASENAME=/usr/bin/basename
LS=/bin/ls
CAT=/bin/cat
GREP=/bin/grep
RM=/bin/rm
MV=/bin/mv
PWD=/bin/pwd
DF=/bin/df
AWK=/bin/awk
SED=/bin/sed
TOUCH=/usr/bin/touch
ECHO=/bin/echo
ERROR=/bin/echo
DATE=/bin/date
MAIL=/usr/ucb/Mail
CHMOD=/bin/chmod
HOSTNAME=/bin/hostname
TR=/usr/bin/tr
LOWERCASE="$TR '[A-Z]' '[a-z]'"

SCRIPTNAME=`$BASENAME $0`

if [ $# = 0 ]
then
        $ECHO "usage: $SCRIPTNAME [-i]"
        exit 11
fi
case "$1" in
-i )
        # Assist naive users in installing mailsort
        #
        # An example .mailsort file appears below:
        #
        #     NAMELIST='rfrank|roger|directors|salessupport|hqsalesmgr'
        #     FOLDLIST='directors|salessuppport'
        #     FOLDFILES='directors|sales'
        #     PERSONAL=/usr/spool/mail/rfrank
        #     MLIST=/usr/u/rfrank/deferred_mail 
        #
        #               | /usr/local/bin/mailsort <the path to your home 
directory>
        #       E.G.
        #               | /usr/local/bin/mailsort /usr/sunwest1/rfrank
        #
        #
        $ECHO "Beginning installation procedure for mailsort"
        $ECHO " "
        $ECHO "We need to define three things."
        $ECHO " 1) The mail addressees to be looked for"
        $ECHO " 2) The file to save the mail for these addressees in"
        $ECHO " 3) The file to save all other mail in"
        $ECHO " "
        user=$USER
        #
        #       Get the string to load into NAMELIST
        #

        inloop=""
        while [ "$inloop" != "done" ]
        do
                namelist=$user
                foldlist=""
                foldfiles=""
                $ECHO "The list of addressees already has '$user' in it."
                $ECHO " "
                $ECHO "Please specify the additional addressees to be scanned 
for."
                $ECHO "Mail for addressees not specified will be saved in the 
'deferred' file."
                $ECHO "Enter only one new address on each line."
                answer=$user
                while [ "$answer" != "no more" ]
                do
                        $ECHO -n "Please enter an additional address to watch 
for (return if done): "
                        read answer
                        case "$answer" in
                        "" )
                                # no more addresses
                                answer="no more"
                                ;;
                        * )
                                namelist="$namelist|$answer"
                                $ECHO -n "Put in folder? "
                                read response
                                case "$response" in
                                "y" | "yes" | "Y" | "YES") 
                                        if [ "$foldlist" = "" ] 
                                        then
                                                foldlist="$answer"
                                        else
                                                foldlist="$foldlist|$answer"
                                        fi
                                        $ECHO -n "Enter folder name: "
                                        read filename
                                        if [ "$foldfiles" = "" ] 
                                        then
                                                foldfiles="$filename"
                                        else
                                                foldfiles="$foldfiles|$filename"
                                        fi
                                        ;;
                                * )
                                        ;;
                                esac
                                ;;
                        esac
                done
                if [ "$namelist" = "" ]
                then
                        $ERROR 1>&2 "No namelist specified."
                        continue
                fi
                $ECHO " "
                namelist=\'$namelist\'
                foldlist=\'$foldlist\'
                foldfiles=\'$foldfiles\'
                $ECHO "NAMELIST will be defined as: $namelist."
                $ECHO "FOLDLIST will be defined as: $foldlist."
                $ECHO "FOLDFILES will be defined as: $foldfiles."
                $ECHO " "
                $ECHO -n "Is this correct (y or n): "
                read response
                case "$response" in
                        "y" | "yes" | "Y" | "YES" )
                        ;;
                * )
                        $ECHO "Okay, try again."
                        $ECHO " "
                        continue
                        ;;
                esac
                inloop="done"
        done
        $ECHO " "
        #
        # Make sure AWK really likes this namelist (the user could have typed
        # in something awk didn't like.
        #
        awk "/$namelist/" << XYZZY 1>&2 /dev/null
        the input to awk which could be anything.
XYZZY
        if [ $? != 0 ]
        then
                $ECHO "Sorry, awk didn't like that namelist.  Try again."
                $ECHO " "
                exit 6
        fi
        
        #
        # Now get the definitions for the mail files
        #
        reponse=""
        while [ "$response" != "yes" ]
        do
                $ECHO " "
                $ECHO "Please specify the full pathname of the file to keep"
                $ECHO "the mail directed to the addressees in NAMELIST (default"
                $ECHO -n "is /usr/spool/mail/$user): "
                read answer
                if [ "$answer" = "" ]
                then
                        personal=/usr/spool/mail/$user
                else
                        personal=$answer
                fi
                $ECHO "You have specified $personal"
                $ECHO -n "Is this correct (y or n): "
                read response
                case "$response" in
                "y" | "yes" | "Y" | "YES" )
                        response="yes"
                        ;;
                * )
                        continue
                        ;;
                esac
        done
        #
        # For the deferred/folder files, suggest something in their folder 
directory.
        #
        if [ -r $HOME/.mailrc ]
        then
                fpath=`$GREP "set folder" $HOME/.mailrc | $TR = \ `
                set $fpath
                if [ $# = 3 ]
                then
                        fpath=$3
                else
                        fpath=$HOME
                fi
        else
                fpath=$HOME
        fi
        #
        # Now get the definitions for the folder path
        #
        response=""
        while [ "$response" != "yes" ]
        do
                $ECHO " "
                $ECHO "Please specify the full pathname of the directory "
                $ECHO "to keep the folders specified in FOLDFILES (default"
                $ECHO -n "is $fpath): "
                read answer
                if [ "$answer" = "" ]
                then
                        flist=$fpath
                else
                        flist=$answer
                fi
                $ECHO "You have specified $flist"
                $ECHO -n "Is this correct (y or n): "
                read response
                case "$response" in
                "y" | "yes" | "Y" | "YES" )
                        response="yes"
                        ;;
                * )
                        
                        ;;
                esac
                #
                # Make sure that the foldlist directory can be 'touched'.
                #
                if [ ! -w $flist ]
                then
                        $ECHO "Error accessing $flist. Please specify another 
writable directory."
                        response=""
                        continue
                fi
        done
        fpath=$fpath/deferred
        response=""
        while [ "$response" != "yes" ]
        do
                $ECHO " "
                $ECHO "Please specify the full pathname of the file to receive"
                $ECHO -n "all remaining mail (default is $fpath): "
                read answer
                if [ "$answer" = "" ]
                then
                        mlist=$fpath
                else
                        mlist=$answer
                fi
                #
                # Make sure that the mlist file can be 'touched'.
                #
                $TOUCH $mlist
                if [ $? != 0 ]
                then
                        $ECHO "Error accessing $mlist. Please specify another 
writable file."
                        response=""
                        continue
                fi
                $CHMOD 600 $mlist       # Make private read-write only
                $ECHO " "
                $ECHO "Remaining mail will go to: $mlist"
                $ECHO -n "Is this correct (y or n): "
                read response
                if [ "$response" = "y" ]
                then
                        response="yes"
                fi
        done
        case "$0" in
        /* )
                SCRIPTPATH=$0
                ;;
        */* )
                SCRIPTPATH=`$PWD`/$0
                ;;
        * )
                $ECHO "Directory where this script lives?"
                $ECHO "Usually /usr/local/bin."
                $ECHO -n "=> "
                read SCRIPTHOME
                if [ ! -r "$SCRIPTHOME"/$SCRIPTNAME ]
                then
                        $ERROR 1>&2 $SCRIPTNAME: I do not exist there.
                        exit 1
                fi
                SCRIPTPATH="$SCRIPTHOME"/$SCRIPTNAME
                if [ "$SCRIPTPATH" != `$ECHO $SCRIPTPATH | $LOWERCASE` ]
                then
                   $ERROR 1>&2 $SCRIPTNAME: Script pathname must be lower case.
                   exit 1
                fi
                ;;
        esac
        quote=\"
        forward="$quote| $SCRIPTPATH $HOME$quote"
        $ECHO " "
        $ECHO "Input complete."
        $ECHO " "
        $ECHO ".mailsort file:"
        $ECHO " "
        $ECHO " NAMELIST=$namelist"
        $ECHO " FOLDLIST=$foldlist"
        $ECHO " FOLDFILES=$foldfiles"
        $ECHO " PERSONAL=$personal"
        $ECHO " FLIST=$flist"
        $ECHO " MLIST=$mlist"
        $ECHO " "
        $ECHO ".forward file:"
        $ECHO " "
        $ECHO " $forward"
        $ECHO " "
        $ECHO ".forward and .mailsort are about to be overwritten with the 
above."
        $ECHO -n "Is this correct ? (y or n): "
        read response
        if [ "$response" != "y" ]
        then
                $ECHO "Exiting $SCRIPTNAME."
                exit 7
        fi
        #
        # Write out new information.
        #
        #
        # Make sure that files will be readable by the sendmail daemon
        # later (it runs as root initially), even if the .forward file
        # and others are being created in a directory in an NFS filesystem.
        #
        umask 133
        $ECHO " "
        if [ -r $HOME/.forward ]
        then
                $ECHO "Saving your old $HOME/.forward file in 
$HOME/.forward.orig"
                $MV $HOME/.forward $HOME/.forward.orig
        fi
        $ECHO "$forward" > $HOME/.forward
        if [ -r $HOME/.mailsort ]
        then
                $ECHO "Saving your old $HOME/.mailsort file in 
$HOME/.mailsort.old"
                $MV $HOME/.mailsort $HOME/.mailsort.old
        fi
        $ECHO "NAMELIST=$namelist" > $HOME/.mailsort
        $ECHO "FOLDLIST=$foldlist" >> $HOME/.mailsort
        $ECHO "FOLDFILES=$foldfiles" >> $HOME/.mailsort
        $ECHO "PERSONAL=$personal" >> $HOME/.mailsort
        $ECHO "FLIST=$flist" >> $HOME/.mailsort
        $ECHO "MLIST=$mlist" >> $HOME/.mailsort
        $ECHO " "
        $ECHO "*** Mailsort installation complete ***"
        $ECHO " "
        exit 0
        ;;
-* )
        $ECHO "usage: $SCRIPTNAME [-i]"
        exit 22
        ;;
esac
#
MSGTMP=/tmp/mail-tmp_$$
#MSGTMP=mail-tmp_$$
MSGTMP1=/tmp/mail-tmp1_$$
#MSGTMP1=mail-tmp1_$$
MSGDATA=/tmp/mail-tmp-data_$$
#MSGDATA=mail-tmp-data_$$
#
$RM -f $MSGTMP $MSGTMP1 $MSGDATA        # Make sure temp files clear
$CAT > $MSGTMP                          # Save incoming message
if [ $? != 0 -o ! -s $MSGTMP ]
then
        #
        # The script will not actually die at this point from
        # losing the incoming message.  But if the message is
        # lost, we may as well stop now and not waste more time
        #
        $ERROR 1>&2 $SCRIPTNAME: Sorry - error while receiving your message.
        $ERROR 1>&2 $SCRIPTNAME: /tmp filesystem on `$HOSTNAME` probably full!
        $RM -f $MSGTMP
        exit 1
fi
#
# Load the environment file that the user specified.
#
if [ -r $1/.mailsort ]
then
        eval `$CAT $1/.mailsort`
else
        $ERROR 1>&2 $SCRIPTNAME Error loading .mailsort parameters.
        $ERROR 1>&2 $SCRIPTNAME: Sorry - error while receiving your message.
        $ERROR 1>&2 $SCRIPTNAME: /tmp filesystem on `$HOSTNAME` probably full!
        $RM -f $MSGTMP
        exit 1
fi
#echo "folders= $FOLDLIST"
#
# Parse the mail message for sender, adressee, subject, etc.
#
#       This horrible awk script attempts to reliably parse lots of
#       the mail header.  The idea is to look for the first "From:"
#       line in the message, the first "Subject:" line [if any], and
#       our own name in the "To:" or "Cc:" lines [if any].  With
#       this information we can avoid answering mail to mailing
#       lists or blind copies.
#
#       But first, protect any single-quote characters in the
#       input message in case they are in e.g. the subject
#       field and would cause problems later on when we
#       instantiate the values of the key fields.  To do so,
#       change all single-quotes (') to ('"'"').
#
#       Interpret the following command as
#        /'/s/'/'  "  '  "  '/g
$SED -e "/'/s/'/'"'"'"'"'"'"'/g" $MSGTMP > $MSGTMP1
if [ $? != 0 -o ! -s $MSGTMP1 ]
then
        $ERROR 1>&2 $SCRIPTNAME: Sorry - error while processing your message.
        $ERROR 1>&2 $SCRIPTNAME: /tmp filesystem on `$HOSTNAME` probably full!
        $RM -f $MSGTMP $MSGTMP1
        exit 1
fi
$AWK -f - $MSGTMP1 << !End!Awk!Script! > $MSGDATA
BEGIN                           { sender = ""; subject = "";
                                  to = ""; into = 0; incc = 0;
                                  inapto = 0;   # in "Apparently-To:" line
                                  }

(sender=="" && \$1=="From:")    { sender = \$2 ;
                                  printf("SENDER='");
                                  for (i=2; i < NF; i++) {
                                        printf("%s ", \$i);
                                  }
                                  printf("%s'\n", \$NF);
                                  next;
                                }

# Look for matching usernames in the "To:" line (which could be
# longer than 1 line:
(to=="" && \$1=="To:")          { into = 1 }
(into!=0 && substr(\$1,length(\$1),1)==":" && \$1!="To:")\
                                { into = 0 }
(into!=0 && /$NAMELIST/)        { to = "[You are on the list]";
                                  toline = \$0;
                                }

# Remember the Subject line:
(subject=="" && \$1=="Subject:")\
                                { subject = \$2;
                                  printf("SUBJECT='");
                                  for (i=2; i < NF; i++) {
                                        printf("%s ", \$i);
                                  }
                                  printf("%s'\n", \$NF);
                                  next;
                                }

# Look for matching usernames in the "Cc:" line (which could be
# longer than 1 line:
(to=="" && \$1=="Cc:")          { incc = 1 }
(incc!=0 && substr(\$1,length(\$1),1)==":" && \$1!="Cc:")\
                                { incc = 0 }
(incc!=0 && /$NAMELIST/)        { to = "[You are on the list]";
                                  toline = \$0;
                                }

# Look for matching usernames in the "Apparently-To:" line (which could
# be longer than 1 line:
(to=="" && \$1=="Apparently-To:")               { inapto = 1 }
(inapto!=0 && substr(\$1,length(\$1),1)==":" && \$1!="Apparently-To:")\
                                { inapto = 0 }
(inapto!=0 && /$NAMELIST/)      { to = "[You are on the list]";
                                  toline = \$0;
                                }

# Remember how many lines are in the mail header:
(NF==0 || \$1=="Status:")       { printf("ENVELOPE_END_LINE=%d\n", NR);
                                  exit }
END                             { if (subject == "") {
                                  subject="?-no subject line in your message-?";
                                     printf("SUBJECT='(%s)'\n", subject);
                                  }
                                  if (sender == "" ) {
                                     sender="No sender!?"
                                     printf("SENDER='(%s)'\n", sender);
                                  }
                                  if (to == "") {
                                        to = "[Not sent to you]";
                                  }
                                  else {
                                        list = "$FOLDLIST";
                                        files = "$FOLDFILES";
                                        n = split(list, foldlist, "|");
                                        x = split(files, foldfiles, "|");
                                        for (i=1; i<=n; i++) {
                                              if (index(toline, foldlist[i]) != 
0) {
                                                 to = foldfiles[i];
                                                 break;
                                                 }
                                              }
                                  }
                                  printf("TO='%s'\n", to);
                                }
!End!Awk!Script!
if [ $? != 0 -o ! -s $MSGDATA ]
then
        $ERROR 1>&2 $SCRIPTNAME: Sorry - error while processing your message.
        $ERROR 1>&2 $SCRIPTNAME: /tmp filesystem on `$HOSTNAME` probably full!
        $RM -f $MSGTMP $MSGTMP1 $MSGDATA
        exit 1
fi

$RM -f $MSGTMP1
#
# Instantiate the environment variable settings created by the awk script
#
. $MSGDATA
$RM $MSGDATA
echo TO = $TO
if [ "$TO" = "[Not sent to you]" ]
then
        echo Not sent to you - filing into mlist
        MAILLOG=$MLIST
elif [ "$TO" = "[You are on the list]" ]
then
        echo Sent to you - filing into personal
        MAILLOG=$PERSONAL
else
        echo Sent to you - filing into folder
        MAILLOG=$FLIST/"_$TO"
fi

# Make sure that the mail log file will not be publicly readable so that
# incoming mail remains private.
#
$TOUCH $MAILLOG                         # Ensure logfile exists
$CHMOD 600 $MAILLOG                     # Make private read-write only
#
# Change all /^From/ lines found in the mail message AFTER the
# mail-headings, to ">From" lines.
# 
$SED -e "$ENVELOPE_END_LINE,\$s/^From />From /" $MSGTMP >> $MAILLOG
$ECHO "" >> $MAILLOG 
$RM -f $MSGTMP
exit 0
------------------CUT HERE----------------

Dan Mutchler                       | ARPA/Internet:  mutchler@EBay.Sun.COM
Sun Federal System Engineer        | UUCP:           ...!sun!mutchler
--------------------------------------------------------------------------
Did I do anything wrong today, or has the world always been like this
and I've been too wrapped up in myself to notice? - Arthur Dent



<Prev in Thread] Current Thread [Next in Thread>
  • Some Mail Help, Dan Mutchler <=