#! /bin/sh
:
# Copyright (c) 1993-1996, S.R. van den Berg, The Netherlands
#$Id: subscribe,v 1.36 1996/12/21 03:28:11 srb Exp $

test=test		# /bin/test
echo=echo		# /bin/echo
cat=cat			# /bin/cat
sed=sed			# /bin/sed
grep=grep		# /bin/grep
date=date		# /bin/date
formail=formail		# /usr/local/bin/formail
multigram=multigram	# ../.bin/multigram
arch_retrieve=arch_retrieve	# ../.bin/arch_retrieve

$test -z "$listaddr" &&
 $echo "Don't start this script directly, it is used in rc.request" && exit 64

tmprequest=tmp.request
tmpfrom=tmp.from
subscribetxt=subscribe.txt
subscribefiles=subscribe.files

if $test ! -r dist
then
  $echo "subscribe: Where did the dist file go?" 1>&2
  $echo "(Only addresses below this line can be automatically removed)" >>dist
fi

case "$X_ENVELOPE_TO" in
  *$list-request*) wrongaddress="" ;;
  *) wrongaddress="WARNING:
	Please try to use '$listreq'
	the next time when issuing (un)subscribe requests.
" ;;
esac

subscraddr=""

address=`$formail -k -xSubject: |
 $sed -n -e 'y/ABCDEFGHIJKLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxyz/' \
  -e '/^[	 ]*[^	 a-z]/ q' \
  -e 's/^[^@]*add[	 ]*'\
'[^	 ]*[	 ]\([^	 ]*[-a-z0-9_][@!][a-z][^	 ]*\).*/\1/p' \
  -e 's/^[^@]*address[	 ]*'\
'[^	 ]*[	 ]\([^	 ]*[-a-z0-9_][@!][a-z][^	 ]*\).*/\1/p' \
  -e 's/^[^@]*sub[	 ]*'\
'[^	 ]*[	 ]\([^	 ]*[-a-z0-9_][@!][a-z][^	 ]*\).*/\1/p' \
  -e 's/^[^@]*subscribe[	 ]*'\
'[^	 ]*[	 ]\([^	 ]*[-a-z0-9_][@!][a-z][^	 ]*\).*/\1/p' `

fromaddr=`$cat $tmpfrom`

for a in $address ""
do
  if $test -z "$subscraddr"
  then
     case $a in
	$listreq|$listaddr) ;;
	?*) subscraddr=$a
	   $echo " $subscraddr" >$tmpfrom ;;
     esac
  fi
done

subscraddr=""	# let multigram eliminate trailing and leading punctuation

address=`$formail -k -xSubject: <$tmprequest |
 $multigram -b1 -x$listreq -x$listaddr -l$off_threshold $tmpfrom |
 $sed -e 's/^  *[^ ][^ ]*  *[^ ][^ ]*  *[^ ][^ ]*  *\([^ ][^ ]*\)/\1/' `

for a in $address ""
do
  $test -z "$subscraddr" && subscraddr=$a
done

$test -z "$subscraddr" && subscraddr=$fromaddr

if $test -f subscreen
then
  ./subscreen "$subscraddr" || exit 1
fi

$grep '^Subject: Re:' <$tmprequest >/dev/null &&
  wrongaddress="${wrongaddress}WARNING:
	Send in a new mail, INSTEAD OF REPLYING
	the next time when issuing (un)subscribe requests.
"

NOT_METOO=""

#$formail -k -xSubject: <$tmprequest |
# $grep 'no.*[^a-z]cop.*[^a-z]please' >/dev/null && NOT_METOO=" (-n)"

$multigram -a "$subscraddr$NOT_METOO" dist >/dev/null

sender=`$formail -rtzcxTo: <$tmprequest`

$test -z "$subscribe_log" ||
 $echo "subscribe: $subscraddr by: $sender  `$date`" >>$subscribe_log

case $subscraddr in
   *$sender*) sender="$subscraddr" ;;
   *) sender="$sender $subscraddr" ;;
esac

( $formail -i"From: $listreq" -rtA"X-Loop: $listaddr" <$tmprequest

  $test ! -z "$wrongaddress" && $echo "$wrongaddress" && wrongaddress=""

  $echo "You have added to the subscriber list of:"
  $echo ""
  $echo "	$listaddr"
  $echo ""
  $echo "the following mail address:"
  $echo ""
  $echo "	$subscraddr"
  $echo ""
# if $test -z "$NOT_METOO"
# then
     $echo "By default, copies of your own submissions will be returned."
# else
#    $echo "As requested, copies of your own submissions will not be returned."
# fi
  $echo ""

  $cat $subscribetxt

  $sed -e 's/^/>/' $tmprequest
) | $SENDMAIL $sendmailOPT $sender

if $test -f $subscribefiles
then
  $formail -X "" -i "Reply-To: $subscraddr" -i "Subject: archive" \
   <$tmprequest >$tmpfrom
  $cat $tmpfrom >$tmprequest
  $echo "" >>$tmprequest
  $cat $subscribefiles >>$tmprequest		# setup fake archive commands
  $echo $subscraddr >$tmpfrom
  exec $arch_retrieve <$tmprequest		# the point of no return
fi
