Merge 0.9->0.10
[prosody.git] / plugins / mod_c2s.lua
index 8a652a3394ac8c1fa90efa30be3c1a8b9160396b..7a8af406acdd6b3fe2a5710bea28338365c08d36 100644 (file)
@@ -38,7 +38,6 @@ local runner_callbacks = {};
 
 --- Stream events handlers
 local stream_xmlns_attr = {xmlns='urn:ietf:params:xml:ns:xmpp-streams'};
-local default_stream_attr = { ["xmlns:stream"] = "http://etherx.jabber.org/streams", xmlns = stream_callbacks.default_ns, version = "1.0", id = "" };
 
 function stream_callbacks.streamopened(session, attr)
        local send = session.send;
@@ -58,9 +57,7 @@ function stream_callbacks.streamopened(session, attr)
                return;
        end
 
-       send("<?xml version='1.0'?>"..st.stanza("stream:stream", {
-               xmlns = 'jabber:client', ["xmlns:stream"] = 'http://etherx.jabber.org/streams';
-               id = session.streamid, from = session.host, version = '1.0', ["xml:lang"] = 'en' }):top_tag());
+       session:open_stream();
 
        (session.log or log)("debug", "Sent reply <stream:stream> to client");
        session.notopen = nil;
@@ -69,12 +66,12 @@ function stream_callbacks.streamopened(session, attr)
        -- since we now have a new stream header, session is secured
        if session.secure == false then
                session.secure = true;
+               session.encrypted = true;
 
                local sock = session.conn:socket();
                if sock.info then
                        local info = sock:info();
-                       (session.log or log)("info", "Stream encrypted (%s) with %s, authenticated with %s and exchanged keys with %s",
-                               info.protocol, info.encryption, info.authentication, info.key);
+                       (session.log or log)("info", "Stream encrypted (%s with %s)", info.protocol, info.cipher);
                        session.compressed = info.compression;
                else
                        (session.log or log)("info", "Stream encrypted");
@@ -129,8 +126,7 @@ local function session_close(session, reason)
        local log = session.log or log;
        if session.conn then
                if session.notopen then
-                       session.send("<?xml version='1.0'?>");
-                       session.send(st.stanza("stream:stream", default_stream_attr):top_tag());
+                       session:open_stream();
                end
                if reason then -- nil == no err, initiated by us, false == initiated by client
                        local stream_error = st.stanza("stream:error");
@@ -158,7 +154,7 @@ local function session_close(session, reason)
                function session.send() return false; end
 
                local reason = (reason and (reason.name or reason.text or reason.condition)) or reason;
-               session.log("info", "c2s stream for %s closed: %s", session.full_jid or ("<"..session.ip..">"), reason or "session closed");
+               session.log("debug", "c2s stream for %s closed: %s", session.full_jid or ("<"..session.ip..">"), reason or "session closed");
 
                -- Authenticated incoming stream may still be sending us stanzas, so wait for </stream:stream> from remote
                local conn = session.conn;
@@ -178,6 +174,19 @@ local function session_close(session, reason)
        end
 end
 
+local function session_open_stream(session)
+       local attr = {
+               ["xmlns:stream"] = 'http://etherx.jabber.org/streams',
+               xmlns = stream_callbacks.default_ns,
+               version = "1.0",
+               ["xml:lang"] = 'en',
+               id = session.streamid or "",
+               from = session.host
+       };
+       session.send("<?xml version='1.0'?>");
+       session.send(st.stanza("stream:stream", attr):top_tag());
+end
+
 module:hook_global("user-deleted", function(event)
        local username, host = event.username, event.host;
        local user = hosts[host].sessions[username];
@@ -210,6 +219,7 @@ function listener.onconnect(conn)
        -- Client is using legacy SSL (otherwise mod_tls sets this flag)
        if conn:ssl() then
                session.secure = true;
+               session.encrypted = true;
 
                -- Check if TLS compression is used
                local sock = conn:socket();
@@ -224,6 +234,7 @@ function listener.onconnect(conn)
                conn:setoption("keepalive", opt_keepalives);
        end
 
+       session.open_stream = session_open_stream;
        session.close = session_close;
 
        local stream = new_xmpp_stream(session, stream_callbacks);