core/componentmanager.lua
changeset 751 7c22619fdb19
parent 703 f9909efed20c
child 758 b1885732e979
equal deleted inserted replaced
750:fbfcf8c1c830 751:7c22619fdb19
    19 
    19 
    20 
    20 
    21 
    21 
    22 
    22 
    23 local log = require "util.logger".init("componentmanager");
    23 local log = require "util.logger".init("componentmanager");
    24 local module_load = require "core.modulemanager".load;
    24 local configmanager = require "core.configmanager";
    25 local module_unload = require "core.modulemanager".unload;
    25 local eventmanager = require "core.eventmanager";
       
    26 local modulemanager = require "core.modulemanager";
    26 local jid_split = require "util.jid".split;
    27 local jid_split = require "util.jid".split;
    27 local hosts = hosts;
    28 local hosts = hosts;
       
    29 
       
    30 local pairs, type, tostring = pairs, type, tostring;
    28 
    31 
    29 local components = {};
    32 local components = {};
    30 
    33 
    31 module "componentmanager"
    34 module "componentmanager"
       
    35 
       
    36 function load_enabled_components(config)
       
    37 	local defined_hosts = config or configmanager.getconfig();
       
    38 		
       
    39 	for host, host_config in pairs(defined_hosts) do
       
    40 		if host ~= "*" and ((host_config.core.enabled == nil or host_config.core.enabled) and type(host_config.core.component_module) == "string") then
       
    41 			hosts[host] = { type = "component", host = host, connected = true, s2sout = {} };
       
    42 			modulemanager.load(host, "dialback");
       
    43 			local ok, err = modulemanager.load(host, host_config.core.component_module);
       
    44 			if not ok then
       
    45 				log("error", "Error loading %s component %s: %s", tostring(host_config.core.component_module), tostring(host), tostring(err));
       
    46 			else
       
    47 				log("info", "Activated %s component: %s", host_config.core.component_module, host);
       
    48 			end
       
    49 			
       
    50 			local ok, component_handler = modulemanager.call_module_method(modulemanager.get_module(host, host_config.core.component_module), "load_component");
       
    51 			if not ok then
       
    52 				log("error", "Error loading %s component %s: %s", tostring(host_config.core.component_module), tostring(host), tostring(component_handler));
       
    53 			else
       
    54 				components[host] = component_handler;
       
    55 			end
       
    56 		end
       
    57 	end
       
    58 end
       
    59 
       
    60 eventmanager.add_event_hook("server-starting", load_enabled_components);
    32 
    61 
    33 function handle_stanza(origin, stanza)
    62 function handle_stanza(origin, stanza)
    34 	local node, host = jid_split(stanza.attr.to);
    63 	local node, host = jid_split(stanza.attr.to);
    35 	local component = nil;
    64 	local component = nil;
    36 	if not component then component = components[stanza.attr.to]; end -- hack to allow hooking node@server/resource and server/resource
    65 	if not component then component = components[stanza.attr.to]; end -- hack to allow hooking node@server/resource and server/resource
    48 	if not hosts[host] then
    77 	if not hosts[host] then
    49 		-- TODO check for host well-formedness
    78 		-- TODO check for host well-formedness
    50 		components[host] = component;
    79 		components[host] = component;
    51 		hosts[host] = { type = "component", host = host, connected = true, s2sout = {} };
    80 		hosts[host] = { type = "component", host = host, connected = true, s2sout = {} };
    52 		-- FIXME only load for a.b.c if b.c has dialback, and/or check in config
    81 		-- FIXME only load for a.b.c if b.c has dialback, and/or check in config
    53 		module_load(host, "dialback");
    82 		modulemanager.load(host, "dialback");
    54 		log("debug", "component added: "..host);
    83 		log("debug", "component added: "..host);
    55 		return hosts[host];
    84 		return hosts[host];
    56 	else
    85 	else
    57 		log("error", "Attempt to set component for existing host: "..host);
    86 		log("error", "Attempt to set component for existing host: "..host);
    58 	end
    87 	end
    59 end
    88 end
    60 
    89 
    61 function deregister_component(host)
    90 function deregister_component(host)
    62 	if components[host] then
    91 	if components[host] then
    63 		module_unload(host, "dialback");
    92 		modulemanager.unload(host, "dialback");
    64 		components[host] = nil;
    93 		components[host] = nil;
    65 		hosts[host] = nil;
    94 		hosts[host] = nil;
    66 		log("debug", "component removed: "..host);
    95 		log("debug", "component removed: "..host);
    67 		return true;
    96 		return true;
    68 	else
    97 	else