core/modulemanager.lua
changeset 591 980ded4c60ef
parent 590 54afe37cccbf
child 592 c6e2c727d0cc
equal deleted inserted replaced
590:54afe37cccbf 591:980ded4c60ef
    44 
    44 
    45 local api = {}; -- Module API container
    45 local api = {}; -- Module API container
    46 
    46 
    47 local modulemap = {};
    47 local modulemap = {};
    48 
    48 
    49 local m_handler_info = multitable_new();
    49 local stanza_handlers = multitable_new();
    50 local m_stanza_handlers = multitable_new();
       
    51 local handler_info = {};
    50 local handler_info = {};
    52 local stanza_handlers = {};
       
    53 
    51 
    54 local modulehelpers = setmetatable({}, { __index = _G });
    52 local modulehelpers = setmetatable({}, { __index = _G });
    55 
    53 
    56 -- Load modules when a host is activated
    54 -- Load modules when a host is activated
    57 function load_modules_for_host(host)
    55 function load_modules_for_host(host)
    75 		return nil, err;
    73 		return nil, err;
    76 	end
    74 	end
    77 	
    75 	
    78 	if not modulemap[host] then
    76 	if not modulemap[host] then
    79 		modulemap[host] = {};
    77 		modulemap[host] = {};
    80 		stanza_handlers[host] = {};
       
    81 	elseif modulemap[host][module_name] then
    78 	elseif modulemap[host][module_name] then
    82 		log("warn", "%s is already loaded for %s, so not loading again", module_name, host);
    79 		log("warn", "%s is already loaded for %s, so not loading again", module_name, host);
    83 		return nil, "module-already-loaded";
    80 		return nil, "module-already-loaded";
    84 	end
    81 	end
    85 	
    82 	
   122 	local name, xmlns, origin_type = stanza.name, stanza.attr.xmlns, origin.type;
   119 	local name, xmlns, origin_type = stanza.name, stanza.attr.xmlns, origin.type;
   123 	if name == "iq" and xmlns == "jabber:client" then
   120 	if name == "iq" and xmlns == "jabber:client" then
   124 		xmlns = stanza.tags[1].attr.xmlns;
   121 		xmlns = stanza.tags[1].attr.xmlns;
   125 		log("debug", "Stanza of type %s from %s has xmlns: %s", name, origin_type, xmlns);
   122 		log("debug", "Stanza of type %s from %s has xmlns: %s", name, origin_type, xmlns);
   126 	end
   123 	end
   127 	local handlers = m_stanza_handlers:get(host, origin_type, name, xmlns);
   124 	local handlers = stanza_handlers:get(host, origin_type, name, xmlns);
   128 	if handlers then
   125 	if handlers then
   129 		log("debug", "Passing stanza to mod_%s", handler_info[handlers[1]].name);
   126 		log("debug", "Passing stanza to mod_%s", handler_info[handlers[1]].name);
   130 		(handlers[1])(origin, stanza);
   127 		(handlers[1])(origin, stanza);
   131 		return true;
   128 		return true;
   132 	else
   129 	else
   146 function api:get_host()
   143 function api:get_host()
   147 	return self.host;
   144 	return self.host;
   148 end
   145 end
   149 
   146 
   150 local function _add_handler(module, origin_type, tag, xmlns, handler)
   147 local function _add_handler(module, origin_type, tag, xmlns, handler)
   151 	local handlers = m_stanza_handlers:get(module.host, origin_type, tag, xmlns);
   148 	local handlers = stanza_handlers:get(module.host, origin_type, tag, xmlns);
   152 	local msg = (tag == "iq") and "namespace" or "payload namespace";
   149 	local msg = (tag == "iq") and "namespace" or "payload namespace";
   153 	if not handlers then
   150 	if not handlers then
   154 		m_stanza_handlers:add(module.host, origin_type, tag, xmlns, handler);
   151 		stanza_handlers:add(module.host, origin_type, tag, xmlns, handler);
   155 		handler_info[handler] = module;
   152 		handler_info[handler] = module;
   156 		module:log("debug", "I now handle tag '%s' [%s] with %s '%s'", tag, origin_type, msg, xmlns);
   153 		module:log("debug", "I now handle tag '%s' [%s] with %s '%s'", tag, origin_type, msg, xmlns);
   157 	else
   154 	else
   158 		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);
   155 		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);
   159 	end
   156 	end