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 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] = getfenv(2).module;
27 log("debug", "mod_%s now handles tag 'iq' with query namespace '%s'", getfenv(2).module.name, xmlns);
29 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);
33 function modulehelpers.add_handler(origin_type, tag, handler)
34 handlers[origin_type] = handlers[origin_type] or {};
35 if not handlers[origin_type][tag] then
36 handlers[origin_type][tag]= handler;
37 handler_info[handler] = getfenv(2).module;
38 log("debug", "mod_%s now handles tag '%s'", getfenv(2).module.name, tag);
39 elseif handler_info[handlers[origin_type][tag]] then
40 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);
51 local mod, err = loadfile("plugins/mod_"..name..".lua");
53 log("error", "Unable to load module '%s': %s", name or "nil", err or "nil");
57 local pluginenv = setmetatable({ module = { name = name } }, { __index = modulehelpers });
59 setfenv(mod, pluginenv);
60 local success, ret = pcall(mod);
62 log("error", "Error initialising module '%s': %s", name or "nil", ret or "nil");
67 function handle_stanza(origin, stanza)
68 local name, xmlns, origin_type = stanza.name, stanza.attr.xmlns, origin.type;
70 if name == "iq" and xmlns == "jabber:client" and handlers[origin_type] then
71 log("debug", "Stanza is an <iq/>");
72 local child = stanza.tags[1];
74 local xmlns = child.attr.xmlns;
75 log("debug", "Stanza has xmlns: %s", xmlns);
76 local handler = handlers[origin_type][name][xmlns];
78 log("debug", "Passing stanza to mod_%s", handler_info[handler].name);
79 return handler(origin, stanza) or true;
83 --FIXME: All iq's must be replied to, here we should return service-unavailable I think
84 elseif handlers[origin_type] then
85 local handler = handlers[origin_type][name];
87 log("debug", "Passing stanza to mod_%s", handler_info[handler].name);
88 return handler(origin, stanza) or true;
91 log("debug", "Stanza unhandled by any modules");
92 return false; -- we didn't handle it
96 local event_handlers = {};
98 function modulehelpers.add_event_hook(name, handler)
99 if not event_handlers[name] then
100 event_handlers[name] = {};
102 t_insert(event_handlers[name] , handler);
105 function fire_event(name, ...)
106 local event_handlers = event_handlers[name];
107 if event_handlers then
108 for name, handler in ipairs(event_handlers) do