#!/bin/sh # # mailout [-v] [file] -- script to filter outgoing mail # # Tack a signature to the end of outgoing mail, and add a face if # available to the outgoing mail if no face is already present. # # Edit to suit. # # NOTE: this program is called by either fake-sendmail or fake-mhsend to # filter outgoing mail. See those programs on how to enable it for use. # # Notes on Signatures. # # By default a random signiture is appended to the end of the message. # This signature is generated by another script ``signed'', which inturn # uses a third script ``message''. If however in the last non-blank # line of the mail is one of the following actions, that line is removed # and the following performed. # # @@ No signiture added (line removed) # @file pass `file' to the signiture generator for a # specific signiture. # # NOTE: the reason for the last three lines, is that xmail appends an # extra blank line on the end of the mail! # # Anthony Thyssen 19 March 1998 # SIGN=$HOME/.signature # to disable signature set to /dev/null FACE=$HOME/.face # to disable face set to a non-existing SIGNEXE=$HOME/bin/scripts/signed # random signiture generator (overrides SIGN) # Files used to hole the two parts of the mail being filtered # The seperator file is to allow mail to either use the blank line # of the normal UNIX mail system or the 8 dashes line of the MH mail # system. # MAIL_HEAD=/tmp/outmail_h.$$ # mail header temporary file MAIL_SEPR=/tmp/outmail_s.$$ # seperator between head and body MAIL_BODY=/tmp/outmail_b.$$ # mail footer temporary file trap 'echo >&2 "Interupt -- mail unsent"; rm -f $MAIL_HEAD $MAIL_SEPR $MAIL_BODY; exit 1' 1 2 3 15 # --- Read Arguments and Mail --- signexe_args= VERBOSE= # Check for the sendmail verbose flag # loop=true # while [ $# -gt 0 -a "$loop" ]; do case "$1" in --) shift; loop= ;; -v) echo >&2 "OutMail Script -- Modifing Outgoing Mail" VERBOSE=true; signexe_args=$i; shift ;; -*) echo >&2 "ERROR: Outmail - Unknown option \"$1\" -- ignored" shift ;; *) loop= ;; esac # done # ---- Split up mail ---- [ "$VERBOSE" ] && echo >&2 "Spliting Mail into header and body parts..." # Sed Program Synopsis:- # head: record header until seperator found # sep: record seperator line between header and body # blank: gobble up any extra blank lines # body: record body until end of file. sed -n ': head /^$/ b sep /^--/ b sep w '$MAIL_HEAD' n; b head :sep w '$MAIL_SEPR' n : blank /^$/ !b body n; b blank : body p; n; b body ' ${1+"$@"} > $MAIL_BODY # ---- Check mail is not an OLD message ---- if grep -i '^From:' $MAIL_HEAD >/dev/null; then [ "$VERBOSE" ] && echo >&2 "From found in Header! Its an OLD Mail Message -- ABORT" cat $MAIL_HEAD $MAIL_SEPR $MAIL_BODY rm -f $MAIL_HEAD $MAIL_SEPR $MAIL_BODY exit 0 fi # --- Skip header edit if it isn't a header! ---- grep -i '^To:' $MAIL_HEAD >/dev/null if [ $? -eq 1 ]; then # The header part has no `To' line! # It is NOT a header and file must only contain a mail body! # Merge all files together to form a correct mail body! # and skipping header actions [ "$VERBOSE" ] && echo >&2 "WARNING: No header was found! -- Assuming only mail body" cat $MAIL_SEPR $MAIL_BODY >>$MAIL_HEAD # concatenate mv $MAIL_HEAD $MAIL_BODY # make it the mail body : > $MAIL_SEPR # clear the other files : > $MAIL_HEAD else # ------------------- Edit Mail Header ------------------ # Add the "face" if available and not already in the mail message grep -i '^Reply-To:' $MAIL_HEAD >/dev/null if [ $? -eq 1 ]; then [ "$VERBOSE" ] && echo >&2 "Adding Reply-To in Header" echo "Reply-To: Anthony Thyssen " >>$MAIL_HEAD fi grep -i '^X-URL:' $MAIL_HEAD >/dev/null if [ $? -eq 1 ]; then [ "$VERBOSE" ] && echo >&2 "Adding URL to Header" echo "X-URL: http://www.cit.gu.edu.au/~anthony/" >>$MAIL_HEAD else [ "$VERBOSE" ] && echo >&2 "URL in header, no need to add one" fi # # Image face URL # # see http://www.aracnet.com/~ovidiu/exmh/ # grep -i '^X-Image-URL:' $MAIL_HEAD >/dev/null # if [ $? -eq 1 ]; then # [ "$VERBOSE" ] && echo >&2 "Adding Image URL to Header" # echo "X-Image-Url: http://www.cit.gu.edu.au/~anthony/images/anthony.gif" # else # [ "$VERBOSE" ] && echo >&2 "Image URL in header, no need to add one" # fi # Face included in the mail message # see http://www.cs.indiana.edu/picons/ftp/faq.html grep -i '^X-Face:' $MAIL_HEAD >/dev/null if [ $? -eq 1 ] && [ "$FACE" ] && [ -r "$FACE" ]; then [ "$VERBOSE" ] && echo >&2 "Adding Face to Header" echo "X-Face: `cat $FACE`" >>$MAIL_HEAD else [ "$VERBOSE" ] && echo >&2 "Face in header, no need to add one" fi fi # ------------------- Edit Mail Body ------------------ # look for a @ line as the last non blank line in the last 3 lines of body # ---- Figure out signature action ---- [ "$VERBOSE" ] && echo >&2 "Collecting and removing any signiture action" action=`tail -3 $MAIL_BODY | sed -n 's/^@//p'` if [ "$action" ]; then # remove the action from mail body ( echo '$g/^$/d' # remove any blank lines at bottom echo '$g/^$/d'; echo '$g/^$/d'; echo '$g/^$/d' echo '$g/^@/d' # remove action line echo 'w' # write file again ) | ed -s $MAIL_BODY # remove the action from mail body case "$action" in # figure out desired action '') # Error -- no action -- random signature ;; @) # No signiture wanted SIGN=''; SIGNEXE='' ;; *) # specific generated signiture signexe_args="$signexe_args $action" ;; esac elif grep 'Anthony Thyssen .*http:' $MAIL_BODY >/dev/null; then # NO action given and signature is already present -- don't re-sign SIGN=''; SIGNEXE='' fi [ "$VERBOSE" ] && echo >&2 "Signiture Action required : \"$action\"" # --- Edit the Mail Body --- # Append the appropraite signiture as required if [ "$SIGNEXE" ] && [ -x "$SIGNEXE" ]; then [ "$VERBOSE" ] && echo >&2 "Appending Signiture from \"$SIGNEXE\"" $SIGNEXE ${VERBOSE:+"-v"} $signexe_args >> $MAIL_BODY elif [ "$SIGN" ] && [ -r "$SIGN" ]; then [ "$VERBOSE" ] && echo >&2 "Appending Signiture file \"$SIGN\"" cat $SIGN >> $MAIL_BODY else [ "$VERBOSE" ] && echo >&2 "No signiture to be appended" : # No signiture found to append fi # ------- Finish up ------- [ "$VERBOSE" ] && echo >&2 "Outputing Filtered Mail" cat $MAIL_HEAD $MAIL_SEPR $MAIL_BODY rm -f $MAIL_HEAD $MAIL_SEPR $MAIL_BODY # ---------------------------