/* MCF_PROTECT.AMIRX
// $VER: MCF_PROTECT.AMIRX 3.0 (18.05.96)
\\ Written by Donald T. Becker (dtbecker@prolog.net) IRC: StarDustr
// Please mail any bug reports/comments to the above address with a subject
\\ header of MCF.AMIRX.
//
\\ ** What to do with this file?
// Put this script in REXX:		It will be used by MCF.AMIRX.
*/

parse arg MCFenv junk

if ~show('L', "rexxsupport.library")			then do
	if ~addlib("rexxsupport.library", 0, -30, 0)	then do
		interpret "address" MCFenv '"echo P="d2c(27)"««MCF»"  "Error opening Rexxsupport.library"'
		exit 10
	end
end
ppos = pos('.', MCFenv) + 1
Client = substr(MCFenv, ppos)
Pname = "PROTECT." || Client

if ~openport(Pname)					then do
	interpret "address" MCFenv '"echo P="d2c(27)"««MCF»"  "Error opening Port "Pname'
	signal EOJ
end

EnvFName = "ENV:MCF.Protect"

if exists(EnvFName)					then call EnvLoad

if Omissions = "OMISSIONS"				then
	Omissions = ""		/* Host Names that are allowed multi-users */
if CTCPMax  = "CTCPMAX"					then
	CTCPMax  = 5		/* Maximum number of allow hits from a User/Host for CTCP Protection */
if CTCPTime = "CTCPTIME"				then
	CTCPTime = 16		/* Maximum time limit used in scanning for CTCP Flooding */
if PRIVMax  = "PRIVMAX"					then
	PRIVMax  = 5		/* Maximum number of allow hits from a User/Host for PRIV Protection */
if PRIVTime = "PRIVTIME"				then
	PRIVTime = 16		/* Maximum time limit used in scanning for PRIV Flooding */
if TEXTMax  = "TEXTMAX"					then
	TEXTMax  = 19		/* Maximum number of allow hits from a User/Host for TEXT Protection */
if TEXTTime = "TEXTTIME"				then
	TEXTTime = 45		/* Maximum time limit used in scanning for TEXT Flooding */

interpret "address" MCFenv 

do	forever
	if ~waitpkt(Pname)				then iterate
	xx = getpkt(Pname)
	if xx = '00000000'x				then iterate
	yy = getarg(xx)
	call reply(xx, 0)
	parse value yy with FType UName UHost junk
	if FType = "%QUIT"				then leave
	UxName = translate(UName, "ZZ", "-~")
	UxHost = translate(UHost, "ZZ", "-~")
	UiHost = "*!*@" || UHost
	Omit ="N"
	if 0 ~= pos(UHost, Omissions)			then do
		UiHost = "*!" || UName || "@" || UHost
		UxHost = UxName || UxHost
		Omit ="Y"
	end
	if FType = "DROP"				then do
		"Say /UnIgnore "UiHost
		UHost1 = "CTCP" || UxHost
		UHost2 = "PRIV" || UxHost
		UHost3 = "TEXT" || UxHost
		interpret "DROP " UHost1
		interpret "DROP " UHost2
		interpret "DROP " UHost3
		iterate
	end

	LDName = FType || UxHost
	if value(LDName) = 'Y'				then iterate

	tme = time(s)
	if upper(LDName) = value(LDName)		then do
		interpret LDName "= tme 1"
		iterate
	end

	interpret "parse value "value(LDName)" with ltme lcnt junk"
	if ltme > tme					then tme = tme + 86400
	diff = tme - ltme

	xtime = FType || Time
	if diff > value(xtime)				then do
		interpret LDName "= tme 1"
		iterate
	end

	xmax = FType || Max
	lcnt = lcnt + 1
	if lcnt > value(xmax)				then do
		"Say /Ignore "UiHost FType
		interpret LDName "= 'Y'"
		iterate
	end
	interpret LDName "= ltme lcnt"
end

EOJ:
	call closeport(Pname)
exit

EnvLoad:
	validnames = "OMIT CTCPMAX CTCPTIME PRIVMAX PRIVTIME TEXTMAX TEXTTIME"
	Omissions = ""
	call open("Input", EnvFName, 'R')
	xx = upper(readln("Input"))
	do	until eof("Input")
		parse value xx with envname envvalue junk
		if 0 ~= pos(envname, validnames)		then
			if envname = "OMIT"			then
				Omissions = Omissions envvalue
			else	interpret envname "= envvalue"
		xx = upper(readln("Input"))
	end
	call close("Input")
return

