2 -- Copyright (C) 2008-2009 Matthew Wild
3 -- Copyright (C) 2008-2009 Waqas Hussain
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
9 local log = require "util.logger".init("stanzarouter")
11 local hosts = _G.hosts;
12 local tostring = tostring;
13 local st = require "util.stanza";
14 local send_s2s = require "core.s2smanager".send_to_host;
15 local modules_handle_stanza = require "core.modulemanager".handle_stanza;
16 local component_handle_stanza = require "core.componentmanager".handle_stanza;
17 local jid_split = require "util.jid".split;
18 local jid_prepped_split = require "util.jid".prepped_split;
20 function core_process_stanza(origin, stanza)
21 (origin.log or log)("debug", "Received[%s]: %s", origin.type, stanza:top_tag())
23 -- Currently we guarantee every stanza to have an xmlns, should we keep this rule?
24 if not stanza.attr.xmlns then stanza.attr.xmlns = "jabber:client"; end
26 -- TODO verify validity of stanza (as well as JID validity)
27 if stanza.attr.type == "error" and #stanza.tags == 0 then return; end -- TODO invalid stanza, log
28 if stanza.name == "iq" then
29 local can_reply = stanza.attr.type == "set" or stanza.attr.type == "get"
30 local missing_id = not stanza.attr.id;
31 if can_reply and (#stanza.tags ~= 1 or missing_id) then
32 origin.send(st.error_reply(stanza, "modify", "bad-request"));
34 elseif missing_id then
39 if origin.type == "c2s" then
40 if not origin.full_jid
41 and not(stanza.name == "iq" and stanza.attr.type == "set" and stanza.tags[1] and stanza.tags[1].name == "bind"
42 and stanza.tags[1].attr.xmlns == "urn:ietf:params:xml:ns:xmpp-bind") then
43 -- authenticated client isn't bound and current stanza is not a bind request
44 origin.send(st.error_reply(stanza, "auth", "not-authorized")); -- FIXME maybe allow stanzas to account or server
48 -- TODO also, stanzas should be returned to their original state before the function ends
49 stanza.attr.from = origin.full_jid;
51 local to, xmlns = stanza.attr.to, stanza.attr.xmlns;
52 local from = stanza.attr.from;
53 local node, host, resource;
54 local from_node, from_host, from_resource;
55 local to_bare, from_bare;
57 if full_sessions[to] or bare_sessions[to] or hosts[to] then
58 node, host = jid_split(to); -- TODO only the host is needed, optimize
60 node, host, resource = jid_prepped_split(to);
62 log("warn", "Received stanza with invalid destination JID: %s", to);
63 if stanza.attr.type ~= "error" and stanza.attr.type ~= "result" then
64 origin.send(st.error_reply(stanza, "modify", "jid-malformed", "The destination address is invalid: "..to));
68 to_bare = node and (node.."@"..host) or host; -- bare JID
69 if resource then to = to_bare.."/"..resource; else to = to_bare; end
73 if from and not origin.full_jid then
74 -- We only stamp the 'from' on c2s stanzas, so we still need to check validity
75 from_node, from_host, from_resource = jid_prepped_split(from);
77 log("warn", "Received stanza with invalid source JID: %s", from);
78 if stanza.attr.type ~= "error" and stanza.attr.type ~= "result" then
79 origin.send(st.error_reply(stanza, "modify", "jid-malformed", "The source address is invalid: "..from));
83 from_bare = from_node and (from_node.."@"..from_host) or from_host; -- bare JID
84 if from_resource then from = from_bare.."/"..from_resource; else from = from_bare; end
85 stanza.attr.from = from;
88 --[[if to and not(hosts[to]) and not(hosts[to_bare]) and (hosts[host] and hosts[host].type ~= "local") then -- not for us?
89 log("warn", "stanza recieved for a non-local server");
90 return; -- FIXME what should we do here?
93 if (origin.type == "s2sin" or origin.type == "c2s" or origin.type == "component") and xmlns == "jabber:client" then
94 if origin.type == "s2sin" and not origin.dummy then
95 local host_status = origin.hosts[from_host];
96 if not host_status or not host_status.authed then -- remote server trying to impersonate some other server?
97 log("warn", "Received a stanza claiming to be from %s, over a stream authed for %s!", from_host, origin.from_host);
98 return; -- FIXME what should we do here? does this work with subdomains?
101 core_post_stanza(origin, stanza);
103 local h = hosts[stanza.attr.to or origin.host or origin.to_host];
106 if stanza.attr.xmlns == "jabber:client" then
107 if stanza.name == "iq" and (stanza.attr.type == "set" or stanza.attr.type == "get") then
108 event = "stanza/iq/"..stanza.tags[1].attr.xmlns..":"..stanza.tags[1].name;
110 event = "stanza/"..stanza.name;
113 event = "stanza/"..stanza.attr.xmlns..":"..stanza.name;
115 if h.events.fire_event(event, {origin = origin, stanza = stanza}) then return; end
117 if host and not hosts[host] then host = nil; end -- COMPAT: workaround for a Pidgin bug which sets 'to' to the SRV result
118 modules_handle_stanza(host or origin.host or origin.to_host, origin, stanza);
122 function core_post_stanza(origin, stanza)
123 local to = stanza.attr.to;
124 local node, host, resource = jid_split(to);
125 local to_bare = node and (node.."@"..host) or host; -- bare JID
133 if node == origin.username and host == origin.host then
134 stanza.attr.to = nil;
145 local event_data = {origin=origin, stanza=stanza};
146 if origin.full_jid == stanza.attr.from then -- c2s connection
147 if hosts[origin.host].events.fire_event('pre-'..stanza.name..to_type, event_data) then return; end -- do preprocessing
149 local h = hosts[to_bare] or hosts[host or origin.host];
151 if h.events.fire_event(stanza.name..to_type, event_data) then return; end -- do processing
153 if h.type == "component" then
154 component_handle_stanza(origin, stanza);
157 modules_handle_stanza(h.host, origin, stanza);
159 core_route_stanza(origin, stanza);
163 function core_route_stanza(origin, stanza)
164 local node, host, resource = jid_split(stanza.attr.to);
165 local from_node, from_host, from_resource = jid_split(stanza.attr.from);
167 -- Auto-detect origin if not specified
168 origin = origin or hosts[from_host];
169 if not origin then return false; end
172 -- old stanza routing code removed
173 core_post_stanza(origin, stanza);
174 elseif origin.type == "c2s" then
176 if not hosts[from_host] then
177 log("error", "No hosts[from_host] (please report): %s", tostring(stanza));
179 if (not hosts[from_host]) or (not hosts[from_host].disallow_s2s) then
180 local xmlns = stanza.attr.xmlns;
181 --stanza.attr.xmlns = "jabber:server";
182 stanza.attr.xmlns = nil;
183 log("debug", "sending s2s stanza: %s", tostring(stanza));
184 send_s2s(origin.host, host, stanza); -- TODO handle remote routing errors
185 stanza.attr.xmlns = xmlns; -- reset
187 core_route_stanza(hosts[from_host], st.error_reply(stanza, "cancel", "not-allowed", "Communication with remote servers is not allowed"));
189 elseif origin.type == "component" or origin.type == "local" then
190 -- Route via s2s for components and modules
191 log("debug", "Routing outgoing stanza for %s to %s", from_host, host);
192 send_s2s(from_host, host, stanza);
194 log("warn", "received stanza from unhandled connection type: %s", origin.type);