0.2->0.3
[prosody.git] / core / stanza_router.lua
index fb5b2cbf069f40f9a1af02ed9fb0e67397105dae..844cbf881a9c5d53eada5d5f6f99c00145ec65fa 100644 (file)
@@ -1,20 +1,9 @@
--- Prosody IM v0.2
+-- Prosody IM v0.3
 -- Copyright (C) 2008 Matthew Wild
 -- Copyright (C) 2008 Waqas Hussain
 -- 
--- This program is free software; you can redistribute it and/or
--- modify it under the terms of the GNU General Public License
--- as published by the Free Software Foundation; either version 2
--- of the License, or (at your option) any later version.
--- 
--- This program is distributed in the hope that it will be useful,
--- but WITHOUT ANY WARRANTY; without even the implied warranty of
--- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
--- GNU General Public License for more details.
--- 
--- You should have received a copy of the GNU General Public License
--- along with this program; if not, write to the Free Software
--- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+-- This project is MIT/X11 licensed. Please see the
+-- COPYING file in the source package for more information.
 --
 
 
@@ -47,10 +36,16 @@ local tonumber = tonumber;
 local s_find = string.find;
 
 local jid_split = require "util.jid".split;
+local jid_prepped_split = require "util.jid".prepped_split;
 local print = print;
+local function checked_error_reply(origin, stanza)
+       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
 
 function core_process_stanza(origin, stanza)
-       (origin.log or log)("debug", "Received[%s]: %s", origin.type, stanza:pretty_print()) --top_tag())
+       (origin.log or log)("debug", "Received[%s]: %s", origin.type, stanza: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)
@@ -73,11 +68,28 @@ function core_process_stanza(origin, stanza)
                stanza.attr.from = origin.full_jid;
        end
        local to, xmlns = stanza.attr.to, stanza.attr.xmlns;
-       local node, host, resource = jid_split(to);
-       local to_bare = node and (node.."@"..host) or host; -- bare JID
        local from = stanza.attr.from;
-       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
+       local node, host, resource;
+       local from_node, from_host, from_resource;
+       local to_bare, from_bare;
+       if to then
+               node, host, resource = jid_prepped_split(to);
+               if not host then
+                       error("Invalid to JID");
+               end
+               to_bare = node and (node.."@"..host) or host; -- bare JID
+               if resource then to = to_bare.."/"..resource; else to = to_bare; end
+               stanza.attr.to = to;
+       end
+       if from then
+               from_node, from_host, from_resource = jid_prepped_split(from);
+               if not from_host then
+                       error("Invalid from JID");
+               end
+               from_bare = from_node and (from_node.."@"..from_host) or from_host; -- bare JID
+               if from_resource then from = from_bare.."/"..from_resource; else from = from_bare; end
+               stanza.attr.from = from;
+       end
 
        --[[if to and not(hosts[to]) and not(hosts[to_bare]) and (hosts[host] and hosts[host].type ~= "local") then -- not for us?
                log("warn", "stanza recieved for a non-local server");
@@ -125,7 +137,7 @@ end
 -- that is, they are handled by this server
 function core_handle_stanza(origin, stanza)
        -- Handlers
-       if modules_handle_stanza(stanza.attr.to or origin.host, origin, stanza) then return; end
+       if modules_handle_stanza(select(2, jid_split(stanza.attr.to)) or origin.host, origin, stanza) then return; end
        if origin.type == "c2s" or origin.type == "s2sin" then
                if origin.type == "c2s" then
                        if stanza.name == "presence" and origin.roster then
@@ -133,24 +145,19 @@ function core_handle_stanza(origin, stanza)
                                        handle_normal_presence(origin, stanza, core_route_stanza);
                                else
                                        log("warn", "Unhandled c2s presence: %s", tostring(stanza));
-                                       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
+                                       checked_error_reply(origin, stanza);
                                end
                        else
                                log("warn", "Unhandled c2s stanza: %s", tostring(stanza));
-                               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
+                               checked_error_reply(origin, stanza);
                        end
                else -- s2s stanzas
                        log("warn", "Unhandled s2s stanza: %s", tostring(stanza));
-                       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
+                       checked_error_reply(origin, stanza);
                end
        else
                log("warn", "Unhandled %s stanza: %s", origin.type, tostring(stanza));
+               checked_error_reply(origin, stanza);
        end
 end