#!/bin/sh

test=test		# /bin/test
echo=echo		# /bin/echo
cat=cat			# /bin/cat
sed=sed			# /bin/sed
grep=grep		# /bin/grep
date=date		# /bin/date
ls=ls                   # /bin/ls
rm=rm                   # /bin/rm
mkdir=mkdir             # /bin/mkdir
formail=formail		# /usr/local/bin/formail
multigram=multigram	# ../.bin/multigram

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

tmprequest=tmp.request
tmpfrom=tmp.from
confirmtxt=confirm.txt
US=$1

$test -d cookies || $mkdir cookies

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=""

if [ "$US" = "unsub" ]
then

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


# create an cookie...this is u for unsubscribe, the date plus the pid
COOKIE="u$($date +%m%d%H%M%S)$$"

else

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' `

fi

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  [ "$US" != "unsub" ]
then

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

# create a fairly unique cookie...this is the date plus the pid
COOKIE="s$($date +%m%d%H%M%S)$$"

# log the attempt
$echo "$0: subscription request from $fromaddr for"
$echo "`cat $tmpfrom` - assigned cookie $COOKIE" >> log

else

$echo "$0: unsubscription request from $fromaddr for"
$echo "`cat $tmpfrom` - assigned cookie $COOKIE" >> log

fi

# create cookie file in cookies directory containing the address
# to be subscribed
$echo $subscraddr > cookies/$COOKIE

$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.
"

( $formail -i"From: $listreq" -rtA"X-Loop: $listaddr" \
           -I"Subject: CONFIRM $COOKIE" <$tmprequest 

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

  $echo "You have requested that the following address:"
  $echo ""
  $echo "       $subscraddr"
  $echo ""

  if [ "$US" = "unsub" ]; then
  
  $echo "be deleted from the $list mailing list."
  $echo ""
  $echo "You have NOT yet been unsubscribed from the list."
  $echo "To unsubscribe you need to confirm your unsubscription"

  else

  $echo "be added to the $list mailing list."
  $echo ""
  $echo "You have NOT yet been subscribed to the mailing list."
  $echo "To subscribe you need to confirm your subscription"

  fi

  $echo "request by sending email to the address:"
  $echo ""
  $echo "        $listreq"
  $echo ""
  $echo "with the Subject string:"
  $echo ""
  $echo "         CONFIRM $COOKIE"
  $echo ""
  $echo "With a reasonable good email program a reply to this" 
  $echo "message should be sufficient"
  $echo ""

  $cat $confirmtxt
  $echo ""

  $rm -f _dummy_ `$ls -td cookies/* | $sed -e '1,'$maxcookies' d' `

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