--// forbid.lua --// forbid.lua: For available commands, see !forbid help --// forbid.lua: (C) Szabolcs Molnar, 2006-2007; fleet@elitemail.hu --// forbid.lua: (C) [VH]adAsz, 2006-2007; vhadasz@yahoo.com --// forbid.lua: You can use and distribute this script for no cost, but you are not allowed to remove this copyright notice --// forbid.lua: Rev 009/20070317 --[[ Changelog: 009/20070317: fc: Fixed: Sometimes the script failed on kicking; Modified: Report goes to "reporttarget" (0.5a) 008/20070108: vad: Added "K" for the triggers which kick the user when reporting to opchat (0.4c) 007/20061115: fc: Fixed bug in pm filtering. Thanks OpChat for reporting this :P (0.4b) 006/20061025: vad: Added expression checker (0.4a) 005/20061024: fc: Fixed an issue with mainchat filtering (thanks VadAsz for noticing me) 004/20061017: fc: Fixed pm filtering (thanks maksalaatikko for reporting this bug) (0.3a) 003/20061001: fc: Added hit counter (0.2a) 002/20061001: fc: If more than one filter applies to the message, all of them is checked, until there is no filters remaining or the user is kicked 001/20060914: fc: Initial release ]] forbidfn = {} forbidfn.internal = {} forbidfn.internal.settingsfile = "scripts/forbid.settings.txt" forbidfn.internal.version = "0.5a" function forbidfn.loadSettings() local o = io.open( forbidfn.internal.settingsfile, "r" ) if o then dofile( forbidfn.internal.settingsfile ) o:close() end forbidfn.initializeSettings() return true end function forbidfn.saveSettings() pickle.store( forbidfn.internal.settingsfile, { forbidsettings = forbidsettings }) return true end function forbidfn.initializeSettings() if not forbidsettings then forbidsettings = {} end if not forbidsettings.words then forbidsettings.words = {} end if not forbidsettings.config then forbidsettings.config = {} end -- if not forbidsettings.config.opchat then forbidsettings.config.opchat = "OpChat" end end function forbidfn.updateConfig() -- Adding hit counter -- and fixing pm/mc filtering issues for k in pairs(forbidsettings.words) do if not forbidsettings.words[k].hits then forbidsettings.words[k].hits = 0 end if not forbidsettings.words[k].added then forbidsettings.words[k].added = os.time() end if forbidsettings.words[k].pm then if forbidsettings.words[k].pm == 0 then forbidsettings.words[k].pm = false end end if forbidsettings.words[k].mc then if forbidsettings.words[k].mc == 0 then forbidsettings.words[k].mc = false end end end end function forbidfn.decide(value, ret1, ret2, invert) if not ret1 then ret1 = "Yes" end if not ret2 then ret2 = "No" end if invert then ret2, ret1 = ret1, ret2 end if value then if value == true or value == 1 then return ret1 end end return ret2 end function forbidfn.getConfig(conf) for k in pairs(forbidsettings.config) do if k == conf then return forbidsettings.config[k] end end return false end function forbidfn.reportOpChat(msg) local oc = GetConfig("reporttarget") if oc ~= "" then PMToNick("", oc, msg) end return true end function forbidfn.kick(nick, reason) if string.find( reason, "_BAN_%d+[smhdwy]" ) then local bantime = string.gsub(reason, ".*_BAN_(%d+[smhdwy]).*", "%1") ChatToAll("", "is kicking " .. nick .. " because: " .. reason) UserBan(nick, bantime, reason ) else ChatToAll("", "is kicking " .. nick .. " because: " .. reason) UserKick( nick, reason ) end end function forbidfn.getHelp() local ret = "\r\n" ret = ret .. "---------------------------------------------------------------------------------------------------------------------------------------------------------\r\n" ret = ret .. "Forbid.lua " .. forbidfn.internal.version .. "\r\n" ret = ret .. "---------------------------------------------------------------------------------------------------------------------------------------------------------\r\n" ret = ret .. "!forbid add [regex/plain] [main/pm/both] [filter regged users (1/0)] [kick [reason]]\r\n" ret = ret .. "!forbid mod [regex/plain] [main/pm/both] [filter regged users (1/0)] [kick [reason]]\r\n" ret = ret .. "!forbid rm \r\n" ret = ret .. "!forbid list [filter] [plaintext (1/0)]\r\n" ret = ret .. "!forbid chk \r\n" ret = ret .. "!forbid help\r\n" ret = ret .. "---------------------------------------------------------------------------------------------------------------------------------------------------------\r\n" return ret end function EventPMOut(nick, fullpm) if UserIsOP(nick) then return false end local target = string.gsub(fullpm, "^%$To: ([^ ]+) From: [^ ]+ %$%b<> (.*)", "%1") local text = string.gsub(fullpm, "^%$To: ([^ ]+) From: [^ ]+ %$%b<> (.*)", "%2") local regged = 0 local hitwords = "" local user_ip = GetUserIP(nick) if UserIsRegistered(nick) then regged = 1 end local ret = false local iskicked = false for k in pairs(forbidsettings.words) do if iskicked then break end if forbidsettings.words[k].pm then if string.find( string.lower(text), forbidsettings.words[k].expression, 1, forbidsettings.words[k].plain ) then if forbidsettings.words[k].regged >= regged then hitwords = hitwords .. " " .. k if forbidsettings.words[k].kick then local reason = forbidsettings.words[k].kickreason if reason == "" then reason = "Ön most nem nyert centrifugát" end forbidfn.kick(nick, reason) iskicked = true hitwords = hitwords .. "K" end forbidsettings.words[k].hits = forbidsettings.words[k].hits + 1 ret = true end end end end if ret then forbidfn.reportOpChat("[" .. tostring(user_ip) .. "] Forbidden message [" .. hitwords .. " ] in private chat to " .. target .. ": <" .. nick .. "> " .. text) RawToNick (nick, fullpm .. "|" ) end -- saving Hit Counter if ret then forbidfn.saveSettings() end return ret end function EventChat(nick, fulltext) if UserIsOP(nick) then return false end local text = string.sub(fulltext, string.len(nick) + 4) local regged = 0 local hitwords = "" local user_ip = GetUserIP(nick) if UserIsRegistered(nick) then regged = 1 end local ret = false local iskicked = false for k in pairs(forbidsettings.words) do if iskicked then break end if forbidsettings.words[k].mc then if string.find( string.lower(text), forbidsettings.words[k].expression, 1, forbidsettings.words[k].plain ) then if forbidsettings.words[k].regged >= regged then hitwords = hitwords .. " " .. k if forbidsettings.words[k].kick then local reason = forbidsettings.words[k].kickreason if reason == "" then reason = "Ön most nem nyert centrifugát" end forbidfn.kick(nick, reason) iskicked = true hitwords = hitwords .. "K" end forbidsettings.words[k].hits = forbidsettings.words[k].hits + 1 ret = true end end end end if ret then forbidfn.reportOpChat("[" .. tostring(user_ip) .. "] Forbidden message [" .. hitwords .. " ] to main chat: " .. fulltext ) RawToNick (nick, fulltext .. "|" ) end -- saving Hit Counter if ret then forbidfn.saveSettings() end return ret end function forbidfn.getList(filter, plain) local exp = "" local plaintext = false if filter then exp = filter if plain then if plain == "1" then plaintext = true elseif plain ~= "0" then return "Unkown parameter (" .. plain .. "). Valid choices are: 1, 0. See !forbid help" end end end local ret = "\r\n" ret = ret .. "----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------\r\n" ret = ret .. " Expression [T: Type] [M/P: Main/Pm] [R: Filter regusers] [K: kick] [H: hits]\r\n" if exp ~= "" then ret = ret .. " Filtered for: \"" .. exp .. "\"\r\n" end ret = ret .. "----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------\r\n" local counter = 0 for k in pairs(forbidsettings.words) do if string.find(forbidsettings.words[k].expression, exp, 1, plaintext) then counter = counter + 1 ret = ret .. tostring(counter) .. ". \"" .. forbidsettings.words[k].expression .. "\" [T: " .. forbidfn.decide(forbidsettings.words[k].plain, "Plain", "Regex") .. "] [M/P: ".. forbidfn.decide(forbidsettings.words[k].mc) .. "/" .. forbidfn.decide(forbidsettings.words[k].pm) .."] [R: " .. tostring(forbidsettings.words[k].regged) .. "] [K: " .. forbidfn.decide(forbidsettings.words[k].kick) .. "]" if forbidsettings.words[k].kick then ret = ret .. " (" .. forbidsettings.words[k].kickreason .. ")" end ret = ret .. " [H: " .. tostring(forbidsettings.words[k].hits) .. "]" ret = ret .. "\r\n" end end ret = ret .. "----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------\r\n" ret = ret .. "Total: " .. tostring(counter) .. " items\r\n" ret = ret .. "----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------\r\n" return ret end function forbidfn.isForbid(expression) for k in pairs(forbidsettings.words) do if forbidsettings.words[k].expression == expression then return true end end return false end function forbidfn.rmForbid(expression) local ret = "Expression not found. See !forbid list or !forbid help" for k in pairs(forbidsettings.words) do if forbidsettings.words[k].expression == expression then table.remove( forbidsettings.words, k ) forbidfn.saveSettings() ret = "OK" break end end return ret end function forbidfn.chkForbid(expression) local ret = "" local counter = 0 ret = ret .. "\r\n----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------\r\n" ret = ret .. " Expression [T: Type] [M/P: Main/Pm] [R: Filter regusers] [K: kick] [H: hits]\r\n" ret = ret .. " Filtering : \"" .. expression .. "\"\r\n" ret = ret .. "----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------\r\n" for k in pairs(forbidsettings.words) do if string.find( string.lower(expression), forbidsettings.words[k].expression, 1, forbidsettings.words[k].plain ) then counter = counter + 1 ret = ret .. tostring(counter) .. ". \"" .. forbidsettings.words[k].expression .. "\" [T: " .. forbidfn.decide(forbidsettings.words[k].plain, "Plain", "Regex") .. "] [M/P: ".. forbidfn.decide(forbidsettings.words[k].mc) .. "/" .. forbidfn.decide(forbidsettings.words[k].pm) .."] [R: " .. tostring(forbidsettings.words[k].regged) .. "] [K: " .. forbidfn.decide(forbidsettings.words[k].kick) .. "]" if forbidsettings.words[k].kick then ret = ret .. " (" .. forbidsettings.words[k].kickreason .. ")" end ret = ret .. " [H: " .. tostring(forbidsettings.words[k].hits) .. "]" ret = ret .. "\r\n" end end ret = ret .. "----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------\r\n" ret = ret .. "Total: " .. tostring(counter) .. " triggers activated\r\n" ret = ret .. "----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------\r\n" return ret end function forbidfn.addForbid(params, onlymodify) local temp = {} temp.expression = "" temp.plain = false temp.pm = false temp.mc = true temp.regged = 0 temp.kick = false temp.kickreason = "" temp.hits = 0 temp.added = os.time() temp.expression = params[2] if params[3] then if params[3] == "plain" or params[3] == "p" then temp.plain = true elseif params[3] ~= "regex" and params[3] ~= "r" then return "Wrong parameter (" .. params[3] .."). Valid choices are: regex, plain" end end if params[4] then if params[4] == "pm" or params[4] == "p" then temp.pm = true temp.mc = false elseif params[4] == "both" or params[4] == "b" then temp.pm = true elseif params[4] ~= "main" and params[4] ~= "m" then return "Unknown parameter (" .. params[4] .. "). Valid choices are: main, pm, both" end end if params[5] then if params[5] == "1" then temp.regged = 1 elseif params[5] ~= "0" then return "Unknown parameter (" .. params[5] .. "). Valid choices are: 1, 0" end end if params[6] then if params[6] == "kick" then temp.kick = 1 else return "Unknown parameter (" .. params[6] .."). Valid choice: kick" end end if params[7] then temp.kickreason = params[7] end if onlymodify then if forbidfn.isForbid(temp.expression) then forbidfn.rmForbid(temp.expression) else return "This expression is not added, thus cannot be modified. See !forbid list" end else if forbidfn.isForbid(temp.expression) then return "This expression is already added. Remove or modify it. See !forbid help" end end table.insert(forbidsettings.words, temp ) forbidfn.saveSettings() return "OK [" .. temp.expression .. "]" end function forbid(nick, params ) if not params[1] then return "Missing or unknown parameters. See !forbid help" elseif params[1] == "help" then return forbidfn.getHelp() elseif params[1] == "chk" then if params[2] then local k = 2 local first = true local allparams = "" while params[k] do if first then first = false else allparams = allparams .. " " end if string.find(params[k], " ") then allparams = allparams .. "\"" .. params[k] .. "\"" else allparams = allparams .. "" .. params[k] end k = k + 1 end return forbidfn.chkForbid(allparams) else return "Usage: !forbid chk " end elseif params[1] == "add" then if params[2] then return forbidfn.addForbid(params) else return "Usage: !forbid add [regex/plain] [main/pm/both] [filter regged users (1/0)] [kick [reason]]" end elseif params[1] == "rm" then if params[2] then return forbidfn.rmForbid(params[2]) else return "Usage: !forbid rm " end elseif params[1] == "mod" then if params[2] then return forbidfn.addForbid(params, true) else return "Usage: !forbid mod [regex/plain] [main/pm/both] [filter regged users (1/0)] [kick [reason]]" end elseif params[1] == "list" then return forbidfn.getList(params[2], params[3]) end return "Unknown parameters. See !forbid help" end --// dofile("scripts/libsimplepickle.lua") forbidfn.loadSettings() forbidfn.updateConfig() RegCommand("forbid", "key", "Chat/PM filter. See !forbid help")