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";
17 local s2s_verify_dialback = require "core.s2smanager".verify_dialback;
18 local s2s_make_authenticated = require "core.s2smanager".make_authenticated;
20 local modules_handle_stanza = require "core.modulemanager".handle_stanza;
21 local component_handle_stanza = require "core.componentmanager".handle_stanza;
23 local format = string.format;
24 local tostring = tostring;
25 local t_concat = table.concat;
26 local t_insert = table.insert;
27 local tonumber = tonumber;
28 local s_find = string.find;
30 local jid_split = require "util.jid".split;
33 function core_process_stanza(origin, stanza)
34 log("debug", "Received["..origin.type.."]: "..tostring(st.reply(st.reply(stanza))))
36 if not stanza.attr.xmlns then stanza.attr.xmlns = "jabber:client"; end -- FIXME Hack. This should be removed when we fix namespace handling.
37 -- TODO verify validity of stanza (as well as JID validity)
38 if stanza.name == "iq" and not(#stanza.tags == 1 and stanza.tags[1].attr.xmlns) then
39 if stanza.attr.type == "set" or stanza.attr.type == "get" then
41 elseif #stanza.tags > 1 and not(stanza.attr.type == "error" or stanza.attr.type == "result") then
46 if origin.type == "c2s" and not origin.full_jid
47 and not(stanza.name == "iq" and stanza.tags[1].name == "bind"
48 and stanza.tags[1].attr.xmlns == "urn:ietf:params:xml:ns:xmpp-bind") then
49 error("Client MUST bind resource after auth");
52 -- TODO also, stazas should be returned to their original state before the function ends
53 if origin.type == "c2s" then
54 stanza.attr.from = origin.full_jid;
56 local to = stanza.attr.to;
57 local node, host, resource = jid_split(to);
58 local to_bare = node and (node.."@"..host) or host; -- bare JID
59 local from = stanza.attr.from;
60 local from_node, from_host, from_resource = jid_split(from);
61 local from_bare = from_node and (from_node.."@"..from_host) or from_host; -- bare JID
63 if origin.type == "s2sin" then
64 if origin.from_host ~= from_host then -- remote server trying to impersonate some other server?
65 log("warn", "Received a stanza claiming to be from %s, over a conn authed for %s!", from, origin.from_host);
66 return; -- FIXME what should we do here? does this work with subdomains?
69 --[[if to and not(hosts[to]) and not(hosts[to_bare]) and (hosts[host] and hosts[host].type ~= "local") then -- not for us?
70 log("warn", "stanza recieved for a non-local server");
71 return; -- FIXME what should we do here?
74 -- FIXME do stanzas not of jabber:client get handled by components?
75 if origin.type == "s2sin" or origin.type == "c2s" then
77 core_handle_stanza(origin, stanza);
78 elseif hosts[to] and hosts[to].type == "local" then -- directed at a local server
79 core_handle_stanza(origin, stanza);
80 elseif stanza.attr.xmlns and stanza.attr.xmlns ~= "jabber:client" and stanza.attr.xmlns ~= "jabber:server" then
81 modules_handle_stanza(origin, stanza);
82 elseif hosts[to_bare] and hosts[to_bare].type == "component" then -- hack to allow components to handle node@server
83 component_handle_stanza(origin, stanza);
84 elseif hosts[to] and hosts[to].type == "component" then -- hack to allow components to handle node@server/resource and server/resource
85 component_handle_stanza(origin, stanza);
86 elseif hosts[host] and hosts[host].type == "component" then -- directed at a component
87 component_handle_stanza(origin, stanza);
88 elseif origin.type == "c2s" and stanza.name == "presence" and stanza.attr.type ~= nil and stanza.attr.type ~= "unavailable" then
89 handle_outbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare);
90 elseif stanza.name == "iq" and not resource then -- directed at bare JID
91 core_handle_stanza(origin, stanza);
93 core_route_stanza(origin, stanza);
96 core_handle_stanza(origin, stanza);
100 -- This function handles stanzas which are not routed any further,
101 -- that is, they are handled by this server
102 function core_handle_stanza(origin, stanza)
104 if modules_handle_stanza(origin, stanza) then return; end
105 if origin.type == "c2s" or origin.type == "c2s_unauthed" then
106 local session = origin;
108 if stanza.name == "presence" and origin.roster then
109 if stanza.attr.type == nil or stanza.attr.type == "unavailable" then
110 for jid in pairs(origin.roster) do -- broadcast to all interested contacts
111 local subscription = origin.roster[jid].subscription;
112 if subscription == "both" or subscription == "from" then
113 stanza.attr.to = jid;
114 core_route_stanza(origin, stanza);
117 local node, host = jid_split(stanza.attr.from);
118 for _, res in pairs(hosts[host].sessions[node].sessions) do -- broadcast to all resources
119 if res ~= origin and res.full_jid then -- to resource. FIXME is res.full_jid the correct check? Maybe it should be res.presence
120 stanza.attr.to = res.full_jid;
121 core_route_stanza(origin, stanza);
124 if stanza.attr.type == nil and not origin.presence then -- initial presence
125 local probe = st.presence({from = origin.full_jid, type = "probe"});
126 for jid in pairs(origin.roster) do -- probe all contacts we are subscribed to
127 local subscription = origin.roster[jid].subscription;
128 if subscription == "both" or subscription == "to" then
130 core_route_stanza(origin, probe);
133 for _, res in pairs(hosts[host].sessions[node].sessions) do -- broadcast from all available resources
134 if res ~= origin and res.presence then
135 res.presence.attr.to = origin.full_jid;
136 core_route_stanza(res, res.presence);
137 res.presence.attr.to = nil;
140 if origin.roster.pending then -- resend incoming subscription requests
141 for jid in pairs(origin.roster.pending) do
142 origin.send(st.presence({type="subscribe", from=jid})); -- TODO add to attribute? Use original?
145 local request = st.presence({type="subscribe", from=origin.username.."@"..origin.host});
146 for jid, item in pairs(origin.roster) do -- resend outgoing subscription requests
148 request.attr.to = jid;
149 core_route_stanza(origin, request);
154 if stanza.attr.type == "unavailable" then
155 origin.presence = nil;
157 origin.presence = stanza;
158 local priority = stanza:child_with_name("priority");
159 if priority and #priority > 0 then
160 priority = t_concat(priority);
161 if s_find(priority, "^[+-]?[0-9]+$") then
162 priority = tonumber(priority);
163 if priority < -128 then priority = -128 end
164 if priority > 127 then priority = 127 end
165 origin.priority = priority;
169 stanza.attr.to = nil; -- reset it
171 -- TODO error, bad type
173 end -- TODO handle other stanzas
175 log("warn", "Unhandled origin: %s", origin.type); -- FIXME reply with error
179 function send_presence_of_available_resources(user, host, jid, recipient_session)
180 local h = hosts[host];
182 if h and h.type == "local" then
183 local u = h.sessions[user];
185 for k, session in pairs(u.sessions) do
186 local pres = session.presence;
189 pres.attr.from = session.full_jid;
190 recipient_session.send(pres);
192 pres.attr.from = nil;
201 function handle_outbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare)
202 local node, host = jid_split(from_bare);
203 local st_from, st_to = stanza.attr.from, stanza.attr.to;
204 stanza.attr.from, stanza.attr.to = from_bare, to_bare;
205 if stanza.attr.type == "subscribe" then
206 log("debug", "outbound subscribe from "..from_bare.." for "..to_bare);
208 -- 2. roster push (subscription = none, ask = subscribe)
209 if rostermanager.set_contact_pending_out(node, host, to_bare) then
210 rostermanager.roster_push(node, host, to_bare);
211 end -- else file error
212 core_route_stanza(origin, stanza);
213 elseif stanza.attr.type == "unsubscribe" then
214 log("debug", "outbound unsubscribe from "..from_bare.." for "..to_bare);
216 -- 2. roster push (subscription = none or from)
217 if rostermanager.unsubscribe(node, host, to_bare) then
218 rostermanager.roster_push(node, host, to_bare); -- FIXME do roster push when roster has in fact not changed?
219 end -- else file error
220 core_route_stanza(origin, stanza);
221 elseif stanza.attr.type == "subscribed" then
222 log("debug", "outbound subscribed from "..from_bare.." for "..to_bare);
225 -- 3. send_presence_of_available_resources
226 if rostermanager.subscribed(node, host, to_bare) then
227 rostermanager.roster_push(node, host, to_bare);
228 core_route_stanza(origin, stanza);
229 send_presence_of_available_resources(node, host, to_bare, origin);
231 elseif stanza.attr.type == "unsubscribed" then
232 log("debug", "outbound unsubscribed from "..from_bare.." for "..to_bare);
234 -- 2. roster push (subscription = none or to)
235 if rostermanager.unsubscribed(node, host, to_bare) then
236 rostermanager.roster_push(node, host, to_bare);
237 core_route_stanza(origin, stanza);
240 stanza.attr.from, stanza.attr.to = st_from, st_to;
243 function handle_inbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare)
244 local node, host = jid_split(to_bare);
245 local st_from, st_to = stanza.attr.from, stanza.attr.to;
246 stanza.attr.from, stanza.attr.to = from_bare, to_bare;
247 if stanza.attr.type == "probe" then
248 log("debug", "inbound probe from "..from_bare.." for "..to_bare);
249 if rostermanager.is_contact_subscribed(node, host, from_bare) then
250 if 0 == send_presence_of_available_resources(node, host, from_bare, origin) then
251 -- TODO send last recieved unavailable presence (or we MAY do nothing, which is fine too)
254 core_route_stanza(origin, st.presence({from=to_bare, to=from_bare, type="unsubscribed"}));
256 elseif stanza.attr.type == "subscribe" then
257 log("debug", "inbound subscribe from "..from_bare.." for "..to_bare);
258 if rostermanager.is_contact_subscribed(node, host, from_bare) then
259 core_route_stanza(origin, st.presence({from=to_bare, to=from_bare, type="subscribed"})); -- already subscribed
261 if not rostermanager.is_contact_pending_in(node, host, from_bare) then
262 if rostermanager.set_contact_pending_in(node, host, from_bare) then
263 sessionmanager.send_to_available_resources(node, host, stanza);
264 end -- TODO else return error, unable to save
267 elseif stanza.attr.type == "unsubscribe" then
268 log("debug", "inbound unsubscribe from "..from_bare.." for "..to_bare);
269 if rostermanager.process_inbound_unsubscribe(node, host, from_bare) then
270 rostermanager.roster_push(node, host, from_bare);
272 elseif stanza.attr.type == "subscribed" then
273 log("debug", "inbound subscribed from "..from_bare.." for "..to_bare);
274 if rostermanager.process_inbound_subscription_approval(node, host, from_bare) then
275 rostermanager.roster_push(node, host, from_bare);
277 elseif stanza.attr.type == "unsubscribed" then
278 log("debug", "inbound unsubscribed from "..from_bare.." for "..to_bare);
279 if rostermanager.process_inbound_subscription_approval(node, host, from_bare) then
280 rostermanager.roster_push(node, host, from_bare);
282 end -- discard any other type
283 stanza.attr.from, stanza.attr.to = st_from, st_to;
286 function core_route_stanza(origin, stanza)
291 local to = stanza.attr.to;
292 local node, host, resource = jid_split(to);
293 local to_bare = node and (node.."@"..host) or host; -- bare JID
294 local from = stanza.attr.from;
295 local from_node, from_host, from_resource = jid_split(from);
296 local from_bare = from_node and (from_node.."@"..from_host) or from_host; -- bare JID
298 if stanza.name == "presence" and (stanza.attr.type ~= nil and stanza.attr.type ~= "unavailable") then resource = nil; end
300 local host_session = hosts[host]
301 if host_session and host_session.type == "local" then
303 local user = host_session.sessions[node];
305 local res = user.sessions[resource];
307 -- if we get here, resource was not specified or was unavailable
308 if stanza.name == "presence" then
309 if stanza.attr.type ~= nil and stanza.attr.type ~= "unavailable" then
310 handle_inbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare);
311 else -- sender is available or unavailable
312 for _, session in pairs(user.sessions) do -- presence broadcast to all user resources.
313 if session.full_jid then -- FIXME should this be just for available resources? Do we need to check subscription?
314 stanza.attr.to = session.full_jid; -- reset at the end of function
315 session.send(stanza);
319 elseif stanza.name == "message" then -- select a resource to recieve message
321 local recipients = {};
322 for _, session in pairs(user.sessions) do -- find resource with greatest priority
323 local p = session.priority;
326 recipients = {session};
327 elseif p == priority then
328 t_insert(recipients, session);
331 for _, session in pairs(recipients) do
332 session.send(stanza);
335 -- TODO send IQ error
338 -- User + resource is online...
339 stanza.attr.to = res.full_jid; -- reset at the end of function
340 res.send(stanza); -- Yay \o/
344 if user_exists(node, host) then
345 if stanza.name == "presence" then
346 if stanza.attr.type ~= nil and stanza.attr.type ~= "unavailable" then
347 handle_inbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare);
349 -- TODO send unavailable presence or unsubscribed
351 elseif stanza.name == "message" then
352 -- TODO send message error, or store offline messages
353 elseif stanza.name == "iq" then
354 -- TODO send IQ error
356 else -- user does not exist
357 -- TODO we would get here for nodeless JIDs too. Do something fun maybe? Echo service? Let plugins use xmpp:server/resource addresses?
358 if stanza.name == "presence" then
359 if stanza.attr.type == "probe" then
360 origin.send(st.presence({from = to_bare, to = from_bare, type = "unsubscribed"}));
364 origin.send(st.error_reply(stanza, "cancel", "service-unavailable"));
368 elseif origin.type == "c2s" then
370 local xmlns = stanza.attr.xmlns;
371 --stanza.attr.xmlns = "jabber:server";
372 stanza.attr.xmlns = nil;
373 log("debug", "sending s2s stanza: %s", tostring(stanza));
374 send_s2s(origin.host, host, stanza); -- TODO handle remote routing errors
375 stanza.attr.xmlns = xmlns; -- reset
376 elseif origin.type == "component" or origin.type == "local" then
377 -- Route via s2s for components and modules
378 log("debug", "Routing outgoing stanza for %s to %s", origin.host, host);
379 for k,v in pairs(origin) do print("origin:", tostring(k), tostring(v)); end
380 print(tostring(host), tostring(from_host))
381 send_s2s(origin.host, host, stanza);
383 log("warn", "received stanza from unhandled connection type: %s", origin.type);
385 stanza.attr.to = to; -- reset
388 function handle_stanza_toremote(stanza)
389 log("error", "Stanza bound for remote host, but s2s is not implemented");