# HG changeset patch # User Matthew Wild # Date 1226985209 0 # Node ID 3be63719428e103445aa827e8c6606d82306adf2 # Parent a1d25769970fe4245e830656d94fc3eaf63509b3 Allow us to close client connections, with or without a stream error. Partially fixes #8, we still need the same for s2s (though it should be almost a straight copy of the code, I'm too tired atm) diff -r a1d25769970f -r 3be63719428e core/sessionmanager.lua --- a/core/sessionmanager.lua Tue Nov 18 05:06:50 2008 +0000 +++ b/core/sessionmanager.lua Tue Nov 18 05:13:29 2008 +0000 @@ -96,13 +96,23 @@ session.host = attr.to or error("Client failed to specify destination hostname"); session.version = tonumber(attr.version) or 0; session.streamid = m_random(1000000, 99999999); - print(session, session.host, "Client opened stream"); - send(""); + (session.log or session)("debug", "Client sent opening to %s", session.host); + + + send(""); send(format("", session.streamid, session.host)); + if not hosts[session.host] then + -- We don't serve this host... + session:disconnect{ condition = "host-unknown", text = "This server does not serve "..tostring(session.host)}; + return; + end + + local features = {}; modulemanager.fire_event("stream-features", session, features); + -- FIXME: Need to send() this all at once send(""); for _, feature in ipairs(features) do diff -r a1d25769970f -r 3be63719428e net/xmppclient_listener.lua --- a/net/xmppclient_listener.lua Tue Nov 18 05:06:50 2008 +0000 +++ b/net/xmppclient_listener.lua Tue Nov 18 05:13:29 2008 +0000 @@ -33,6 +33,39 @@ return true; end + +local stream_xmlns_attr = {xmlns='urn:ietf:params:xml:ns:xmpp-streams'}; +local function session_disconnect(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 client, 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 client, is: %s", tostring(stanza)); + session.send(stanza); + elseif reason.name then -- a stanza + log("info", "Disconnecting client, is: %s", tostring(reason)); + session.send(reason); + end + end + end + session.send(""); + session.conn.close(); + xmppclient.disconnect(session.conn, "stream error"); + end +end + + -- End of session methods -- function xmppclient.listener(conn, data) @@ -54,6 +87,7 @@ print("Client connected"); session.reset_stream = session_reset_stream; + session.disconnect = session_disconnect; session_reset_stream(session); -- Initialise, ready for use