#!/bin/sh

##  Author:
##      Werner Reisberger     <werner at pure dot ch> 
##  Description:
##    confirm_del allows unsubscriptions to mailing lists running with  
##    SmartList  depending on a confirmation. If automatic unsubscription is 
##    not allowed the confirmation can be done by the list maintainer(s).
##---------------------------------------------------------------------------##
##    
##    Copyright (C) 1998-2002   Werner Reisberger  <werner at pure dot ch>
##    Copyright (C) 1997        Michelle Dick <artemis@rahul.net> and
##                              Werner Reisberger  <werner at pure dot ch>
##
##    This program is free software; you can redistribute it and/or modify
##    it under the terms of the GNU General Public License as published by
##    the Free Software Foundation; either version 2 of the License, or
##    (at your option) any later version.
##
##    This program is distributed in the hope that it will be useful,
##    but WITHOUT ANY WARRANTY; without even the implied warranty of
##    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
##    GNU General Public License for more details.
##
##    You should have received a copy of the GNU General Public License
##    along with this program; if not, write to the Free Software
##    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
##---------------------------------------------------------------------------##

test=test		# /bin/test
dist=dist
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

$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 unsubscribe requests.
" ;;
esac

subscraddr=""

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


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

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

# is the address on the list ?
$sed 's/\([^ 	]*\) *.*/\1/' $dist | $grep -i "^${subscraddr}$" > /dev/null

if [ $? != 0 ]; then

  $echo "$0: unsubscription request from $fromaddr for" >> $LOGFILE
  $echo "unknown address: $subscraddr" >> $LOGFILE

    
  ( $formail -i"From: SmartList <$listreq>" -rtA"X-Loop: $listaddr" \
             -I"Subject: unsuccessful unsubscribe request" <$tmprequest 
  
    $test ! -z "$wrongaddress" && $echo "$wrongaddress" && wrongaddress=""
  
    $echo "It has been requested that the following address:"
    $echo ""
    $echo "       $subscraddr"
    $echo ""
  
    $echo "should be deleted from the $list mailing list."
    $echo ""
    $echo "Sorry, but this address has NOT been found on the list."

    if $test ! -z "$unsub_assist" -a 0 != "$unsub_assist"
    then
      $echo "What I did find were the following approximate matches:"
      $echo ""
      $echo $subscraddr | $multigram -m -b$unsub_assist -l-32767 $dist
      $echo ""
      $echo "If you recognise one of these addresses as being the one you"
      $echo "wanted to unsubscribe, send in a new unsubscribe request"
      $echo "containing the text:  unsubscribe the_address_you_meant."

    else
 
      $echo ""
      $echo "Please check carefully the spelling of this address."
      $echo "You may send an unsubscribe request with another address."
      $echo "If the address is on the list you will receive a confirmation"
      $echo "message. A reply to this message will remove the address."
      $echo ""

    fi

    $echo "It follows a copy of your unsubscribe request"
    $echo ""

    $sed -e 's/^/>/' $tmprequest
  ) | $SENDMAIL $sendmailOPT $fromaddr
  
  exit 0

fi

$echo "$0: unsubscription request from $fromaddr for" >> $LOGFILE
$echo "$subscraddr - assigned cookie $COOKIE" >> $LOGFILE

# create cookie file in cookies directory containing the address
# to be unsubscribed
$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 unsubscribe requests.
"
  
( $formail -i"From: SmartList <$listreq>" -rtA"X-Loop: $listaddr" \
           -I"Subject: CONFIRM $COOKIE" <$tmprequest 

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

  $echo "It has been requested that the following address:"
  $echo ""
  $echo "       $subscraddr"
  $echo ""

  $echo "should be deleted from the $list mailing list."
  $echo ""
  $echo "It has NOT yet been unsubscribed from the list."
  $echo "To unsubscribe you need to confirm the unsubscription"
  $echo "request by sending an 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
