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";
13 local set
= require
"util.set";
15 local new_multitable
= require
"util.multitable".new
;
16 local api
= require
"core.moduleapi"; -- Module API container
18 local prosody
= prosody
;
19 local hosts
= prosody
.hosts
;
21 local xpcall
= require
"util.xpcall".xpcall
;
22 local debug_traceback
= debug
.traceback
;
23 local setmetatable
, rawget = setmetatable
, rawget;
24 local ipairs
, pairs
, type, t_insert
= ipairs
, pairs
, type, table.insert
;
26 local autoload_modules
= {prosody
.platform
, "presence", "message", "iq", "offline", "c2s", "s2s", "s2s_auth_certs"};
27 local component_inheritable_modules
= {"tls", "saslauth", "dialback", "iq", "s2s", "s2s_bidi"};
29 -- We need this to let modules access the real global namespace
35 local load_modules_for_host
, load
, unload
, reload
, get_module
, get_items
;
36 local get_modules
, is_loaded
, module_has_method
, call_module_method
;
38 -- [host] = { [module] = module_env }
39 local modulemap
= { ["*"] = {} };
41 -- Get the list of modules to be loaded on a host
42 local function get_modules_for_host(host
)
43 local component
= config
.get(host
, "component_module");
45 local global_modules_enabled
= config
.get("*", "modules_enabled");
46 local global_modules_disabled
= config
.get("*", "modules_disabled");
47 local host_modules_enabled
= config
.get(host
, "modules_enabled");
48 local host_modules_disabled
= config
.get(host
, "modules_disabled");
50 if host_modules_enabled
== global_modules_enabled
then host_modules_enabled
= nil; end
51 if host_modules_disabled
== global_modules_disabled
then host_modules_disabled
= nil; end
53 local global_modules
= set
.new(autoload_modules
) + set
.new(global_modules_enabled
) - set
.new(global_modules_disabled
);
55 global_modules
= set
.intersection(set
.new(component_inheritable_modules
), global_modules
);
57 local modules
= (global_modules
+ set
.new(host_modules_enabled
)) - set
.new(host_modules_disabled
);
60 if modules
:contains("console") then
61 log("error", "The mod_console plugin has been renamed to mod_admin_telnet. Please update your config.");
62 modules
:remove("console");
63 modules
:add("admin_telnet");
66 return modules
, component
;
69 -- Load modules when a host is activated
70 function load_modules_for_host(host
)
71 local modules
, component_module
= get_modules_for_host(host
);
73 -- Ensure component module is loaded first
74 if component_module
then
75 load(host
, component_module
);
77 for module
in modules
do
81 prosody
.events
.add_handler("host-activated", load_modules_for_host
);
82 prosody
.events
.add_handler("host-deactivated", function (host
)
83 modulemap
[host
] = nil;
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 object
, event
, handler
in mod.module
.event_handlers
:iter(nil, nil, nil) do
100 object
.remove_handler(event
, handler
);
103 if mod.module
.items
then -- remove items
104 local events
= (host
== "*" and prosody
.events
) or hosts
[host
].events
;
105 for key
,t
in pairs(mod.module
.items
) do
109 events
.fire_event("item-removed/"..key
, {source
= mod.module
, item
= value
});
113 mod.module
.loaded
= false;
114 modulemap
[host
][name
] = nil;
118 local function do_load_module(host
, module_name
, state
)
119 if not (host
and module_name
) then
120 return nil, "insufficient-parameters";
121 elseif not hosts
[host
] and host
~= "*"then
122 return nil, "unknown-host";
125 if not modulemap
[host
] then
126 modulemap
[host
] = hosts
[host
].modules
;
129 if modulemap
[host
][module_name
] then
130 if not modulemap
["*"][module_name
] then
131 log("debug", "%s is already loaded for %s, so not loading again", module_name
, host
);
133 return nil, "module-already-loaded";
134 elseif modulemap
["*"][module_name
] then
135 local mod = modulemap
["*"][module_name
];
136 if module_has_method(mod, "add_host") then
137 local _log
= logger
.init(host
..":"..module_name
);
138 local host_module_api
= setmetatable({
139 host
= host
, event_handlers
= new_multitable(), items
= {};
140 _log
= _log
, log = function (self
, ...) return _log(...); end; --luacheck: ignore 212/self
142 __index
= modulemap
["*"][module_name
].module
;
144 local host_module
= setmetatable({ module
= host_module_api
}, { __index
= mod });
145 host_module_api
.environment
= host_module
;
146 modulemap
[host
][module_name
] = host_module
;
147 local ok
, result
, module_err
= call_module_method(mod, "add_host", host_module_api
);
148 if not ok
or result
== false then
149 modulemap
[host
][module_name
] = nil;
150 return nil, ok
and module_err
or result
;
154 return nil, "global-module-already-loaded";
159 local _log
= logger
.init(host
..":"..module_name
);
160 local api_instance
= setmetatable({ name
= module_name
, host
= host
,
161 _log
= _log
, log = function (self
, ...) return _log(...); end, --luacheck: ignore 212/self
162 event_handlers
= new_multitable(), reloading
= not not state
,
163 saved_state
= state
~=true and state
or nil }
164 , { __index
= api
});
166 local pluginenv
= setmetatable({ module
= api_instance
}, { __index
= _G
});
167 api_instance
.environment
= pluginenv
;
169 local mod, err
= pluginloader
.load_code(module_name
, nil, pluginenv
);
171 log("error", "Unable to load module '%s': %s", module_name
or "nil", err
or "nil");
172 api_instance
:set_status("error", "Failed to load (see log)");
176 api_instance
.path
= err
;
178 modulemap
[host
][module_name
] = pluginenv
;
179 local ok
, err
= xpcall(mod, debug_traceback
);
181 -- Call module's "load"
182 if module_has_method(pluginenv
, "load") then
183 ok
, err
= call_module_method(pluginenv
, "load");
185 log("warn", "Error loading module '%s' on '%s': %s", module_name
, host
, err
or "nil");
186 api_instance
:set_status("warn", "Error during load (see log)");
189 api_instance
.reloading
, api_instance
.saved_state
= nil, nil;
191 if api_instance
.host
== "*" then
192 if not api_instance
.global
then -- COMPAT w/pre-0.9
194 log("warn", "mod_%s: Setting module.host = '*' deprecated, call module:set_global() instead", module_name
);
196 api_instance
:set_global();
198 modulemap
[host
][module_name
] = nil;
199 modulemap
[api_instance
.host
][module_name
] = pluginenv
;
200 if host
~= api_instance
.host
and module_has_method(pluginenv
, "add_host") then
201 -- Now load the module again onto the host it was originally being loaded on
202 ok
, err
= do_load_module(host
, module_name
);
207 modulemap
[api_instance
.host
][module_name
] = nil;
208 log("error", "Error initializing module '%s' on '%s': %s", module_name
, host
, err
or "nil");
209 api_instance
:set_status("warn", "Error during load (see log)");
211 api_instance
:set_status("core", "Loaded", false);
213 return ok
and pluginenv
, err
;
216 local function do_reload_module(host
, name
)
217 local mod = get_module(host
, name
);
218 if not mod then return nil, "module-not-loaded"; end
220 local _mod
, err
= pluginloader
.load_code(name
); -- checking for syntax errors
222 log("error", "Unable to load module '%s': %s", name
or "nil", err
or "nil");
227 if module_has_method(mod, "save") then
228 local ok
, ret
, err
= call_module_method(mod, "save");
232 log("warn", "Error saving module '%s:%s' state: %s", host
, name
, ret
);
233 if not config
.get(host
, "force_module_reload") then
234 log("warn", "Aborting reload due to error, set force_module_reload to ignore this");
235 return nil, "save-state-failed";
237 log("warn", "Continuing with reload (using the force)");
242 mod.module
.reloading
= true;
243 do_unload_module(host
, name
);
244 local ok
, err
= do_load_module(host
, name
, saved
or true);
246 mod = get_module(host
, name
);
247 if module_has_method(mod, "restore") then
248 local ok
, err
= call_module_method(mod, "restore", saved
or {})
249 if (not ok
) and err
then
250 log("warn", "Error restoring module '%s' from '%s': %s", name
, host
, err
);
254 return ok
and mod, err
;
259 -- Load a module and fire module-loaded event
260 function load(host
, name
)
261 local mod, err
= do_load_module(host
, name
);
263 (hosts
[mod.module
.host
] or prosody
).events
.fire_event("module-loaded", { module
= name
, host
= mod.module
.host
});
268 -- Unload a module and fire module-unloaded
269 function unload(host
, name
)
270 local ok
, err
= do_unload_module(host
, name
);
272 (hosts
[host
] or prosody
).events
.fire_event("module-unloaded", { module
= name
, host
= host
});
277 function reload(host
, name
)
278 local mod, err
= do_reload_module(host
, name
);
280 modulemap
[host
][name
].module
.reloading
= true;
281 (hosts
[host
] or prosody
).events
.fire_event("module-reloaded", { module
= name
, host
= host
});
282 mod.module
.reloading
= nil;
283 elseif not is_loaded(host
, name
) then
284 (hosts
[host
] or prosody
).events
.fire_event("module-unloaded", { module
= name
, host
= host
});
289 function get_module(host
, name
)
290 return modulemap
[host
] and modulemap
[host
][name
];
293 function get_items(key
, host
)
295 local modules
= modulemap
[host
];
296 if not key
or not host
or not modules
then return nil; end
298 for _
, module
in pairs(modules
) do
299 local mod = module
.module
;
300 if mod.items
and mod.items
[key
] then
301 for _
, value
in ipairs(mod.items
[key
]) do
302 t_insert(result
, value
);
310 function get_modules(host
)
311 return modulemap
[host
];
314 function is_loaded(host
, name
)
315 return modulemap
[host
] and modulemap
[host
][name
] and true;
318 function module_has_method(module
, method
)
319 return type(rawget(module
.module
, method
)) == "function";
322 function call_module_method(module
, method
, ...)
323 local f
= rawget(module
.module
, method
);
324 if type(f
) == "function" then
325 return xpcall(f
, debug_traceback
, ...);
327 return false, "no-such-method";
332 get_modules_for_host
= get_modules_for_host
;
333 load_modules_for_host
= load_modules_for_host
;
337 get_module
= get_module
;
338 get_items
= get_items
;
339 get_modules
= get_modules
;
340 is_loaded
= is_loaded
;
341 module_has_method
= module_has_method
;
342 call_module_method
= call_module_method
;