#!/bin/sh

# Script to retrieve Blacklists from the net and put
# them into a usable dataformat

# Written by Christoph Lameter <clameter@debian.org> Oct 25, 1997
# improved and modularised by Craig Sanders <cas@taz.net.au> 19971122

source /usr/lib/spamdb/constants
source /etc/spamdb/config

[ -n "$http_proxy" ] && export http_proxy
[ -n "$ftp_proxy" ] && export ftp_proxy

cd $SPAMDB
tmpfile=$SPAMDB/$$.tmp

fetch=0 ; generate=0 ; convert=0

case $1 in 
	-f) fetch=1 ;;
	-g) generate=1 ;;
	-c) convert=1 ;;
	*) fetch=1 ; generate=1 ; convert=1 ;;
esac

if [ $fetch -eq 1 ] ; then 
	# Retrival of blacklists. 
	#   execute each script in /usr/lib/spamdb/fetchers.  They can use whatever
	#   method they like to retrieve the list.  The script should save the
	#   spamdomains file in $INCDIR/SCRIPTNAME.domains
	#  
	#   Future versions will include support for spammers and spamnets
	#   files as well as spamdomains.

	run-parts /usr/lib/spamdb/fetchers

	# if you are porting this to a non-debian system then run-parts can
	# be emulated with:
	#    for i in /usr/lib/spamdb/fetchers/* ; do 
	#        $i 
	#    done
fi

if [ $generate -eq 1 ] ; then 
	# merge spamdomains lists.  ignore comments.  convert to lowercase. 
	# uniq them.
	cat $INCDIR/*.domains $INCDOM | \
		sed -e '/^#/d' \
			-e 's/#.*$//' \
			-e 's/[ 	]//g' \
			-e '/^$/d' | \
		tr 'A-Z' 'a-z' | \
		sort -u > $tmpfile

	# Now convert the exclusion list to a sed script...
	cat $EXCDOM | \
		sed -e '/^#/d' \
			-e 's/#.*$//' \
			-e 's/[ 	]//g' \
			-e '/^$/d' | \
		tr 'A-Z' 'a-z' | \
		sort -u | \
		sed -e 's:\(.*\):/\1\$/d:' >$tmpfile.sed

	# and run it...
	sed -f $tmpfile.sed <$tmpfile >$DOMAINS_FILE

	# merge spammers lists.  ignore comments.  convert to lowercase. uniq them.
	cat $INCDIR/*.spammers $INCSPM | \
		sed -e '/^#/d' \
			-e 's/#.*$//' \
			-e 's/[ 	]//g' \
			-e '/^$/d' | \
		tr 'A-Z' 'a-z' | \
		sort -u > $tmpfile

	# Now convert the exclusion list to a sed script...
	# also exclude domains from Spammers which are already listed in SpamDomains
	# (good idea, Joey)
	cat $EXCSPM $DOMAINS_FILE | \
		sed -e '/^#/d' \
			-e 's/#.*$//' \
			-e 's/[ 	]//g' \
			-e '/^$/d' | \
		tr 'A-Z' 'a-z' | \
		sort -u | \
		sed -e 's:\(.*\):/\1\$/d:' >$tmpfile.sed

	# and run it...
	sed -f $tmpfile.sed <$tmpfile >$SPAMMERS_FILE

	# merge spamnets lists.  ignore comments.  convert to lowercase. uniq them.
	# note that the spamdb doesn't currently use this.  wait for exciting
	# developments in the next release.
	cat $INCDIR/*.spamnets $INCNET | \
		sed -e '/^#/d' \
			-e 's/#.*$//' \
			-e 's/[ 	]//g' \
			-e '/^$/d' | \
		tr 'A-Z' 'a-z' | \
		sort -u > $tmpfile

	# Now convert the exclusion list to a sed script...
	cat $EXCNET | \
		sed -e '/^#/d' \
			-e 's/[ 	]//g' \
			-e 's/#.*$//' \
			-e '/^$/d' | \
		tr 'A-Z' 'a-z' | \
		sort -u | \
		sed -e 's:\(.*\):/\1\$/d:' >$tmpfile.sed

	# and run it...
	sed -f $tmpfile.sed <$tmpfile >$NETS_FILE


	# now clean up...
	rm -f $tmpfile $tmpfile.sed
fi


if [ $convert -eq 1 ] ; then 
	# Process blacklists into a usable .db database
	if [ -n "$CONVERTER" ]; then
		if [ -x /usr/lib/spamdb/converters/$CONVERTER ] ; then
			 /usr/lib/spamdb/converters/$CONVERTER 
		else
			 echo "/usr/lib/spamdb/converters/$CONVERTER does not exist"
		fi
	else
		cat <<__EOF__
Cannot build host map for your mailer. Configure a converter by editing
/etc/spamdb/config.  Then run '/usr/sbin/spamdb -c'
__EOF__
	fi
fi
