- if not hosts[session.host].connected then
- local send = session.send;
- session.component_session = cm_register_component(session.host, function (_, data) return send(data); end);
- hosts[session.host].connected = true;
- else
- log("error", "Multiple components bound to the same address, first one wins (TODO: Implement stanza distribution)");
- end
-
- -- Signal successful authentication
- session.send(st.stanza("handshake"));
-end
-
-module:add_handler("component", "handshake", xmlns_component, handle_component_auth);
-
---[[
--- Helper function to deal with errors processing component stanzas
-local function handleerr(err) log("error", "Traceback[component]: %s: %s", tostring(err), debug.traceback()); end
-function stream_callbacks.handlestanza(a, b)
- xpcall(function () core_process_stanza(a, b) end, handleerr);
-end
-]]
-
---- Closing a component connection
-local stream_xmlns_attr = {xmlns='urn:ietf:params:xml:ns:xmpp-streams'};
-local function session_close(session, reason)
- local log = session.log or log;
- if session.conn then
- if reason then
- if type(reason) == "string" then -- assume stream error
- log("info", "Disconnecting component, <stream:error> is: %s", reason);
- session.send(st.stanza("stream:error"):tag(reason, {xmlns = 'urn:ietf:params:xml:ns:xmpp-streams' }));
- elseif type(reason) == "table" then
- if reason.condition then
- local stanza = st.stanza("stream:error"):tag(reason.condition, stream_xmlns_attr):up();
- if reason.text then
- stanza:tag("text", stream_xmlns_attr):text(reason.text):up();
- end
- if reason.extra then
- stanza:add_child(reason.extra);
- end
- log("info", "Disconnecting component, <stream:error> is: %s", tostring(stanza));
- session.send(stanza);
- elseif reason.name then -- a stanza
- log("info", "Disconnecting component, <stream:error> is: %s", tostring(reason));
- session.send(reason);
- end
- end
- end
- session.send("</stream:stream>");
- session.conn.close();
- component_listener.disconnect(session.conn, "stream error");
+ if not main_session then
+ send = session.send;
+ main_session = session;
+ session.on_destroy = on_destroy;
+ session.component_validate_from = module:get_option_boolean("validate_from_addresses") ~= false;
+ log("info", "Component successfully authenticated: %s", session.host);
+ session.send(st.stanza("handshake"));
+ else -- TODO: Implement stanza distribution
+ log("error", "Multiple components bound to the same address, first one wins: %s", session.host);
+ session:close{ condition = "conflict", text = "Component already connected" };