modulemanager: load(): Return and use the correct module object
[prosody.git] / core / modulemanager.lua
index bbe24e32071a79b7799328a3b1b2eec890caf0fb..a192e6372902e4aada59c98d28ba92fa6ef7f40f 100644 (file)
@@ -9,13 +9,10 @@
 local logger = require "util.logger";
 local log = logger.init("modulemanager");
 local config = require "core.configmanager";
-local multitable_new = require "util.multitable".new;
-local st = require "util.stanza";
 local pluginloader = require "util.pluginloader";
 
 local hosts = hosts;
 local prosody = prosody;
-local prosody_events = prosody.events;
 
 local loadfile, pcall, xpcall = loadfile, pcall, xpcall;
 local setmetatable, setfenv, getfenv = setmetatable, setfenv, getfenv;
@@ -47,12 +44,9 @@ module "modulemanager"
 
 local api = _G.require "core.moduleapi"; -- Module API container
 
+-- [host] = { [module] = module_env }
 local modulemap = { ["*"] = {} };
 
-local modulehelpers = setmetatable({}, { __index = _G });
-
-local hooks = multitable_new();
-
 local NULL = {};
 
 -- Load modules when a host is activated
@@ -87,10 +81,39 @@ function load_modules_for_host(host)
                load(host, module);
        end
 end
-prosody_events.add_handler("host-activated", load_modules_for_host);
---
+prosody.events.add_handler("host-activated", load_modules_for_host);
 
-function load(host, module_name, config)
+--- Private helpers ---
+
+local function do_unload_module(host, name)
+       local mod = get_module(host, name);
+       if not mod then return nil, "module-not-loaded"; end
+       
+       if module_has_method(mod, "unload") then
+               local ok, err = call_module_method(mod, "unload");
+               if (not ok) and err then
+                       log("warn", "Non-fatal error unloading module '%s' on '%s': %s", name, host, err);
+               end
+       end
+       
+       for handler, event in pairs(mod.module.event_handlers) do
+               event.object.remove_handler(event.name, handler);
+       end
+       
+       if mod.module.items then -- remove items
+               for key,t in pairs(mod.module.items) do
+                       for i = #t,1,-1 do
+                               local value = t[i];
+                               t[i] = nil;
+                               hosts[host].events.fire_event("item-removed/"..key, {source = mod.module, item = value});
+                       end
+               end
+       end
+       modulemap[host][name] = nil;
+       return true;
+end
+
+local function do_load_module(host, module_name)
        if not (host and module_name) then
                return nil, "insufficient-parameters";
        elseif not hosts[host] then
@@ -116,7 +139,9 @@ function load(host, module_name, config)
        end
 
        local _log = logger.init(host..":"..module_name);
-       local api_instance = setmetatable({ name = module_name, host = host, path = err, _log = _log, log = function (self, ...) return _log(...); end }, { __index = api });
+       local api_instance = setmetatable({ name = module_name, host = host, path = err,
+               _log = _log, log = function (self, ...) return _log(...); end, event_handlers = {} }
+               , { __index = api });
 
        local pluginenv = setmetatable({ module = api_instance }, { __index = _G });
        api_instance.environment = pluginenv;
@@ -125,11 +150,12 @@ function load(host, module_name, config)
        hosts[host].modules = modulemap[host];
        modulemap[host][module_name] = pluginenv;
        
-       local success, err = pcall(mod);
-       if success then
+       local ok, err = pcall(mod);
+       if ok then
+               -- Call module's "load"
                if module_has_method(pluginenv, "load") then
-                       success, err = call_module_method(pluginenv, "load");
-                       if not success then
+                       ok, err = call_module_method(pluginenv, "load");
+                       if not ok then
                                log("warn", "Error loading module '%s' on '%s': %s", module_name, host, err or "nil");
                        end
                end
@@ -142,62 +168,12 @@ function load(host, module_name, config)
                end
        else
                log("error", "Error initializing module '%s' on '%s': %s", module_name, host, err or "nil");
+               do_unload_module(api_instance.host, module_name); -- Ignore error, module may be partially-loaded
        end
