import EXT
import HOST
VHOME=`/home/vpopmail/bin/vuserinfo -d $EXT@$HOST`
DOMDIR=`/home/vpopmail/bin/vdominfo -d $HOST`
JUNK="$VHOME/Maildir/.Junk/"
DELETESCORE=20
GLOBALJUNK="/home/vpopmail/domains/shupp.org/bill/Maildir/.Junk/"


DUMMY=`test -d $VHOME/Maildir`
if ( $RETURNCODE == 1 )
{
	DUMMY=`grep bounce-no-mailbox $DOMDIR/.qmail-default`
	if ( $RETURNCODE == 0 )
	{
   		echo "Sorry, no mailbox here by that name. (#5.1.1) - maildrop"
   		EXITCODE=100
   		exit
	}
	DUMMY=`grep ' delete$' $DOMDIR/.qmail-default`
	if ( $RETURNCODE == 0 )
	{
   		echo "Deleted mail for non-existent user $EXT@$HOST - maildrop"
   		EXITCODE=0
   		exit
	}
	# else, it's either a Maildir or forward
	ARG4=`awk '{ print $4 }' $DOMDIR/.qmail-default`
	# check forward first
	DUMMY=`echo $ARG4 | grep '@'`
	if ( $RETURNCODE == 0 )
	{
		to '| /var/qmail/bin/qmail-inject'
	}
	# then check Maildir
	DUMMY=`echo $ARG4 | grep '^/'`
	if ( $RETURNCODE == 0 )
	{
		VHOME="$ARG4"
		JUNK="$ARG4/Maildir/.Junk/"
	}
}


if ( $SIZE < 262144 )
{
        exception {
                xfilter "/usr/bin/spamc -f -u $EXT@$HOST"
        }
}

if (/^X-Spam-Flag: *YES/)
{
        exception {
		/X-Spam-Status: Yes, hits=!-*[0-9]+\.[0-9]+/
		SCORE=$MATCH2
		if ($SCORE >= $DELETESCORE )
		{
			if ( /^From: *!.*/ )
			{
   				ADDR=getaddr($MATCH2)
			}

        		echo "auto-deleted spam from $FROM::$ADDR to $EXT@$HOST with score of $SCORE"
			to /dev/null
		}
        }
	DUMMY=`test -d $JUNK`
	if ( $RETURNCODE == 1 )
	{
		`/var/qmail/bin/maildirmake $JUNK`
                 echo "created $JUNK"
	}
	if($GLOBALJUNK != $JUNK)
	{
        	echo "maildrop: filtered junk with score of $SCORE"
        	cc $GLOBALJUNK
	}
        to $JUNK
}
else
{
	echo "maildrop: non-filtered mail delivery"
        to $VHOME/Maildir/
}
