sessionmanager: Make session.send() return true unless there really is an error ...
[prosody.git] / plugins / mod_component.lua
index 16084a78a69b08f8bf79221f67bfdaec85fba69b..11abab799e3b2be35979ad4ba3ccc87e287ca22f 100644 (file)
@@ -9,6 +9,8 @@
 module:set_global();
 
 local t_concat = table.concat;
+local xpcall, tostring, type = xpcall, tostring, type;
+local traceback = debug.traceback;
 
 local logger = require "util.logger";
 local sha1 = require "util.hashes".sha1;
@@ -19,10 +21,12 @@ local new_xmpp_stream = require "util.xmppstream".new;
 local uuid_gen = require "util.uuid".generate;
 
 local core_process_stanza = prosody.core_process_stanza;
-
+local hosts = prosody.hosts;
 
 local log = module._log;
 
+local opt_keepalives = module:get_option_boolean("component_tcp_keepalives", module:get_option_boolean("tcp_keepalives", true));
+
 local sessions = module:shared("sessions");
 
 function module.add_host(module)
@@ -81,10 +85,11 @@ function module.add_host(module)
                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
-       module:hook("stanza/jabber:component:accept:handshake", handle_component_auth);
+       module:hook("stanza/jabber:component:accept:handshake", handle_component_auth, -1);
 
        -- Handle stanzas addressed to this component
        local function handle_stanza(event)
@@ -183,6 +188,7 @@ function stream_callbacks.streamclosed(session)
        session:close();
 end
 
+local function handleerr(err) log("error", "Traceback[component]: %s", traceback(tostring(err), 2)); end
 function stream_callbacks.handlestanza(session, stanza)
        -- Namespaces are icky.
        if not stanza.attr.xmlns and stanza.name == "handshake" then
@@ -213,7 +219,10 @@ function stream_callbacks.handlestanza(session, stanza)
                        return;
                end
        end
-       return core_process_stanza(session, stanza);
+
+       if stanza then
+               return xpcall(function () return core_process_stanza(session, stanza) end, handleerr);
+       end
 end
 
 --- Closing a component connection
@@ -260,9 +269,13 @@ function listener.onconnect(conn)
        local session = { type = "component_unauthed", conn = conn, send = function (data) return _send(conn, tostring(data)); end };
 
        -- Logging functions --
-       local conn_name = "jcp"..tostring(conn):match("[a-f0-9]+$");
+       local conn_name = "jcp"..tostring(session):match("[a-f0-9]+$");
        session.log = logger.init(conn_name);
        session.close = session_close;
+
+       if opt_keepalives then
+               conn:setoption("keepalive", opt_keepalives);
+       end
        
        session.log("info", "Incoming Jabber component connection");
        
@@ -307,12 +320,16 @@ function listener.ondisconnect(conn, err)
        end
 end
 
+function listener.ondetach(conn)
+       sessions[conn] = nil;
+end
+
 module:provides("net", {
        name = "component";
        private = true;
        listener = listener;
        default_port = 5347;
        multiplex = {
-               pattern = "^<.*:stream.*%sxmlns%s*=%s*(['\"])jabber:component%1.*>";
+               pattern = "^<.*:stream.*%sxmlns%s*=%s*(['\"])jabber:component:accept%1.*>";
        };
 });