#!/bin/sh
#
# this script appends some text to the end of an incoming message for
#    the mailing list, saves a copy in the archive, then sends the 
#    message out to everyone on the mailing list.
#
# Usage: mailer.sh list-name
# (e.g. mailer.sh rhl-test)
#
# List list-name will be archived in directory $ROOTDIR/list-name, and the
# file ROOTDIR/list-name.list contains the member's email addresses
#
SENDMAIL=/usr/sbin/sendmail
TEMP=/tmp/mailer.$$
TEMP2=/tmp/mailer2.$$
PERL=/u/strauss/apo/mailer/bin/mail2html
ROOTDIR=/u/strauss/apo/mailer
MACHINE=astro.princeton.edu
OWNER=owner-apo35
REQUEST=apo35-request
USERLIST1=/u/strauss/apo/mailer/apo35-general.list
USERLIST2=
umask 002

# first, copy the incoming message to the file $TEMP
cat > $TEMP
#
# figure out who it's from
#
from=`sed -n -e '/^From: /{
	s/From: //
	s/ *([^)]*)//
	s/[^>]*<\([^>]*\)>/\1/
	p
	q
	}' $TEMP`
user=`echo $from | sed -e 's/@.*//'`
#
# And see if we accept mail from there
#
#allowed="?"
allowed=0

for f in "$USERLIST1" "$USERLIST2"; do
	if [ "$f" != "" ]; then
		if grep -s "$user" $f; then
			allowed=1
		fi
	fi
done

#if [ "$allowed" = "?" -a \
#     "`echo $from | sed -n -e '/\.com*$/p' -e '/\.net$/p' -e '/\.co.[^.]*$/p'`" != "" ]; then
#	allowed=0
#fi

# See if spamassasin classified message as spam; we round hits DOWN to an int
#
spam=`sed -n -e 's/^X-Spam-Status: .*hits=\([0-9]*\).*/\1/p' $TEMP`

if [ X"$spam" != X"" -a $spam"" -ge 1 ]; then
      allowed=0
fi
#
# Did we reject that user or message?
  #
if [ "$allowed" = "0" ]; then
	cat > $TEMP2 <<EOF

	Following message for mailing list $1 from $from was refused

--------------------------------------------------------------------------------

EOF
	sed -e 's/^/> /' $TEMP >> $TEMP2
	mail -s "APO 3.5m mail from $from" $REQUEST < $TEMP2
	rm -f $TEMP $TEMP2

	exit 1
fi

# next, figure out the ID number of this message - it should be the integer
#    one larger than the current largest file number in the archive 
#    directory.  all files in the archive have names like "msg.23".
cd $ROOTDIR/$1
n=`ls msg.*.html | sed -e 's/^msg\.\([0-9]*\)\.html/\1/g' | sort -n | tail -1`
if [ X"$n" = X"" ]; then
	n=0
fi
n=`echo $n + 1 | bc`

# tack an extra bit on the end of the file, telling people about
#   the mailing list and about this message
cat >> $TEMP <<EOF

APO APO APO APO APO  Apache Point Observatory 3.5m  APO APO APO
APO
APO  This is message $n in the $1 archive. You can find
APO  the archive on http://www.astro.princeton.edu/APO/$1/INDEX.html
APO  To join/leave the list, send mail to $REQUEST@$MACHINE
APO  To post a message, mail it to $1@$MACHINE
APO
APO APO APO APO APO APO APO APO APO APO APO APO APO APO APO APO
EOF
#     pipe a copy of the msg through Robert's perl script that
#     creates a .html version of the message.  Note that
#     the PERL script assumes that the "tail" has been added to the
#     message already.
$PERL < $TEMP > msg.$n.html
# now, make a copy of the message, but change some of the mail-header
#    information so that errors don't cause floods, and so that
#    users of the "vacation" program won't reply to the group, either.
cat > $TEMP2 <<EOF
Reply-To: $from
Errors-To: $REQUEST@$MACHINE
EOF
sed -e '/^Reply-To:/d' -e '/^Sender:/d' -e '/^From /d' $TEMP >> $TEMP2
cat $TEMP2 | $SENDMAIL -f $OWNER $1-out

/bin/rm -f $TEMP $TEMP2

exit 0

