about summary refs log tree commit homepage
path: root/scripts/dc-dlvr
blob: 90aab73b45c716eaaf1e15faa1eff5cb1af2a0f2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/bin/sh
# Copyright (C) 2008-2020 all contributors <meta@public-inbox.org>
# License: GPL-3.0+ <http://www.gnu.org/licenses/gpl-3.0.txt>
# 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