#!/bin/sh
# Copyright (C) 2008-2021 all contributors
# License: GPL-3.0+
# This is installed as /etc/dc-dcvr on my system
# to use with postfix main.cf: mailbox_command = /etc/dc-dlvr "$EXTENSION"
DELIVER=/usr/lib/dovecot/deliver
CLAMDSCAN=clamdscan
# change if your spamc/spamd listens elsewhere
spamc='spamc'
# allow plus addressing to train spam filters, $1 is the $EXTENSION
# which may be "trainspam" or "trainham". Only allow spam training
# when $CLIENT_ADDRESS is empty (local client)
case $1,$CLIENT_ADDRESS in
trainspam,) exec $spamc -L spam > /dev/null 2>&1 ;;
trainham,) exec $spamc -L ham > /dev/null 2>&1 ;;
esac
TMPMSG=$(mktemp -t dc-dlvr.orig.$USER.XXXXXX || exit 1)
CDMSG=$(mktemp -t dc-dlvr.orig.$USER.XXXXXX || exit 1)
rm_list="$TMPMSG $CDMSG"
cat >$CDMSG
$CLAMDSCAN --quiet - <$CDMSG
if test $? -eq 1
then
$DELIVER -m INBOX.spam <$CDMSG
exec rm -f $rm_list
fi
# pre-filter, for infrequently read lists which do their own spam filtering:
if test -r ~/.dc-dlvr.pre
then
set -e
mv -f $CDMSG $TMPMSG
DEFAULT_INBOX=$(. ~/.dc-dlvr.pre)
case $DEFAULT_INBOX in
'') exec rm -f $rm_list ;;
INBOX) ;; # do nothing
*)
$DELIVER -m $DEFAULT_INBOX <$TMPMSG
exec rm -f $rm_list
;;
esac
PREMSG=$(mktemp -t dc-dlvr.orig.$USER.XXXXXX || exit 1)
rm_list="$rm_list $PREMSG"
set +e
mv -f $TMPMSG $PREMSG
$spamc -E --headers <$PREMSG >$TMPMSG
else
$spamc -E --headers <$CDMSG >$TMPMSG
fi
err=$?
# normal delivery
set -e
case $err in
1) $DELIVER -m INBOX.spam <$TMPMSG ;;
*)
# users may override normal delivery and have it go elsewhere
if test -r ~/.dc-dlvr.rc
then
. ~/.dc-dlvr.rc
else
$DELIVER -m INBOX <$TMPMSG
fi
;;
esac
exec rm -f $rm_list