#!/bin/sh # # Save mail from "SourceNames" in file "FileName" # # Notes: (1) Mail header lines are translated to lower case! # # ftm (9-14-93) # trm (11-29-93) # trm (09-24-94) # By default the mail is printed but not saved and not deleted # from the spool file. If -delete is on the command line, then # the mail is printed, saved, and deleted from the spool file. # SourceNames is list of sender names who's message will be saved\deleted # For example, "name1" or "name1 name2 name3" # Constraint is a word to look for; if found message is deleted and not saved # Set Constraint to "NoConstraint" or something to turn it off. # DaysToKeep is the number of files to keep in the save directory # Root is the directory path in which the files are saved # FileExt is the file name extension for the saved files, ie date.FileExt # MailBin is the directory where the mail program is located # MailSwitch is appended to the mail command line #SourceNames="owner-wx-tropl@ wx-tropl@ owner-wx-atlan@ wx-atlan@ mail-storm-epac mail-storm-atlan" # Following accepts basically all source names: SourceNames="a b c d e f g h i j k l m n o p q r s t u v w x y z" Constraint="NoConstraint" DaysToKeep=15 Root="/data1/WWW/Tropical/WXTropl" FileExt="WX" MailBin="/usr/ucb/mail" MHeaderBin="/data1/WWW/Tropical/Bin/mail_header" #MailFile="/var/mail/webmastr" MailFile="/data1/WWW/Tropical/inmail" #MailSwitch="-f /var/mail/webmastr" MailSwitch="-f /data1/WWW/Tropical/inmail" PSBin="ps -Af" #PSBin="ps -ag" #PSBin="ps -vaw" # FileName is the file which will hold the selected mail messages # Format: yymmdd.Ext (i.e., date.Ext) FileNameStore="${Root}/"`date -u +%y%m%d`".${FileExt}" FileName="${Root}/"`date -u +%y%m%d`".${FileExt}.$$" # If another ScanMail is running, wait for it to finish # In $Running, 2 means no others running since the grep $0 counts as one. # Ignore any emacs or vi processes containing $0. Used for testing/modifying. for i in 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 ; do Running=`$PSBin | grep -i $0 | grep -iv emacs | grep -civ vi | wc -l` if [ ${Running} -gt 2 ]; then echo "Sleeping 1 minute ..." sleep 60 else break fi if [ $i -eq 20 ]; then echo "$0 timed out waiting for another $0 to finish" echo ps -ag | grep -i $0 exit fi done for SourceN in ${SourceNames} ; do # awk processing of the mail headers: # Note: both fields $2 and $3 must be checked (in case $1 is a # "mail-read" indicator) SHeaderNos=`$MHeaderBin $MailFile | tr A-Z a-z | awk '\ {if (substr($2,1,length(srcname))==srcname&&index($0,skipmess)==0)\ {printf " %s",$1}\ else {if (substr($3,1,length(srcname))==srcname&&index($0,skipmess)==0)\ {printf " %s",$2}}\ }' srcname=${SourceN} skipmess=${Constraint} | tr A-Z \ | tr a-z \ ` # End of awk processing of the mail headers # TempFile will hold mail processing commands TempFile="${Root}/${FileExt}Mail.${FileExt}.$$" # # Write SourceName mail messages to file FileName # if [ -n "$SHeaderNos" ]; then echo "set hold" >> $TempFile for Num in $SHeaderNos; do echo "s ${Num} ${FileName}" >> $TempFile if [ "$1" = "-delete" ]; then echo "d ${Num}" >> $TempFile fi done if [ "$1" = "-delete" ]; then echo "quit" >> $TempFile cat $TempFile | ${MailBin} $MailSwitch > /dev/null else echo "exit" >> $TempFile cat $TempFile | ${MailBin} $MailSwitch > /dev/null fi rm -f $TempFile fi done for SourceN in $SourceNames ; do # # Process for deletion of constrained messages from SourceName: # DHeaderNos=`$MHeaderBin $MailFile| tr A-Z a-z | awk '\ {if (substr($2,1,length(srcname))==srcname&&index($0,skipmess)!=0)\ {printf " %s",$1}\ else {if (substr($3,1,length(srcname))==srcname&&index($0,skipmess)!=0)\ {printf " %s",$2}}\ }' srcname=${SourceN} skipmess=${Constraint} | tr A-Z \ | tr a-z \ ` rm -f $TempFile if [ -n "$DHeaderNos" ]; then for Num in $DHeaderNos; do echo "d" ${Num} >> $TempFile done echo "quit" >> $TempFile cat $TempFile | ${MailBin} $MailSwitch > /dev/null rm -f $TempFile fi done if [ -s ${FileName} ]; then cat ${FileName} # Clean up temporary files if [ "$1" = "-delete" ]; then cat $FileName >> $FileNameStore fi for i in `ls -1 ${Root}/*.${FileExt}.*`; do rm -f $i done fi # # Keep only the latest "DaysToKeep" *.Ext files: # Count=0 for i in `ls -t ${Root}/*.${FileExt}`; do Count=`expr $Count + 1` if [ $Count -gt $DaysToKeep ]; then rm -f $i fi done