From 142f3d425d5478221b76e2be4fb92f85996caabb Mon Sep 17 00:00:00 2001 From: Egor Date: Thu, 12 Feb 2026 17:07:14 +0200 Subject: [PATCH] lua: add authcommands for doing custom command authorization --- lua/autojoin.lua | 4 +--- lua/init.lua | 34 +++++++++++++++------------------- lua/spam.lua | 4 +--- 3 files changed, 17 insertions(+), 25 deletions(-) diff --git a/lua/autojoin.lua b/lua/autojoin.lua index 74c483c..32e6a09 100644 --- a/lua/autojoin.lua +++ b/lua/autojoin.lua @@ -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) diff --git a/lua/init.lua b/lua/init.lua index 4ecb5f0..d576ffd 100644 --- a/lua/init.lua +++ b/lua/init.lua @@ -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) diff --git a/lua/spam.lua b/lua/spam.lua index 81b574e..5877341 100644 --- a/lua/spam.lua +++ b/lua/spam.lua @@ -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 -- 2.56.0