/* GVFix WhoIs Script */

parse arg gvrexxport nick user wildcarduser master ownnick showownuserhost

address value gvrexxport

carriagereturn="0A"X
bold=""

addfriendfile="T:"||address()||"AddFriend"

friendslistfile="ENVARC:GVFix/Friends.list"
friendslistfilecopy="ENV:GVFix/Friends.list"

kicknotfriendsfile="ENV:GVFix/KICKNOTFRIENDS"

bankicknotfriendsfile="ENV:GVFix/BANKICKNOTFRIENDS"

addfriend=0

friends=""
friendsoptions=""
ctcpoptions="HELP"

showownuserhost=substr(showownuserhost,2)

if user="" then do
	"WhoIs" nick
	exit
end

if showownuserhost="Yes" then do
	"FakeCommand" "«Master» Your Own User@Host is: "||user||" which Has Master Status."
	exit
end

if exists(addfriendfile) then do
	fileopen=0
	do until fileopen=1
		if open(6addfriendfile,addfriendfile,r) then do
			fileopen=1
			friendnick=readln(6addfriendfile)
			userhosttype=readln(6addfriendfile)
			optionsandnoopunbanchannels=readln(6addfriendfile)
			call close(6addfriendfile)
		end
		else address command "Wait 1"
	end
	if upper(nick)=upper(friendnick) then do
		addfriend=1
		fileexists=1
		do until fileexists=0
			fileexists=exists(addfriendfile)
			if fileexists=1 then address command "Delete >NIL: "||addfriendfile
		end
	end
end

if addfriend=1 then do
	if master="Yes" then "FakeCommand" "«AddFriend» Adding Yourself isn't Needed Because You've Got Master Status."
	else do
		if open(6friendslistfile,friendslistfile,r) then do
			do until eof(6friendslistfile)
				userinfo=readln(6friendslistfile)
				if userinfo~="" then do
					startfrienduserhostpos=pos(" (",userinfo||" ")
					endfrienduserhostpos=pos(") ",userinfo||" ",startfrienduserhostpos)
					frienduserhost=substr(userinfo,startfrienduserhostpos+2,endfrienduserhostpos-startfrienduserhostpos-2)
					if pos("*",frienduserhost)~=0 then tempuserhost=wildcarduser
					else tempuserhost=user
					if upper(tempuserhost)=upper(frienduserhost) then do
						addfriend=0
						"FakeCommand" "«AddFriend» Your Friends.list Already Has: "||userinfo
						call seek(6friendslistfile,0,E)
					end
					friends=friends||userinfo||carriagereturn
				end
			end
			call close(6friendslistfile)
		end
		if addfriend=1 then do
			if userhosttype="*" then frienduserhost=wildcarduser
			else frienduserhost=user
			friends=friends||nick||" ("||frienduserhost||")"
			endoptionspos=pos(" ",optionsandnoopunbanchannels)
			if endoptionspos~=0 then do
				options=substr(optionsandnoopunbanchannels,1,endoptionspos-1)
				if pos("S",options)~=0 then do
					friendsoptions=friendsoptions||"S"
					if ctcpoptions~="" then ctcpoptions=ctcpoptions||" "
					ctcpoptions=ctcpoptions||"SEND"
				end
				if pos("X",options)~=0 then do
					friendsoptions=friendsoptions||"X"
					if ctcpoptions~="" then ctcpoptions=ctcpoptions||" "
					ctcpoptions=ctcpoptions||"XDCC"
				end
				if pos("I",options)~=0 then do
					friendsoptions=friendsoptions||"I"
					if ctcpoptions~="" then ctcpoptions=ctcpoptions||" "
					ctcpoptions=ctcpoptions||"INVITE"
				end
				if pos("O",options)~=0 then do
					friendsoptions=friendsoptions||"O"
					if ctcpoptions~="" then ctcpoptions=ctcpoptions||" "
					ctcpoptions=ctcpoptions||"OP"
				end
				if pos("U",options)~=0 then do
					friendsoptions=friendsoptions||"U"
					if ctcpoptions~="" then ctcpoptions=ctcpoptions||" "
					ctcpoptions=ctcpoptions||"UNBAN"
				end
				if friendsoptions~="" then friends=friends||" "||friendsoptions
				noopunbanchannels=substr(optionsandnoopunbanchannels,endoptionspos+1)
				if noopunbanchannels~="" then friends=friends||" "||noopunbanchannels
			end
			friends=friends||carriagereturn
			if open(6friendslistfile,friendslistfile,w) then do
				call writech(6friendslistfile,friends)
				call close(6friendslistfile)
				address command "Copy "||friendslistfile||" "||friendslistfilecopy
				"Notice" nick "Your User@Host Has Been Added to My Friends.list as: "||bold||frienduserhost||bold
				if exists(bankicknotfriendsfile) then "Notice" nick "Friends can Join Private Channels Without Getting Ban Kicked."
				if exists(kicknotfriendsfile) then "Notice" nick "Friends can Send CTCP XDCC List Requests Without Getting Kicked."
				if ctcpoptions~="" then do
					ctcpoptionstext="You Have "||bold||"/CTCP "||ownnick||bold||" <"||bold||"Command"||bold||"> Access to the Following Command"
					if pos(" ",ctcpoptions)~=0 then ctcpoptionstext=ctcpoptionstext||"s"
					ctcpoptionstext=ctcpoptionstext||": "||bold||ctcpoptions||bold
					"Notice" nick ctcpoptionstext
				end
			end
		end
	end
end
