BadNickChecker v1.2
 

BadNickChecker v1.2

Started by baba.runner, 09 July, 2008, 22:06:32

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

baba.runner

--[[BadNickChecker V1.2 for LUA 5.1x API2
	by baba.runner
		Checks the nick of the user against the table containing the prohibited strings. If match is found the user get a NickBan.
		A Big "THANK YOU" to Rincewind, who explained me and teched me a bit of scripting.
		Also a big "THANK YOU" to CrazyGuy and Snooze for their assistance.

	Update from v1.1:
		Bug fixed: Error after having added a certain number of entries
		
	Update from v1.0:
		Bug fixed: Error when adding a string to the empty table ( thx to dimetrius)
]]--

--Checking if table exists and creates atable if it does not exist. And some General settings.
function OnStartup()
	local sPXPath = Core.GetPtokaXPath().."scripts/BadNickChecker/" 
		sFile = sPXPath.."Word.txt"
	if loadfile(sFile) then
		dofile(sFile)
		else
			tWords = {}
			pxpath = Core.GetPtokaXPath():gsub("/","\\")
			os.execute("mkdir \""..pxpath.."\"\scripts\\BadNickChecker")
			SaveFile(sFile, tWords, "tWords")			
	end
	sBot = "?Sicherheits Personal?" --Core.GetHubSecAlias()	--This is the Bot-Name
	sMessage = "bad nick containing "	--The message used in the Ban
	sRC = "BadNick"	--The name for the Rightclick menu
end  

-- allowed Profiles to use Commands "1 = Yes; 0 = No"
-- View prohibited Strings
tAllowView = {
	[-1] = 1,
	[0] = 1,
	[1] = 1,
	[2] = 1,
	[3] = 1,
	[4] = 1,
	[5] = 1,
}
-- Add a prohibited String
tAllowAdd = {
	[-1] = 0,
	[0] = 1,
	[1] = 1,
	[2] = 0,
	[3] = 0,
	[4] = 1,
	[5] = 1,
}
-- Remove a prohibited String
tAllowRemove = {
	[-1] = 0,
	[0] = 1,
	[1] = 0,
	[2] = 0,
	[3] = 0,
	[4] = 1,
	[5] = 1,
}

--Check the nick if prohinited String is included
function UserConnected(user) 
	if user.iProfile == -1 then 
		for key,word in pairs(tWords) do
			local sLowerNick = string.lower(user.sNick)
			if sLowerNick:find(word) then  
				Core.SendPmToNick(user.sNick, sBot, "Your nick is now Banned for containing -> "..word.." <- !!") 
				Core.SendPmToOps(sBot," "..user.sNick.." has been NickBanned for having -> "..word.." <- in his nick!")
				BanMan.BanNick(user.sNick,sMessage..word,sBot)
				break
			end 
		end 
	end 
	if user.sNick then
		RightClick(user)
	end

end

RegConnected = UserConnected
OpConnected = UserConnected

-- Right Click for allowed Profiles
function RightClick(user)
	Core.SendToNick(user.sNick,"$UserCommand 0 3 |")
	if tAllowView[user.iProfile] == 1 then
		Core.SendToNick(user.sNick,"$UserCommand 1 3 ?"..sRC.."?\\View prohibited Strings$<%[mynick]> +vword&#124;|")
	end
	if tAllowAdd[user.iProfile] == 1 then
		Core.SendToNick(user.sNick,"$UserCommand 1 3 ?"..sRC.."?\\Add a prohibited String$<%[mynick]> +aword %[line:Enter String to be prohibited]&#124;|")
	end
	if tAllowRemove[user.iProfile] == 1 then
		Core.SendToNick(user.sNick,"$UserCommand 1 3 ?"..sRC.."?\\Remove a prohibited String by entering the String$<%[mynick]> +rwords %[line:Enter prohibited String you want to remove!]&#124;|")
	end
		if tAllowRemove[user.iProfile] == 1 then
		Core.SendToNick(user.sNick,"$UserCommand 1 3 ?"..sRC.."?\\Remove a prohibited String by entering the ID of the String$<%[mynick]> +rwordi %[line:Enter ID of prohibited String you want to remove!]&#124;|")
	end
end


-- Function Main Chat Arrival
function ChatArrival(user, data)
	local data = string.sub(data, 1, -2)
	local s,e,cmd = nil,nil,nil
	_,_,cmd = string.find(data, "%b<>%s+[%!%+%-%?%#](%S+)")
	if cmd then
		local tCmds = {
			["vword"] = function(user, data)
				if tAllowView[user.iProfile] == 1 then
					ViewStrings(user) return true
				else
					Core.SendToNick(user.sNick,"<"..sBot.."> You are not authorised to use this command") return true
				end
			end,
			["aword"] = function(user, data)
				if tAllowAdd[user.iProfile] == 1 then
					AddString(user, data, "word") return true
				else
					Core.SendToNick(user.sNick,"<"..sBot.."> You are not authorised to use this command") return true
				end
			end,
			["rwords"] = function(user, data)
				if tAllowRemove[user.iProfile] == 1 then
					RemoveString(user, data, "word") return true
				else
					Core.SendToNick(user.sNick,"<"..sBot.."> You are not authorised to use this command") return true
				end
			end,
			["rwordi"] = function(user, data)
				if tAllowRemove[user.iProfile] == 1 then
					RemoveID(user, data, "word") return true
				else
					Core.SendToNick(user.sNick,"<"..sBot.."> You are not authorised to use this command") return true
				end
			end,
		}
		
		if tCmds[cmd] then
			return tCmds[cmd](user, data)
		end
	end
	
