core/modulemanager.lua
changeset 584 eb0cea29c8d7
parent 579 81e68e5afce2
child 592 c6e2c727d0cc
equal deleted inserted replaced
583:5821eaa80baa 584:eb0cea29c8d7
    42 
    42 
    43 module "modulemanager"
    43 module "modulemanager"
    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 m_handler_info = multitable_new();
    50 local m_stanza_handlers = multitable_new();
    50 local m_stanza_handlers = multitable_new();
    51 local handler_info = {};
    51 local handler_info = {};
    52 local stanza_handlers = {};
    52 local stanza_handlers = {};
    67 
    67 
    68 function load(host, module_name, config)
    68 function load(host, module_name, config)
    69 	if not (host and module_name) then
    69 	if not (host and module_name) then
    70 		return nil, "insufficient-parameters";
    70 		return nil, "insufficient-parameters";
    71 	end
    71 	end
    72 	local mod, err = loadfile(plugin_dir.."mod_"..module_name..".lua");
       
    73 	if not mod then
       
    74 		log("error", "Unable to load module '%s': %s", module_name or "nil", err or "nil");
       
    75 		return nil, err;
       
    76 	end
       
    77 	
    72 	
    78 	if not modulemap[host] then
    73 	if not modulemap[host] then
    79 		modulemap[host] = {};
    74 		modulemap[host] = {};
    80 		stanza_handlers[host] = {};
    75 		stanza_handlers[host] = {};
    81 	elseif modulemap[host][module_name] then
    76 	elseif modulemap[host][module_name] then
    82 		log("warn", "%s is already loaded for %s, so not loading again", module_name, host);
    77 		log("warn", "%s is already loaded for %s, so not loading again", module_name, host);
    83 		return nil, "module-already-loaded";
    78 		return nil, "module-already-loaded";
    84 	end
    79 	elseif modulemap["*"][module_name] then
    85 	
    80 		return nil, "global-module-already-loaded";
       
    81 	end
       
    82 	
       
    83 
       
    84 	local mod, err = loadfile(plugin_dir.."mod_"..module_name..".lua");
       
    85 	if not mod then
       
    86 		log("error", "Unable to load module '%s': %s", module_name or "nil", err or "nil");
       
    87 		return nil, err;
       
    88 	end
       
    89 
    86 	local _log = logger.init(host..":"..module_name);
    90 	local _log = logger.init(host..":"..module_name);
    87 	local api_instance = setmetatable({ name = module_name, host = host, config = config,  _log = _log, log = function (self, ...) return _log(...); end }, { __index = api });
    91 	local api_instance = setmetatable({ name = module_name, host = host, config = config,  _log = _log, log = function (self, ...) return _log(...); end }, { __index = api });
    88 
    92 
    89 	local pluginenv = setmetatable({ module = api_instance }, { __index = _G });
    93 	local pluginenv = setmetatable({ module = api_instance }, { __index = _G });
    90 	
    94 	
    94 	if not success then
    98 	if not success then
    95 		log("error", "Error initialising module '%s': %s", name or "nil", ret or "nil");
    99 		log("error", "Error initialising module '%s': %s", name or "nil", ret or "nil");
    96 		return nil, ret;
   100 		return nil, ret;
    97 	end
   101 	end
    98 	
   102 	
    99 	modulemap[host][module_name] = mod;
   103 	-- Use modified host, if the module set one
       
   104 	modulemap[api_instance.host][module_name] = mod;
   100 	
   105 	
   101 	return true;
   106 	return true;
   102 end
   107 end
   103 
   108 
   104 function is_loaded(host, name)
   109 function is_loaded(host, name)