Wednesday, October 8, 2008

spamchk

The original spamchk filter used by postfix is described on http://www.akadia.com/services/postfix_spamassassin.html. Remebered to post some advanced settings i made, you find it usefull if you want to split email to spam, nospam and inbetween (lightspam).

#!/bin/sh
# -----------------------------------------------------------------
# File:        spamchk
# Purpose:     SPAMASSASIN shell-based filter
# Location:    /usr/local/bin
# Usage:       Call this script from master.cf (Postfix)
# Certified:   GENTOO Linux, Spamassassin 3.0, Postfix
# -----------------------------------------------------------------

# Variables
#SENDMAIL="/usr/local/postfix/sendmail/sendmail -i"
SENDMAIL="/usr/sbin/sendmail -i"
EGREP=/bin/egrep

# Exit codes from
EX_UNAVAILABLE=69

# Number of *'s in X-Spam-level header needed to sideline message:
# (Eg. Score of 5.5 = "*****" )
SPAMLIMIT=4.5
SPAMLIMITKILL=6.1

# Clean up when done or when aborting.
trap "rm -f /var/tempfs/out.$$" 0 1 2 3 15

# Pipe message to spamc
cat | /usr/bin/spamc -s 1024000 -u filter > /var/tempfs/out.$$

#----- advanced AWK filter by simakas --------------------------------
# 050915 AWK part and splitting method to light_spam & spam

STATUS=`more /var/tempfs/out.$$ | grep -m 1 X-Spam-Status | awk '{print $3}' | awk -F= '{print $2}'`
warn=$(LC_ALL=C  awk 'BEGIN{if('$STATUS'>='$SPAMLIMIT')print "SPAM";else print "NOSPAM"}')
kill=$(LC_ALL=C  awk 'BEGIN{if('$STATUS'>='$SPAMLIMITKILL')print "SPAM";else print "NOSPAM"}')

# Are there more than $SPAMLIMIT stars in X-Spam-Level header? :
#if $EGREP -q "^X-Spam-Level: \*{$SPAMLIMIT,}" < /var/tempfs/out.$$
if [ "$warn" = "SPAM" ]
then
  # Option 1: Move high scoring messages to sideline dir so
  # a human can look at them later:
  # mv out.$$ $SIDELINE_DIR/`date +%Y-%m-%d_%R`-$$
  # Option 2: Divert to an alternate e-mail address:
  # 050915 Drop mail if SPAMLIMITKILL is reached or deliver it to human recheck
  #  if $EGREP -q "^X-Spam-Level: \*{$SPAMLIMITKILL,}" < /var/tempfs/out.$$
     if [ "$kill" = "SPAM" ]
     then
       #Drop it like its hot
       #cp /var/tempfs/out.$$ /tmp/`date +%Y-%m-%d_%R`-$$
       rm -f /var/tempfs/out.$$
     else
       #Or deliver to trashbox for human action
       $SENDMAIL trash@svelita.lt  < /var/tempfs/out.$$
     fi
  # Option 3: Delete the message
  #rm -f /var/tempfs/out.$$
else
  $SENDMAIL "$@" < /var/tempfs/out.$$
fi
# Postfix returns the exit status of the Postfix sendmail command.
exit $?

No comments: