#!/bin/bash

ips()
{
	if [ -x /sbin/ipset ]; then
		IPSCMD=/sbin/ipset
	elif [ -x /usr/sbin/ipset ]; then
		IPSCMD=/usr/sbin/ipset
	fi

	if ! (${IPSCMD} $@);then
		echo "CMD: ipset $@"
	fi
}

DROPDIR=/var/lib/droplist

[ -d $DROPDIR ] || mkdir $DROPDIR

DROPLIST=$DROPDIR/drop.txt
[ -f $DROPLIST ] && rm $DROPLIST
wget "https://www.spamhaus.org/drop/drop.txt" -q -O $DROPLIST
#wget "https://pub.3gteam.hu/droplist/spamhaus-drop.txt" -q -O $DROPLIST

[ -f $DROPLIST ] || exit 124

ips flush dropset4

cat "$DROPLIST" \
 | sed -e 's/;.*//' \
 | grep -v '^ *$' \
 | sort \
 | uniq \
 | while read NB ; do
	ips add dropset4 "$NB"
done

DROPLIST6=$DROPDIR/drop6.txt
[ -f $DROPLIST6 ] && rm $DROPLIST6
wget "https://www.spamhaus.org/drop/dropv6.txt" -q -O $DROPLIST6
#wget "https://pub.3gteam.hu/droplist/spamhaus-drop6.txt" -q -O $DROPLIST6

[ -f $DROPLIST6 ] || exit 126

ips flush dropset6

cat "$DROPLIST6" \
 | sed -e 's/;.*//' \
 | grep -v '^ *$' \
 | sort \
 | uniq \
 | while read NB ; do
	ips add dropset6 "$NB"
done
