#!/bin/bash

#############
# Checks sender IP versus RBL from spamcop.net
# Depends on "rblcheck" (mail-filter/rblcheck on gentoo)
# (c) 2007,2008 by Thomas Foerster <tf@1agency.de>
#############

TO="$SMTPRCPTTO"
FROM="$SMTPMAILFROM"
IP="$TCPREMOTEIP"
AUTHU="$SMTPAUTHUSER"

if [ "$IP" == "" ]
then
  echo "IP-CHECK: No IP seen" >&2
  echo "N"
  exit 0
fi

if [ "$AUTHU" != "" ]
then 
    # SMTP-Verbindung über AUTH -> Regulärer User will senden
    echo "IP-CHECK: AUTHED User $AUTHU OK" >&2
    echo "N" # next - accept current SMTP command (do not execute remaining plugins for this command)
else
    # Standard-SMTP-Verbindung -> Spamcheck!
    /usr/bin/rblcheck -c -m -q -s bl.spamcop.net "$IP" 2>&1 >/dev/null
    
    if [ $? != 0 ]
    then
        echo "IP-CHECK: IP blacklisted: $TO ( from: $FROM, ip: $IP )" >&2
        echo "R553 sorry, spam is not accepted. See http://www.spamcop.net/w3m?action=checkblock&ip=$IP (#5.7.1)"
        exit 0
    else
      echo "N"
      exit 0
    fi
fi

