2 local log = require "util.logger".init("modulemanager")
4 local loadfile, pcall = loadfile, pcall;
5 local setmetatable, setfenv, getfenv = setmetatable, setfenv, getfenv;
6 local pairs, ipairs = pairs, ipairs;
7 local t_insert = table.insert;
10 local tostring, print = tostring, print;
14 module "modulemanager"
16 local handler_info = {};
19 local modulehelpers = setmetatable({}, { __index = _G });
21 local function _add_iq_handler(module, origin_type, xmlns, handler)
22 handlers[origin_type] = handlers[origin_type] or {};
23 handlers[origin_type].iq = handlers[origin_type].iq or {};
24 if not handlers[origin_type].iq[xmlns] then
25 handlers[origin_type].iq[xmlns]= handler;
26 handler_info[handler] = module;
27 log("debug", "mod_%s now handles tag 'iq' with query namespace '%s'", module.name, xmlns);
29 log("warning", "mod_%s wants to handle tag 'iq' with query namespace '%s' but mod_%s already handles that", module.name, xmlns, handler_info[handlers[origin_type].iq[xmlns]].module.name);
33 function modulehelpers.add_iq_handler(origin_type, xmlns, handler)
34 if not (origin_type and handler and xmlns) then return false; end
35 if type(origin_type) == "table" then
36 for _, origin_type in ipairs(origin_type) do
37 _add_iq_handler(getfenv(2).module, origin_type, xmlns, handler);
41 _add_iq_handler(getfenv(2).module, origin_type, xmlns, handler);
44 local function _add_handler(module, origin_type, tag, xmlns, handler)
45 handlers[origin_type] = handlers[origin_type] or {};
46 if not handlers[origin_type][tag] then
47 handlers[origin_type][tag] = handlers[origin_type][tag] or {};
48 handlers[origin_type][tag][xmlns]= handler;
49 handler_info[handler] = module;
50 log("debug", "mod_%s now handles tag '%s'", module.name, tag);
51 elseif handler_info[handlers[origin_type][tag]] then
52 log("warning", "mod_%s wants to handle tag '%s' but mod_%s already handles that", module.name, tag, handler_info[handlers[origin_type][tag]].module.name);
56 function modulehelpers.add_handler(origin_type, tag, xmlns, handler)
57 if not (origin_type and tag and xmlns and handler) then return false; end
58 if type(origin_type) == "table" then
59 for _, origin_type in ipairs(origin_type) do
60 _add_handler(getfenv(2).module, origin_type, tag, xmlns, handler);
64 _add_handler(getfenv(2).module, origin_type, tag, xmlns, handler);
68 local mod, err = loadfile("plugins/mod_"..name..".lua");
70 log("error", "Unable to load module '%s': %s", name or "nil", err or "nil");
74 local pluginenv = setmetatable({ module = { name = name } }, { __index = modulehelpers });
76 setfenv(mod, pluginenv);
77 local success, ret = pcall(mod);
79 log("error", "Error initialising module '%s': %s", name or "nil", ret or "nil");
85 function handle_stanza(origin, stanza)
86 local name, xmlns, origin_type = stanza.name, stanza.attr.xmlns, origin.type;
88 if name == "iq" and xmlns == "jabber:client" and handlers[origin_type] then
89 log("debug", "Stanza is an <iq/>");
90 local child = stanza.tags[1];
92 local xmlns = child.attr.xmlns;
93 log("debug", "Stanza has xmlns: %s", xmlns);
94 local handler = handlers[origin_type][name][xmlns];
96 log("debug", "Passing stanza to mod_%s", handler_info[handler].name);
97 return handler(origin, stanza) or true;
101 elseif handlers[origin_type] then
102 local handler = handlers[origin_type][name];
104 handler = handler[xmlns];
106 log("debug", "Passing stanza to mod_%s", handler_info[handler].name);
107 return handler(origin, stanza) or true;
111 log("debug", "Stanza unhandled by any modules, xmlns: %s", stanza.attr.xmlns);
112 return false; -- we didn't handle it
116 local event_handlers = {};
118 function modulehelpers.add_event_hook(name, handler)
119 if not event_handlers[name] then
120 event_handlers[name] = {};
122 t_insert(event_handlers[name] , handler);
125 function fire_event(name, ...)
126 local event_handlers = event_handlers[name];
127 if event_handlers then
128 for name, handler in ipairs(event_handlers) do