- if not component then component = components[stanza.attr.to]; end -- hack to allow hooking node@server/resource and server/resource
- if not component then component = components[node.."@"..host]; end -- hack to allow hooking node@server\r
- if not component then component = components[host]; end
- if component then\r
- log("debug", "stanza being handled by component: "..host);\r
- component(origin, stanza, hosts[host]);\r
- else\r
- log("error", "Component manager recieved a stanza for a non-existing component: " .. stanza.attr.to);\r
- end\r
-end\r
-\r
-function register_component(host, component)\r
- if not hosts[host] or (hosts[host].type == 'component' and not hosts[host].connected) then\r
- -- TODO check for host well-formedness\r
- components[host] = component;\r
- hosts[host] = { type = "component", host = host, connected = true, s2sout = {} };
- -- FIXME only load for a.b.c if b.c has dialback, and/or check in config
- modulemanager.load(host, "dialback");\r
- log("debug", "component added: "..host);\r
- return hosts[host];\r
- else\r
- log("error", "Attempt to set component for existing host: "..host);\r
- end\r
-end\r
+ if host then
+ if node then component = components[node.."@"..host]; end -- hack to allow hooking node@server
+ if not component then component = components[host]; end
+ end
+ if component then
+ log("debug", "%s stanza being handled by component: %s", stanza.name, host);
+ component(origin, stanza, hosts[host]);
+ else
+ log("error", "Component manager recieved a stanza for a non-existing component: "..tostring(stanza));
+ default_component_handler(origin, stanza);
+ end
+end
+
+function create_component(host, component, events)
+ -- TODO check for host well-formedness
+ local ssl_ctx, ssl_ctx_in;
+ if host and ssl then
+ -- We need to find SSL context to use...
+ -- Discussion in prosody@ concluded that
+ -- 1 level back is usually enough by default
+ local base_host = host:gsub("^[^%.]+%.", "");
+ if hosts[base_host] then
+ ssl_ctx = hosts[base_host].ssl_ctx;
+ ssl_ctx_in = hosts[base_host].ssl_ctx_in;
+ else
+ -- We have no cert, and no parent host to borrow a cert from
+ -- Use global/default cert if there is one
+ ssl_ctx = certmanager.create_context(host, "client");
+ ssl_ctx_in = certmanager.create_context(host, "server");
+ end
+ end
+ return { type = "component", host = host, connected = true, s2sout = {},
+ ssl_ctx = ssl_ctx, ssl_ctx_in = ssl_ctx_in, events = events or events_new(),
+ dialback_secret = configmanager.get(host, "core", "dialback_secret") or uuid_gen() };
+end
+
+function register_component(host, component, session)
+ if not hosts[host] or (hosts[host].type == 'component' and not hosts[host].connected) then
+ local old_events = hosts[host] and hosts[host].events;
+
+ components[host] = component;
+ hosts[host] = session or create_component(host, component, old_events);
+
+ -- Add events object if not already one
+ if not hosts[host].events then
+ hosts[host].events = old_events or events_new();
+ end
+
+ if not hosts[host].dialback_secret then
+ hosts[host].dialback_secret = configmanager.get(host, "core", "dialback_secret") or uuid_gen();
+ end
+
+ -- add to disco_items
+ if not(host:find("@", 1, true) or host:find("/", 1, true)) and host:find(".", 1, true) then
+ disco_items:set(host:sub(host:find(".", 1, true)+1), host, true);
+ end
+ modulemanager.load(host, "dialback");
+ modulemanager.load(host, "tls");
+ log("debug", "component added: "..host);
+ return session or hosts[host];
+ else
+ log("error", "Attempt to set component for existing host: "..host);
+ end
+end