2 -- The code in this file should be self-explanatory, though the logic is horrible
3 -- for more info on that, see doc/stanza_routing.txt, which attempts to condense
4 -- the rules from the RFCs (mainly 3921)
6 require "core.servermanager"
8 local log = require "util.logger".init("stanzarouter")
10 local st = require "util.stanza";
11 local send_s2s = require "core.s2smanager".send_to_host;
12 local user_exists = require "core.usermanager".user_exists;
14 local rostermanager = require "core.rostermanager";
15 local sessionmanager = require "core.sessionmanager";
16 local offlinemanager = require "core.offlinemanager";
18 local s2s_verify_dialback = require "core.s2smanager".verify_dialback;
19 local s2s_make_authenticated = require "core.s2smanager".make_authenticated;
21 local modules_handle_stanza = require "core.modulemanager".handle_stanza;
22 local component_handle_stanza = require "core.componentmanager".handle_stanza;
24 local handle_outbound_presence_subscriptions_and_probes = require "core.presencemanager".handle_outbound_presence_subscriptions_and_probes;
25 local handle_inbound_presence_subscriptions_and_probes = require "core.presencemanager".handle_inbound_presence_subscriptions_and_probes;
27 local format = string.format;
28 local tostring = tostring;
29 local t_concat = table.concat;
30 local t_insert = table.insert;
31 local tonumber = tonumber;
32 local s_find = string.find;
34 local jid_split = require "util.jid".split;
37 function core_process_stanza(origin, stanza)
38 (origin.log or log)("debug", "Received[%s]: %s", origin.type, stanza:pretty_print()) --top_tag())
40 if not stanza.attr.xmlns then stanza.attr.xmlns = "jabber:client"; end -- FIXME Hack. This should be removed when we fix namespace handling.
41 -- TODO verify validity of stanza (as well as JID validity)
42 if stanza.name == "iq" and not(#stanza.tags == 1 and stanza.tags[1].attr.xmlns) then
43 if stanza.attr.type == "set" or stanza.attr.type == "get" then
45 elseif #stanza.tags > 1 and not(stanza.attr.type == "error" or stanza.attr.type == "result") then
50 if origin.type == "c2s" and not origin.full_jid
51 and not(stanza.name == "iq" and stanza.tags[1].name == "bind"
52 and stanza.tags[1].attr.xmlns == "urn:ietf:params:xml:ns:xmpp-bind") then
53 error("Client MUST bind resource after auth");
56 -- TODO also, stanzas should be returned to their original state before the function ends
57 if origin.type == "c2s" then
58 stanza.attr.from = origin.full_jid;
60 local to = stanza.attr.to;
61 local node, host, resource = jid_split(to);
62 local to_bare = node and (node.."@"..host) or host; -- bare JID
63 local from = stanza.attr.from;
64 local from_node, from_host, from_resource = jid_split(from);
65 local from_bare = from_node and (from_node.."@"..from_host) or from_host; -- bare JID
67 if origin.type == "s2sin" then
68 if origin.from_host ~= from_host then -- remote server trying to impersonate some other server?
69 log("warn", "Received a stanza claiming to be from %s, over a conn authed for %s!", from, origin.from_host);
70 return; -- FIXME what should we do here? does this work with subdomains?
73 --[[if to and not(hosts[to]) and not(hosts[to_bare]) and (hosts[host] and hosts[host].type ~= "local") then -- not for us?
74 log("warn", "stanza recieved for a non-local server");
75 return; -- FIXME what should we do here?
78 -- FIXME do stanzas not of jabber:client get handled by components?
79 if origin.type == "s2sin" or origin.type == "c2s" then
81 core_handle_stanza(origin, stanza);
82 elseif hosts[to] and hosts[to].type == "local" then -- directed at a local server
83 core_handle_stanza(origin, stanza);
84 elseif stanza.attr.xmlns and stanza.attr.xmlns ~= "jabber:client" and stanza.attr.xmlns ~= "jabber:server" then
85 modules_handle_stanza(origin, stanza);
86 elseif hosts[to_bare] and hosts[to_bare].type == "component" then -- hack to allow components to handle node@server
87 component_handle_stanza(origin, stanza);
88 elseif hosts[to] and hosts[to].type == "component" then -- hack to allow components to handle node@server/resource and server/resource
89 component_handle_stanza(origin, stanza);
90 elseif hosts[host] and hosts[host].type == "component" then -- directed at a component
91 component_handle_stanza(origin, stanza);
92 elseif origin.type == "c2s" and stanza.name == "presence" and stanza.attr.type ~= nil and stanza.attr.type ~= "unavailable" then
93 handle_outbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare, core_route_stanza);
94 elseif origin.type ~= "c2s" and stanza.name == "iq" and not resource then -- directed at bare JID
95 core_handle_stanza(origin, stanza);
97 core_route_stanza(origin, stanza);
100 core_handle_stanza(origin, stanza);
104 -- This function handles stanzas which are not routed any further,
105 -- that is, they are handled by this server
106 function core_handle_stanza(origin, stanza)
108 if modules_handle_stanza(origin, stanza) then return; end
109 if origin.type == "c2s" or origin.type == "c2s_unauthed" then
110 local session = origin;
112 if stanza.name == "presence" and origin.roster then
113 if stanza.attr.type == nil or stanza.attr.type == "unavailable" then
114 for jid in pairs(origin.roster) do -- broadcast to all interested contacts
115 local subscription = origin.roster[jid].subscription;
116 if subscription == "both" or subscription == "from" then
117 stanza.attr.to = jid;
118 core_route_stanza(origin, stanza);
121 local node, host = jid_split(stanza.attr.from);
122 for _, res in pairs(hosts[host].sessions[node].sessions) do -- broadcast to all resources
123 if res ~= origin and res.full_jid then -- to resource. FIXME is res.full_jid the correct check? Maybe it should be res.presence
124 stanza.attr.to = res.full_jid;
125 core_route_stanza(origin, stanza);
128 if stanza.attr.type == nil and not origin.presence then -- initial presence
129 local probe = st.presence({from = origin.full_jid, type = "probe"});
130 for jid in pairs(origin.roster) do -- probe all contacts we are subscribed to
131 local subscription = origin.roster[jid].subscription;
132 if subscription == "both" or subscription == "to" then
134 core_route_stanza(origin, probe);
137 for _, res in pairs(hosts[host].sessions[node].sessions) do -- broadcast from all available resources
138 if res ~= origin and res.presence then
139 res.presence.attr.to = origin.full_jid;
140 core_route_stanza(res, res.presence);
141 res.presence.attr.to = nil;
144 if origin.roster.pending then -- resend incoming subscription requests
145 for jid in pairs(origin.roster.pending) do
146 origin.send(st.presence({type="subscribe", from=jid})); -- TODO add to attribute? Use original?
149 local request = st.presence({type="subscribe", from=origin.username.."@"..origin.host});
150 for jid, item in pairs(origin.roster) do -- resend outgoing subscription requests
152 request.attr.to = jid;
153 core_route_stanza(origin, request);
156 for _, msg in ipairs(offlinemanager.load(node, host) or {}) do
157 origin.send(msg); -- FIXME do we need to modify to/from in any way?
159 offlinemanager.deleteAll(node, host);
162 if stanza.attr.type == "unavailable" then
163 origin.presence = nil;
165 origin.presence = stanza;
166 local priority = stanza:child_with_name("priority");
167 if priority and #priority > 0 then
168 priority = t_concat(priority);
169 if s_find(priority, "^[+-]?[0-9]+$") then
170 priority = tonumber(priority);
171 if priority < -128 then priority = -128 end
172 if priority > 127 then priority = 127 end
173 origin.priority = priority;
177 stanza.attr.to = nil; -- reset it
179 log("warn", "Unhandled c2s presence: %s", tostring(stanza));
180 if (stanza.attr.xmlns == "jabber:client" or stanza.attr.xmlns == "jabber:server") and stanza.attr.type ~= "error" then
181 origin.send(st.error_reply(stanza, "cancel", "service-unavailable")); -- FIXME correct error?
185 log("warn", "Unhandled c2s stanza: %s", tostring(stanza));
186 if (stanza.attr.xmlns == "jabber:client" or stanza.attr.xmlns == "jabber:server") and stanza.attr.type ~= "error" and stanza.attr.type ~= "result" then
187 origin.send(st.error_reply(stanza, "cancel", "service-unavailable")); -- FIXME correct error?
189 end -- TODO handle other stanzas
191 log("warn", "Unhandled origin: %s", origin.type);
192 if (stanza.attr.xmlns == "jabber:client" or stanza.attr.xmlns == "jabber:server") and stanza.attr.type ~= "error" and stanza.attr.type ~= "result" then
193 -- s2s stanzas can get here
194 origin.send(st.error_reply(stanza, "cancel", "service-unavailable")); -- FIXME correct error?
199 function core_route_stanza(origin, stanza)
204 local to = stanza.attr.to;
205 local node, host, resource = jid_split(to);
206 local to_bare = node and (node.."@"..host) or host; -- bare JID
207 local from = stanza.attr.from;
208 local from_node, from_host, from_resource = jid_split(from);
209 local from_bare = from_node and (from_node.."@"..from_host) or from_host; -- bare JID
211 -- Auto-detect origin if not specified
212 origin = origin or hosts[from_host];
213 if not origin then return false; end
215 if stanza.name == "presence" and (stanza.attr.type ~= nil and stanza.attr.type ~= "unavailable") then resource = nil; end
217 local host_session = hosts[host]
218 if host_session and host_session.type == "local" then
220 local user = host_session.sessions[node];
222 local res = user.sessions[resource];
224 -- if we get here, resource was not specified or was unavailable
225 if stanza.name == "presence" then
226 if stanza.attr.type ~= nil and stanza.attr.type ~= "unavailable" then
227 handle_inbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare, core_route_stanza);
228 else -- sender is available or unavailable
229 for _, session in pairs(user.sessions) do -- presence broadcast to all user resources.
230 if session.full_jid then -- FIXME should this be just for available resources? Do we need to check subscription?
231 stanza.attr.to = session.full_jid; -- reset at the end of function
232 session.send(stanza);
236 elseif stanza.name == "message" then -- select a resource to recieve message
238 local recipients = {};
239 for _, session in pairs(user.sessions) do -- find resource with greatest priority
240 local p = session.priority;
243 recipients = {session};
244 elseif p == priority then
245 t_insert(recipients, session);
249 for _, session in pairs(recipients) do
250 session.send(stanza);
254 offlinemanager.store(node, host, stanza);
255 -- TODO deal with storage errors
258 -- TODO send IQ error
261 -- User + resource is online...
262 stanza.attr.to = res.full_jid; -- reset at the end of function
263 res.send(stanza); -- Yay \o/
267 if user_exists(node, host) then
268 if stanza.name == "presence" then
269 if stanza.attr.type ~= nil and stanza.attr.type ~= "unavailable" then
270 handle_inbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare, core_route_stanza);
272 -- TODO send unavailable presence or unsubscribed
274 elseif stanza.name == "message" then
275 if stanza.attr.type == "chat" or stanza.attr.type == "normal" or not stanza.attr.type then
276 offlinemanager.store(node, host, stanza);
277 -- FIXME don't store messages with only chat state notifications
279 -- TODO allow configuration of offline storage
280 -- TODO send error if not storing offline
281 elseif stanza.name == "iq" then
282 -- TODO send IQ error
284 else -- user does not exist
285 -- TODO we would get here for nodeless JIDs too. Do something fun maybe? Echo service? Let plugins use xmpp:server/resource addresses?
286 if stanza.name == "presence" then
287 if stanza.attr.type == "probe" then
288 origin.send(st.presence({from = to_bare, to = from_bare, type = "unsubscribed"}));
292 origin.send(st.error_reply(stanza, "cancel", "service-unavailable"));
296 elseif origin.type == "c2s" then
298 local xmlns = stanza.attr.xmlns;
299 --stanza.attr.xmlns = "jabber:server";
300 stanza.attr.xmlns = nil;
301 log("debug", "sending s2s stanza: %s", tostring(stanza));
302 send_s2s(origin.host, host, stanza); -- TODO handle remote routing errors
303 stanza.attr.xmlns = xmlns; -- reset
304 elseif origin.type == "component" or origin.type == "local" then
305 -- Route via s2s for components and modules
306 log("debug", "Routing outgoing stanza for %s to %s", origin.host, host);
307 send_s2s(origin.host, host, stanza);
309 log("warn", "received stanza from unhandled connection type: %s", origin.type);
311 stanza.attr.to = to; -- reset
314 function handle_stanza_toremote(stanza)
315 log("error", "Stanza bound for remote host, but s2s is not implemented");