#! /bin/sh
:
#$Id: x_command,v 1.22 1995/10/30 02:58:35 srb Exp $

echo=echo		# /bin/echo
test=test		# /bin/test
cat=cat			# /bin/cat
rm=rm			# /bin/rm
formail=formail		# /usr/local/bin/formail
subscribe=subscribe	# ../SedBinDir/subscribe
unsubscribe=unsubscribe # ../SedBinDir/unsubscribe
multigram=multigram	# ../SedBinDir/multigram
showlist=showlist	# ../SedBinDir/showlist

tmprequest=tmp.request
tmpfrom=tmp.from
dist=dist
log=log

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

X_ENVELOPE_TO=$list-request	# to convince (un)subscribe we used the right
export X_ENVELOPE_TO		# address


$cat >$tmprequest
$formail -R$X_COMMAND: X-Processed: <$tmprequest

set dummy `$formail -x$X_COMMAND: <$tmprequest`
shift; shift
test "_$X_COMMAND_PASSWORD" = "_$1" && shift

while $test $# != 0
do
  case "_$1" in
     _subscribe|_unsubscribe|_checkdist)
	 if $test $# = 1
	 then
	    $echo "X-Diagnostic: Missing argument for $1"
	    set help
	 fi;;
  esac
  case "_$1" in
     _subscribe)
	 $echo "The prospective subscriber address generates the following"
	 $echo "multigram matches to the current list:"
	 $echo ""
	 $echo "From $2" | $multigram -m -b8 -l-32767 dist
	 $echo ""
	 $echo "From $2 " >$tmprequest
	 $echo "From: request ($listreq)" >>$tmprequest
	 $echo "Reply-To: $2" >>$tmprequest
	 $echo "To: $listreq" >>$tmprequest
	 $echo "Subject: subscribe $2" >>$tmprequest
	 $echo "$2" >$tmpfrom
	 $test -z "$subscribe_log" ||
	  $echo "x_command: subscribe" >>$subscribe_log
	 $subscribe <$tmprequest ||
	  $echo "X-Diagnostic: Unscreened, not subscribed"
	 shift ;;
     _unsubscribe)
	 $echo "The address that is about to unsubscribed generates the"
	 $echo "following multigram matches to the current list:"
	 $echo ""
	 $echo "From $2" | $multigram -m -b8 -l-32767 dist
	 $echo ""
	 $echo "From $2 " >$tmprequest
	 $echo "From: $listreq" >>$tmprequest
	 $echo "Reply-To: $2" >>$tmprequest
	 $echo "To: $listreq" >>$tmprequest
	 $echo "Subject: unsubscribe $2" >>$tmprequest
	 $echo "$maintainer" "$2" >$tmpfrom
	 $test -z "$subscribe_log" ||
	  $echo "x_command: unsubscribe" >>$subscribe_log
	 $unsubscribe -D <$tmprequest | $SENDMAIL $sendmailOPT `cat $tmpfrom`
	 shift ;;
     _checkdist)
	 $echo "Multigram checking the dist file for matches with"
	 $echo "$2:"
	 $echo ""
	 $echo "From $2" | $multigram -m -b8 -l-32767 dist
	 $echo ""
	 shift ;;
     _showlist)
	 $showlist -l $list ;;
     _showdist)
	 $echo "--- Current subscribers:"
	 $cat $dist
	 $echo "--- End of subscriber list" ;;
     _showlog)
	 $echo "--- Current log:"
	 $cat $log
	 $echo "--- End of log" ;;
     _wipelog)
	 $cat /dev/null >$log ;;
     _version)
	 flist -v 2>&1
	 echo ""
	 procmail -v 2>&1 ;;
     _help|_info)
	 $echo "Known $X_COMMAND keywords:"
	 $echo "	subscribe mailaddress"
	 $echo "	unsubscribe mailaddress"
	 $echo "	checkdist mailaddress"
	 $echo "	showdist"
	 $echo "	showlist"
	 $echo "	showlog"
	 $echo "	wipelog"
	 $echo "	version"
	 $echo "	help"
	 $echo "	info" ;;
     *) $echo "X-Diagnostic: Unknown command $1" ; set dummy help ;;
  esac
  shift
done
