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.
9 local prosody = prosody;
10 local log = require "util.logger".init("componentmanager");
11 local configmanager = require "core.configmanager";
12 local modulemanager = require "core.modulemanager";
13 local jid_split = require "util.jid".split;
14 local events_new = require "util.events".new;
15 local st = require "util.stanza";
18 local pairs, type, tostring = pairs, type, tostring;
20 local components = {};
22 local disco_items = require "util.multitable".new();
25 module "componentmanager"
27 local function default_component_handler(origin, stanza)
28 log("warn", "Stanza being handled by default component, bouncing error");
29 if stanza.attr.type ~= "error" and stanza.attr.type ~= "result" then
30 origin.send(st.error_reply(stanza, "wait", "service-unavailable", "Component unavailable"));
34 function load_enabled_components(config)
35 local defined_hosts = config or configmanager.getconfig();
37 for host, host_config in pairs(defined_hosts) do
38 if host ~= "*" and ((host_config.core.enabled == nil or host_config.core.enabled) and type(host_config.core.component_module) == "string") then
39 hosts[host] = { type = "component", host = host, connected = false, s2sout = {}, events = events_new() };
40 components[host] = default_component_handler;
41 local ok, err = modulemanager.load(host, host_config.core.component_module);
43 log("error", "Error loading %s component %s: %s", tostring(host_config.core.component_module), tostring(host), tostring(err));
45 log("debug", "Activated %s component: %s", host_config.core.component_module, host);
51 prosody.events.add_handler("server-starting", load_enabled_components);
53 function handle_stanza(origin, stanza)
54 local node, host = jid_split(stanza.attr.to);
55 local component = nil;
57 if node then component = components[node.."@"..host]; end -- hack to allow hooking node@server
58 if not component then component = components[host]; end
61 log("debug", "%s stanza being handled by component: %s", stanza.name, host);
62 component(origin, stanza, hosts[host]);
64 log("error", "Component manager recieved a stanza for a non-existing component: "..tostring(stanza));
68 function create_component(host, component)
69 -- TODO check for host well-formedness
70 return { type = "component", host = host, connected = true, s2sout = {}, events = events_new() };
73 function register_component(host, component, session)
74 if not hosts[host] or (hosts[host].type == 'component' and not hosts[host].connected) then
75 local old_events = hosts[host] and hosts[host].events;
77 components[host] = component;
78 hosts[host] = session or create_component(host, component);
80 -- Add events object if not already one
81 if not hosts[host].events then
82 hosts[host].events = old_events or events_new();
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 hosts[host].connected = nil;
102 local host_config = configmanager.getconfig()[host];
103 if host_config and ((host_config.core.enabled == nil or host_config.core.enabled) and type(host_config.core.component_module) == "string") then
104 -- Set default handler
105 components[host] = default_component_handler;
107 -- Component not in config, or disabled, remove
109 components[host] = nil;
111 -- remove from disco_items
112 if not(host:find("@", 1, true) or host:find("/", 1, true)) and host:find(".", 1, true) then
113 disco_items:remove(host:sub(host:find(".", 1, true)+1), host);
115 log("debug", "component removed: "..host);
118 log("error", "Attempt to remove component for non-existing host: "..host);
122 function set_component_handler(host, handler)
123 components[host] = handler;
126 function get_children(host)
127 return disco_items:get(host) or NULL;