MUC: Changed a MUC library method into a function.
[prosody.git] / core / s2smanager.lua
index 63605d5ec8601519fd3c9e8eee4e57191d069f2f..ab2e4a5c83d2bd27d09eccbf63bce22d64a9c4d6 100644 (file)
@@ -1,4 +1,4 @@
--- Prosody IM v0.4
+-- Prosody IM
 -- Copyright (C) 2008-2009 Matthew Wild
 -- Copyright (C) 2008-2009 Waqas Hussain
 -- 
@@ -126,6 +126,7 @@ function new_incoming(conn)
        end
        open_sessions = open_sessions + 1;
        local w, log = conn.write, logger_init("s2sin"..tostring(conn):match("[a-f0-9]+$"));
+       session.log = log;
        session.sends2s = function (t) log("debug", "sending: %s", tostring(t)); w(tostring(t)); end
        incoming_s2s[session] = true;
        return session;
@@ -142,6 +143,7 @@ function new_outgoing(from_host, to_host)
                        host_session.log = log;
                end
                
+               -- This is the first call, can't fail (the first step is DNS lookup)
                attempt_connection(host_session);
                
                if not host_session.sends2s then                
@@ -195,7 +197,13 @@ function attempt_connection(host_session, err)
                                log("debug", to_host.." has no SRV records, falling back to A");
                        end
                        -- Try with SRV, or just the plain hostname if no SRV
-                       return try_connect(host_session, connect_host, connect_port);
+                       local ok, err = try_connect(host_session, connect_host, connect_port);
+                       if not ok then
+                               if not attempt_connection(host_session, err) then
+                                       -- No more attempts will be made
+                                       destroy_session(host_session);
+                               end
+                       end
                end, "_xmpp-server._tcp."..connect_host..".", "SRV");
                
                -- Set handler for DNS timeout
@@ -238,8 +246,8 @@ function try_connect(host_session, connect_host, connect_port)
        conn:settimeout(0);
        local success, err = conn:connect(connect_host, connect_port);
        if not success and err ~= "timeout" then
-               log("warn", "s2s connect() failed: %s", err);
-               return false;
+               log("warn", "s2s connect() to %s (%s:%d) failed: %s", host_session.to_host, connect_host, connect_port, err);
+               return false, err;
        end
        
        local cl = connlisteners_get("xmppserver");