#!/bin/bash

tmp1="/tmp/t1.$$"
tmp2="/tmp/t2.$$"
tmp3="/tmp/t3.$$"
tmp4="/tmp/t4.$$"
tmp5="/tmp/t5.$$"

log="/var/log/spam.log"
localdomains="/etc/mail/relay-domains"
localrejects="/var/db/spamilter/db.rcpt"

mail="0"

while [ "$1" != "" ]; do
	case $1 in
		-mail)
			mail="1"
		;;
		-log)
			shift
			log="$1"
		;;
		*)
		;;
	esac
	shift
done

header()
{
cat<<EOF
This report details the Spam filter actions for any email
addressed to $1 for the preceeding 24 hour period.

EOF
}

stripsyslogentries()
{
	awk '{print $6" "$7" "$8" "$9}'
}

stripbraces()
{
	sed -e's/<//' -e's/>$//' -e's/[ 	]//g' -e'/^$/d'
}

sortbyto()
{
	awk '{print $4}' | stripbraces | sort -u 
}

sortbyfrom()
{
	awk '{print $3}' | stripbraces | sort -u
}

filterbysender()
{
	while read a b c d e; do
		if [ "$c" = "$1" ]; then
			echo "$a $b $c $d $e"
		fi;
	done
}

filterbyrcpt()
{
	while read a b c d e; do
		if [ "$d" = "$1" ]; then
			echo "$a $b $c $d $e"
		fi;
	done
}

# for each of the addresses, create a hit list
rptaddresses()
{
	if [ "$1" == "-s" ]; then
		silent=1;
	else
		silent=0;
	fi
	shift
	while read a; do
		cat $1 | filterbysender "<$a>" > $2
		if [ $silent -eq 0 ]; then
			echo -e "\t`cat $2 | wc -l | sed -e's/ //g'` - $a"
			cat $2 | sortbyto | { while read b; do echo -e "\t\t$b"; done; }
		else
			cat $2 | sortbyto | { while read b; do echo -e "\t$b"; done; }
		fi
	done
}

# for each of the addresses, create a hit list
senderaddresses()
{
	if [ "$1" == "-s" ]; then
		silent=1;
	else
		silent=0;
	fi
	shift
	while read a; do
		cat $1 | filterbyrcpt "<$a>" > $2
		if [ $silent -eq 0 ]; then
			echo -e "\t`cat $2 | wc -l | sed -e's/ //g'` - $a"
			cat $2 | sortbyfrom | { while read b; do echo -e "\t\t$b"; done; }
		else
			cat $2 | sortbyfrom | { while read b; do echo -e "\t$b"; done; }
		fi
	done
}

creatercptlist()
{
	tmpf1="/tmp/tmp.$$.f1"
	tmpf2="/tmp/tmp.$$.f2"

	# sed filter list of recipients not to create reports for
	cat $localrejects | sed -e's/#.*//' -e's/[ 	]\{1,\}/ /g' -e's/|//g' -e'/^$/d'|awk '{print $2}'|sort -u|{ while read a; do echo "/$a/d"; done; } > $tmpf1

	# sed filter list of localdomain recipents
	cat $localdomains |{ while read a; do echo "/$a/p"; done; } > $tmpf2

	# create list of localdomain recipients
	if [ `cat $tmpf1 | wc -l | sed -e's/ //g'` -eq 0 ]; then
		sortbyto | sed -n -f $tmpf2
	else
		sortbyto | sed -f $tmpf1 | sed -n -f $tmpf2
	fi

	# cleanup
	rm -f $tmpf1 $tmpf2
}

# using localdomain recipients, create a report of email activity from the log file
cat $log | grep -v newsyslog | stripsyslogentries | tee $tmp1 | creatercptlist | {
while read user; do
	cat /dev/null > $tmp5
	if [ "$mail" = "0" ]; then
		echo -e "--- $user ---" >> $tmp5
	else
		header "$user" >> $tmp5
	fi
	grep "<$user>" $tmp1 |{ while read a b c d; do if [ "<$user>" == "$d" ]; then echo "$a $b $c $d"; fi; done; } > $tmp4
	qty="`cat $tmp4|wc -l|sed -e's/ //g'`"
	if [ $qty -eq 0 ]; then
		echo "No activity" >> $tmp5
	else
		for rpt in Accepted Discarded Rejected Spam TempFailed; do
			qty="`grep \^$rpt $tmp4 | tee $tmp2 | wc -l | sed -e's/ //g'`"
			if [ $qty -gt 0 ]; then
				echo -en "--- $rpt - $qty --\n" >> $tmp5
				cat $tmp2 | sortbyto | senderaddresses -s $tmp2 $tmp3 >> $tmp5
				echo -e "\n" >> $tmp5
			fi
		done;
	fi
	if [ "$mail" = "0" ]; then
		cat $tmp5
	else
		cat $tmp5 | mail -s "Spam filter activity report" $user
	fi
		
done; }

rm -f $tmp1 $tmp2 $tmp3 $tmp4 $tmp5
