#!/bin/ksh # A wrapper for SpamAssassin suitable for calling from a dot-qmail file # Version: 1.5 # # Usage: # |ifspamh mail-address # # Mail will be reinjected to the given mail-address, with annotatations # if SpamAssassin thinks that it is a likely spam message. # # For example, in your .qmail file: # |ifspamh -isspam # ./Mailbox # (or however you want to deliver the mail if it isn't a spam message) # # This will forward anything that SpamAssassin deems spam to the # given address. # # in your .qmail-isspam file: # ./Mail/isspam # (or wherever you want to save the Spam mail) # # For more details on SpamAssassin, how to set preferences, whitelists, # etc, see 'Mail::SpamAssassin::Conf(3)' - and put the preferences # in ~/.spamassassin/user_prefs # # For more detail on .qmail files, see dot-qmail(5) and qmail-command(8) # # Author: James R Grinter # jrg@watching.org 24/03/2002 # Last Update: 28/02/2003 # URL: http://www.gbnet.net/~jrg/qmail/ifspamh/ # # Requires: spamc, spamd, qmail and 822field (from DJB's mess822 package) # to be installed and working. # # Also requires your /bin/sh to have a "printf" available # (most do, if yours doesn't then consider fetching ksh-93 # from http://www.research.att.com/sw/download/, or using bash) # # N.B. # If you are using vpopmail, make sure you are using at least # version 5.3.6. # # If you want to run SpamAssassin globally for every email, see the # qmail-spamc/qmail-scanner approach in the qmail/ subdirectory of the # SpamAssassin distribution # # spamc - client location SPAMC=/usr/local/bin/spamc # qmail's forward program location FORWARDBIN=/var/qmail/bin/forward # mess822 822field location M822FIELD=/usr/local/bin/822field ################################################################# # nothing beyond here should require adjustment ################################################################# FORWARD="$1" if [ -z "$FORWARD" ]; then echo "Usage: ifspamh [address]" exit 111 fi # we used to skip 'From ' - the 1st line that got added by spamd/spamc # but it seems newer versions don't add it (and SA 2.50 adds a 3 line # Received header) output="`$SPAMC | sed '1{/^From .*/d;}'`" exitcode=$? # spamc will not process a "large email" msize=`echo "$output" | wc -c` # there's also a sizelimit with some shells that triggers around the 512kB mark # with an external printf and arguments size so we stop at this point if [ $msize -gt 250000 ]; then # probably deemed too large anyway .. let it through exit 0 fi flagvalue=`printf "%s\n" "$output" | $M822FIELD X-Spam-Flag | sed 's/^ //'` # X-Spam-Flag might contain "YES" if [ "$flagvalue" = "YES" ]; then # match - likely spam # no match - not a spam # forward on the email printf "%s\n" "$output" | $FORWARDBIN $FORWARD if [ $? -eq 0 ]; then # so qmail will not do any further deliveries in .qmail file exit 99 fi # problem calling inject - temp failure exit 111 else # spamc doesn't distinguish temporary failure # if we're passing through the message # so look for signs that spamd provided the output.. flag2value=`printf "%s\n" "$output" | $M822FIELD X-Spam-Status` exitcode2=$? if [ $exitcode2 -ne 0 ]; then # X-Spam-Status header not present in message -> failure of spamc/spamd? # spamc will not process a "large email", but this is accounted for above # so indicate temporary failure echo "spamc returned temporary failure" exit 111 fi # indicate that qmail should continue processing dot-qmail file exit 0 fi