#!/bin/bash
# 
# ***** BEGIN LICENSE BLOCK *****
# Version: MPL 1.1
# 
# The contents of this file are subject to the Mozilla Public License
# Version 1.1 ("License"); you may not use this file except in
# compliance with the License. You may obtain a copy of the License at
# http://www.zimbra.com/license
# 
# Software distributed under the License is distributed on an "AS IS"
# basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
# the License for the specific language governing rights and limitations
# under the License.
# 
# The Original Code is: Zimbra Collaboration Suite Server.
# 
# The Initial Developer of the Original Code is Zimbra, Inc.
# Portions created by Zimbra are Copyright (C) 2005, 2006 Zimbra, Inc.
# All Rights Reserved.
# 
# Contributor(s):
# 
# ***** END LICENSE BLOCK *****
# 

autoTrainSystem() {
	spamdir=/tmp/spam.$$
	hamdir=/tmp/ham.$$
	mkdir ${spamdir}
	mkdir ${hamdir}
	/opt/zimbra/bin/zmspamextract -s -o ${spamdir}
	/opt/zimbra/bin/zmspamextract -n -o ${hamdir}

#	${zimbra_home}/libexec/sa-learn -p ${zimbra_home}/conf/salocal.cf \
#		--dbpath ${zimbra_home}/amavisd/.spamassassin/ --import

	${zimbra_home}/libexec/sa-learn -p ${zimbra_home}/conf/salocal.cf \
		--dbpath ${zimbra_home}/amavisd/.spamassassin/ -L --no-sync \
		--siteconfigpath ${zimbra_home}/conf/spamassassin --spam ${spamdir}

	${zimbra_home}/libexec/sa-learn -p ${zimbra_home}/conf/salocal.cf \
		--dbpath ${zimbra_home}/amavisd/.spamassassin/ -L --no-sync \
		--siteconfigpath ${zimbra_home}/conf/spamassassin --ham ${hamdir}

	${zimbra_home}/libexec/sa-learn -p ${zimbra_home}/conf/salocal.cf \
		--siteconfigpath ${zimbra_home}/conf/spamassassin \
		--dbpath ${zimbra_home}/amavisd/.spamassassin/ --sync

	#for f in ${spamdir}/*; do
		#test -f ${f} && ${zimbra_home}/dspam/bin/dspam_corpus --addspam zimbra ${f}
	#done
	#for f in ${hamdir}/*; do
		#test -f ${f} && ${zimbra_home}/dspam/bin/dspam_corpus zimbra ${f}
	#done

#	/opt/zimbra/dspam/bin/dspam_clean -p0 $USER

	/bin/rm -rf ${spamdir} ${hamdir}
}

if [ x`whoami` != xzimbra ]; then
    echo Error: must be run as zimbra user
	exit 1
fi

source `dirname $0`/zmshutil || exit 1
zmsetvars \
	zimbra_home \
	zimbra_server_hostname

# No argument mode uses zmspamextract for auto-training.
if [ x$1 = "x" ]; then
	autoTrainSystem
	exit
fi

if [ x$1 = "x--cleanup" ]; then
	mydir=/tmp/cleanup.$$
	mkdir -p ${mydir}
	/opt/zimbra/bin/zmspamextract -s -o ${mydir} -d
	/opt/zimbra/bin/zmspamextract -n -o ${mydir} -d
	/bin/rm -rf ${mydir}
	exit
fi


SERVER=$1
USER=$2
PASS=$3
MODE=$4
FOLDER=$5

if [ x$MODE = "x" -o x$PASS = "x" -o x$USER = "x" -o x$SERVER = "x" ]; then
	echo "Usage: $0 <server> <user> <pass> <spam|ham> [folder]"
	exit 1
fi

if [ ! -f /usr/bin/fetchmail ]; then
	exit 1;
fi

echo "server $SERVER protocol IMAP user $USER@$SERVER password $PASS keep" > ${zimbra_home}/conf/.fetchmailrc
chmod 600 ${zimbra_home}/conf/.fetchmailrc

export HOME=${zimbra_home}/conf


if [ "x$FOLDER" != "x" ]; then
	/usr/bin/fetchmail -f ${zimbra_home}/conf/.fetchmailrc -n -a -u $USER -r "$FOLDER" -m "${zimbra_home}/libexec/sa-learn -p ${zimbra_home}/conf/salocal.cf --dbpath ${zimbra_home}/amavisd/.spamassassin/ --no-sync --showdots -L --${MODE}" ${SERVER}
else
	/usr/bin/fetchmail -f ${zimbra_home}/conf/.fetchmailrc -n -a -u $USER -m "${zimbra_home}/libexec/sa-learn -p ${zimbra_home}/conf/salocal.cf --dbpath ${zimbra_home}/amavisd/.spamassassin/ --no-sync --showdots -L --${MODE}" ${SERVER}
fi

${zimbra_home}/libexec/sa-learn -p ${zimbra_home}/conf/salocal.cf --dbpath ${zimbra_home}/amavisd/.spamassassin/ --sync

if [ $MODE == "ham" ]; then
	MODE="innocent"
fi

echo "Training DSPAM"

#/usr/bin/fetchmail -f ${zimbra_home}/conf/.fetchmailrc -n -a -u $USER -r "$FOLDER" -m "/opt/zimbra/dspam/bin/dspam --user zimbra --class=${MODE} --source=corpus --mode=teft --feature=chained,noise --stdout" ${SERVER}

#/opt/zimbra/dspam/bin/dspam_clean -p0 $USER

rm -f ${zimbra_home}/conf/.fetchmailrc
