2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 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 = _G.prosody;
10 local log = require "util.logger".init("componentmanager");
11 local certmanager = require "core.certmanager";
12 local configmanager = require "core.configmanager";
13 local modulemanager = require "core.modulemanager";
14 local jid_split = require "util.jid".split;
15 local fire_event = prosody.events.fire_event;
16 local events_new = require "util.events".new;
17 local st = require "util.stanza";
18 local prosody, hosts = prosody, prosody.hosts;
20 local uuid_gen = require "util.uuid".generate;
22 local pairs, setmetatable, type, tostring = pairs, setmetatable, type, tostring;
24 local components = {};
26 local disco_items = require "util.multitable".new();
29 module "componentmanager"
31 local function default_component_handler(origin, stanza)
32 log("warn", "Stanza being handled by default component; bouncing error for: %s", stanza:top_tag());
33 if stanza.attr.type ~= "error" and stanza.attr.type ~= "result" then
34 origin.send(st.error_reply(stanza, "wait", "service-unavailable", "Component unavailable"));
38 function load_enabled_components(config)
39 local defined_hosts = config or configmanager.getconfig();
41 for host, host_config in pairs(defined_hosts) do
42 if host ~= "*" and ((host_config.core.enabled == nil or host_config.core.enabled) and type(host_config.core.component_module) == "string") then
43 hosts[host] = create_component(host);
44 hosts[host].connected = false;
45 components[host] = default_component_handler;
46 local ok, err = modulemanager.load(host, host_config.core.component_module);
48 log("error", "Error loading %s component %s: %s", tostring(host_config.core.component_module), tostring(host), tostring(err));
50 fire_event("component-activated", host, host_config);
51 log("debug", "Activated %s component: %s", host_config.core.component_module, host);
57 if prosody and prosody.events then
58 prosody.events.add_handler("server-starting", load_enabled_components);
61 function handle_stanza(origin, stanza)
62 local node, host = jid_split(stanza.attr.to);
63 local component = nil;
65 if node then component = components[node.."@"..host]; end -- hack to allow hooking node@server
66 if not component then component = components[host]; end
69 log("debug", "%s stanza being handled by component: %s", stanza.name, host);
70 component(origin, stanza, hosts[host]);
72 log("error", "Component manager recieved a stanza for a non-existing component: "..tostring(stanza));
73 default_component_handler(origin, stanza);
77 function create_component(host, component, events)
78 -- TODO check for host well-formedness
79 local ssl_ctx, ssl_ctx_in;
81 -- We need to find SSL context to use...
82 -- Discussion in prosody@ concluded that
83 -- 1 level back is usually enough by default
84 local base_host = host:gsub("^[^%.]+%.", "");
85 if hosts[base_host] then
86 ssl_ctx = hosts[base_host].ssl_ctx;
87 ssl_ctx_in = hosts[base_host].ssl_ctx_in;
89 -- We have no cert, and no parent host to borrow a cert from
90 -- Use global/default cert if there is one
91 ssl_ctx = certmanager.create_context(host, "client");
92 ssl_ctx_in = certmanager.create_context(host, "server");
95 return { type = "component", host = host, connected = true, s2sout = {},
96 ssl_ctx = ssl_ctx, ssl_ctx_in = ssl_ctx_in, events = events or events_new(),
97 dialback_secret = configmanager.get(host, "core", "dialback_secret") or uuid_gen() };
100 function register_component(host, component, session)
101 if not hosts[host] or (hosts[host].type == 'component' and not hosts[host].connected) then
102 local old_events = hosts[host] and hosts[host].events;
104 components[host] = component;
105 hosts[host] = session or create_component(host, component, old_events);
107 -- Add events object if not already one
108 if not hosts[host].events then
109 hosts[host].events = old_events or events_new();
112 if not hosts[host].dialback_secret then
113 hosts[host].dialback_secret = configmanager.get(host, "core", "dialback_secret") or uuid_gen();
116 -- add to disco_items
117 if not(host:find("@", 1, true) or host:find("/", 1, true)) and host:find(".", 1, true) then
118 disco_items:set(host:sub(host:find(".", 1, true)+1), host, true);
120 modulemanager.load(host, "dialback");
121 modulemanager.load(host, "tls");
122 log("debug", "component added: "..host);
123 return session or hosts[host];
125 log("error", "Attempt to set component for existing host: "..host);
129 function deregister_component(host)
130 if components[host] then
131 modulemanager.unload(host, "tls");
132 modulemanager.unload(host, "dialback");
133 hosts[host].connected = nil;
134 local host_config = configmanager.getconfig()[host];
135 if host_config and ((host_config.core.enabled == nil or host_config.core.enabled) and type(host_config.core.component_module) == "string") then
136 -- Set default handler
137 components[host] = default_component_handler;
139 -- Component not in config, or disabled, remove
140 hosts[host] = nil; -- FIXME do proper unload of all modules and other cleanup before removing
141 components[host] = nil;
143 -- remove from disco_items
144 if not(host:find("@", 1, true) or host:find("/", 1, true)) and host:find(".", 1, true) then
145 disco_items:remove(host:sub(host:find(".", 1, true)+1), host);
147 log("debug", "component removed: "..host);
150 log("error", "Attempt to remove component for non-existing host: "..host);
154 function set_component_handler(host, handler)
155 components[host] = handler;
158 function get_children(host)
159 return disco_items:get(host) or NULL;