:
##########################################################################
# Shellscript:	junkmail - reply to junkmail
# Author     :	Heiner Steven <heiner.steven@odn.de>
# Date       :	1997-06-30
# Category   :	Mail
# SCCS-Id.   :	@(#) junkmail	1.3 02/02/26
##########################################################################
# Changes
# 03.11.1997 hs	Use "Reply-To" header field, if present (0.2)
##########################################################################

PN=`basename "$0"`			# Program name
VER='1.3'

N=100					# Send the mail this number of times
SleepTime=1				# ...after each mail batch of 10 mails
Force=no
Edit=no
Reply=${TMPDIR:=/tmp}/jm.reply.$$

# Determine mail program knowing about the "-s subject" flag
for Mailer in /bin/mailx /usr/ucb/mailx /bin/Mail /usr/ucb/Mail
do [ -x "$Mailer" ] && { Mail="$Mailer"; break; }
done

: ${Mail:=mail}

Usage () {
    echo >&2 "$PN - answer to junkmail $VER (hs '97)
usage: $PN [-ef] [-n count] [-r address] [-s subject] mailfile ...
    -n:	send the mail count times (default value is $N)
    -f:	force non-interactive execution
    -e: edit outgoing mail template
    -r:	recipient's address (default: take address from \"Reply-To\" or \"From\"
	header field)
    -s:	set subject"
    exit 1
}

Msg () {
    for MsgLine
    do echo "$PN: $MsgLine" >&2
    done
}

Fatal () { Msg "$@"; exit 1; }

YesNo () {
    while true
    do
	echo >&2 "$*" "(y/n)? \c"
	read yesno || exit 2
	case "$yesno" in
	    [yY]|[yY][eE][sS])	return 0;;
	    [nN]|[nN][oO])	return 1;;
	esac
	echo >&2 "
Please enter 'y' for \"yes\"
         or  'n' for \"no\"
"
    done
}

ExtractAddress () {
    NonEmail="][{}()\<\>\"'\`\$&/=?\\,;:#"

    for Addr
    do
    	case "$Addr" in
	    *@*)
		echo "$Addr" |
		    sed -e 's|^['"$NonEmail"']*||g'	\
		    	-e 's|\([^'"$NonEmail"'][^'"$NonEmail"']*@[^'"$NonEmail"'][^'"$NonEmail"']*\)|\1|'				#\
			#-e 's|['"$NonEmail"']*||g'
		return
		;;
	esac
    done
}

n=$N
set -- `getopt efhn:r:s: "${1+$@}"`
while [ $# -gt 0 ]
do
    case "$1" in
    	-e)	Edit=yes;;
	-f)	Force=yes;;
	-n)	n=$2; shift;;
	-r)	Address="$2"; shift;;
	-s)	Subject="$2"; shift;;
	--)	shift; break;;
	-h)	Usage;;
	-*)	Usage;;
	*)	break;;			# First file name
    esac
    shift
done

[ $# -lt 1 ] && Usage

# Remove temporary file at interrupt or exit
trap 'rm -f "$Reply" >/dev/null 2>&1' 0
trap "exit 2" 1 2 3 15

for File
do
    [ -r "$File" ] || continue
    [ -f "$File" ] || continue

    if [ -z "$Subject" ]
    then
	Subject=`grep 'Subject: ' "$File" | cut -d: -f2- | sed 's:^ ::' | head -1`
	Subject="${Subject:+Re: $Subject}"
    fi

    # Try to determine the mail recipient in the following order:
    #	1. specified with the "-r" flag
    #	2. "Reply-To" header
    #	3. "From:" header

    if [ -z "$Address" ]
    then
	ReplyTo=`grep 'Reply-To: ' "$File" | cut -d: -f2- | sed 's:^ ::' | head -1`
	Address=`ExtractAddress $ReplyTo`
    fi

    if [ -z "$Address" ]
    then
	From=`grep 'From: ' "$File" | cut -d: -f2- | sed 's:^ ::' | head -1`
	Address=`ExtractAddress $From`
    fi

    if [ -z "$Address" ]
    then
    	Msg "could not determine sender of $File (From $From) - ignoring file"
	continue
    fi

    (cat <<-! ) > "$Reply" || continue
	I'm not interested in your offer. Please exclude me from
	your mailing list.

	---------------------------------------------------------
	!

    sed 's:^: > :g' "$File" >> "$Reply" || continue

    [ $Edit = yes ] && ${EDITOR:-vi} "$Reply"

    Msg "$From: \"$Subject\""
    if [ $Force != yes ]
    then
	YesNo "Really reply $n mails to $Address" || continue
    fi

    Msg "sending $n replies to $Address..."

    i=0
    while [ $i -lt $n ]
    do
    	$Mail -s "$Subject" $Address < "$Reply" || exit
	# Send mails in batches of 10. Give the mailer
	# time to send the mail.
	expr $i % 10 >/dev/null || sleep $SleepTime
	i=`expr $i + 1`
    done
done
