#! /bin/sh
# Time-stamp: <2001-05-25 08:16:53 cymbala>
#
DEBUG="TEST"
DEBUG=""
#            rjc_espm.sh -- script for sending choice spam email.
# -------------------------------------------------------
#
# man -k alias
# Mail::Alias (3pm)
#        - maniulate mail alias files of various formats
#            (typo)
#        - " ... NOTE: This package is unfinished"
#        - I have no intent to do any more work on it. I have been 
#          trying to find a new owner for a while, but with no luck
#          Graham.  [ Graham Barr <gbarr@pobox.com> ]  Thu, 24 May 2001
# -------------------------------------------------------
# Subject and body (bottom pair will be used).
#
# 2001.05.24 - Hasta La Victoria!
#            - heating up.
    subject="A critical point in campaign to save the Pacifica network."
    body=~/Misc/pacifica_20010520.fwd
# -------------------------------------------------------


# Group size (maximum number of BCC: recipients).
grp_size=10

# Expressions.
re_cymbala="[Cc][Yy][Mm][Bb][Aa][Ll][Aa]"
re_mother="[jJ]${re_cymbala}"               # jcymbala@capcod.net
mail_rc=~/.mailrc

# Look at addresses.
function see_addr ()
{
    # echo ${#addresses[@]}
    case $1 in
	LOOP)
	    number=0
	    while [ $number -lt $n_addr_original ]; do
		foo="${addresses[$number]}"
		if [ $foo ]; then
		    echo $foo
		fi
		let number="$number"+1
	    done
	    ;;
	ECHO)
	    for foo in ${addresses[@]}; do
		echo $foo
	    done
	    ;;
        *)
	    echo "Invalid option."
	    exit 1
	    ;;
    esac
}

# Modify list of addresses.
function mod_addr ()
{
    number=$[n_addr_original -1]
    while [ $number -ge 0 ]; do
	original="${addresses[$number]}"
	case $1 in
	    TEST)
		if [ "$original" = "${original/${re_cymbala}}" ] ||
		   [ ! "$original" = "${original/${re_mother}}" ] ; then
		    unset addresses[$number]
		fi
		;;
	    *)
		if [ "$original" != "${original/${re_cymbala}}" ]; then
		    unset addresses[$number]
		fi
		;;
		# echo "Invalid option."
		# exit 1
	esac
	let number="$number"-1
    done
}

# Function to work with .mailrc
function mail_alias_resolve ()
{
    case $1 in
	ADDRESSES)
	    keep_list=""
	    number=1
	    max=${#alias_pairs[@]}
	    while [ "$number" -lt $max ]; do
		foo="${alias_pairs[${number}]}"
		if [ "$foo" != "${foo/@}" ]; then
		    keep_list="${keep_list} ${foo}"
		fi
		let number="$number"+2
	    done
	    #
	    addresses=(${keep_list})
	    n_addr_original=${#addresses[@]}
	    ;;
	ALIAS)
	    # Replace alias with value.
	    addr="$2"
	    while [ "${addr}" == "${addr/@}" ]; do
		number=1
		max=${#alias_pairs[@]}
		while [ $number -lt $max ]; do
		    let back_one="$number"-1
		    if [ "${addr}" = "${alias_pairs[$back_one]}" ]; then
			addr=${alias_pairs[$number]}
		    fi
		    let number="$number"+2
		done
	    done
	    ;;
	ALIASES)
	    mail_alias_resolve "ALIAS" "me"
		addr_me="${addr}"
	    mail_alias_resolve "ALIAS" "myself"
		addr_myself="${addr}"
	    ;;
	*)
	    alias_pairs=(`awk '/^[\t ]*alias / { print $2, $3 }' ${mail_rc}`)
	    if [ $[ ${#alias_pairs[@]} % 2 ] -ne 0 ]; then
		echo "Uneven number in pairs array: ${#alias_pairs[@]}"
		exit
	    fi
	    ;;
    esac
}

# Send spam email
#   (Use infrequently!  1st use: Pacifica crisis, Gonzalez email 2001.05.20).
#
function spam_er () {
    #
    # Send an email every (x) recipients.
    # `man mail'

    case $1 in
	SPAM)
	    bcc_addr="${bcc_addr#,}"
	    if [ $bcc_addr ]; then
		let batch_no="$batch_no"+1
		echo "BATCH ${batch_no}: ${bcc_addr}"
		echo " ...email sent!"
		echo
		# -------------------------------------------------------
		mail -s "$subject" -b $bcc_addr $addr_myself < $body
		# -------------------------------------------------------
	    fi
	    bcc_addr=""
	    ;;
	REMAINDER)
	    if [ 0 -eq $[$2 % ${grp_size}] ]; then
		# 1 of 2:
		spam_er "SPAM"
	    fi
	    ;;
	*)
	    bcc_addr=""
	    batch_no=0
	    number=0
	    for addr in ${addresses[@]}; do
		let number="$number"+1
		bcc_addr="${bcc_addr},$addr"
		spam_er "REMAINDER" $number
	    done
	    # 2 of 2:
	    spam_er "SPAM"
	    ;;
    esac
}

# -------------------------------------------------------
# Addresses.
#
mail_alias_resolve
mail_alias_resolve "ADDRESSES"
mail_alias_resolve "ALIASES"

mod_addr "${DEBUG}"

# - Pick one, two or none:
# see_addr "LOOP"
# see_addr "ECHO"

spam_er

###
#