#!/bin/sh

# send alert to administrator about emails mailscanner is 
# having problems processing
#
# created:	Jerry Benton <mailscanner@mailborder.com>
#			28 APR 2016

ms_conf=/etc/MailScanner/MailScanner.conf

# basic config file
if [ -f /etc/MailScanner/defaults ] ; then
	. /etc/MailScanner/defaults
else
	logger -i -p mail.notice "ERROR: ms-msg-alert missing configuration file /etc/MailScanner/defaults"
	echo "Aborted: missing configuration file /etc/MailScanner/defaults"
    exit 1
fi

noticesfrom=`/usr/sbin/ms-peek noticesfrom ${ms_conf}`
localpostmaster=`/usr/sbin/ms-peek localpostmaster ${ms_conf}`
noticesto=`/usr/sbin/ms-peek noticesto ${ms_conf}`
sendnotices=`/usr/sbin/ms-peek sendnotices ${ms_conf}`

# Only do this if they want to send notices to postmaster at all
if [ "x$sendnotices" = "xno" ]; then
  exit 0
fi

# If there was a / at the start of the destination email address, then the
# setting was a ruleset which ms-peek does not support. So use the
# localpostmaster setting instead, it's better than creating an error.
if ( echo $noticesto | grep -q '^/' ); then
  noticesto=$localpostmaster
fi

alert=`/usr/sbin/MailScanner --processing`

if [ \! "x$alert" = "x" ]; then
  #cat <<EOM
  sendmail -oem -t <<EOM
From: $noticesfrom <$localpostmaster>
To: Administrator <$noticesto>
Subject: Problem Messages

$alert

-- 
MailScanner
EOM
fi
