#!/bin/sh -e
#
# Manage the ratelimit database
#
# $Cambridge: hermes/conf/exim/sbin/ratelimit,v 1.5 2009/07/08 18:30:16 fanf2 Exp $

PATH=/opt/exim/bin:$PATH

usage() {
	cat <<EOF
usage: ratelimit <command> <crsid | IPaddr>
	Apply the given command to all entries in the ratelimit
	database for the given user or host.
	The available commands are:
list
	show the user's ratelimit entries
clear
	remove the user's ratelimit entries
EOF
	exit 1
}

case "$2" in
"")	usage
esac

# not IP address and not CRSID => try hostname
if ! expr "$2" : '[0-9.]*$' >/dev/null &&
   ! expr "$2" : '[a-z0-9]*$' >/dev/null
then
	for A in $(dig +short A ${2#H=})
	do $0 $1 $A
	done
	exit
fi

keys="1d/per_mail/$2 1h/per_mail/$2 1d/per_rcpt/$2 1h/per_rcpt/$2"

fixdb() {
	exim_fixdb /spool/exim ratelimit | sed 's/> //'
}

case "$1" in
list)	{
		for k in $keys
		do echo $k
		done
		echo q
	} | fixdb
	exit 0
	;;
clear)	{
		for k in $keys
		do	echo $k
			echo d
		done
		echo q
	} | fixdb
	exit 0
	;;
*)	usage
	;;
esac

echo whut?
exit 1
