/* MCF.AMIRX
// $VER: MCF.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:
//
\\  What else to do with this file... (if not Auto-started by MCF_JOIN.AMIRX)
// Put this script in REXX:, connect to an IRC server and type /Rx MCF args
\\  where args are:   NOOPS/S FSERV/S NOSOUND/S NORCMDS/S GREETS/S
*/

arg argline

OPTIONS RESULTS

MCFenv = address()
xl = pos(".", MCFenv) + 1
Client = substr(MCFenv, xl)

EnvFName = "ENV:MCF.StartUp"

if exists(EnvFName)			then call EnvLoad

if 0 ~= pos("NOOPS", argline)		then OPS    = "N"
if 0 ~= pos("FSERV", argline)		then FSERV  = "Y"
if 0 ~= pos("NOSOUND", argline)		then SOUNDS = "N"
if 0 ~= pos("NORCMDS", argline)		then RCMDS  = "N"

if SoundDir = "SOUNDDIR"		then	/* Directory where sounds are stored */
	SoundDir = "Sounds:"		
if SoundPlayer = "SOUNDPLAYER"		then	/* Name of Player to use for playing sounds */
	SoundPlayer = "DH0:C/Play16"	
if FSERV = "FSERV"			then	/* AUTO switch for File Server functions */
	FSERV.Client = "N"
else	FSERV.Client = FSERV
if GREETS = "GREETS"			then	/* AUTO switch for Auto-Greetings */
	GREETS.Client = "N"
else	GREETS.Client = GREETS
if OPS = "OPS"				then	/* AUTO switch for Auto-Ops */
	OPS.Client = "Y"
else	OPS.Client = OPS
if RCMDS = "RCMDS"			then	/* AUTO switch for Remote Commands */
	RCMDS.Client = "Y"
else	RCMDS.Client = RCMDS
if SOUNDS = "SOUNDS"			then	/* AUTO switch for CTCP Sounds */
	SOUNDS.Client = "Y"
else	SOUNDS.Client = SOUNDS

OPCHAR.Client = "@"				/* AUTO Operator recognition character */

PRO = "PROTECT." || Client
CMC = "CMDS." || Client
FSC = "FSERV." || Client
GRC = "GREETS." || Client
OPC = "OPS." || Client
OCC = "OPCHAR." || Client
OLC = "OPLINE." || Client
RCC = "RCMDS." || Client
SCC = "SOUNDS." || Client

call setclip(FSC, FSERV.Client)
call setclip(OPC, OPS.Client)
call setclip(GRC, GREETS.Client)
call setclip(OCC, OPCHAR.Client)
call setclip(MCFenv, "Y")

Cname = "MCF:MCF.Cmds"
fname = "MCF:MCF.Ops"

call RU()
call RC()

address command "Run >nil: Rx MCF_PROTECT.AMIRX" MCFenv
address command WaitForPort PRO

"echo P="d2c(27)"««MCF»"  "Master Control Functions are now loaded."

do	forever
	getline
	interpret "APort = show('P', "MCFenv")"
	if APort = 0						then do
		MyNick = "X"
		signal QUIT
	end
	lpos = pos("!", line.prefix) - 1
	if lpos < 0						then iterate
	Unick = substr(line.prefix, 1, lpos)
	Rnick = upper(Unick)
	getmynick
	MyNick = upper(RESULT)
	if MyNick ~= Rnick					then
		address command "Run >nil: Rx ENV:MCF_QProtect.AMIRX " Client Mynick line.prefix line.command line.dest line.rest
	if 0 ~= pos('01'x, line.rest)				then do
		xx = upper(line.rest)
		if 0 ~= pos('01'x || "SOUND", xx)		then do
			if SOUNDS.Client = "Y"		then
				address command "Run >nil: Rx MCF_SOUND.AMIRX "MCFenv Unick SoundPlayer SoundDir line.rest
			iterate
		end
	end
	cpos = pos("%", line.rest)
	if cpos ~= 2						then iterate
	idata = upper(line.rest) || " "
	spos = pos(" ", idata) - 2
	Rcmd = substr(idata, 2, spos)
	rpos = pos(Rcmd, CmdLine)
	if rpos = 0						then iterate
	Rauth = substr(CmdLine, rpos - 3, 2)

	auth = ""
	if MyNick = Rnick					then auth = 99

	if auth = ""						then do
		npos = pos("@", line.prefix)
		Uname = upper(left(line.prefix, npos))
		UIP = upper(substr(line.prefix, npos + 1))
		apos = 1
		do cauth = 1 by 1		until auth ~= ""
			upos = pos(Uname, OpLine, apos)
			if upos = 0				then auth = 00
			if auth = ""				then do
				apos = upos + npos + 1
				ipos = pos(" ", OpLine,  apos)
				OIP = substr(OpLine, apos, ipos - apos)
				if 0 ~= pos(OIP, UIP)		then 
					auth = substr(OpLine, upos - 3, 2)
			end
		end cauth

	end

	if auth < Rauth						then iterate

	Rcmd = substr(Rcmd, 2, spos - 1)
	RxCmd = "say /Rx MCF_" || Rcmd || " "
	if Rauth = 99						then do
		interpret "CALL" Rcmd
		iterate
	end

	if RCMDS.Client ~= "Y"					then iterate

	if Rauth < 30						then do
		RxCmd auth Unick line.rest
		iterate
	end

	Dst = upper(line.dest)
	if 0 ~= pos(":", Dst)					then Dst = substr(Dst, 2)
	if MyNick = Dst						then iterate

	opnick = " " || MyNick || " "
	channels
	ChList = upper(result)
	if ChList = "" | 0 = pos(Dst, ChList)			then iterate

	do chcnt = 1 by 1		until chan.chcnt = Dst
		parse value ChList with chan.chcnt ChList
	end chcnt

	getusers chcnt
	ChUs = " " || upper(result) || " "
	opstat = pos(MyNick, ChUs)
	nostat = pos(opnick, ChUs)
	if opstat > 0 & nostat > 0				then iterate

	CName = chan.chcnt || "." || Client
	interpret "call setclip("CName", ChUs)"
	RxCmd MyNick Unick chan.chcnt line.rest

