]> PatchyGit - patchyircd.git/commitdiff
spam.lua: use one set of hooks for all simple filters
authorEgor <egor@patchysicp.xyz>
Sat, 17 Aug 2024 22:23:40 +0000 (01:23 +0300)
committerEgor <egor@patchysicp.xyz>
Sat, 17 Aug 2024 22:23:40 +0000 (01:23 +0300)
lua/spam.lua

index bb73106..07a757f 100644 (file)
@@ -6,37 +6,39 @@ local function ascii(txt)
        txt = txt:gsub("[^ -~]", ""):lower()
        return txt
 end
-local function simple_filter(id, badword, level)
-       hook_add("sendtochannel", id.."_filter", spamlevel >= level and function(hook, cl1, cl2, chan, txt)
-               if cl1.umode & 2 ~= 0 then -- UMODE_OPER
-                       return 0
-               end
-               if cl1.umode & 8 ~= 0 then -- UMODE_REGISTERED
-                       return 0
-               end
-               txt = ascii(txt)
-               if not chan.v[cl1.key] and not chan.o[cl1.key] and txt:find(badword, 1, true) then
+local simple_filters = {}
+hook_add("sendtochannel", "spam_simple_filter", function(hook, cl1, cl2, chan, txt)
+       -- 10 is UMODE_OPER|UMODE_REGISTERED
+       if cl1.umode & 10 ~= 0 or chan.v[cl1.key] or chan.o[cl1.key] then
+               return 0
+       end
+       txt = ascii(txt)
+       for k, _ in simple_filters do
+               if txt:find(k, 1, true) then
                        return -1
                end
-       end)
-       hook_add("sendtoclient", id.."_pm_filter", spamlevel >= level and function(hook, cl1, cl2, chan, txt)
-               if cl1.umode & 2 ~= 0 then -- UMODE_OPER
-                       return 0
-               end
-               if cl1.umode & 8 ~= 0 then -- UMODE_REGISTERED
-                       return 0
-               end
-               txt = ascii(txt)
-               if txt:find(badword, 1, true) then
+       end
+end)
+hook_add("sendtoclient", "spam_simple_pm_filter", function(hook, cl1, cl2, chan, txt)
+       -- 10 is UMODE_OPER|UMODE_REGISTERED
+       if cl1.umode & 10 ~= 0 then
+               return 0
+       end
+       txt = ascii(txt)
+       for k, _ in simple_filters do
+               if txt:find(k, 1, true) then
                        return -1
                end
-       end)
+       end
+end)
+function spam_set_simple_filter(badword, on)
+       simple_filters[badword] = on or nil
 end
 function setspamlevel(newlevel)
        spamlevel = newlevel
-       simple_filter("supernets", "irc.supernets.org", 10)
-       simple_filter("hardchats", "hardchat", 10)
-       simple_filter("l0de_radio_hour", "1-877-293-1933", 10)
+       spam_set_simple_filter("irc.supernets.org", spamlevel >= 10) -- supernets
+       spam_set_simple_filter("hardchat", spamlevel >= 10) -- hardchats
+       spam_set_simple_filter("1-877-293-1933", spamlevel >= 10) -- l0de_radio_hour
        hook_add("sendtoclient", "no_unsolicited_pm", spamlevel >= 20 and function(hook, cl1, cl2, chan, txt)
                if cl1.umode & 2 ~= 0 then -- UMODE_OPER
                        return 0