Backed out the backout. Now we're back out.
[prosody.git] / core / modulemanager.lua
index 6d803a262f38eee776d333d3cde0d895222ad352..b0fb6cd9b7dc39b21707a15dea3aba408e410cad 100644 (file)
@@ -10,6 +10,7 @@ local type = type;
 local tostring, print = tostring, print;
 
 local _G = _G;
+local debug = debug;
 
 module "modulemanager"
 
@@ -18,43 +19,57 @@ local handlers = {};
                                        
 local modulehelpers = setmetatable({}, { __index = _G });
 
-function modulehelpers.add_iq_handler(origin_type, xmlns, handler)
-       if not (origin_type and handler and xmlns) then return false; end
+local function _add_iq_handler(module, origin_type, xmlns, handler)
        handlers[origin_type] = handlers[origin_type] or {};
        handlers[origin_type].iq = handlers[origin_type].iq or {};
        if not handlers[origin_type].iq[xmlns] then
                handlers[origin_type].iq[xmlns]= handler;
-               handler_info[handler] = getfenv(2).module;
-               log("debug", "mod_%s now handles tag 'iq' with query namespace '%s'", getfenv(2).module.name, xmlns);
+               handler_info[handler] = module;
+               log("debug", "mod_%s now handles tag 'iq' with query namespace '%s'", module.name, xmlns);
        else
-               log("warning", "mod_%s wants to handle tag 'iq' with query namespace '%s' but mod_%s already handles that", getfenv(2).module.name, xmlns, handler_info[handlers[origin_type].iq[xmlns]].module.name);
+               log("warning", "mod_%s wants to handle tag 'iq' with query namespace '%s' but mod_%s already handles that", module.name, xmlns, handler_info[handlers[origin_type].iq[xmlns]].module.name);
        end
 end
 
-function modulehelpers.add_handler(origin_type, tag, xmlns, handler)
-       if not (origin_type and tag and xmlns and handler) then return false; end
+function modulehelpers.add_iq_handler(origin_type, xmlns, handler)
+       if not (origin_type and handler and xmlns) then return false; end
+       if type(origin_type) == "table" then
+               for _, origin_type in ipairs(origin_type) do
+                       _add_iq_handler(getfenv(2).module, origin_type, xmlns, handler);
+               end
+               return;
+       end
+       _add_iq_handler(getfenv(2).module, origin_type, xmlns, handler);
+end
+
+local function _add_handler(module, origin_type, tag, xmlns, handler)
        handlers[origin_type] = handlers[origin_type] or {};
        if not handlers[origin_type][tag] then
                handlers[origin_type][tag] = handlers[origin_type][tag] or {};
                handlers[origin_type][tag][xmlns]= handler;
-               handler_info[handler] = getfenv(2).module;
-               log("debug", "mod_%s now handles tag '%s'", getfenv(2).module.name, tag);
+               handler_info[handler] = module;
+               log("debug", "mod_%s now handles tag '%s'", module.name, tag);
        elseif handler_info[handlers[origin_type][tag]] then
-               log("warning", "mod_%s wants to handle tag '%s' but mod_%s already handles that", getfenv(2).module.name, tag, handler_info[handlers[origin_type][tag]].module.name);
+               log("warning", "mod_%s wants to handle tag '%s' but mod_%s already handles that", module.name, tag, handler_info[handlers[origin_type][tag]].module.name);
        end
 end
 
-function loadall()
-       load("saslauth");
-       load("legacyauth");
-       load("roster");
+function modulehelpers.add_handler(origin_type, tag, xmlns, handler)
+       if not (origin_type and tag and xmlns and handler) then return false; end
+       if type(origin_type) == "table" then
+               for _, origin_type in ipairs(origin_type) do
+                       _add_handler(getfenv(2).module, origin_type, tag, xmlns, handler);
+               end
+               return;
+       end
+       _add_handler(getfenv(2).module, origin_type, tag, xmlns, handler);
 end
 
 function load(name)
        local mod, err = loadfile("plugins/mod_"..name..".lua");
        if not mod then
                log("error", "Unable to load module '%s': %s", name or "nil", err or "nil");
-               return;
+               return nil, err;
        end
        
        local pluginenv = setmetatable({ module = { name = name } }, { __index = modulehelpers });
@@ -63,8 +78,9 @@ function load(name)
        local success, ret = pcall(mod);
        if not success then
                log("error", "Error initialising module '%s': %s", name or "nil", ret or "nil");
-               return;
+               return nil, ret;
        end
+       return true;
 end
 
 function handle_stanza(origin, stanza)
@@ -74,14 +90,13 @@ function handle_stanza(origin, stanza)
                log("debug", "Stanza is an <iq/>");
                local child = stanza.tags[1];
                if child then
-                       local xmlns = child.attr.xmlns;
-                       log("debug", "Stanza has xmlns: %s", xmlns);
+                       local xmlns = child.attr.xmlns or xmlns;
+                       log("debug", "Stanza of type %s from %s has xmlns: %s", name, origin_type, xmlns);
                        local handler = handlers[origin_type][name][xmlns];
                        if  handler then
                                log("debug", "Passing stanza to mod_%s", handler_info[handler].name);
                                return handler(origin, stanza) or true;
                        end
-
                end
        elseif handlers[origin_type] then
                local handler = handlers[origin_type][name];