2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 Waqas Hussain
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
9 local logger = require "util.logger";
10 local log = logger.init("modulemanager");
11 local config = require "core.configmanager";
12 local pluginloader = require "util.pluginloader";
15 local prosody = prosody;
17 local loadfile, pcall, xpcall = loadfile, pcall, xpcall;
18 local setmetatable, setfenv, getfenv = setmetatable, setfenv, getfenv;
19 local pairs, ipairs = pairs, ipairs;
20 local t_insert, t_concat = table.insert, table.concat;
23 local rawget = rawget;
25 local tostring, tonumber = tostring, tonumber;
27 local debug_traceback = debug.traceback;
28 local unpack, select = unpack, select;
29 pcall = function(f, ...)
30 local n = select("#", ...);
32 return xpcall(function() return f(unpack(params, 1, n)) end, function(e) return tostring(e).."\n"..debug_traceback(); end);
35 local array, set = require "util.array", require "util.set";
37 local autoload_modules = {"presence", "message", "iq", "offline", "c2s", "s2s"};
38 local component_inheritable_modules = {"tls", "dialback", "iq"};
40 -- We need this to let modules access the real global namespace
43 module "modulemanager"
45 local api = _G.require "core.moduleapi"; -- Module API container
47 -- [host] = { [module] = module_env }
48 local modulemap = { ["*"] = {} };
52 -- Load modules when a host is activated
53 function load_modules_for_host(host)
54 local component = config.get(host, "core", "component_module");
56 local global_modules_enabled = config.get("*", "core", "modules_enabled");
57 local global_modules_disabled = config.get("*", "core", "modules_disabled");
58 local host_modules_enabled = config.get(host, "core", "modules_enabled");
59 local host_modules_disabled = config.get(host, "core", "modules_disabled");
61 if host_modules_enabled == global_modules_enabled then host_modules_enabled = nil; end
62 if host_modules_disabled == global_modules_disabled then host_modules_disabled = nil; end
64 local global_modules = set.new(autoload_modules) + set.new(global_modules_enabled) - set.new(global_modules_disabled);
66 global_modules = set.intersection(set.new(component_inheritable_modules), global_modules);
68 local modules = (global_modules + set.new(host_modules_enabled)) - set.new(host_modules_disabled);
71 if modules:contains("console") then
72 log("error", "The mod_console plugin has been renamed to mod_admin_telnet. Please update your config.");
73 modules:remove("console");
74 modules:add("admin_telnet");
78 load(host, component);
80 for module in modules do
84 prosody.events.add_handler("host-activated", load_modules_for_host);
86 --- Private helpers ---
88 local function do_unload_module(host, name)
89 local mod = get_module(host, name);
90 if not mod then return nil, "module-not-loaded"; end
92 if module_has_method(mod, "unload") then
93 local ok, err = call_module_method(mod, "unload");
94 if (not ok) and err then
95 log("warn", "Non-fatal error unloading module '%s' on '%s': %s", name, host, err);
99 for handler, event in pairs(mod.module.event_handlers) do
100 event.object.remove_handler(event.name, handler);
103 if mod.module.items then -- remove items
104 for key,t in pairs(mod.module.items) do
108 hosts[host].events.fire_event("item-removed/"..key, {source = mod.module, item = value});
112 modulemap[host][name] = nil;
116 local function do_load_module(host, module_name)
117 if not (host and module_name) then
118 return nil, "insufficient-parameters";
119 elseif not hosts[host] then
120 return nil, "unknown-host";
123 if not modulemap[host] then
124 modulemap[host] = {};
127 if modulemap[host][module_name] then
128 log("warn", "%s is already loaded for %s, so not loading again", module_name, host);
129 return nil, "module-already-loaded";
130 elseif modulemap["*"][module_name] then
131 return nil, "global-module-already-loaded";
135 local mod, err = pluginloader.load_code(module_name);
137 log("error", "Unable to load module '%s': %s", module_name or "nil", err or "nil");
141 local _log = logger.init(host..":"..module_name);
142 local api_instance = setmetatable({ name = module_name, host = host, path = err,
143 _log = _log, log = function (self, ...) return _log(...); end, event_handlers = {} }
144 , { __index = api });
146 local pluginenv = setmetatable({ module = api_instance }, { __index = _G });
147 api_instance.environment = pluginenv;
149 setfenv(mod, pluginenv);
150 hosts[host].modules = modulemap[host];
151 modulemap[host][module_name] = pluginenv;
153 local ok, err = pcall(mod);
155 -- Call module's "load"
156 if module_has_method(pluginenv, "load") then
157 ok, err = call_module_method(pluginenv, "load");
159 log("warn", "Error loading module '%s' on '%s': %s", module_name, host, err or "nil");
163 -- Use modified host, if the module set one
164 if api_instance.host == "*" and host ~= "*" then
165 modulemap[host][module_name] = nil;
166 modulemap["*"][module_name] = pluginenv;
167 api_instance:set_global();
170 log("error", "Error initializing module '%s' on '%s': %s", module_name, host, err or "nil");
171 do_unload_module(api_instance.host, module_name); -- Ignore error, module may be partially-loaded
173 return ok and pluginenv, err;
176 local function do_reload_module(host, name)
177 local mod = get_module(host, name);
178 if not mod then return nil, "module-not-loaded"; end
180 local _mod, err = pluginloader.load_code(name); -- checking for syntax errors
182 log("error", "Unable to load module '%s': %s", name or "nil", err or "nil");
187 if module_has_method(mod, "save") then
188 local ok, ret, err = call_module_method(mod, "save");
192 log("warn", "Error saving module '%s:%s' state: %s", host, name, ret);
193 if not config.get(host, "core", "force_module_reload") then
194 log("warn", "Aborting reload due to error, set force_module_reload to ignore this");
195 return nil, "save-state-failed";
197 log("warn", "Continuing with reload (using the force)");
202 do_unload_module(host, name);
203 local ok, err = do_load_module(host, name);
205 mod = get_module(host, name);
206 if module_has_method(mod, "restore") then
207 local ok, err = call_module_method(mod, "restore", saved or {})
208 if (not ok) and err then
209 log("warn", "Error restoring module '%s' from '%s': %s", name, host, err);
213 return ok and mod, err;
218 -- Load a module and fire module-loaded event
219 function load(host, name)
220 local mod, err = do_load_module(host, name);
222 (hosts[mod.module.host] or prosody).events.fire_event("module-loaded", { module = name, host = host });
227 -- Unload a module and fire module-unloaded
228 function unload(host, name)
229 local ok, err = do_unload_module(host, name);
231 (hosts[host] or prosody).events.fire_event("module-unloaded", { module = name, host = host });
236 function reload(host, name)
237 local ok, err = do_reload_module(host, name);
239 (hosts[host] or prosody).events.fire_event("module-reloaded", { module = name, host = host });
240 elseif not is_loaded(host, name) then
241 (hosts[host] or prosody).events.fire_event("module-unloaded", { module = name, host = host });
246 function get_module(host, name)
247 return modulemap[host] and modulemap[host][name];
250 function get_modules(host)
251 return modulemap[host];
254 function is_loaded(host, name)
255 return modulemap[host] and modulemap[host][name] and true;
258 function module_has_method(module, method)
259 return type(module.module[method]) == "function";
262 function call_module_method(module, method, ...)
263 if module_has_method(module, method) then
264 local f = module.module[method];
265 return pcall(f, ...);
267 return false, "no-such-method";