]> PatchyGit - patchyircd.git/commitdiff
lua: add authcommands for doing custom command authorization
authorEgor <egor@patchysicp.xyz>
Thu, 12 Feb 2026 15:07:14 +0000 (17:07 +0200)
committerEgor <egor@patchysicp.xyz>
Thu, 12 Feb 2026 15:07:14 +0000 (17:07 +0200)
lua/autojoin.lua
lua/init.lua
lua/spam.lua

index 74c483c..32e6a09 100644 (file)
@@ -1,8 +1,6 @@
 -- Copyright (c) 2026 Egor
 -- SPDX-License-Identifier: GPL-2.0-or-later
-if not autojoin then
-       autojoin = {}
-end
+global_once "autojoin"
 hook_add("newuser", "autojoin", function(hook, cl1, cl2, chan, txt)
        for k in pairs(autojoin) do
                channel_join(cl1, k)
index 4ecb5f0..d576ffd 100644 (file)
@@ -1,16 +1,18 @@
 -- Copyright (c) 2023-2026 Egor
 -- SPDX-License-Identifier: GPL-2.0-or-later
-if not commands then
-       commands = {}
-end
+function global_once(name, init) if not _G[name] then _G[name] = init or {} end end
+global_once "commands"
+global_once "authcommands"
 function dispatch_command(client, source, command, ...)
-       if client.regstate ~= 2 then -- REG_REGISTERED
-               err_notregistered(client)
-               return
-       end
-       if client.umode & 2 == 0 then -- UMODE_OPER
-               err_noprivileges(client)
-               return
+       if not authcommands[command] then
+               if client.regstate ~= 2 then -- REG_REGISTERED
+                       err_notregistered(client)
+                       return
+               end
+               if client.umode & 2 == 0 then -- UMODE_OPER
+                       err_noprivileges(client)
+                       return
+               end
        end
        commands[command](client, source, command, table.pack(...))
 end
@@ -23,15 +25,11 @@ function commands.reload(client, source, command, args)
        client_notice(client, "done")
 end
 setmetatable(_G, getmetatable(ircd))
-if not hooks then
-       hooks = {}
-end
+global_once "hooks"
 function dispatch_hook(hook, cl1, cl2, chan, txt)
        return hooks[hook](hook, cl1, cl2, chan, txt)
 end
-if not managed_hooks then
-       managed_hooks = {}
-end
+global_once "managed_hooks"
 function hook_setfold(name, fn)
        if not managed_hooks[name] then
                managed_hooks[name] = {n=0,fns={}}
@@ -75,9 +73,7 @@ hook_setfold("nickchange", fold_sendtox)
 hook_setfold("privmsgtoservice", fold_sendtox)
 hook_setfold("noticetoservice", fold_sendtox)
 hook_setfold("whois", fold_sendtox)
-if not client_data then
-       client_data = {}
-end
+global_once "client_data"
 hook_add("nickadd", "clientdata_add", function(hook, cl1, cl2, chan, txt)
        client_data[txt] = {}
 end)
index 81b574e..5877341 100644 (file)
@@ -1,9 +1,7 @@
 -- Copyright (c) 2024-2025 Egor
 -- SPDX-License-Identifier: GPL-2.0-or-later
 -- spam filtering
-if not spamlevel then
-       spamlevel = 0
-end
+global_once(spamlevel, 0)
 local function ascii(txt)
        txt = txt:gsub("[^ -~]", ""):lower()
        return txt