Merge
[prosody.git] / plugins / mod_component.lua
index c5a1da81ce49e3f920eae0dab735d92977043c3e..a5136f6ca528f3628e32b065b81dcb09f871ebc7 100644 (file)
@@ -1,7 +1,7 @@
 -- Prosody IM
 -- Copyright (C) 2008-2010 Matthew Wild
 -- Copyright (C) 2008-2010 Waqas Hussain
--- 
+--
 -- This project is MIT/X11 licensed. Please see the
 -- COPYING file in the source package for more information.
 --
@@ -36,11 +36,13 @@ function module.add_host(module)
        
        local env = module.environment;
        env.connected = false;
+       env.session = false;
 
        local send;
 
        local function on_destroy(session, err)
                env.connected = false;
+               env.session = false;
                send = nil;
                session.on_destroy = nil;
        end
@@ -73,18 +75,25 @@ function module.add_host(module)
                end
                
                if env.connected then
-                       module:log("error", "Second component attempted to connect, denying connection");
-                       session:close{ condition = "conflict", text = "Component already connected" };
-                       return true;
+                       local policy = module:get_option_string("component_conflict_resolve", "kick_new");
+                       if policy == "kick_old" then
+                               env.session:close{ condition = "conflict", text = "Replaced by a new connection" };
+                       else -- kick_new
+                               module:log("error", "Second component attempted to connect, denying connection");
+                               session:close{ condition = "conflict", text = "Component already connected" };
+                               return true;
+                       end
                end
                
                env.connected = true;
+               env.session = session;
                send = session.send;
                session.on_destroy = on_destroy;
                session.component_validate_from = module:get_option_boolean("validate_from_addresses", true);
                session.type = "component";
                module:log("info", "External component successfully authenticated");
                session.send(st.stanza("handshake"));
+               module:fire_event("component-authenticated", { session = session });
        
                return true;
        end
@@ -177,9 +186,7 @@ function stream_callbacks.streamopened(session, attr)
        session.streamid = uuid_gen();
        session.notopen = nil;
        -- Return stream header
-       session.send("<?xml version='1.0'?>");
-       session.send(st.stanza("stream:stream", { xmlns=xmlns_component,
-                       ["xmlns:stream"]='http://etherx.jabber.org/streams', id=session.streamid, from=session.host }):top_tag());
+       session:open_stream();
 end
 
 function stream_callbacks.streamclosed(session)
@@ -319,6 +326,10 @@ function listener.ondisconnect(conn, err)
        end
 end
 
+function listener.ondetach(conn)
+       sessions[conn] = nil;
+end
+
 module:provides("net", {
        name = "component";
        private = true;