end

exit

AUTO:
	parse value idata with junk funct opt junk
	xl = pos(funct, "OPS FSERV GREETS OPCHAR RCMDS SOUNDS")
	if xl = 0						then return
	Ifunct = funct || "." || Client
	interpret Ifunct "= opt"
	if xl < 17						then
		interpret "call setclip(Ifunct, "Ifunct")"

	"echo P="d2c(27)"««MCF»"  funct" is now set to "d2c(2)value(Ifunct)
return

AU:
DU:
USERS:
	RxCmd line.rest
return

OPTS:
	"echo P="d2c(27)"««MCF»"  "FSERV  = "FSERV.Client
/*	"echo P="d2c(27)"««MCF»"  "GREETS = "GREETS.Client
*/	"echo P="d2c(27)"««MCF»"  "OPS    = "OPS.Client
	"echo P="d2c(27)"««MCF»"  "RCMDS  = "RCMDS.Client
	"echo P="d2c(27)"««MCF»"  "SOUNDS = "SOUNDS.Client
	"echo P="d2c(27)"««MCF»"  "OPCHAR = "OPCHAR.Client
return

RC:
	xx = ""
	CmdLine = ""
	call open("Input", Cname, 'R')
	do	until eof("Input")
		CmdLine = CmdLine xx
		xx = readln("Input")
	end
	call close("Input")
	CmdLine = CmdLine || " "
	CmdLine = upper(CmdLine)
	call setclip(CMC, CmdLine)
return

RU:
	xx = ""
	OpLine = ""
	call open("Input", fname, 'R')
	do	until eof("Input")
		OpLine = OpLine xx
		xx = readln("Input")
	end
	call close("Input")
	OpLine = OpLine || " "
	OpLine = upper(OpLine)
	call setclip(OLC, OpLine)
return

EnvLoad:
	validnames = "SOUNDDIR SOUNDPLAYER FSERV GREETS OPS RCMDS SOUNDS"
	call open("Input", EnvFName, 'R')
	xx = upper(readln("Input"))
	do	until eof("Input")
		parse value xx with envname envvalue junk
		if envname = "SOUNDDIR"				then do
			lc = right(envvalue, 1)
			if lc ~= ':' & lc ~= '/'		then
				envvalue = envvalue || '/'
		end
		if 0 ~= pos(envname, validnames)		then
			interpret envname "= envvalue"
		xx = upper(readln("Input"))
	end
	call close("Input")
return

QUIT:
	if APort ~= 0						then
		"echo P="d2c(27)"««MCF»"  "Quitting at the request of"d2c(2) Unick
	clips = '|' || show(c,, '|') || '|'
	loc = '.' || Client || '|'
	xl = 1
	do 		until xl = 0
		xl = pos(loc, clips, xl)
		if xl = 0		then iterate
		fl = lastpos('|', clips, xl) + 1
		len = xl - fl + 2
		cname = substr(clips, fl, len)
		call setclip(cname, "")
		xl = xl + 1
	end
	address command "Run >nil: Rx ENV:MCF_QProtect.AMIRX " Client MyNick " %QUIT"
exit

