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 -- TODO verify validity of stanza (as well as JID validity)
37 if stanza.name == "iq" and not(#stanza.tags == 1 and stanza.tags[1].attr.xmlns) then
38 if stanza.attr.type == "set" or stanza.attr.type == "get" then
40 elseif #stanza.tags > 1 and not(stanza.attr.type == "error" or stanza.attr.type == "result") then
45 if origin.type == "c2s" and not origin.full_jid
46 and not(stanza.name == "iq" and stanza.tags[1].name == "bind"
47 and stanza.tags[1].attr.xmlns == "urn:ietf:params:xml:ns:xmpp-bind") then
48 error("Client MUST bind resource after auth");
51 -- TODO also, stazas should be returned to their original state before the function ends
52 if origin.type == "c2s" then
53 stanza.attr.from = origin.full_jid;
55 local to = stanza.attr.to;
56 local node, host, resource = jid_split(to);
57 local to_bare = node and (node.."@"..host) or host; -- bare JID
58 local from = stanza.attr.from;
59 local from_node, from_host, from_resource = jid_split(from);
60 local from_bare = from_node and (from_node.."@"..from_host) or from_host; -- bare JID
62 if origin.type == "s2sin" then
63 if origin.from_host ~= from_host then -- remote server trying to impersonate some other server?
64 log("warn", "Received a stanza claiming to be from %s, over a conn authed for %s!", from, origin.from_host);
65 return; -- FIXME what should we do here? does this work with subdomains?
68 --[[if to and not(hosts[to]) and not(hosts[to_bare]) and (hosts[host] and hosts[host].type ~= "local") then -- not for us?
69 log("warn", "stanza recieved for a non-local server");
70 return; -- FIXME what should we do here?
73 -- FIXME do stanzas not of jabber:client get handled by components?
75 core_handle_stanza(origin, stanza);
76 elseif hosts[to] and hosts[to].type == "local" then -- directed at a local server
77 core_handle_stanza(origin, stanza);
78 elseif stanza.attr.xmlns and stanza.attr.xmlns ~= "jabber:client" and stanza.attr.xmlns ~= "jabber:server" then
79 modules_handle_stanza(origin, stanza);
80 elseif hosts[to_bare] and hosts[to_bare].type == "component" then -- hack to allow components to handle node@server
81 component_handle_stanza(origin, stanza);
82 elseif hosts[to] and hosts[to].type == "component" then -- hack to allow components to handle node@server/resource and server/resource
83 component_handle_stanza(origin, stanza);
84 elseif hosts[host] and hosts[host].type == "component" then -- directed at a component
85 component_handle_stanza(origin, stanza);
86 elseif origin.type == "c2s" and stanza.name == "presence" and stanza.attr.type ~= nil and stanza.attr.type ~= "unavailable" then
87 handle_outbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare);
88 elseif stanza.name == "iq" and not resource then -- directed at bare JID
89 core_handle_stanza(origin, stanza);
90 elseif origin.type == "c2s" or origin.type == "s2sin" then
91 core_route_stanza(origin, stanza);
93 log("warn", "stanza not processed");
97 -- This function handles stanzas which are not routed any further,
98 -- that is, they are handled by this server
99 function core_handle_stanza(origin, stanza)
101 if modules_handle_stanza(origin, stanza) then return; end
102 if origin.type == "c2s" or origin.type == "c2s_unauthed" then
103 local session = origin;
105 if stanza.name == "presence" and origin.roster then
106 if stanza.attr.type == nil or stanza.attr.type == "unavailable" then
107 for jid in pairs(origin.roster) do -- broadcast to all interested contacts
108 local subscription = origin.roster[jid].subscription;
109 if subscription == "both" or subscription == "from" then
110 stanza.attr.to = jid;
111 core_route_stanza(origin, stanza);
114 local node, host = jid_split(stanza.attr.from);
115 for _, res in pairs(hosts[host].sessions[node].sessions) do -- broadcast to all resources
116 if res ~= origin and res.full_jid then -- to resource. FIXME is res.full_jid the correct check? Maybe it should be res.presence
117 stanza.attr.to = res.full_jid;
118 core_route_stanza(origin, stanza);
121 if stanza.attr.type == nil and not origin.presence then -- initial presence
122 local probe = st.presence({from = origin.full_jid, type = "probe"});
123 for jid in pairs(origin.roster) do -- probe all contacts we are subscribed to
124 local subscription = origin.roster[jid].subscription;
125 if subscription == "both" or subscription == "to" then
127 core_route_stanza(origin, probe);
130 for _, res in pairs(hosts[host].sessions[node].sessions) do -- broadcast from all available resources
131 if res ~= origin and res.presence then
132 res.presence.attr.to = origin.full_jid;
133 core_route_stanza(res, res.presence);
134 res.presence.attr.to = nil;
137 if origin.roster.pending then -- resend incoming subscription requests
138 for jid in pairs(origin.roster.pending) do
139 origin.send(st.presence({type="subscribe", from=jid})); -- TODO add to attribute? Use original?
142 local request = st.presence({type="subscribe", from=origin.username.."@"..origin.host});
143 for jid, item in pairs(origin.roster) do -- resend outgoing subscription requests
145 request.attr.to = jid;
146 core_route_stanza(origin, request);
151 if stanza.attr.type == "unavailable" then
152 origin.presence = nil;
154 origin.presence = stanza;
155 local priority = stanza:child_with_name("priority");
156 if priority and #priority > 0 then
157 priority = t_concat(priority);
158 if s_find(priority, "^[+-]?[0-9]+$") then
159 priority = tonumber(priority);
160 if priority < -128 then priority = -128 end
161 if priority > 127 then priority = 127 end
162 origin.priority = priority;
166 stanza.attr.to = nil; -- reset it
168 -- TODO error, bad type
170 end -- TODO handle other stanzas
172 log("warn", "Unhandled origin: %s", origin.type); -- FIXME reply with error
176 function send_presence_of_available_resources(user, host, jid, recipient_session)
177 local h = hosts[host];
179 if h and h.type == "local" then
180 local u = h.sessions[user];
182 for k, session in pairs(u.sessions) do
183 local pres = session.presence;
186 pres.attr.from = session.full_jid;
187 recipient_session.send(pres);
189 pres.attr.from = nil;
198 function handle_outbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare)
199 local node, host = jid_split(from_bare);
200 local st_from, st_to = stanza.attr.from, stanza.attr.to;
201 stanza.attr.from, stanza.attr.to = from_bare, to_bare;
202 if stanza.attr.type == "subscribe" then
203 log("debug", "outbound subscribe from "..from_bare.." for "..to_bare);
205 -- 2. roster push (subscription = none, ask = subscribe)
206 if rostermanager.set_contact_pending_out(node, host, to_bare) then
207 rostermanager.roster_push(node, host, to_bare);
208 end -- else file error
209 core_route_stanza(origin, stanza);
210 elseif stanza.attr.type == "unsubscribe" then
211 log("debug", "outbound unsubscribe from "..from_bare.." for "..to_bare);
213 -- 2. roster push (subscription = none or from)
214 if rostermanager.unsubscribe(node, host, to_bare) then
215 rostermanager.roster_push(node, host, to_bare); -- FIXME do roster push when roster has in fact not changed?
216 end -- else file error
217 core_route_stanza(origin, stanza);
218 elseif stanza.attr.type == "subscribed" then
219 log("debug", "outbound subscribed from "..from_bare.." for "..to_bare);
222 -- 3. send_presence_of_available_resources
223 if rostermanager.subscribed(node, host, to_bare) then
224 rostermanager.roster_push(node, host, to_bare);
225 core_route_stanza(origin, stanza);
226 send_presence_of_available_resources(node, host, to_bare, origin);
228 elseif stanza.attr.type == "unsubscribed" then
229 log("debug", "outbound unsubscribed from "..from_bare.." for "..to_bare);
231 -- 2. roster push (subscription = none or to)
232 if rostermanager.unsubscribed(node, host, to_bare) then
233 rostermanager.roster_push(node, host, to_bare);
234 core_route_stanza(origin, stanza);
237 stanza.attr.from, stanza.attr.to = st_from, st_to;
240 function handle_inbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare)
241 local node, host = jid_split(to_bare);
242 local st_from, st_to = stanza.attr.from, stanza.attr.to;
243 stanza.attr.from, stanza.attr.to = from_bare, to_bare;
244 if stanza.attr.type == "probe" then
245 log("debug", "inbound probe from "..from_bare.." for "..to_bare);
246 if rostermanager.is_contact_subscribed(node, host, from_bare) then
247 if 0 == send_presence_of_available_resources(node, host, from_bare, origin) then
248 -- TODO send last recieved unavailable presence (or we MAY do nothing, which is fine too)
251 core_route_stanza(origin, st.presence({from=to_bare, to=from_bare, type="unsubscribed"}));
253 elseif stanza.attr.type == "subscribe" then
254 log("debug", "inbound subscribe from "..from_bare.." for "..to_bare);
255 if rostermanager.is_contact_subscribed(node, host, from_bare) then
256 core_route_stanza(origin, st.presence({from=to_bare, to=from_bare, type="subscribed"})); -- already subscribed
258 if not rostermanager.is_contact_pending_in(node, host, from_bare) then
259 if rostermanager.set_contact_pending_in(node, host, from_bare) then
260 sessionmanager.send_to_available_resources(node, host, stanza);
261 end -- TODO else return error, unable to save
264 elseif stanza.attr.type == "unsubscribe" then
265 log("debug", "inbound unsubscribe from "..from_bare.." for "..to_bare);
266 if rostermanager.process_inbound_unsubscribe(node, host, from_bare) then
267 rostermanager.roster_push(node, host, from_bare);
269 elseif stanza.attr.type == "subscribed" then
270 log("debug", "inbound subscribed from "..from_bare.." for "..to_bare);
271 if rostermanager.process_inbound_subscription_approval(node, host, from_bare) then
272 rostermanager.roster_push(node, host, from_bare);
274 elseif stanza.attr.type == "unsubscribed" then
275 log("debug", "inbound unsubscribed from "..from_bare.." for "..to_bare);
276 if rostermanager.process_inbound_subscription_approval(node, host, from_bare) then
277 rostermanager.roster_push(node, host, from_bare);
279 end -- discard any other type
280 stanza.attr.from, stanza.attr.to = st_from, st_to;
283 function core_route_stanza(origin, stanza)
288 local to = stanza.attr.to;
289 local node, host, resource = jid_split(to);
290 local to_bare = node and (node.."@"..host) or host; -- bare JID
291 local from = stanza.attr.from;
292 local from_node, from_host, from_resource = jid_split(from);
293 local from_bare = from_node and (from_node.."@"..from_host) or from_host; -- bare JID
295 if stanza.name == "presence" and (stanza.attr.type ~= nil and stanza.attr.type ~= "unavailable") then resource = nil; end
297 local host_session = hosts[host]
298 if host_session and host_session.type == "local" then
300 local user = host_session.sessions[node];
302 local res = user.sessions[resource];
304 -- if we get here, resource was not specified or was unavailable
305 if stanza.name == "presence" then
306 if stanza.attr.type ~= nil and stanza.attr.type ~= "unavailable" then
307 handle_inbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare);
308 else -- sender is available or unavailable
309 for _, session in pairs(user.sessions) do -- presence broadcast to all user resources.
310 if session.full_jid then -- FIXME should this be just for available resources? Do we need to check subscription?
311 stanza.attr.to = session.full_jid; -- reset at the end of function
312 session.send(stanza);
316 elseif stanza.name == "message" then -- select a resource to recieve message
318 local recipients = {};
319 for _, session in pairs(user.sessions) do -- find resource with greatest priority
320 local p = session.priority;
323 recipients = {session};
324 elseif p == priority then
325 t_insert(recipients, session);
328 for _, session in pairs(recipient) do
329 session.send(stanza);
332 -- TODO send IQ error
335 -- User + resource is online...
336 stanza.attr.to = res.full_jid; -- reset at the end of function
337 res.send(stanza); -- Yay \o/
341 if user_exists(node, host) then
342 if stanza.name == "presence" then
343 if stanza.attr.type ~= nil and stanza.attr.type ~= "unavailable" then
344 handle_inbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare);
346 -- TODO send unavailable presence or unsubscribed
348 elseif stanza.name == "message" then
349 -- TODO send message error, or store offline messages
350 elseif stanza.name == "iq" then
351 -- TODO send IQ error
353 else -- user does not exist
354 -- TODO we would get here for nodeless JIDs too. Do something fun maybe? Echo service? Let plugins use xmpp:server/resource addresses?
355 if stanza.name == "presence" then
356 if stanza.attr.type == "probe" then
357 origin.send(st.presence({from = to_bare, to = from_bare, type = "unsubscribed"}));
361 origin.send(st.error_reply(stanza, "cancel", "service-unavailable"));
365 elseif origin.type == "c2s" then
367 local xmlns = stanza.attr.xmlns;
368 --stanza.attr.xmlns = "jabber:server";
369 stanza.attr.xmlns = nil;
370 log("debug", "sending s2s stanza: %s", tostring(stanza));
371 send_s2s(origin.host, host, stanza); -- TODO handle remote routing errors
372 stanza.attr.xmlns = xmlns; -- reset
373 elseif origin.type == "component" or origin.type == "local" then
374 -- Route via s2s for components and modules
375 log("debug", "Routing outgoing stanza for %s to %s", origin.host, host);
376 for k,v in pairs(origin) do print("origin:", tostring(k), tostring(v)); end
377 print(tostring(host), tostring(from_host))
378 send_s2s(origin.host, host, stanza);
380 log("warn", "received stanza from unhandled connection type: %s", origin.type);
382 stanza.attr.to = to; -- reset
385 function handle_stanza_toremote(stanza)
386 log("error", "Stanza bound for remote host, but s2s is not implemented");