#!/bin/sh
#
# $Header: /local/src/zmailer/utils/RCS/distribute,v 1.4 1994/07/20 06:51:15 ken Exp $
#
# Distribute stdin (presumed to be a digest or direct-mail article from
# a mailing list) according to the following flags
#
# -n newsgroup		post article to specified newsgroup.
# -a file		append to the file specified.
# -m people		remail to people, people may be a filename
#			in which case the contents will be assumed
#			to be a list of recipients, one to a line.
#			note that in the case of a file all recipients
#			will appear on the headers, and therefore
#			an alias is best used for large lists.
#			If a file named "people" is found in the PEOPLE
#			directory, then that is used.
# -d directory		store into a file named by volume and
#			issue under "directory".  Useful for digests.
# -am name		shorthand for '-a name -m $MAILVAR/lists/name'
# -dm name		shorthand for '-d name -m $MAILVAR/lists/name'
#
# Notes:
# - If invoked from sendmail, all arguments will be lowercase only!
#
# Rayan Zachariassen - rayan@cs.toronto.edu
# Jean-Francois Lamy - lamy@cs.toronto.edu

#exec >> /tmp/dist.trace 2>&1
#set -x
PATH=/usr/ucb:/usr/bin:/bin:/cs/bin:/local/bin
export PATH
umask 022

USAGE='distribute [-n newsgroup] [-a archivefile] [-d archivedir] [-m mailto]'

# get the zmailer configuration
. /etc/zmailer.conf
cd $MAILVAR

# could be the same site, though not necessarily
thissite=cs.toronto.edu
newsserver=news-server.cs.toronto.edu
# machine to uux the article to.
newsuucp=utjarvis
# program to use for logging each message -- gets newsgroup and messageid
# as arguments
logger="logger -p local4.notice -t"

# we keep the archives under our anonymous FTP directory so other people
# can get at them.
archdir=$MAILVAR/list-archives

# this person receives bounces and such like.  Make it an alias as it
# will appear on the sender: line of messages.
admin=list-admin

PEOPLE=$MAILVAR/lists

Mail=/usr/ucb/Mail
sendmail=/usr/lib/sendmail
# This program should be suid daemon, so that sensitive files can be
# protected.  Only needed if you have an old flakey sendmail, really.
blankcat=$MAILBIN/bin/appendfile

#
# Parse arguments
#
ARTICLE=/tmp/dist$$
newsgroup=""
newsgroups=""
file=""
directory=""
mailto=""
case $# in
0|1)	echo Usage: $USAGE
	exit 1 ;;
esac
state=x
for i in $@
do
	case "$i" in
	-*)	state=$i ;;
	*)	case $state in
		-n)	newsgroup="$i"
			newsgroups=${newsgroup}${newsgroups:+",$newsgroups"}
			distribution="`expr $newsgroup : '\([a-z]*\)\.*'`"
			;;
		-n*)	newsgroup="$i"
			newsgroups=${newsgroup}${newsgroups:+",$newsgroups"}
			distribution="`expr $state : '-n\(.*\)'`"
			;;
		-a)	file="$i" ;;
		-d)	directory="$i" ;;
		-m)	if [ -f $PEOPLE/$i ]; then
				mailto="$PEOPLE/$i"
			else
				mailto="$i"
			fi
			;;
		-am)	file="$i"
			mailto="$PEOPLE/$i" ;;
		-dm)	directory="$i"
			mailto="$PEOPLE/$i" ;;
		x)	echo Usage: $USAGE
			exit 2 ;;
		esac
		state=x ;;
	esac
done
#
case "${newsgroup}${file}${directory}${mailto}" in
# zmailer adds a spurious blank line at the bottom. sigh.
# delete such a line if one is found.  one should revert to using
#  cat - > $ARTICLE
# after the bug is fixed!!!
?*)	sed -e ':a
$b e
n
b a
:e
/^$/d' > $ARTICLE ;;
*)	exec $Mail -s "No options to $MAILBIN/bin/distribute" list-admin
	exec /bin/mail list-admin
	exit 4 ;;
esac
#cp $ARTICLE /tmp/distart

