Forced merge.
[prosody.git] / core / stanza_router.lua
index 9aa923eb1b0f59f01199971f5d126cdb496be848..308ae2f4be0f6350c92c00b0c0befe285adb7f71 100644 (file)
@@ -1,10 +1,4 @@
 
--- The code in this file should be self-explanatory, though the logic is horrible
--- for more info on that, see doc/stanza_routing.txt, which attempts to condense
--- the rules from the RFCs (mainly 3921)
-
-require "core.servermanager"
-
 local log = require "util.logger".init("stanzarouter")
 
 local st = require "util.stanza";
@@ -35,7 +29,7 @@ local jid_split = require "util.jid".split;
 local print = print;
 
 function core_process_stanza(origin, stanza)
-       log("debug", "Received[%s]: %s", origin.type, stanza:pretty_top_tag())
+       (origin.log or log)("debug", "Received[%s]: %s", origin.type, stanza:pretty_print()) --top_tag())
 
        if not stanza.attr.xmlns then stanza.attr.xmlns = "jabber:client"; end -- FIXME Hack. This should be removed when we fix namespace handling.
        -- TODO verify validity of stanza (as well as JID validity)
@@ -82,7 +76,7 @@ function core_process_stanza(origin, stanza)
                elseif hosts[to] and hosts[to].type == "local" then -- directed at a local server
                        core_handle_stanza(origin, stanza);
                elseif stanza.attr.xmlns and stanza.attr.xmlns ~= "jabber:client" and stanza.attr.xmlns ~= "jabber:server" then
-                       modules_handle_stanza(origin, stanza);
+                       modules_handle_stanza(host or origin.host or origin.to_host, origin, stanza);
                elseif hosts[to_bare] and hosts[to_bare].type == "component" then -- hack to allow components to handle node@server
                        component_handle_stanza(origin, stanza);
                elseif hosts[to] and hosts[to].type == "component" then -- hack to allow components to handle node@server/resource and server/resource
@@ -105,7 +99,7 @@ end
 -- that is, they are handled by this server
 function core_handle_stanza(origin, stanza)
        -- Handlers
-       if modules_handle_stanza(origin, stanza) then return; end
+       if modules_handle_stanza(stanza.attr.to or origin.host, origin, stanza) then return; end
        if origin.type == "c2s" or origin.type == "c2s_unauthed" then
                local session = origin;
 
@@ -177,21 +171,21 @@ function core_handle_stanza(origin, stanza)
                                stanza.attr.to = nil; -- reset it
                        else
                                log("warn", "Unhandled c2s presence: %s", tostring(stanza));
-                               if stanza.attr.type ~= "error" then
+                               if (stanza.attr.xmlns == "jabber:client" or stanza.attr.xmlns == "jabber:server") and stanza.attr.type ~= "error" then
                                        origin.send(st.error_reply(stanza, "cancel", "service-unavailable")); -- FIXME correct error?
                                end
                        end
                else
                        log("warn", "Unhandled c2s stanza: %s", tostring(stanza));
-                       if stanza.attr.type ~= "error" and stanza.attr.type ~= "result" then
+                       if (stanza.attr.xmlns == "jabber:client" or stanza.attr.xmlns == "jabber:server") and stanza.attr.type ~= "error" and stanza.attr.type ~= "result" then
                                origin.send(st.error_reply(stanza, "cancel", "service-unavailable")); -- FIXME correct error?
                        end
                end -- TODO handle other stanzas
        else
                log("warn", "Unhandled origin: %s", origin.type);
-               if stanza.attr.type ~= "error" and stanza.attr.type ~= "result" then
+               if (stanza.attr.xmlns == "jabber:client" or stanza.attr.xmlns == "jabber:server") and stanza.attr.type ~= "error" and stanza.attr.type ~= "result" then
                        -- s2s stanzas can get here
-                       (origin.sends2s or origin.send)(st.error_reply(stanza, "cancel", "service-unavailable")); -- FIXME correct error?
+                       origin.send(st.error_reply(stanza, "cancel", "service-unavailable")); -- FIXME correct error?
                end
        end
 end
@@ -208,6 +202,10 @@ function core_route_stanza(origin, stanza)
        local from_node, from_host, from_resource = jid_split(from);
        local from_bare = from_node and (from_node.."@"..from_host) or from_host; -- bare JID
 
+       -- Auto-detect origin if not specified
+       origin = origin or hosts[from_host];
+       if not origin then return false; end
+       
        if stanza.name == "presence" and (stanza.attr.type ~= nil and stanza.attr.type ~= "unavailable") then resource = nil; end
 
        local host_session = hosts[host]
@@ -300,8 +298,6 @@ function core_route_stanza(origin, stanza)
        elseif origin.type == "component" or origin.type == "local" then
                -- Route via s2s for components and modules
                log("debug", "Routing outgoing stanza for %s to %s", origin.host, host);
-               for k,v in pairs(origin) do print("origin:", tostring(k), tostring(v)); end
-               print(tostring(host), tostring(from_host))
                send_s2s(origin.host, host, stanza);
        else
                log("warn", "received stanza from unhandled connection type: %s", origin.type);