-       if success then
-               (hosts[api_instance.host] or prosody).events.fire_event("module-loaded", { module = module_name, host = host });
-               return true;
-       else -- load failed, unloading
-               unload(api_instance.host, module_name);
-               return nil, err;
-       end
-end
-
-function get_module(host, name)
-       return modulemap[host] and modulemap[host][name];
-end
-
-function is_loaded(host, name)
-       return modulemap[host] and modulemap[host][name] and true;
-end
-
-function unload(host, name, ...)
-       local mod = get_module(host, name);
-       if not mod then return nil, "module-not-loaded"; end
-       
-       if module_has_method(mod, "unload") then
-               local ok, err = call_module_method(mod, "unload");
-               if (not ok) and err then
-                       log("warn", "Non-fatal error unloading module '%s' on '%s': %s", name, host, err);
-               end
-       end
-       -- unhook event handlers hooked by module:hook
-       for event, handlers in pairs(hooks:get(host, name) or NULL) do
-               for handler in pairs(handlers or NULL) do
-                       (hosts[host] or prosody).events.remove_handler(event, handler);
-               end
-       end
-       -- unhook event handlers hooked by module:hook_global
-       for event, handlers in pairs(hooks:get("*", name) or NULL) do
-               for handler in pairs(handlers or NULL) do
-                       prosody.events.remove_handler(event, handler);
-               end
-       end
-       hooks:remove(host, name);
-       if mod.module.items then -- remove items
-               for key,t in pairs(mod.module.items) do
-                       for i = #t,1,-1 do
-                               local value = t[i];
-                               t[i] = nil;
-                               hosts[host].events.fire_event("item-removed/"..key, {source = mod.module, item = value});
-                       end
-               end
-       end
-       modulemap[host][name] = nil;
-       (hosts[host] or prosody).events.fire_event("module-unloaded", { module = name, host = host });
-       return true;
+       return ok and pluginenv, err;
 end
 
-function reload(host, name, ...)
+local function do_reload_module(host, name)
        local mod = get_module(host, name);
        if not mod then return nil, "module-not-loaded"; end
 
@@ -208,7 +184,6 @@ function reload(host, name, ...)
        end
 
        local saved;
-
        if module_has_method(mod, "save") then
                local ok, ret, err = call_module_method(mod, "save");
                if ok then
@@ -224,8 +199,8 @@ function reload(host, name, ...)
                end
        end
 
-       unload(host, name, ...);
-       local ok, err = load(host, name, ...);
+       do_unload_module(host, name);
+       local ok, err = do_load_module(host, name);
        if ok then
                mod = get_module(host, name);
                if module_has_method(mod, "restore") then
@@ -234,11 +209,52 @@ function reload(host, name, ...)
                                log("warn", "Error restoring module '%s' from '%s': %s", name, host, err);
                        end
                end
-               return true;
+       end
+       return ok and mod, err;
+end
+
+--- Public API ---
+
+-- Load a module and fire module-loaded event
+function load(host, name)
+       local mod, err = do_load_module(host, name);
+       if mod then
+               (hosts[mod.module.host] or prosody).events.fire_event("module-loaded", { module = name, host = host });
+       end
+       return mod, err;
+end
+
+-- Unload a module and fire module-unloaded
+function unload(host, name)
+       local ok, err = do_unload_module(host, name);
+       if ok then
+               (hosts[host] or prosody).events.fire_event("module-unloaded", { module = name, host = host });
+       end
+       return ok, err;
+end
+
+function reload(host, name)
+       local ok, err = do_reload_module(host, name);
+       if ok then
+               (hosts[host] or prosody).events.fire_event("module-reloaded", { module = name, host = host });
+       elseif not is_loaded(host, name) then
+               (hosts[host] or prosody).events.fire_event("module-unloaded", { module = name, host = host });
        end
        return ok, err;
 end
 
+function get_module(host, name)
+       return modulemap[host] and modulemap[host][name];
+end
+
+function get_modules(host)
+       return modulemap[host];
+end
+
+function is_loaded(host, name)
+       return modulemap[host] and modulemap[host][name] and true;
+end
+
 function module_has_method(module, method)
        return type(module.module[method]) == "function";
 end