end

ToArrival = ChatArrival

-- View All The Prohibited Strings
function ViewStrings(user)
	sWordList = "!Prohibited Strings are:  \r\n"
	if table.maxn(tWords) >= 1 then
		for key,word in pairs(tWords) do
			sWordList = sWordList.."\tID: "..key.."\t->\t"..word.."\r\n"
		end
	else
		sWordList = sWordList .."\t No Strings yet defined. The table is empty!"
	end
	Core.SendPmToNick(user.sNick, sBot, sWordList)
end

--Adding New Prohibited String
function AddString(user, data)
	local _,_,sCmd,sWord = string.find(data, "%b<>%s+(%S+)%s+(%S+)")
	if sWord then
		local sWord = string.lower(sWord)
		if table.maxn(tWords) == 0 then
			table.insert(tWords, sWord)
			SaveFile(sFile, tWords, "tWords")
			Core.SendToOpChat(user.sNick.." successfully added "..sWord.." to the table!")
			Core.SendToNick(user.sNick, "<"..sBot.."> ***Thank you! You successfully added "..sWord.." to the table of prohibited strings!")
		else
			for key=1,#tWords do
				if sWord == tWords[key] then
					Core.SendToNick(user.sNick, "<"..sBot.."> *** The String you wanted to add already exists in the table with ID: "..key.." ! Your String is not added to the table!")
					break
					else sCount = key
				end
			end
			if table.maxn(tWords) == sCount then
				table.insert(tWords, sWord)
				SaveFile(sFile, tWords, "tWords")
				Core.SendToOpChat(user.sNick.." successfully added "..sWord.." to the table!")
				Core.SendToNick(user.sNick, "<"..sBot.."> ***Thank you! You successfully added "..sWord.." to the table of prohibited strings!")
			end
		end
	else
		Core.SendToNick(user.sNick, "<"..sBot.."> ***You need to enter a string to be added to the prohibited strings table")
	end
	sWord = {}
end

--Removing Existing Prohibited String By Entering The String
function RemoveString(user, data)
	local _,_,sCmd,sWord = string.find(data, "%b<>%s+(%S+)%s+(%S+)")
	local sNrs = table.maxn(tWords)
	if sWord then
		local sWord = string.lower(sWord)
		if sNrs >= 1 then
			for key=1,#tWords do
				if sWord == tWords[key] then
					table.remove(tWords, key)
					SaveFile(sFile, tWords, "tWords")
					Core.SendToOpChat(user.sNick.." successfully removed "..sWord.." from the table!")
					Core.SendToNick(user.sNick, "<"..sBot.."> ***Thank you! You successfully removed "..sWord.." with the ID: "..key.." from the table of prohibited strings!")
					break
					else sCount = key
				end
			end
			if sNrs == sCount then
				Core.SendToNick(user.sNick, "<"..sBot.."> ***The String "..sWord.." could not be remowed from the table of prohibited strings, as the string could not be found in the table of prohibited strings!")
			end
			else
			Core.SendToNick(user.sNick, "<"..sBot.."> ***The String "..sWord.." could not be remowed from the table of prohibited strings, as the table is empty!")
		end
		else
		Core.SendToNick(user.sNick, "<"..sBot.."> ***You need to enter the string that should be removed from the prohibited strings table")
	end
end

--Removing Existing Prohibited String By Entering The String-ID
function RemoveID(user, data)
	local _,_,sCmd,sID = string.find(data, "%b<>%s+(%S+)%s+(%S+)")
	if sID then	
		if tWords[tonumber(sID)] then
			table.remove(tWords, sID)
			SaveFile(sFile, tWords, "tWords")
			Core.SendToOpChat(user.sNick.." successfully removed the string with the ID: "..sID.." from the table!")
			Core.SendToNick(user.sNick, "<"..sBot.."> ***Thank you! You successfully removed the string with the ID: "..sID.." from the table of prohibited strings!")
			else
			Core.SendToNick(user.sNick, "<"..sBot.."> ***The String with the ID "..sID.." could not be remowed from the table of prohibited strings, as the table is either empty or the entered ID does not exist!")
		end
		else
		Core.SendToNick(user.sNick, "<"..sBot.."> ***You need to enter the string-ID that should be removed from the prohibited strings table")
	end
end

--Saving The File	
function SaveFile(sPath, tTable, sTableName)
	local handle = io.open(sPath,"w+")
    handle:write(SerialiseFile(tTable, sTableName))
	handle:flush()
    handle:close() 
