modulemanager: Reduce warning to debug level message about modules already being...
[prosody.git] / core / modulemanager.lua
index 488319c3c855ac5d89babcc7046b49f8234a470d..4df950697d905984808ac38eec0d030b36f9c9cf 100644 (file)
@@ -18,8 +18,8 @@ local hosts = hosts;
 local prosody = prosody;
 
 local pcall, xpcall = pcall, xpcall;
-local setmetatable, rawget, setfenv = setmetatable, rawget, setfenv;
-local pairs, type, tostring = pairs, type, tostring;
+local setmetatable, rawget = setmetatable, rawget;
+local ipairs, pairs, type, tostring, t_insert = ipairs, pairs, type, tostring, table.insert;
 
 local debug_traceback = debug.traceback;
 local unpack, select = unpack, select;
@@ -30,7 +30,7 @@ pcall = function(f, ...)
 end
 
 local autoload_modules = {"presence", "message", "iq", "offline", "c2s", "s2s"};
-local component_inheritable_modules = {"tls", "dialback", "iq", "s2s"};
+local component_inheritable_modules = {"tls", "saslauth", "dialback", "iq", "s2s"};
 
 -- We need this to let modules access the real global namespace
 local _G = _G;
@@ -44,12 +44,12 @@ local modulemap = { ["*"] = {} };
 
 -- Load modules when a host is activated
 function load_modules_for_host(host)
-       local component = config.get(host, "core", "component_module");
+       local component = config.get(host, "component_module");
        
-       local global_modules_enabled = config.get("*", "core", "modules_enabled");
-       local global_modules_disabled = config.get("*", "core", "modules_disabled");
-       local host_modules_enabled = config.get(host, "core", "modules_enabled");
-       local host_modules_disabled = config.get(host, "core", "modules_disabled");
+       local global_modules_enabled = config.get("*", "modules_enabled");
+       local global_modules_disabled = config.get("*", "modules_disabled");
+       local host_modules_enabled = config.get(host, "modules_enabled");
+       local host_modules_disabled = config.get(host, "modules_disabled");
        
        if host_modules_enabled == global_modules_enabled then host_modules_enabled = nil; end
        if host_modules_disabled == global_modules_disabled then host_modules_disabled = nil; end
@@ -111,7 +111,7 @@ local function do_unload_module(host, name)
        return true;
 end
 
-local function do_load_module(host, module_name)
+local function do_load_module(host, module_name, state)
        if not (host and module_name) then
                return nil, "insufficient-parameters";
        elseif not hosts[host] and host ~= "*"then
@@ -119,14 +119,11 @@ local function do_load_module(host, module_name)
        end
        
        if not modulemap[host] then
-               modulemap[host] = {};
-               if host ~= "*" then
-                       hosts[host].modules = modulemap[host];
-               end
+               modulemap[host] = hosts[host].modules;
        end
        
        if modulemap[host][module_name] then
-               log("warn", "%s is already loaded for %s, so not loading again", module_name, host);
+               log("debug", "%s is already loaded for %s, so not loading again", module_name, host);
                return nil, "module-already-loaded";
        elseif modulemap["*"][module_name] then
                local mod = modulemap["*"][module_name];
@@ -152,22 +149,24 @@ local function do_load_module(host, module_name)
        end
        
 
-       local mod, err = pluginloader.load_code(module_name);
-       if not mod then
-               log("error", "Unable to load module '%s': %s", module_name or "nil", err or "nil");
-               return nil, err;
-       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, event_handlers = new_multitable() }
+       local api_instance = setmetatable({ name = module_name, host = host,
+               _log = _log, log = function (self, ...) return _log(...); end, event_handlers = new_multitable(),
+               reloading = not not state, saved_state = state~=true and state or nil }
                , { __index = api });
 
        local pluginenv = setmetatable({ module = api_instance }, { __index = _G });
        api_instance.environment = pluginenv;
        
-       setfenv(mod, pluginenv);
-       
+       local mod, err = pluginloader.load_code(module_name, nil, pluginenv);
+       if not mod then
+               log("error", "Unable to load module '%s': %s", module_name or "nil", err or "nil");
+               return nil, err;
+       end
+
+       api_instance.path = err;
+
        modulemap[host][module_name] = pluginenv;
        local ok, err = pcall(mod);
        if ok then
@@ -178,6 +177,7 @@ local function do_load_module(host, module_name)
                                log("warn", "Error loading module '%s' on '%s': %s", module_name, host, err or "nil");
                        end
                end
+               api_instance.reloading, api_instance.saved_state = nil, nil;
 
                if api_instance.host == "*" then
                        if not api_instance.global then -- COMPAT w/pre-0.9
@@ -218,7 +218,7 @@ local function do_reload_module(host, name)
                        saved = ret;
                else
                        log("warn", "Error saving module '%s:%s' state: %s", host, name, ret);
-                       if not config.get(host, "core", "force_module_reload") then
+                       if not config.get(host, "force_module_reload") then
                                log("warn", "Aborting reload due to error, set force_module_reload to ignore this");
                                return nil, "save-state-failed";
                        else
@@ -227,8 +227,9 @@ local function do_reload_module(host, name)
                end
        end
 
+       mod.module.reloading = true;
        do_unload_module(host, name);
-       local ok, err = do_load_module(host, name);
+       local ok, err = do_load_module(host, name, saved or true);
        if ok then
                mod = get_module(host, name);
                if module_has_method(mod, "restore") then
@@ -277,6 +278,23 @@ function get_module(host, name)
        return modulemap[host] and modulemap[host][name];
 end
 
+function get_items(key, host)
+       local result = {};
+       local modules = modulemap[host];
+       if not key or not host or not modules then return nil; end
+
+       for _, module in pairs(modules) do
+               local mod = module.module;
+               if mod.items and mod.items[key] then
+                       for _, value in ipairs(mod.items[key]) do
+                               t_insert(result, value);
+                       end
+               end
+       end
+
+       return result;
+end
+
 function get_modules(host)
        return modulemap[host];
 end