case $file in
?*)	$blankcat -lists/"${file}" $ARTICLE ;;
esac
case "$directory" in
?*)	eval `sed 40q $ARTICLE \
	| fgrep -i digest \
	| sed -n \
	-e 's/.*Vol[^0-9]*\([0-9][0-9]*\)[^0-9][^0-9]*\([0-9][0-9]*\)[^0-9]*/VOL=\1 NUM=\2/p'`
	$blankcat "-$directory/V${VOL}.${NUM}" $ARTICLE ;;
*)	directory="$archdir" ;;
esac

# extract return path from article.
ed - $ARTICLE <<EOF
1s/From \\([^ ]*\\) .*/\\1/
1w $ARTICLE.from
1d
w
q
EOF

# forwarding via mail
if [ -s $ARTICLE.from ]; then
	path="`cat $ARTICLE.from`"
	case "$mailto" in
	*/*)	( cd $directory ;
		if [ -s $mailto ]; then
			$sendmail -f "$path" `cat $mailto` < $ARTICLE
		fi
		)
		;;
	?*)	$sendmail -f "$path" $mailto < $ARTICLE
		;;
	esac
else
	path="$newsserver!news"
fi

# forwarding to newsgroups
case "$newsgroup" in
?*)	case "$path" in
	*!*!*)	path=`expr "$path" : '.*!\([^!]*![^!]*\)$'`;;
	esac
	case "$path" in
	*.*!*)	;;
	*!*)	path=`echo "$path" | sed 's/!/.uucp&/'` ;;
	esac
	# Stick our name on.  Note that this means the article will not
	# get fed back to us, which shouldn't be a problem.
	path="$thissite!$path"
	lines=`sed -e '1,/^[ 	]*$/d' $ARTICLE|wc -l`
# All this does is massage the headers so they look like what news
# software expects.  To:, Cc: and Resent-*: headers are masked.
# So are Newsgroups: headers, in case we're gatewaying from a 
# mailing list that is itself a gateway!
# Reply-To: is turned into references, which is questionable (could
# just as well be dropped.
#
# The From: line is rewritten to use the "address (comments)" form
# instead of "phrase <route>" form our mailer uses.  Also, addresses
# with no "@domainname" are assumed to originate locally, and so are
# given a domain.
#
# The Sender: field below reflects the address of the person who
# maintains our mailing lists.  The Approved: field is in a special
# form, so that we can do bidirectional gatewaying.  Any message
# in a newsgroup that bears this stamp will not be fed into the
# matching mailing list.
	sed -n -e "1{i\\
		Path: $path
		}" \
	    -e ":a
		/^Newsgroups:/s/^/Original-/
		/^[Rr]eceived:/b r
		/^[Tt][Oo]:/s/^/Original-/
		/^[Cc][Cc]:/s/^/Original-/
		/^[Rr][Ee][Ss][Ee][Nn][Tt]-.*/s/^/Original-/
		s/^[Ii]n-[Rr]eply-[Tt]o:/References:/
		/^From:/{
			s/<\([^@]*\)>\$/<\1@$thissite>/
			s/^From:[ 	][	]*\(.*\)  *<\(.*\)>\$/From: \2 (\1)/
			}
		s/-[Ii]d:/-ID:/
		s/^\([^:]*:\)[	 ]*/\1 /
		/^\$/{i\\
			Newsgroups: $newsgroups\\
			Distribution: $distribution\\
			Sender: $admin@$thissite\\
			Approved: $newsgroup@mail.cs.toronto.edu\\
			Lines: $lines
			b e
		}
		p
		n
		b a
		:r
		s/.*//g
		n
		/^[ 	]/b r
		b a
		:e
		p
		n
		b e" < $ARTICLE |
		uux - $newsuucp!rnews > /tmp/disterr$$ 2>&1
		if test -s /tmp/disterr$$; then
			$Mail -s 'distribute error' $admin < /tmp/disterr$$
		else
			# update the logs
			messageid=`egrep -i '^message-id:[ 	]' $ARTICLE`
			messageid=`expr "$messageid" : '..........:[ 	]\(.*\)'`
			$logger "$newsgroups" "$messageid"
		fi
		rm -f /tmp/disterr$$
		;;
esac
rm -f $ARTICLE $ARTICLE.from
exit 0
