/*
just copy ban.wizard and bang.amirx to your rexx drawner of Amirc2
in Amirc do a /rx ban and a window will be opened, if a nick is selected
that nick will be the active, if you select a nick you can make it active
with the Selected gadget.
*/

signal on syntax
parse arg n

options results

res=OpenSurface("rexx/ban.wizard","K&B")
if res~=0 then call error(res)

win.PubSCreen="IRC"
win.FallBack=1
win.Gads=22
win.ID=1
res=OpenWindow("K&B","K&BW","WIN")
if res~=0 then call error(res)

global.reasone = ""
global.MaskType = 0
call selected(n)

stop=0
do while ~stop
	stop=Handle("K&BW")
	say "Nick:    " global.nick
	say "Reasone: " global.reasone
	say "Mask:    " global.mask
	say "MaskType:" global.MaskType
end

call CloseWindow("K&B","K&BW")
call SnapShot("K&B")
exit

/**************************************************************************/
error:
parse arg code
	string=GetRxWizString(code)
	if string=="" then string="(codice sconosciuto: " || code || ")"
	if RXWIZERR~="RXWIZERR" then string = string  "(field:" RXWIZERR || ")"
	"ECHO" string "(" || "linea" SIGL-1 ||")"
	exit
/**************************************************************************/
handle: PROCEDURE expose global.
parse arg o
	s=0
	h.wait=1
	res=HandleWindow("K&B",o,"H")
	if res~=0 then call error(res)
	select
		when h.imsg>0 then
			do i=0 to h.imsg-1
				select
					when h.i.class=="CLOSEWINDOW" then s=1
					when h.i.class=="IDCMPUPDATE" then do
						select
							when h.i.ObjectID=="NICK" then global.nick=h.i.string
							when h.i.ObjectID=="REASONE" then global.reasone=h.i.string
							when h.i.ObjectID=="BANMASK" then global.banMask=h.i.string
							when h.i.ObjectID=="SELECTED" then call selected
							when h.i.ObjectID=="OP" then call op
							when h.i.ObjectID=="WHOIS" then call whois
							when h.i.ObjectID=="DEOP" then call deop
							when h.i.ObjectID=="KICK" then call kick
							when h.i.ObjectID=="BAN" then call ban
							when h.i.ObjectID=="MASKTYPE" then do
								global.MaskType=h.i.CycleActive
								global.banMask=makeMask()
								set.string=global.banMask
								call SetWizAttrs("K&B","K&BW","BANMASK","SET")
								drop set.
							end
							when h.i.ObjectID=="KBAN" then call kban
							otherwise nop
						end
					end
					otherwise nop
				end
			end
		otherwise nop
	end
	return s
/**************************************************************************/
whois: procedure expose global.
	if global.nick=="" then return
	"RAW WHOIS" global.nick
	return
/**************************************************************************/
op: procedure expose global.
	if global.nick=="" then return
	"SAY /OP" global.nick
	return
/**************************************************************************/
deop: procedure expose global.
	if global.nick=="" then return
	"SAY /DEOP" global.nick
	return
/**************************************************************************/
kick: procedure expose global.
	if global.nick=="" then return
	"SAY /KICK" global.nick global.reasone
	return
/**************************************************************************/
ban: procedure expose global.
	if global.nick=="" | global.banMask="" then return
	"SAY /BAN" global.banMask
	return
/**************************************************************************/
kban: procedure expose global.
	if global.nick=="" | global.banMask="" then return
	if a="" then return
	"SAY /DEOP" global.nick
	"SAY /BAN" global.banMask
	call kick
	return
/**************************************************************************/
selected: procedure expose global.
parse arg n
	if n=="" then do
		"GETSELECTEDUSER"
		if rc==5 then return
		global.nick=result
	end
	else global.nick=n
	set.string=global.nick
	call SetWizAttrs("K&B","K&BW","NICK","SET")
	global.banMask=makeMask()
	set.string=global.banMask
	call SetWizAttrs("K&B","K&BW","BANMASK","SET")
	return
/**************************************************************************/
syntax:
	"ECHO" rc sigl
	exit
/**************************************************************************/
makeMask: procedure expose global.
	"USERHOST" global.nick
	if rc==5 then return ""
	u=result
	parse var u user"@"rest
	if pos(left(user,1),"~-")~=0 then parse var user +1 user
	select
		when global.MaskType==0 then do
			mask="*"global.nick"*!*@*"
			return mask
		end
		when global.MaskType==1 then do
			mask="*!*"user"*@*"
			return mask
		end
		when global.MaskType==2 then do
			mask="*!*@"rest
			return mask
		end
		when global.MaskType==3 then do
			if IsDotAddr(rest) then return "*!*@"rest
			mask="*!*@"
			do i=1 to length(rest)
				if DataType(substr(rest,i,1),"N") then do
					mask=mask||"*"
					do while DataType(substr(rest,i,1),"N");i=i+1;end
				end
				mask=mask||substr(rest,i,1)
			end
			return mask
		end
		when global.MaskType==4 then do
			if IsDotAddr(rest) then do
				parse var rest p1"."p2"."
				mask="*!*@"p1"."p2".*"
			end
			else do
				p=LastPos(".",rest)
				mask="*!*@*."right(rest,length(rest)-p)
			end
			return mask
		end
		otherwise nop
	end
	return u
/**************************************************************************/

