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 local disco_items = require "util.multitable".new();
25 require "core.discomanager".addDiscoItemsHandler("*host", function(reply, to, from, node)
26 if #node == 0 and hosts[to] then
27 for jid in pairs(disco_items:get(to) or NULL) do
28 reply:tag("item", {jid = jid}):up();
35 module "componentmanager"
37 local function default_component_handler(origin, stanza)
38 origin.send(st.error_reply(stanza, "wait", "service-unavailable", "Component unavailable"));
42 function load_enabled_components(config)
43 local defined_hosts = config or configmanager.getconfig();
45 for host, host_config in pairs(defined_hosts) do
46 if host ~= "*" and ((host_config.core.enabled == nil or host_config.core.enabled) and type(host_config.core.component_module) == "string") then
47 hosts[host] = { type = "component", host = host, connected = false, s2sout = {} };
48 components[host] = default_component_handler;
49 local ok, err = modulemanager.load(host, host_config.core.component_module);
51 log("error", "Error loading %s component %s: %s", tostring(host_config.core.component_module), tostring(host), tostring(err));
53 log("info", "Activated %s component: %s", host_config.core.component_module, host);
59 eventmanager.add_event_hook("server-starting", load_enabled_components);
61 function handle_stanza(origin, stanza)
62 local node, host = jid_split(stanza.attr.to);
63 local component = nil;
64 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[node.."@"..host]; end -- hack to allow hooking node@server
66 if not component then component = components[host]; end
68 log("debug", "stanza being handled by component: "..host);
69 component(origin, stanza, hosts[host]);
71 log("error", "Component manager recieved a stanza for a non-existing component: " .. stanza.attr.to);
75 function create_component(host, component)
76 -- TODO check for host well-formedness
77 local session = session or { type = "component", host = host, connected = true, s2sout = {}, send = component };
81 function register_component(host, component, session)
82 if not hosts[host] or (hosts[host].type == 'component' and not hosts[host].connected) then
83 components[host] = component;
84 hosts[host] = session or create_component(host, component);
86 if not(host:find("@", 1, true) or host:find("/", 1, true)) and host:find(".", 1, true) then
87 disco_items:set(host:sub(host:find(".", 1, true)+1), host, true);
89 -- FIXME only load for a.b.c if b.c has dialback, and/or check in config
90 modulemanager.load(host, "dialback");
91 log("debug", "component added: "..host);
92 return session or hosts[host];
94 log("error", "Attempt to set component for existing host: "..host);
98 function deregister_component(host)
99 if components[host] then
100 modulemanager.unload(host, "dialback");
101 components[host] = nil;
102 local host_config = defined_hosts[host];
103 if ((host_config.core.enabled == nil or host_config.core.enabled) and type(host_config.core.component_module) == "string") then
104 -- Set default handler
106 -- Component not in config, or disabled, remove
109 -- remove from disco_items
110 if not(host:find("@", 1, true) or host:find("/", 1, true)) and host:find(".", 1, true) then
111 disco_items:remove(host:sub(host:find(".", 1, true)+1), host);
113 log("debug", "component removed: "..host);
116 log("error", "Attempt to remove component for non-existing host: "..host);
120 function set_component_handler(host, handler)
121 components[host] = handler;