diff options
-rw-r--r-- | core/modulemanager.lua | 111 |
1 files changed, 16 insertions, 95 deletions
diff --git a/core/modulemanager.lua b/core/modulemanager.lua index 65ab3e1a..3c27e7a0 100644 --- a/core/modulemanager.lua +++ b/core/modulemanager.lua @@ -46,10 +46,8 @@ local api = {}; -- Module API container local modulemap = { ["*"] = {} }; -local m_handler_info = multitable_new(); -local m_stanza_handlers = multitable_new(); +local stanza_handlers = multitable_new(); local handler_info = {}; -local stanza_handlers = {}; local modulehelpers = setmetatable({}, { __index = _G }); @@ -72,7 +70,6 @@ function load(host, module_name, config) if not modulemap[host] then modulemap[host] = {}; - stanza_handlers[host] = {}; elseif modulemap[host][module_name] then log("warn", "%s is already loaded for %s, so not loading again", module_name, host); return nil, "module-already-loaded"; @@ -123,46 +120,13 @@ function unload(host, name, ...) end -function _handle_stanza(host, origin, stanza) - local name, xmlns, origin_type = stanza.name, stanza.attr.xmlns, origin.type; - - local handlers = stanza_handlers[host]; - if not handlers then - log("warn", "No handlers for %s", host); - return false; - end - - if name == "iq" and xmlns == "jabber:client" and handlers[origin_type] then - local child = stanza.tags[1]; - if child then - 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] and 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]; - if handler then - handler = handler[xmlns]; - if handler then - log("debug", "Passing stanza to mod_%s", handler_info[handler].name); - return handler(origin, stanza) or true; - end - end - end - log("debug", "Stanza unhandled by any modules, xmlns: %s", stanza.attr.xmlns); - return false; -- we didn't handle it -end function handle_stanza(host, origin, stanza) local name, xmlns, origin_type = stanza.name, stanza.attr.xmlns, origin.type; if name == "iq" and xmlns == "jabber:client" then xmlns = stanza.tags[1].attr.xmlns; log("debug", "Stanza of type %s from %s has xmlns: %s", name, origin_type, xmlns); end - local handlers = m_stanza_handlers:get(host, origin_type, name, xmlns); + local handlers = stanza_handlers:get(host, origin_type, name, xmlns); if handlers then log("debug", "Passing stanza to mod_%s", handler_info[handlers[1]].name); (handlers[1])(origin, stanza); @@ -185,39 +149,30 @@ function api:get_host() return self.host; end - -local function __add_iq_handler(module, origin_type, xmlns, handler) - local handlers = stanza_handlers[module.host]; - 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] = module; - module:log("debug", "I now handle tag 'iq' [%s] with payload namespace '%s'", origin_type, xmlns); - else - module:log("warn", "I wanted to handle tag 'iq' [%s] with payload namespace '%s' but mod_%s already handles that", origin_type, xmlns, handler_info[handlers[origin_type].iq[xmlns]].name); - end -end -local function _add_iq_handler(module, origin_type, xmlns, handler) - local handlers = m_stanza_handlers:get(module.host, origin_type, "iq", xmlns); +local function _add_handler(module, origin_type, tag, xmlns, handler) + local handlers = stanza_handlers:get(module.host, origin_type, tag, xmlns); + local msg = (tag == "iq") and "namespace" or "payload namespace"; if not handlers then - m_stanza_handlers:add(module.host, origin_type, "iq", xmlns, handler); + stanza_handlers:add(module.host, origin_type, tag, xmlns, handler); handler_info[handler] = module; - module:log("debug", "I now handle tag 'iq' [%s] with payload namespace '%s'", origin_type, xmlns); + module:log("debug", "I now handle tag '%s' [%s] with %s '%s'", tag, origin_type, msg, xmlns); else - module:log("warn", "I wanted to handle tag 'iq' [%s] with payload namespace '%s' but mod_%s already handles that", origin_type, xmlns, handler_info[handlers[1]].name); + module:log("warn", "I wanted to handle tag '%s' [%s] with %s '%s' but mod_%s already handles that", tag, origin_type, msg, xmlns, handler_info[handlers[1]].module.name); end end -function api:add_iq_handler(origin_type, xmlns, handler) - if not (origin_type and handler and xmlns) then return false; end +function api: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_iq_handler(self, origin_type, xmlns, handler); + _add_handler(self, origin_type, tag, xmlns, handler); end - return; + else + _add_handler(self, origin_type, tag, xmlns, handler); end - _add_iq_handler(self, origin_type, xmlns, handler); +end +function api:add_iq_handler(origin_type, xmlns, handler) + self:add_handler(origin_type, "iq", xmlns, handler); end function api:add_feature(xmlns) @@ -231,40 +186,6 @@ end function api:add_event_hook (...) return eventmanager.add_event_hook(...); end -local function __add_handler(module, origin_type, tag, xmlns, handler) - local handlers = stanza_handlers[module.host]; - 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] = module; - module:log("debug", "I now handle tag '%s' [%s] with xmlns '%s'", tag, origin_type, xmlns); - elseif handler_info[handlers[origin_type][tag]] then - log("warning", "I wanted to handle tag '%s' [%s] but mod_%s already handles that", tag, origin_type, handler_info[handlers[origin_type][tag]].module.name); - end -end -local function _add_handler(module, origin_type, tag, xmlns, handler) - local handlers = m_stanza_handlers:get(module.host, origin_type, tag, xmlns); - if not handlers then - m_stanza_handlers:add(module.host, origin_type, tag, xmlns, handler); - handler_info[handler] = module; - module:log("debug", "I now handle tag '%s' [%s] with xmlns '%s'", tag, origin_type, xmlns); - else - module:log("warning", "I wanted to handle tag '%s' [%s] but mod_%s already handles that", tag, origin_type, handler_info[handlers[1]].module.name); - end -end - -function api: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(self, origin_type, tag, xmlns, handler); - end - return; - end - _add_handler(self, origin_type, tag, xmlns, handler); -end - -------------------------------------------------------------------- return _M; |