2 -- Copyright (C) 2008-2009 Matthew Wild
3 -- Copyright (C) 2008-2009 Waqas Hussain
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
12 local log = require "util.logger".init("componentmanager");
13 local configmanager = require "core.configmanager";
14 local eventmanager = require "core.eventmanager";
15 local modulemanager = require "core.modulemanager";
16 local jid_split = require "util.jid".split;
19 local pairs, type, tostring = pairs, type, tostring;
21 local components = {};
23 module "componentmanager"
25 function load_enabled_components(config)
26 local defined_hosts = config or configmanager.getconfig();
28 for host, host_config in pairs(defined_hosts) do
29 if host ~= "*" and ((host_config.core.enabled == nil or host_config.core.enabled) and type(host_config.core.component_module) == "string") then
30 hosts[host] = { type = "component", host = host, connected = false, s2sout = {} };
31 local ok, err = modulemanager.load(host, host_config.core.component_module);
33 log("error", "Error loading %s component %s: %s", tostring(host_config.core.component_module), tostring(host), tostring(err));
35 log("info", "Activated %s component: %s", host_config.core.component_module, host);
41 eventmanager.add_event_hook("server-starting", load_enabled_components);
43 function handle_stanza(origin, stanza)
44 local node, host = jid_split(stanza.attr.to);
45 local component = nil;
46 if not component then component = components[stanza.attr.to]; end -- hack to allow hooking node@server/resource and server/resource
47 if not component then component = components[node.."@"..host]; end -- hack to allow hooking node@server
48 if not component then component = components[host]; end
50 log("debug", "stanza being handled by component: "..host);
51 component(origin, stanza, hosts[host]);
53 log("error", "Component manager recieved a stanza for a non-existing component: " .. stanza.attr.to);
57 function create_component(host, component)
58 -- TODO check for host well-formedness
59 session = session or { type = "component", host = host, connected = true, s2sout = {}, send = component };
63 function register_component(host, component, session)
64 if not hosts[host] or (hosts[host].type == 'component' and not hosts[host].connected) then
65 components[host] = component;
66 hosts[host] = session or create_component(host, component);
68 -- FIXME only load for a.b.c if b.c has dialback, and/or check in config
69 modulemanager.load(host, "dialback");
70 log("debug", "component added: "..host);
71 return session or hosts[host];
73 log("error", "Attempt to set component for existing host: "..host);
77 function deregister_component(host)
78 if components[host] then
79 modulemanager.unload(host, "dialback");
80 components[host] = nil;
82 log("debug", "component removed: "..host);
85 log("error", "Attempt to remove component for non-existing host: "..host);