3 local logger = require "util.logger";
4 local log = logger.init("modulemanager")
6 local loadfile, pcall = loadfile, pcall;
7 local setmetatable, setfenv, getfenv = setmetatable, setfenv, getfenv;
8 local pairs, ipairs = pairs, ipairs;
9 local t_insert = table.insert;
12 local tostring, print = tostring, print;
17 module "modulemanager"
19 local api = {}; -- Module API container
23 local handler_info = {};
24 local stanza_handlers = {};
26 local modulehelpers = setmetatable({}, { __index = _G });
29 function load(host, module_name, config)
30 local mod, err = loadfile("plugins/mod_"..module_name..".lua");
32 log("error", "Unable to load module '%s': %s", module_name or "nil", err or "nil");
36 if not modulemap[host] then
38 stanza_handlers[host] = {};
39 elseif modulemap[host][module_name] then
40 log("warn", "%s is already loaded for %s, so not loading again", module_name, host);
41 return nil, "module-already-loaded";
44 local _log = logger.init(host..":"..module_name);
45 local api_instance = setmetatable({ name = module_name, host = host, config = config, _log = _log, log = function (self, ...) return _log(...); end }, { __index = api });
47 local pluginenv = setmetatable({ module = api_instance }, { __index = _G });
49 setfenv(mod, pluginenv);
51 local success, ret = pcall(mod);
53 log("error", "Error initialising module '%s': %s", name or "nil", ret or "nil");
57 modulemap[host][module_name] = mod;
62 function handle_stanza(host, origin, stanza)
63 local name, xmlns, origin_type = stanza.name, stanza.attr.xmlns, origin.type;
65 local handlers = stanza_handlers[host];
67 log("warn", "No handlers for %s", host);
71 if name == "iq" and xmlns == "jabber:client" and handlers[origin_type] then
72 local child = stanza.tags[1];
74 local xmlns = child.attr.xmlns or xmlns;
75 log("debug", "Stanza of type %s from %s has xmlns: %s", name, origin_type, xmlns);
76 local handler = handlers[origin_type][name] and handlers[origin_type][name][xmlns];
78 log("debug", "Passing stanza to mod_%s", handler_info[handler].name);
79 return handler(origin, stanza) or true;
82 elseif handlers[origin_type] then
83 local handler = handlers[origin_type][name];
85 handler = handler[xmlns];
87 log("debug", "Passing stanza to mod_%s", handler_info[handler].name);
88 return handler(origin, stanza) or true;
92 log("debug", "Stanza unhandled by any modules, xmlns: %s", stanza.attr.xmlns);
93 return false; -- we didn't handle it
96 ----- API functions exposed to modules -----------
97 -- Must all be in api.*
99 -- Returns the name of the current module
100 function api:get_name()
104 -- Returns the host that the current module is serving
105 function api:get_host()
110 local function _add_iq_handler(module, origin_type, xmlns, handler)
111 local handlers = stanza_handlers[module.host];
112 handlers[origin_type] = handlers[origin_type] or {};
113 handlers[origin_type].iq = handlers[origin_type].iq or {};
114 if not handlers[origin_type].iq[xmlns] then
115 handlers[origin_type].iq[xmlns]= handler;
116 handler_info[handler] = module;
117 module:log("debug", "I now handle tag 'iq' [%s] with payload namespace '%s'", origin_type, xmlns);
119 module:log("warn", "I wanted to handle tag 'iq' [%s] with payload namespace '%s' but mod_%s already handles that", origin_type, xmlns, handler_info[handlers[origin_type].iq[xmlns]].name);
123 function api:add_iq_handler(origin_type, xmlns, handler)
124 if not (origin_type and handler and xmlns) then return false; end
125 if type(origin_type) == "table" then
126 for _, origin_type in ipairs(origin_type) do
127 _add_iq_handler(self, origin_type, xmlns, handler);
131 _add_iq_handler(self, origin_type, xmlns, handler);
136 local event_handlers = {};
138 function api:add_event_hook(name, handler)
139 if not event_handlers[name] then
140 event_handlers[name] = {};
142 t_insert(event_handlers[name] , handler);
143 self:log("debug", "Subscribed to %s", name);
146 function fire_event(name, ...)
147 local event_handlers = event_handlers[name];
148 if event_handlers then
149 for name, handler in ipairs(event_handlers) do
157 local function _add_handler(module, origin_type, tag, xmlns, handler)
158 local handlers = stanza_handlers[module.host];
159 handlers[origin_type] = handlers[origin_type] or {};
160 if not handlers[origin_type][tag] then
161 handlers[origin_type][tag] = handlers[origin_type][tag] or {};
162 handlers[origin_type][tag][xmlns]= handler;
163 handler_info[handler] = module;
164 module:log("debug", "I now handle tag '%s' [%s] with xmlns '%s'", tag, origin_type, xmlns);
165 elseif handler_info[handlers[origin_type][tag]] then
166 log("warning", "I wanted to handle tag '%s' [%s] but mod_%s already handles that", tag, origin_type, handler_info[handlers[origin_type][tag]].module.name);
170 function api:add_handler(origin_type, tag, xmlns, handler)
171 if not (origin_type and tag and xmlns and handler) then return false; end
172 if type(origin_type) == "table" then
173 for _, origin_type in ipairs(origin_type) do
174 _add_handler(self, origin_type, tag, xmlns, handler);
178 _add_handler(self, origin_type, tag, xmlns, handler);
181 --------------------------------------------------------------------