prosody, mod_c2s, mod_s2s: Move closing of c2s and s2s sessions to respective plugins
[prosody.git] / plugins / mod_c2s.lua
index 55c53e2de905a491dce7b4c80af60c155cf2c141..89d678caedc62ef446ab0163f353a7cd5345c42e 100644 (file)
@@ -24,9 +24,11 @@ local xmlns_xmpp_streams = "urn:ietf:params:xml:ns:xmpp-streams";
 local log = module._log;
 
 local c2s_timeout = module:get_option_number("c2s_timeout");
+local stream_close_timeout = module:get_option_number("c2s_close_timeout", 5);
 local opt_keepalives = module:get_option_boolean("tcp_keepalives", false);
 
 local sessions = module:shared("sessions");
+local core_process_stanza = prosody.core_process_stanza;
 
 local stream_callbacks = { default_ns = "jabber:client", handlestanza = core_process_stanza };
 local listener = {};
@@ -64,6 +66,14 @@ 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;
+
+               -- Check if TLS compression is used
+               local sock = session.conn:socket();
+               if sock.info then
+                       session.compressed = sock:info"compression";
+               elseif sock.compression then
+                       session.compressed = sock:compression(); --COMPAT mw/luasec-hg
+               end
        end
 
        local features = st.stanza("stream:features");
@@ -75,7 +85,7 @@ end
 
 function stream_callbacks.streamclosed(session)
        session.log("debug", "Received </stream:stream>");
-       session:close();
+       session:close(false);
 end
 
 function stream_callbacks.error(session, error, data)
@@ -121,9 +131,9 @@ local function session_close(session, reason)
                        session.send("<?xml version='1.0'?>");
                        session.send(st.stanza("stream:stream", default_stream_attr):top_tag());
                end
-               if reason then
+               if reason then -- nil == no err, initiated by us, false == initiated by client
                        if type(reason) == "string" then -- assume stream error
-                               log("info", "Disconnecting client, <stream:error> is: %s", reason);
+                               log("debug", "Disconnecting client, <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
@@ -134,20 +144,49 @@ local function session_close(session, reason)
                                        if reason.extra then
                                                stanza:add_child(reason.extra);
                                        end
-                                       log("info", "Disconnecting client, <stream:error> is: %s", tostring(stanza));
+                                       log("debug", "Disconnecting client, <stream:error> is: %s", tostring(stanza));
                                        session.send(stanza);
                                elseif reason.name then -- a stanza
-                                       log("info", "Disconnecting client, <stream:error> is: %s", tostring(reason));
+                                       log("debug", "Disconnecting client, <stream:error> is: %s", tostring(reason));
                                        session.send(reason);
                                end
                        end
                end
+               
                session.send("</stream:stream>");
-               session.conn:close();
-               listener.ondisconnect(session.conn, (reason and (reason.text or reason.condition)) or reason or "session closed");
+               function session.send() return false; end
+               
+               local reason = (reason and (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");
+
+               -- Authenticated incoming stream may still be sending us stanzas, so wait for </stream:stream> from remote
+               local conn = session.conn;
+               if reason == nil and not session.notopen and session.type == "c2s" then
+                       -- Grace time to process data from authenticated cleanly-closed stream
+                       add_task(stream_close_timeout, function ()
+                               if not session.destroyed then
+                                       session.log("warn", "Failed to receive a stream close response, closing connection anyway...");
+                                       sm_destroy_session(session, reason);
+                                       conn:close();
+                               end
+                       end);
+               else
+                       sm_destroy_session(session, reason);
+                       conn:close();
+               end
        end
 end
 
+module:hook_global("user-deleted", function(event)
+       local username, host = event.username, event.host;
+       local user = hosts[host].sessions[username];
+       if user and user.sessions then
+               for jid, session in pairs(user.sessions) do
+                       session:close{ condition = "not-authorized", text = "Account deleted" };
+               end
+       end
+end, 200);
+
 --- Port listener
 function listener.onconnect(conn)
        local session = sm_new_session(conn);
@@ -158,6 +197,14 @@ function listener.onconnect(conn)
        -- Client is using legacy SSL (otherwise mod_tls sets this flag)
        if conn:ssl() then
                session.secure = true;
+
+               -- Check if TLS compression is used
+               local sock = conn:socket();
+               if sock.info then
+                       session.compressed = sock:info"compression";
+               elseif sock.compression then
+                       session.compressed = sock:compression(); --COMPAT mw/luasec-hg
+               end
        end
        
        if opt_keepalives then
@@ -208,10 +255,9 @@ end
 function listener.ondisconnect(conn, err)
        local session = sessions[conn];
        if session then
-               (session.log or log)("info", "Client disconnected: %s", err);
+               (session.log or log)("info", "Client disconnected: %s", err or "connection closed");
                sm_destroy_session(session, err);
                sessions[conn]  = nil;
-               session = nil;
        end
 end
 
@@ -219,7 +265,16 @@ function listener.associate_session(conn, session)
        sessions[conn] = session;
 end
 
-module:add_item("net-provider", {
+module:hook("server-stopping", function(event)
+       local reason = event.reason;
+       for _, session in pairs(sessions) do
+               session:close{ condition = "system-shutdown", text = reason };
+       end
+end, 1000);
+
+
+
+module:provides("net", {
        name = "c2s";
        listener = listener;
        default_port = 5222;
@@ -229,7 +284,7 @@ module:add_item("net-provider", {
        };
 });
 
-module:add_item("net-provider", {
+module:provides("net", {
        name = "legacy_ssl";
        listener = listener;
        encryption = "ssl";