egscripts/egspamtraining/egspamtraining

79 lines
2.2 KiB
Bash
Executable file

#!/bin/bash
#
# egspamtraining
#
# (c) 2009 Egressive Limited
# Spam and ham training for spam assassin
# by ross@egressive.com
# Command definitions
CAT_COMMAND=`which cat`
MKDIR_COMMAND=`which mkdir`
CP_COMMAND=`which cp`
SUDO_COMMAND=`which sudo`
CHOWN_COMMAND=`which chown`
MAIL_COMMAND=`which mail`
RM_COMMAND=`which rm`
HEAD_COMMAND=`which head`
TAIL_COMMAND=`which tail`
DIRNAME_COMMAND=`which dirname`
BASENAME_COMMAND=`which basename`
SA_LEARN_COMMAND=`which sa-learn`
#defining conf file
EGST_DIR=`$DIRNAME_COMMAND $0`
EGST_SCRIPT=`$BASENAME_COMMAND $0`
CONF_FILE=$EGST_DIR/$EGST_SCRIPT.conf
TRAIN_TYPE=$1 #ham or spam training
CONF2_FILE=$EGST_DIR/$1.conf
MAIL_TO=root # placeholder, defined in egspamtraining.conf
# checking for egspamtraining.conf
if [ -f $CONF_FILE ] ; then
. $CONF_FILE
else
echo "Cannot find conf file" | $MAIL_COMMAND -s "[ERROR] Could not locate conf file for Spam/Ham training" $MAIL_TO
exit 1
fi
# Checking that spam/ham was specified when executing script
if [ "$TRAIN_TYPE" == "spam" ] ; then
echo "Spam Training" >> $LOGFILE
elif [ "$TRAIN_TYPE" == "ham" ] ; then
echo "Ham Training" >> $LOGFILE
else
echo "Training type not specified" | $MAIL_COMMAND -s "[ERROR] Training not specified" $MAIL_TO
exit 1
fi
if ! [ -f $CONF2_FILE ] ; then
echo "Cannot find $CONF2_FILE" | $MAIL_COMMAND -s "[ERROR] Could not locate $CONF2_FILE for $TRAIN_TYPE training" $MAIL_TO
exit 1
fi
# Here the training begins
echo `date` >> $LOGFILE
$MKDIR_COMMAND $TMPDIR # temp directory to copy emails to
# getting list of directories to copy emails from from ham/spam.conf and copying emails to temp directory.
LINES=2
NO_LINES=`wc -l $CONF2_FILE`
LINE_TOTAL=`expr "$NO_LINES" : '\([0-9]*\)'`
while [ "$LINES" -lt "$LINE_TOTAL" ]
do
let LINES=LINES+1
$CP_COMMAND `head -n $LINES $CONF2_FILE | tail -n 1` $TMPDIR
done
$CHOWN_COMMAND -R postfix:postfix $TMPDIR #need to change ownership of files for postfix to read
# Running the training as postfix to preserve bayes db permissions
$SUDO_COMMAND -H -u postfix $SA_LEARN_COMMAND -C $MAILSCANNER_CONF --$TRAIN_TYPE $TMPDIR/ >> $LOGFILE 2>> $LOGFILE
$RM_COMMAND -r $TMPDIR # removing temp directory
exit 0