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";
28 local config = require "core.configmanager";
29 local multitable_new = require "util.multitable".new;
32 local loadfile, pcall = loadfile, pcall;
33 local setmetatable, setfenv, getfenv = setmetatable, setfenv, getfenv;
34 local pairs, ipairs = pairs, ipairs;
35 local t_insert = table.insert;
38 local tostring, print = tostring, print;
40 -- We need this to let modules access the real global namespace
43 module "modulemanager"
45 local api = {}; -- Module API container
47 local modulemap = { ["*"] = {} };
49 local stanza_handlers = multitable_new();
50 local handler_info = {};
52 local modulehelpers = setmetatable({}, { __index = _G });
54 -- Load modules when a host is activated
55 function load_modules_for_host(host)
56 local modules_enabled = config.get(host, "core", "modules_enabled");
57 if modules_enabled then
58 for _, module in pairs(modules_enabled) do
63 eventmanager.add_event_hook("host-activated", load_modules_for_host);
66 function load(host, module_name, config)
67 if not (host and module_name) then
68 return nil, "insufficient-parameters";
71 if not modulemap[host] then
73 elseif modulemap[host][module_name] then
74 log("warn", "%s is already loaded for %s, so not loading again", module_name, host);
75 return nil, "module-already-loaded";
76 elseif modulemap["*"][module_name] then
77 return nil, "global-module-already-loaded";
81 local mod, err = loadfile(plugin_dir.."mod_"..module_name..".lua");
83 log("error", "Unable to load module '%s': %s", module_name or "nil", err or "nil");
87 local _log = logger.init(host..":"..module_name);
88 local api_instance = setmetatable({ name = module_name, host = host, config = config, _log = _log, log = function (self, ...) return _log(...); end }, { __index = api });
90 local pluginenv = setmetatable({ module = api_instance }, { __index = _G });
92 setfenv(mod, pluginenv);
94 local success, ret = pcall(mod);
96 log("error", "Error initialising module '%s': %s", name or "nil", ret or "nil");
100 -- Use modified host, if the module set one
101 modulemap[api_instance.host][module_name] = mod;
106 function is_loaded(host, name)
107 return modulemap[host] and modulemap[host][name] and true;
110 function unload(host, name, ...)
111 local mod = modulemap[host] and modulemap[host][name];
112 if not mod then return nil, "module-not-loaded"; end
114 if type(mod.unload) == "function" then
115 local ok, err = pcall(mod.unload, ...)
116 if (not ok) and err then
117 log("warn", "Non-fatal error unloading module '%s' from '%s': %s", name, host, err);
123 function handle_stanza(host, origin, stanza)
124 local name, xmlns, origin_type = stanza.name, stanza.attr.xmlns, origin.type;
125 if name == "iq" and xmlns == "jabber:client" then
126 xmlns = stanza.tags[1].attr.xmlns;
127 log("debug", "Stanza of type %s from %s has xmlns: %s", name, origin_type, xmlns);
129 local handlers = stanza_handlers:get(host, origin_type, name, xmlns);
131 log("debug", "Passing stanza to mod_%s", handler_info[handlers[1]].name);
132 (handlers[1])(origin, stanza);
135 log("debug", "Stanza unhandled by any modules, xmlns: %s", stanza.attr.xmlns); -- we didn't handle it
139 ----- API functions exposed to modules -----------
140 -- Must all be in api.*
142 -- Returns the name of the current module
143 function api:get_name()
147 -- Returns the host that the current module is serving
148 function api:get_host()
152 local function _add_handler(module, origin_type, tag, xmlns, handler)
153 local handlers = stanza_handlers:get(module.host, origin_type, tag, xmlns);
154 local msg = (tag == "iq") and "namespace" or "payload namespace";
156 stanza_handlers:add(module.host, origin_type, tag, xmlns, handler);
157 handler_info[handler] = module;
158 module:log("debug", "I now handle tag '%s' [%s] with %s '%s'", tag, origin_type, msg, xmlns);
160 module:log("warn", "I wanted to handle tag '%s' [%s] with %s '%s' but mod_%s already handles that", tag, origin_type, msg, xmlns, handler_info[handlers[1]].module.name);
164 function api:add_handler(origin_type, tag, xmlns, handler)
165 if not (origin_type and tag and xmlns and handler) then return false; end
166 if type(origin_type) == "table" then
167 for _, origin_type in ipairs(origin_type) do
168 _add_handler(self, origin_type, tag, xmlns, handler);
171 _add_handler(self, origin_type, tag, xmlns, handler);
174 function api:add_iq_handler(origin_type, xmlns, handler)
175 self:add_handler(origin_type, "iq", 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 function api:add_event_hook (...) return eventmanager.add_event_hook(...); end
189 --------------------------------------------------------------------