Comparison

core/modulemanager.lua @ 439:6608ad3a72f3

is_loaded() and incomplete unload() for modules
author Matthew Wild <mwild1@gmail.com>
date Thu, 27 Nov 2008 16:52:30 +0000
parent 438:193f9dd64f17
child 467:66f145f5c932
comparison
equal deleted inserted replaced
438:193f9dd64f17 439:6608ad3a72f3
25 25
26 local modulehelpers = setmetatable({}, { __index = _G }); 26 local modulehelpers = setmetatable({}, { __index = _G });
27 27
28 28
29 function load(host, module_name, config) 29 function load(host, module_name, config)
30 if not (host and module_name) then
31 return nil, "insufficient-parameters";
32 end
30 local mod, err = loadfile("plugins/mod_"..module_name..".lua"); 33 local mod, err = loadfile("plugins/mod_"..module_name..".lua");
31 if not mod then 34 if not mod then
32 log("error", "Unable to load module '%s': %s", module_name or "nil", err or "nil"); 35 log("error", "Unable to load module '%s': %s", module_name or "nil", err or "nil");
33 return nil, err; 36 return nil, err;
34 end 37 end
55 end 58 end
56 59
57 modulemap[host][module_name] = mod; 60 modulemap[host][module_name] = mod;
58 61
59 return true; 62 return true;
63 end
64
65 function is_loaded(host, name)
66 return modulemap[host] and modulemap[host][name] and true;
67 end
68
69 function unload(host, name, ...)
70 local mod = modulemap[host] and modulemap[host][name];
71 if not mod then return nil, "module-not-loaded"; end
72
73 if type(mod.unload) == "function" then
74 local ok, err = pcall(mod.unload, ...)
75 if (not ok) and err then
76 log("warn", "Non-fatal error unloading module '%s' from '%s': %s", name, host, err);
77 end
78 end
79
60 end 80 end
61 81
62 function handle_stanza(host, origin, stanza) 82 function handle_stanza(host, origin, stanza)
63 local name, xmlns, origin_type = stanza.name, stanza.attr.xmlns, origin.type; 83 local name, xmlns, origin_type = stanza.name, stanza.attr.xmlns, origin.type;
64 84