/* KickBan v1.1 for AmIRC (c)1996 by George Stagakis. IRC: Gallant */

/*
** Features:
**
** KICKBAN: Deops, Smart Bans, Kicks <nick>
** BAN:     Smart Bans <nick>
** KICK:    Kicks OPs or does a MASS Kick on a channel.
** IGNORE:  Ignores <nick> Smart
*/

/*
** Don't forget to /ALIAS KB /RX KickBan.AMIRX KBan %p
**                 /ALIAS B /RX KickBan.AMIRX Ban %p
**                 /ALIAS K /RX KickBan.AMIRX Kick %p
**                 /ALIAS I /RX KickBan.AMIRX Ignore %p
**
** Many thanks to CDiMa for giving me his old code which was for ban mask
** creation. There were many bugs there... Now works fine :)
**
** Also many thanks to SimD, CobraLSD and Stainless for test and debug!
**
** Many greets to all the people from #amiga and to all the developers of
**                        _   _  _  __ __    _         / /
**                       /|  /| /|  / / __  /|        / /
**                     _/¯|_/ |/ |_/_ \_/ _/¯|_  \ \ / /
**                                                \_X_/
** Future: I will add Smart Ban on OPSKick and MASSKick so that they can no
**         longer /JOIN #channel
*/

options results

parse arg cmd' 'args

if upper(cmd) ~= 'KICK' then do

if args = '' & upper(cmd) = 'KBAN' then do
	'echo P='d2c(27)'b«Error» Usage: /KB [channel] nick [-n,-h,-d,-c] [reason/-r]'
	exit
end
if args = '' & upper(cmd) = 'BAN' then do
	'echo P='d2c(27)'b«Error» Usage: /B [channel] nick [-n,-h,-d,-c]'
	exit
end
if args = '' & upper(cmd) = 'IGNORE' then do
	'echo P='d2c(27)'b«Error» Usage: /I nick [-n,-h,-d,-c]'
	exit
end

if upper(cmd) ~= 'IGNORE' then do
	if left(args, 1) = '#' then do
		parse var args channel' 'nickname' 'reason
	end
	if left(args, 1) ~= '#' then do
		parse var args nickname' 'reason
		getchannel
		channel = RESULT
	end
end
if upper(cmd) = 'IGNORE' then do
	parse var args nickname' 'reason
end

if upper(cmd) ~= 'KBAN' & upper(cmd) ~= 'IGNORE' & upper(cmd) ~= 'BAN' then do
	'echo P='d2c(27)'b«Error» Usage: /KB [channel] nick [-n,-h,-d,-c] [reason/-r]'
	exit
end

getmynick
mynick = RESULT
if upper(mynick) = upper(nickname) then do
	if upper(cmd) = 'KBAN' then 'echo P='d2c(27)'b«Error» You cannot KickBan yourself!'
	if upper(cmd) = 'BAN' then 'echo P='d2c(27)'b«Error» You cannot Ban yourself!'
	if upper(cmd) = 'IGNORE' then 'echo P='d2c(27)'b«Error» You cannot Ignore yourself!'
	exit
end

userhost nickname
uhost = RESULT

opts = left(reason, 2)

parse var uhost 1 flag 2 user'@'host
parse var host a1'.'a2'.'a3'.'a4

if ~verify(flag,'+-~','m') then user = flag || user

if upper(user) = upper(mynick) then exit

if datatype(a4,'WHOLE') then
	domainmask = a1'.'a2'.'a3'.*'
else do
	if (a3 ~= "") then do
		parse var host .'.'host2
		country = right(host2, 3)
		if right(country, 1) ~= '.' then country = right(host2, 4)
		parse var country '.'country
	end
	domainmask = '*.'host2
end
if length(user) < 10 then banmask = '*!*'user'@'domainmask
if length(user) = 10 then banmask = '*!'user'@'domainmask
if upper(right(host,11)) = 'DEMON.CO.UK' then banmask = '*!*@'host

if upper(opts) = '-N' then do
	parse var reason opt' 'reason
	banmask = nickname'!*@*'
	reason = 'Change that nick!'
end
if upper(opts) = '-H' then do
	parse var reason opt' 'reason
	banmask = '*!*@'domainmask
	reason = 'Your host is so lame!'
end
if upper(opts) = '-D' then do
	parse var reason opt' 'reason
	banmask = '*!*@'host
end
if upper(opts) = '-C' then do
	parse var reason opt' 'reason
	banmask = '*!*@*.'country
	reason = 'I really hate your country!'
	if heisnum = 1 then do
		'echo P='d2c(27)'b«Error» User does not have a .country suffix!'
		exit
	end
end

if upper(reason) = '-R' then do
	reason.1 = "Go away!"
	reason.2 = "Goodbye!"
	reason.3 = "Fuck off!"
	reason.4 = "And don't come back!"
	reason.5 = "Bye bye!"
	reason.6 = "Cya!"
	reason.7 = "Who let you in?"
	call random(,,TIME("S"))
	picked = random(1,7)
	reason = reason.picked
end

if upper(cmd) = 'KBAN' then do
	'say /mode 'channel' -o+b 'nickname' 'banmask
	'say /kick 'channel' 'nickname' 'reason
end
if upper(cmd) = 'BAN' then do
	'say /ban 'channel' 'banmask
end
if upper(cmd) = 'IGNORE' then do
	'say /ignore 'banmask' ALL'
end

exit

end

if upper(cmd) = 'KICK' then do

kickstat = ''

if args = '' then do
	'echo P='d2c(27)'b«Error» Usage: /K nick/-ops/-mass [reason/-r]'
	exit
end

parse var args nickorcmd' 'reason
getchannel
channel = RESULT

getmynick
mynick = RESULT

if upper(reason) = '-R' then do
	reason.1 = "Go away!"
	reason.2 = "Goodbye!"
	reason.3 = "Fuck off!"
	reason.4 = "Bye bye!"
	reason.5 = "Fuck out!"
	call random(,,TIME("S"))
	picked = random(1,5)
	reason = reason.picked
end

if upper(nickorcmd) = '-OPS' then kickstat = 'op'
if upper(nickorcmd) = '-MASS' then kickstat = 'mass'

if kickstat = '' then do
	'say /kick 'channel' 'nickorcmd' 'reason
	exit
end

namevalid = 0
count = 0
rc = 0

do while namevalid = 0
	getuserstate count
	if rc = 5 then break
	if rc = 1 then do
		getuser count
		nick = result
		if mynick ~= nick then 'say /kick 'channel' 'nick' 'reason
	end
	if rc ~= 5 & kickstat = 'mass' then do
		getuser count
		nick = result
		if mynick ~= nick then 'say /kick 'channel' 'nick' 'reason
	end
	count = count + 1
end

if kickstat = 'op' then 'echo P='d2c(27)'b«OPSKick» Attempting to kick all channel operators.'
if kickstat = 'mass' then 'echo P='d2c(27)'b«MASSKick» Attempting to kick all users.'

exit

end