end

--Serializing The File
function SerialiseFile(tTable, sTableName, sTab)

        assert(tTable, "tTable equals nil");
        assert(sTableName, "sTableName equals nil");
        assert(type(tTable) == "table", "tTable must be a table!");
        assert(type(sTableName) == "string", "sTableName must be a string!");
        sTab = sTab or "";
        local sTmp = ""
        sTmp = sTmp..sTab..sTableName.." = {\n"
        for key, value in pairs(tTable) do
                local sKey = (type(key) == "string") and string.format("[%q]",key) or string.format("[%d]",key);
                if(type(value) == "table") then
                        sTmp = sTmp..SerialiseFile(value, sKey, sTab.."\t");
                else
                        local sValue = (type(value) == "string") and string.format("%q",value) or tostring(value);
                        sTmp = sTmp..sTab.."\t"..sKey.." = "..sValue
                end
                sTmp = sTmp..",\n"
        end
        sTmp = sTmp..sTab.."}"
        return sTmp

end

bastya_elvtars

Nice. Some questions/suggestions:
- I do not get why you convert between slashes in pathnames.
- To avoid confusion when there are fancy characters in nick, you should use it like string.find(nick,string,1,true) -- thus it will not handle special characters as patterns to match.
Everything could have been anything else and it would have just as much meaning.

baba.runner

Quote from: bastya_elvtars on 09 July, 2008, 22:44:03
- I do not get why you convert between slashes in pathnames.
It was a suggestion from Snooze to use it that way  ;D


Quote from: bastya_elvtars on 09 July, 2008, 22:44:03
- To avoid confusion when there are fancy characters in nick, you should use it like string.find(nick,string,1,true) -- thus it will not handle special characters as patterns to match.
As in PtokaX-settings there is no more the possibility to specify prohibtes characters, my idea was to give the OPs again the possibility to dissallow special characters in the users nick by this script.

dimetrius

TEsted on PtokaX 0.4.1.1
QuoteWhen I try to add the script did not react! In the chat, too, does not transmit this phrase!

baba.runner

Hi dimetrius

Which DC-client do you use?

Because I can not reproduce this error you mention.

dimetrius


baba.runner

#6
Thank you dimetrius.

Indeed this was a little bug when adding a string to the empty table.

First post is updated ;)

dimetrius

Thank you! I will soon make a translation into Russian!
Uploaded here, if you are not against

baba.runner

Quote from: dimetrius on 21 July, 2008, 23:26:28
Thank you! I will soon make a translation into Russian!
Uploaded here, if you are not against

You are wellcome to do so.
Just please send it also to me by PM ;) Thx.

BTW.
First post updated with v1.2

Natas

Nice script, baba, but it seems to work a little suspect.

Adding .de to BadNickTable it nickbans users only having de in nick.

I don't want users to use webaddresses or similar as nick. Any idea please?

Madman

Quote from: Natas on 26 January, 2009, 11:03:16
Nice script, baba, but it seems to work a little suspect.

Adding .de to BadNickTable it nickbans users only having de in nick.

I don't want users to use webaddresses or similar as nick. Any idea please?

. is a magic lua char. It stands for any symbol. So by adding .de you tell the script to look for any name that has some text before de.
So you have to escape it by using % so remove .de and add %.de instead.
That should fix your problem
We suffer in silence, we lurk in the shadows, we kill in the night
Site currently down, ETA of returning online is 2099 ;p

CrazyGuy

No it won't, as the problem not lies with the user typing the pattern in mainchat, but in the way the pattern is handled by functions AddString and RemoveString.

local _,_,sCmd,sWord = string.find(data, "%b<>%s+(%S+)%s+(%S+)")


will not allow for punctuation, magic or whitespace chars to be included in the pattern ;)

Natas

Quote from: CrazyGuy on 26 January, 2009, 16:35:53
No it won't, as the problem not lies with the user typing the pattern in mainchat, but in the way the pattern is handled by functions AddString and RemoveString.

local _,_,sCmd,sWord = string.find(data, "%b<>%s+(%S+)%s+(%S+)")


will not allow for punctuation, magic or whitespace chars to be included in the pattern ;)
well, after reading tons of pages about string manipulation in lua I still don't know what "%b<>%s+(%S+)%s+(%S+)" exactly means. Is anyone able to lead me to a link, where I can read (a lot) about Patterns and how to construct them? f. eks. patterns and regexes for dummies or something like that ;)

The good thing is: I found another script which seems exactly being able to find the strings I want it to, but still don't understand how and why it works.

The sad thing: I completely translated baba's script to german before running into the glitch with the period in top level domains.

Now I'm willing to learn lua to write my own scripts, so please lead me in the right direction. Thank you.


CrazyGuy

You may want to check out a guide I wrote for writing scripts for PtokaX.
Although it is based on PtokaX 0.3.6.0, it does cover string manipulation for LUA 5.1.x, so there's no change to that :)

You can find the guide attached to this post

SMF spam blocked by CleanTalk