Merge 0.6.2/waqas with 0.6.2/MattJ
[prosody.git] / core / modulemanager.lua
index 6bc8e28565ba3a2fcad9ff0013199070ef2a9f17..ecb1bc6c7c020873f23181dd90e144fb54f1d62e 100644 (file)
@@ -56,22 +56,27 @@ local NULL = {};
 
 -- Load modules when a host is activated
 function load_modules_for_host(host)
+       local disabled_set = {};
+       local modules_disabled = config.get(host, "core", "modules_disabled");
+       if modules_disabled then
+               for _, module in ipairs(modules_disabled) do
+                       disabled_set[module] = true;
+               end
+       end
+
+       -- Load auto-loaded modules for this host
+       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
+
+       -- Load modules from global section
        if config.get(host, "core", "load_global_modules") ~= false then
-               -- Load modules from global section
                local modules_enabled = config.get("*", "core", "modules_enabled");
-               local modules_disabled = config.get(host, "core", "modules_disabled");
-               local disabled_set = {};
                if modules_enabled then
-                       if modules_disabled then
-                               for _, module in ipairs(modules_disabled) do
-                                       disabled_set[module] = true;
-                               end
-                       end
-                       for _, module in ipairs(autoload_modules) do
-                               if not disabled_set[module] then
-                                       load(host, module);
-                               end
-                       end
                        for _, module in ipairs(modules_enabled) do
                                if not disabled_set[module] and not is_loaded(host, module) then
                                        load(host, module);
@@ -91,6 +96,7 @@ function load_modules_for_host(host)
        end
 end
 eventmanager.add_event_hook("host-activated", load_modules_for_host);
+eventmanager.add_event_hook("component-activated", load_modules_for_host);
 --
 
 function load(host, module_name, config)
@@ -122,29 +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
-       
-       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)
@@ -165,7 +181,6 @@ function unload(host, name, ...)
                        log("warn", "Non-fatal error unloading module '%s' on '%s': %s", name, host, err);
                end
        end
-       modulemap[host][name] = nil;
        local params = handler_table:get(host, name); -- , {module.host, origin_type, tag, xmlns}
        for _, param in pairs(params or NULL) do
                local handlers = stanza_handlers:get(param[1], param[2], param[3], param[4]);
@@ -182,6 +197,16 @@ function unload(host, name, ...)
                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 = self, item = value});
+                       end
+               end
+       end
+       modulemap[host][name] = nil;
        return true;
 end
 
@@ -245,12 +270,13 @@ function handle_stanza(host, origin, stanza)
                (handlers[1])(origin, stanza);
                return true;
        else
-               log("debug", "Unhandled %s stanza: %s; xmlns=%s", origin.type, stanza.name, xmlns); -- we didn't handle it
                if stanza.attr.xmlns == "jabber:client" then
+                       log("debug", "Unhandled %s stanza: %s; xmlns=%s", origin.type, stanza.name, xmlns); -- we didn't handle it
                        if stanza.attr.type ~= "error" and stanza.attr.type ~= "result" then
                                origin.send(st.error_reply(stanza, "cancel", "service-unavailable"));
                        end
                elseif not((name == "features" or name == "error") and xmlns == "http://etherx.jabber.org/streams") then -- FIXME remove check once we handle S2S features
+                       log("warn", "Unhandled %s stream element: %s; xmlns=%s: %s", origin.type, stanza.name, xmlns, tostring(stanza)); -- we didn't handle it
                        origin:close("unsupported-stanza-type");
                end
        end
@@ -373,7 +399,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;
@@ -405,6 +438,14 @@ function api:get_host_items(key)
                        end
                end
        end
+       for mod_name, module in pairs(modulemap["*"]) do
+               module = module.module;
+               if module.items then
+                       for _, item in ipairs(module.items[key] or NULL) do
+                               t_insert(result, item);
+                       end
+               end
+       end
        return result;
 end