#!/bin/sh

# This plugin demonstrates the idea of using plugins to return a whitelist
# code. In this instance, it checks the source IP against a list of trusted
# sender IP addresses, and returns -2000 (act as whitelisted) if a match is
# found.

# It expects to find a file /etc/mailstripper/trusted_ips.dat, which contains
# a list of IP addresses to always accept mail from. If you wish to
# accept all mail from Eridani, for instance, you'd add the IP '62.49.18.88'
# to the list - note that unlike the badips file there is no trailing . after
# the IP address.

# Written by Michael McConnell, Eridani Star System. This demo file is in the
# public domain. Feel free to use this script as a template for your own.

# We are not concerned with the last two options for this demo, this is
# purely for completeness
MSTRIP_CMD=$1
MSTRIP_VER=$2
MSTRIP_SRCIP=$3
MSTRIP_RMSG=$4
MSTRIP_SMSG=$5

# Identify which grep to use.
if [ -f /usr/xpg4/bin/grep ]; then
  GREP_CMD=/usr/xpg4/bin/grep
elif [ -f /usr/bin/grep ]; then
  GREP_CMD=/usr/bin/grep
else
  GREP_CMD=/bin/grep
fi

# Does our grep recognise -F? If so we should use it. All the greps tested
# so far use exit code 0 for a match, 1 for no match and 2 for an invocation
# error, e.g. unrecognised option.
$GREP_CMD -F grep $0 >/dev/null 2>1
RES=$?
if [ $RES -eq 2 ]; then
  GREP_OPTS=
else
  GREP_OPTS=-F
fi

if [ "x$MSTRIP_CMD" = "x-id" ]; then
  echo "Trusted IPs Plugin"
  echo "0.2.0"
  echo "Eridani Star System, this demo is in the public domain"
  if [ "x$MSTRIP_VER" = "x" ]; then
    echo "No"
  else
    echo "Yes"
  fi
elif [ "x$MSTRIP_CMD" = "x-scan" ]; then
  if [ ! -f /etc/mailstripper/trusted_ips.dat ]; then
    echo "#Error, unable to open trusted IPs list"
    echo "0"
    exit
  fi
  
  $GREP_CMD $GREP_OPTS $MSTRIP_SRCIP /etc/mailstripper/trusted_ips.dat >/dev/null 2>&1
  RES=$?

  if [ $RES -eq 0 ]; then
    echo "-2000"
  else
    echo "0"
  fi
fi

