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 function modulehelpers.add_iq_handler(origin_type, xmlns, handler)
22 if not (origin_type and handler and xmlns) then return false; end
23 handlers[origin_type] = handlers[origin_type] or {};
24 handlers[origin_type].iq = handlers[origin_type].iq or {};
25 if not handlers[origin_type].iq[xmlns] then
26 handlers[origin_type].iq[xmlns]= handler;
27 handler_info[handler] = getfenv(2).module;
28 log("debug", "mod_%s now handles tag 'iq' with query namespace '%s'", getfenv(2).module.name, xmlns);
30 log("warning", "mod_%s wants to handle tag 'iq' with query namespace '%s' but mod_%s already handles that", getfenv(2).module.name, xmlns, handler_info[handlers[origin_type].iq[xmlns]].module.name);
34 function modulehelpers.add_handler(origin_type, tag, xmlns, handler)
35 if not (origin_type and tag and xmlns and handler) then return false; end
36 handlers[origin_type] = handlers[origin_type] or {};
37 if not handlers[origin_type][tag] then
38 handlers[origin_type][tag] = handlers[origin_type][tag] or {};
39 handlers[origin_type][tag][xmlns]= handler;
40 handler_info[handler] = getfenv(2).module;
41 log("debug", "mod_%s now handles tag '%s'", getfenv(2).module.name, tag);
42 elseif handler_info[handlers[origin_type][tag]] then
43 log("warning", "mod_%s wants to handle tag '%s' but mod_%s already handles that", getfenv(2).module.name, tag, handler_info[handlers[origin_type][tag]].module.name);
57 local mod, err = loadfile("plugins/mod_"..name..".lua");
59 log("error", "Unable to load module '%s': %s", name or "nil", err or "nil");
63 local pluginenv = setmetatable({ module = { name = name } }, { __index = modulehelpers });
65 setfenv(mod, pluginenv);
66 local success, ret = pcall(mod);
68 log("error", "Error initialising module '%s': %s", name or "nil", ret or "nil");
73 function handle_stanza(origin, stanza)
74 local name, xmlns, origin_type = stanza.name, stanza.attr.xmlns, origin.type;
76 if name == "iq" and xmlns == "jabber:client" and handlers[origin_type] then
77 log("debug", "Stanza is an <iq/>");
78 local child = stanza.tags[1];
80 local xmlns = child.attr.xmlns;
81 log("debug", "Stanza has xmlns: %s", xmlns);
82 local handler = handlers[origin_type][name][xmlns];
84 log("debug", "Passing stanza to mod_%s", handler_info[handler].name);
85 return handler(origin, stanza) or true;
89 elseif handlers[origin_type] then
90 local handler = handlers[origin_type][name];
92 handler = handler[xmlns];
94 log("debug", "Passing stanza to mod_%s", handler_info[handler].name);
95 return handler(origin, stanza) or true;
99 log("debug", "Stanza unhandled by any modules, xmlns: %s", stanza.attr.xmlns);
100 return false; -- we didn't handle it
104 local event_handlers = {};
106 function modulehelpers.add_event_hook(name, handler)
107 if not event_handlers[name] then
108 event_handlers[name] = {};
110 t_insert(event_handlers[name] , handler);
113 function fire_event(name, ...)
114 local event_handlers = event_handlers[name];
115 if event_handlers then
116 for name, handler in ipairs(event_handlers) do