2 -- Copyright (C) 2008 Matthew Wild
3 -- Copyright (C) 2008 Waqas Hussain
5 -- This program is free software; you can redistribute it and/or
6 -- modify it under the terms of the GNU General Public License
7 -- as published by the Free Software Foundation; either version 2
8 -- of the License, or (at your option) any later version.
10 -- This program is distributed in the hope that it will be useful,
11 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
12 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 -- GNU General Public License for more details.
15 -- You should have received a copy of the GNU General Public License
16 -- along with this program; if not, write to the Free Software
17 -- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 local plugin_dir = CFG_PLUGINDIR or "./plugins/";
24 local logger = require "util.logger";
25 local log = logger.init("modulemanager");
26 local addDiscoInfoHandler = require "core.discomanager".addDiscoInfoHandler;
27 local eventmanager = require "core.eventmanager";
30 local loadfile, pcall = loadfile, pcall;
31 local setmetatable, setfenv, getfenv = setmetatable, setfenv, getfenv;
32 local pairs, ipairs = pairs, ipairs;
33 local t_insert = table.insert;
36 local tostring, print = tostring, print;
38 -- We need this to let modules access the real global namespace
41 module "modulemanager"
43 local api = {}; -- Module API container
47 local handler_info = {};
48 local stanza_handlers = {};
50 local modulehelpers = setmetatable({}, { __index = _G });
53 function load(host, module_name, config)
54 if not (host and module_name) then
55 return nil, "insufficient-parameters";
57 local mod, err = loadfile(plugin_dir.."mod_"..module_name..".lua");
59 log("error", "Unable to load module '%s': %s", module_name or "nil", err or "nil");
63 if not modulemap[host] then
65 stanza_handlers[host] = {};
66 elseif modulemap[host][module_name] then
67 log("warn", "%s is already loaded for %s, so not loading again", module_name, host);
68 return nil, "module-already-loaded";
71 local _log = logger.init(host..":"..module_name);
72 local api_instance = setmetatable({ name = module_name, host = host, config = config, _log = _log, log = function (self, ...) return _log(...); end }, { __index = api });
74 local pluginenv = setmetatable({ module = api_instance }, { __index = _G });
76 setfenv(mod, pluginenv);
78 local success, ret = pcall(mod);
80 log("error", "Error initialising module '%s': %s", name or "nil", ret or "nil");
84 modulemap[host][module_name] = mod;
89 function is_loaded(host, name)
90 return modulemap[host] and modulemap[host][name] and true;
93 function unload(host, name, ...)
94 local mod = modulemap[host] and modulemap[host][name];
95 if not mod then return nil, "module-not-loaded"; end
97 if type(mod.unload) == "function" then
98 local ok, err = pcall(mod.unload, ...)
99 if (not ok) and err then
100 log("warn", "Non-fatal error unloading module '%s' from '%s': %s", name, host, err);
106 function handle_stanza(host, origin, stanza)
107 local name, xmlns, origin_type = stanza.name, stanza.attr.xmlns, origin.type;
109 local handlers = stanza_handlers[host];
111 log("warn", "No handlers for %s", host);
115 if name == "iq" and xmlns == "jabber:client" and handlers[origin_type] then
116 local child = stanza.tags[1];
118 local xmlns = child.attr.xmlns or xmlns;
119 log("debug", "Stanza of type %s from %s has xmlns: %s", name, origin_type, xmlns);
120 local handler = handlers[origin_type][name] and handlers[origin_type][name][xmlns];
122 log("debug", "Passing stanza to mod_%s", handler_info[handler].name);
123 return handler(origin, stanza) or true;
126 elseif handlers[origin_type] then
127 local handler = handlers[origin_type][name];
129 handler = handler[xmlns];
131 log("debug", "Passing stanza to mod_%s", handler_info[handler].name);
132 return handler(origin, stanza) or true;
136 log("debug", "Stanza unhandled by any modules, xmlns: %s", stanza.attr.xmlns);
137 return false; -- we didn't handle it
140 ----- API functions exposed to modules -----------
141 -- Must all be in api.*
143 -- Returns the name of the current module
144 function api:get_name()
148 -- Returns the host that the current module is serving
149 function api:get_host()
154 local function _add_iq_handler(module, origin_type, xmlns, handler)
155 local handlers = stanza_handlers[module.host];
156 handlers[origin_type] = handlers[origin_type] or {};
157 handlers[origin_type].iq = handlers[origin_type].iq or {};
158 if not handlers[origin_type].iq[xmlns] then
159 handlers[origin_type].iq[xmlns]= handler;
160 handler_info[handler] = module;
161 module:log("debug", "I now handle tag 'iq' [%s] with payload namespace '%s'", origin_type, xmlns);
163 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);
167 function api:add_iq_handler(origin_type, xmlns, handler)
168 if not (origin_type and handler and xmlns) then return false; end
169 if type(origin_type) == "table" then
170 for _, origin_type in ipairs(origin_type) do
171 _add_iq_handler(self, origin_type, xmlns, handler);
175 _add_iq_handler(self, origin_type, xmlns, handler);
178 function api:add_feature(xmlns)
179 addDiscoInfoHandler(self.host, function(reply, to, from, node)
181 reply:tag("feature", {var = xmlns}):up();
187 api.add_event_hook = eventmanager.add_event_hook;
189 local function _add_handler(module, origin_type, tag, xmlns, handler)
190 local handlers = stanza_handlers[module.host];
191 handlers[origin_type] = handlers[origin_type] or {};
192 if not handlers[origin_type][tag] then
193 handlers[origin_type][tag] = handlers[origin_type][tag] or {};
194 handlers[origin_type][tag][xmlns]= handler;
195 handler_info[handler] = module;
196 module:log("debug", "I now handle tag '%s' [%s] with xmlns '%s'", tag, origin_type, xmlns);
197 elseif handler_info[handlers[origin_type][tag]] then
198 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);
202 function api:add_handler(origin_type, tag, xmlns, handler)
203 if not (origin_type and tag and xmlns and handler) then return false; end
204 if type(origin_type) == "table" then
205 for _, origin_type in ipairs(origin_type) do
206 _add_handler(self, origin_type, tag, xmlns, handler);
210 _add_handler(self, origin_type, tag, xmlns, handler);
213 --------------------------------------------------------------------