SASLprep authentication and password in SASL PLAIN implementation.
[prosody.git] / core / modulemanager.lua
index 7c30cb379629d0949e878750013cb75e3af60a83..9cd5618722e5baba6ff3ddeaaf2864773773828b 100644 (file)
@@ -65,9 +65,11 @@ function load_modules_for_host(host)
        end
 
        -- Load auto-loaded modules for this host
-       for _, module in ipairs(autoload_modules) do
-               if not disabled_set[module] then
-                       load(host, module);
+       if hosts[host].type == "local" then
+               for _, module in ipairs(autoload_modules) do
+                       if not disabled_set[module] then
+                               load(host, module);
+                       end
                end
        end
 
@@ -126,30 +128,39 @@ function load(host, module_name, config)
        local pluginenv = setmetatable({ module = api_instance }, { __index = _G });
        
        setfenv(mod, pluginenv);
-       if not hosts[host] then hosts[host] = { type = "component", host = host, connected = false, s2sout = {} }; end
-       hosts[host].modules = modulemap[host];
-       
-       local success, ret = pcall(mod);
-       if not success then
-               log("error", "Error initialising module '%s': %s", module_name or "nil", ret or "nil");
-               return nil, ret;
+       if not hosts[host] then
+               local create_component = _G.require "core.componentmanager".create_component;
+               hosts[host] = create_component(host);
+               hosts[host].connected = false;
+               log("debug", "Created new component: %s", host);
        end
+       hosts[host].modules = modulemap[host];
+       modulemap[host][module_name] = pluginenv;
        
-       if module_has_method(pluginenv, "load") then
-               local ok, err = call_module_method(pluginenv, "load");
-               if (not ok) and err then
-                       log("warn", "Error loading module '%s' on '%s': %s", module_name, host, err);
+       local success, err = pcall(mod);
+       if success then
+               if module_has_method(pluginenv, "load") then
+                       success, err = call_module_method(pluginenv, "load");
+                       if not success then
+                               log("warn", "Error loading module '%s' on '%s': %s", module_name, host, err or "nil");
+                       end
                end
-       end
 
-       -- Use modified host, if the module set one
-       modulemap[api_instance.host][module_name] = pluginenv;
-       
-       if api_instance.host == "*" and host ~= "*" then
-               api_instance:set_global();
+               -- Use modified host, if the module set one
+               if api_instance.host == "*" and host ~= "*" then
+                       modulemap[host][module_name] = nil;
+                       modulemap["*"][module_name] = pluginenv;
+                       api_instance:set_global();
+               end
+       else
+               log("error", "Error initializing module '%s' on '%s': %s", module_name, host, err or "nil");
+       end
+       if success then
+               return true;
+       else -- load failed, unloading
+               unload(api_instance.host, module_name);
+               return nil, err;
        end
-               
-       return true;
 end
 
 function get_module(host, name)
@@ -379,7 +390,14 @@ function api:require(lib)
 end
 
 function api:get_option(name, default_value)
-       return config.get(self.host, self.name, name) or config.get(self.host, "core", name) or default_value;
+       local value = config.get(self.host, self.name, name);
+       if value == nil then
+               value = config.get(self.host, "core", name);
+               if value == nil then
+                       value = default_value;
+               end
+       end
+       return value;
 end
 
 local t_remove = _G.table.remove;