SHELL="/bin/sh"
import EXT
import HOST
VPOP="| /usr/local/vpopmail/bin/vdelivermail '' bounce-no-mailbox"
VHOME=`/usr/local/vpopmail/bin/vuserinfo -d $EXT@$HOST`

##
#  title:  mailfilter-domain
#  author: Matt Simerson
#  version 2.0
#  http://www.tnpi.biz/internet/mail/toaster/etc/mailfilter-domain
#
#  Usage: Install this file in ~vpopmail/domains/example.com/mailfilter
#
#  Create a .qmail-default file with the following:
#    "| /usr/local/bin/maildrop mailfilter"
#
#  After your satisifed that this is working properly, comment out all
#  the lines that start with "log"
##

logfile "/var/log/mail/maildrop.log"
log "====  BEGIN maildrop processing for $EXT@$HOST ==="

if ( $VHOME eq "" )
{
  LOG "  ERROR: VHOME isn't set, falling back to vdelivermail"
  LOG "===  EXIT  ===  "
  to "$VPOP"
}

##
# Comment out this section if you run SpamAssassin via qmailscanner
# Spamassassin is slow enough, no point in doubling your expense
#
# Note that if you want to pass a message larger than 250k to spamd
# and have it processed, you'll need to also set spamc -s. See the 
# spamc man page for more details. 
##

if ( $SIZE < 256000 ) # Filter if message is less than 250k
{
	log "  running message through spamc"
	exception {
		xfilter '/usr/local/bin/spamc -u "$EXT@$HOST"'
	}
}
	

##
# Include any rules set up for the user - this gives the 
#   administrator a way to override the domain's mailfilter file
#
#   this is also the "suggested" way to set individual values
#   for maildrop such as quota.
##

`test -r $VHOME/.mailfilter`
if( $RETURNCODE == 0 )
{
	log "  including $VHOME/.mailfilter"
	exception {
		include $VHOME/.mailfilter
	}
}

## 
# create the maildirsize file if it doesn't already exist
# (could also be done via "deliverquota user@dom.com 10MS,1000C)
##
`test -e $VHOME/Maildir/maildirsize`
if( $RETURNCODE == 1)
{
	log "  creating $VHOME/Maildir/maildirsize for quotas"
	`/usr/local/vpopmail/bin/vuserinfo -Q $EXT@$HOST`

	`test -s "$VHOME/Maildir/maildirsize"`
   if ( $RETURNCODE == 0 )
   {
      `/usr/sbin/chown vpopmail:vchkpw $VHOME/Maildir/maildirsize`
		`/bin/chmod 640 $VHOME/Maildir/maildirsize`
   }
}

##
# Set MAILDIRQUOTA. If this isn't set, maildrop and deliverquota
# will not enforce quotas for message delivery.
#
# I find this much easier than creating yet another config file
# to store this in. This way, any time the quota is changed in
# vpopmail, it'll get noticed by maildrop immediately.
##

MAILDIRQUOTA=`/usr/bin/head -n1 $VHOME/Maildir/maildirsize`

##
# The message should be tagged, so let's bag it.
##
if ( /^X-Spam-Status: *Yes/)
{
	`test -d $VHOME/Maildir/.Spam`   # make sure .Spam folder exists
	if( $RETURNCODE == 1 )
	{
		log "  creating $VHOME/Maildir/.Spam "
		`maildirmake -f Spam $VHOME/Maildir`
		`/usr/local/sbin/subscribeIMAP.sh Spam $VHOME`
	}

	log "  SPAM: attempting quota delivery to $VHOME/Maildir/.Spam"
	exception {
		xfilter "/usr/local/bin/deliverquota $VHOME/Maildir/.Spam"
	}

	if( $RETURNCODE == 77)
	{
		log "  QUOTA FAILURE: bouncesaying '$EXT@$HOST is over quota'"
		to "|/var/qmail/bin/bouncesaying '$EXT@$HOST is over quota'"
	}

	log "=== END ===  success"
	exit
}

log "  message is clean"

##
# Include any other rules that the user might have from
# sqwebmail or other compatible program
##

`test -r $VHOME/Maildir/.mailfilter`
if( $RETURNCODE == 0 )
{
	log "  including $VHOME/Maildir/.mailfilter"
	exception {
		include $VHOME/Maildir/.mailfilter
	}
}

exception {
	log     "  quota enabled delivery to $VHOME/Maildir"
	xfilter "/usr/local/bin/deliverquota $VHOME/Maildir"
}

## 
# check to make sure the message was delivered 
# returncode 77 means that out maildir was overquota - bounce mail
##
if( $RETURNCODE == 77)
{
	log "  BOUNCED: bouncesaying '$EXT@$HOST is over quota'"
	to "|/var/qmail/bin/bouncesaying '$EXT@$HOST is over quota'"
}

log "=== END === success"
