mod_s2s, mod_saslauth, mod_compression: Refactor to have common code for opening...
authorKim Alvefur <zash@zash.se>
Sat, 16 Mar 2013 16:46:43 +0000 (17:46 +0100)
committerKim Alvefur <zash@zash.se>
Sat, 16 Mar 2013 16:46:43 +0000 (17:46 +0100)
plugins/mod_compression.lua
plugins/mod_s2s/mod_s2s.lua
plugins/mod_s2s/s2sout.lib.lua
plugins/mod_saslauth.lua

index 67a88eb978ccb7467ec1ae11f17b007ff1f57d66..44bc05fe880ea7dad94508bd965577bad031b125 100644 (file)
@@ -141,10 +141,7 @@ module:hook("stanza/http://jabber.org/protocol/compress:compressed", function(ev
                -- setup decompression for session.data
                setup_decompression(session, inflate_stream);
                session:reset_stream();
-               local default_stream_attr = {xmlns = "jabber:server", ["xmlns:stream"] = "http://etherx.jabber.org/streams",
-                                                                       ["xmlns:db"] = 'jabber:server:dialback', version = "1.0", to = session.to_host, from = session.from_host};
-               session.sends2s("<?xml version='1.0'?>");
-               session.sends2s(st.stanza("stream:stream", default_stream_attr):top_tag());
+               session:open_stream();
                session.compressed = true;
                return true;
        end
index 6d4900fa4a6be702367bb069bec05a419ab074db..8d99b855d47f781459d0435cf480efa5a0282b1e 100644 (file)
@@ -248,10 +248,7 @@ function stream_callbacks.streamopened(session, attr)
 
                if session.secure and not session.cert_chain_status then check_cert_status(session); end
 
-               send("<?xml version='1.0'?>");
-               send(st.stanza("stream:stream", { xmlns='jabber:server',
-                               ["xmlns:db"]= hosts[to].modules.dialback and 'jabber:server:dialback' or nil,
-                               ["xmlns:stream"]='http://etherx.jabber.org/streams', id=session.streamid, from=to, to=from, version=(session.version > 0 and "1.0" or nil) }):top_tag());
+               session:open_stream()
                if session.version >= 1.0 then
                        local features = st.stanza("stream:features");
                        
@@ -348,8 +345,7 @@ local function session_close(session, reason, remote_reason)
        local log = session.log or log;
        if session.conn then
                if session.notopen then
-                       session.sends2s("<?xml version='1.0'?>");
-                       session.sends2s(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 remote
                        if type(reason) == "string" then -- assume stream error
@@ -396,6 +392,27 @@ local function session_close(session, reason, remote_reason)
        end
 end
 
+function session_open_stream(session, from, to)
+       local from = from or session.from_host;
+       local to = to or session.to_host;
+       local attr = {
+               ["xmlns:stream"] = 'http://etherx.jabber.org/streams',
+               xmlns = 'jabber:server',
+               version = session.version and (session.version > 0 and "1.0" or nil),
+               ["xml:lang"] = 'en',
+               id = session.streamid,
+               from = from, to = to,
+       }
+       local local_host = session.direction == "outgoing" and from or to;
+       if not local_host or hosts[local_host].modules.dialback then
+               attr["xmlns:db"] = 'jabber:server:dialback';
+       end
+
+       session.sends2s("<?xml version='1.0'?>");
+       session.sends2s(st.stanza("stream:stream", attr):top_tag());
+       return true;
+end
+
 -- Session initialization logic shared by incoming and outgoing
 local function initialize_session(session)
        local stream = new_xmpp_stream(session, stream_callbacks);
@@ -407,6 +424,8 @@ local function initialize_session(session)
                session.notopen = true;
                session.stream:reset();
        end
+
+       session.open_stream = session_open_stream;
        
        local filter = session.filter;
        function session.data(data)
index 076239680f41b7d6a69f6017780672d23d9417b0..5ebbee8e46ed30b3ddbf6b025171e0d71ae3507f 100644 (file)
@@ -44,15 +44,9 @@ local function compare_srv_priorities(a,b)
        return a.priority < b.priority or (a.priority == b.priority and a.weight > b.weight);
 end
 
-local function session_open_stream(session, from, to)
-       session.sends2s(st.stanza("stream:stream", {
-               xmlns='jabber:server', ["xmlns:db"]='jabber:server:dialback',
-               ["xmlns:stream"]='http://etherx.jabber.org/streams',
-               from=from, to=to, version='1.0', ["xml:lang"]='en'}):top_tag());
-end
-
 function s2sout.initiate_connection(host_session)
        initialize_filters(host_session);
+       host_session.version = 1;
        host_session.open_stream = session_open_stream;
        
        -- Kick the connection attempting machine into life
index f6abd3b88838f34371ba7524cab26b378e07a63b..264ee96755c232365849220adb0715c7e5dc4dcf 100644 (file)
@@ -88,11 +88,7 @@ module:hook_stanza(xmlns_sasl, "success", function (session, stanza)
        module:log("debug", "SASL EXTERNAL with %s succeeded", session.to_host);
        session.external_auth = "succeeded"
        session:reset_stream();
-
-       local default_stream_attr = {xmlns = "jabber:server", ["xmlns:stream"] = "http://etherx.jabber.org/streams",
-                                   ["xmlns:db"] = 'jabber:server:dialback', version = "1.0", to = session.to_host, from = session.from_host};
-       session.sends2s("<?xml version='1.0'?>");
-       session.sends2s(st.stanza("stream:stream", default_stream_attr):top_tag());
+       session:open_stream();
 
        s2s_make_authenticated(session, session.to_host);
        return true;