#!/bin/sh
# $Log: bogofilter-qfe.in,v $
# Revision 1.1  2019-05-29 00:46:11+05:30  Cprogrammer
# Initial revision
#
#
# This a qmail specific bogofilter frontend script which allows the use of a 
# centralized bogofilter running on an smtp mail server
# Copyright (C) Gyepi Sam <gyepi@praxis-sw.com> 2002
# $Id: bogofilter-qfe.in,v 1.1 2019-05-29 00:46:11+05:30 Cprogrammer Exp mbhangui $
 
domain=$HOST
#
# set REPORT if statistics is to be sent back
# set REPORT_TO if the report is not to be sent to the SENDER
# set attach_report to send the report as an attached zip
#
REPORT=yes
REPORT_TO=postmaster
attach_report=no
sender_domain=`echo $SENDER | cut -d@ -f2`
if [ " $sender_domain" = " " -o " $sender_domain" = " #[]" ] ; then
	echo "$SENDER: Sorry, You are not authorized to email $EXT@$domain"
	exit 100
fi
auth_flag=0
/bin/grep $sender_domain /etc/indimail/control/rcpthosts > /dev/null 2>&1
if [ $? -eq 0 ] ; then
	auth_flag=1
fi
if [ $auth_flag -eq 0 ] ; then
	/bin/grep $sender_domain /etc/indimail/control/locals > /dev/null 2>&1
	if [ $? -eq 0 ] ; then
		auth_flag=1
	fi
fi
if [ $auth_flag -eq 0 ] ; then
	/bin/grep "@$sender_domain" /etc/indimail/control/spamignore > /dev/null 2>&1
	if [ $? -eq 0 ] ; then
		auth_flag=1
	fi
fi
#
# Sigh :( my last hope
#
if [ $auth_flag -eq 0 ] ; then
	/bin/grep $SENDER /etc/indimail/control/spamignore > /dev/null 2>&1
	if [ $? -ne 0 ] ; then
		echo "$SENDER: Sorry, You are not authorized to email $EXT@$domain"
		exit 100
	fi
fi
if [ -f /var/indimail/domains/$domain/wordlist.db ] ; then
	wordlist=/var/indimail/domains/$domain
else
	wordlist=/etc/indimail
fi
if [ -f /var/indimail/domains/$domain/bogofilter.cf ] ; then
	config_file=/var/indimail/domains/$domain/bogofilter.cf
else
	config_file=/etc/indimail/bogofilter.cf
fi
spam_header_name=`grep ^spam_header_name $config_file 2>/dev/null | cut -d= -f2`
if [ " $spam_header_name" = " " ] ; then
	PAT="/^X-Spam-Rating/d; /^X-Filter/d; /^X-Bogosity/d; /^Delivered-To/d;"
else
	PAT="/^X-Spam-Rating/d; /^X-Filter/d; /^$spam_header_name/d; /^Delivered-To/d;"
fi
opt="X"
case "$EXT" in
	Bregister-spam|register-spam)		# register as spam
	PAT="$PAT /$domain/d;"
	opt="-s"
;;
	Bregister-ham|register-ham|Bregister-nonspam|register-nonspam)	# register as ham
	opt="-n"
;;
	Bspam|spam)	# unregister as ham + register as spam
	PAT="$PAT /$domain/d;"
	opt="-Ns"
;;
	Bham|ham|Bnonspam|nonspam)	# unregister as spam + register as ham
	opt="-Sn"
;;
esac
(
if [ -f /etc/indimail/control/spamignoreheaders ] ; then
	/bin/grep -i -v -f /etc/indimail/control/spamignoreheaders
else
	/bin/cat -
fi
) | sed -e 's,.\**SPAM.\**,,g' > /tmp/bogofilter.$$
if [ ! " $REPORT" = " " ] ; then
	subject="SpamFilter Analysis (mail sent to $EXT@$HOST)"
	if [ " $attach_report" = " " -o " $attach_report" != " yes" ] ; then
		(
		if [ " $REPORT_TO" = " " ] ; then
			echo "To: $SENDER"
		else
			echo "To: $REPORT_TO"
		fi
		echo "From: \"IndiMail Spam Reporter\" <Mailer-Daemon>"
		echo "Date: `/bin/date -R`"
		echo "Subject: $subject"
		echo
		echo "Analysis of mail sent by $SENDER to $EXT@$HOST"
		echo
		/usr/bin/bogofilter -c $config_file -d $wordlist -vvv < /tmp/bogofilter.$$
		) | /var/indimail/bin/sendmail -t
	else
		(
		echo
		echo "Analysis of mail sent by $SENDER to $EXT@$HOST"
		echo "on `/bin/date -R`"
		echo
		) > /tmp/descfile.$$
		if [ " $REPORT_TO" = " " ] ; then
			postmaster="$SENDER"
		else
			postmaster="$REPORT_TO"
		fi
		/usr/bin/bogofilter -c $config_file -d $wordlist -vvv < /tmp/bogofilter.$$ > /tmp/report.$$
		if [ " $opt" = " -s" -o " $opt" = " -Ns" ] ; then
			/usr/bin/zip -q -j /tmp/report.$$.zip /tmp/report.$$ /tmp/bogofilter.$$
			/bin/rm -f /tmp/report.$$
		else
			# Could be confidential
			/usr/bin/zip -q -j -m /tmp/report.$$.zip /tmp/report.$$
		fi
		QMAILUSER=postmaster
		if [ ! " $default_domain" = " " ] ; then
			QMAILHOST=$default_domain
		fi
		QMAILNAME="IndiMail SPAM Reporter"
		export QMAILNAME
		/usr/bin/mpack -s "$subject" -d /tmp/descfile.$$ \
			-c "application/zip" /tmp/report.$$.zip $postmaster
		/bin/rm -f /tmp/descfile.$$ /tmp/report.$$.zip
	fi
fi
headcount=`/var/indimail/bin/822header < /tmp/bogofilter.$$| wc -l`
(
	if [ " $headcount" = " " ] ; then
		# Something wrong with the mail format.
		/bin/cat /tmp/bogofilter.$$ | /bin/sed "$PAT"
	else
		# Output headers after eliminating headers containing the 
		# sender's email id and domain
		/var/indimail/bin/822header < /tmp/bogofilter.$$ | /bin/sed "$PAT"
		# get the body
		headcount=`expr $headcount + 1`
		tail --lines=+$headcount < /tmp/bogofilter.$$
	fi
	/bin/rm -f /tmp/bogofilter.$$
) | /usr/bin/bogofilter -c $config_file -d $wordlist $opt
status=$?
if [ $status = 0 ] ; then
	exit 0
else
	exit 111
fi
