3 -- The code in this file should be self-explanatory, though the logic is horrible
\r
4 -- for more info on that, see doc/stanza_routing.txt, which attempts to condense
\r
5 -- the rules from the RFCs (mainly 3921)
\r
7 require "core.servermanager"
\r
9 local log = require "util.logger".init("stanzarouter")
\r
11 local st = require "util.stanza";
\r
12 local send = require "core.sessionmanager".send_to_session;
\r
13 -- local send_s2s = require "core.s2smanager".send_to_host;
\r
14 local user_exists = require "core.usermanager".user_exists;
\r
16 local jid_split = require "util.jid".split;
\r
17 local print, debug = print, debug;
\r
19 function core_process_stanza(origin, stanza)
\r
20 log("debug", "Received: "..tostring(stanza))
\r
21 -- TODO verify validity of stanza (as well as JID validity)
\r
22 if stanza.name == "iq" and not(#stanza.tags == 1 and stanza.tags[1].attr.xmlns) then
\r
23 if stanza.attr.type == "set" or stanza.attr.type == "get" then
\r
24 error("Invalid IQ");
\r
25 elseif #stanza.tags > 1 or not(stanza.attr.type == "error" or stanza.attr.type == "result") then
\r
26 error("Invalid IQ");
\r
30 if origin.type == "c2s" and not origin.full_jid
\r
31 and not(stanza.name == "iq" and stanza.tags[1].name == "bind"
\r
32 and stanza.tags[1].attr.xmlns == "urn:ietf:params:xml:ns:xmpp-bind") then
\r
33 error("Client MUST bind resource after auth");
\r
36 local to = stanza.attr.to;
\r
37 stanza.attr.from = origin.full_jid; -- quick fix to prevent impersonation (FIXME this would be incorrect when the origin is not c2s)
\r
38 -- TODO also, stazas should be returned to their original state before the function ends
\r
40 -- TODO presence subscriptions
\r
42 core_handle_stanza(origin, stanza);
\r
43 elseif hosts[to] and hosts[to].type == "local" then
\r
44 core_handle_stanza(origin, stanza);
\r
45 elseif stanza.name == "iq" and not select(3, jid_split(to)) then
\r
46 core_handle_stanza(origin, stanza);
\r
47 elseif origin.type == "c2s" then
\r
48 core_route_stanza(origin, stanza);
\r
52 -- This function handles stanzas which are not routed any further,
\r
53 -- that is, they are handled by this server
\r
54 function core_handle_stanza(origin, stanza)
\r
56 if origin.type == "c2s" or origin.type == "c2s_unauthed" then
\r
57 local session = origin;
\r
59 if stanza.name == "presence" and origin.roster then
\r
60 if stanza.attr.type == nil or stanza.attr.type == "available" or stanza.attr.type == "unavailable" then
\r
61 for jid in pairs(origin.roster) do -- broadcast to all interested contacts
\r
62 local subscription = origin.roster[jid].subscription;
\r
63 if subscription == "both" or subscription == "from" then
\r
64 stanza.attr.to = jid;
\r
65 core_route_stanza(origin, stanza);
\r
68 local node, host = jid_split(stanza.attr.from);
\r
69 for _, res in pairs(hosts[host].sessions[node].sessions) do -- broadcast to all resources and from resources
\r
70 if res ~= origin then
\r
71 if res.full_jid then -- to resource. FIXME is this check correct? Maybe it should be res.presence
\r
72 stanza.attr.to = res.full_jid;
\r
73 core_route_stanza(origin, stanza);
\r
75 if res.presence then -- from all resources for which we have presence
\r
76 res.presence.attr.to = origin.full_jid;
\r
77 core_route_stanza(res, res.presence);
\r
78 res.presence.attr.to = nil;
\r
82 if not origin.presence then -- presence probes on initial presence
\r
83 local probe = st.presence({from = origin.full_jid, type = "probe"});
\r
84 for jid in pairs(origin.roster) do
\r
85 local subscription = origin.roster[jid].subscription;
\r
86 if subscription == "both" or subscription == "to" then
\r
87 probe.attr.to = jid;
\r
88 core_route_stanza(origin, probe);
\r
91 -- TODO resend subscription requests
\r
93 origin.presence = stanza;
\r
94 stanza.attr.to = nil; -- reset it
\r
96 -- TODO error, bad type
\r
99 log("debug", "Routing stanza to local");
\r
100 handle_stanza(session, stanza);
\r
105 function is_authorized_to_see_presence(origin, username, host)
\r
106 local roster = datamanager.load(username, host, "roster") or {};
\r
107 local item = roster[origin.username.."@"..origin.host];
\r
108 return item and (item.subscription == "both" or item.subscription == "from");
\r
111 function core_route_stanza(origin, stanza)
\r
116 local to = stanza.attr.to;
\r
117 local node, host, resource = jid_split(to);
\r
119 if stanza.name == "presence" and (stanza.attr.type ~= nil and stanza.attr.type ~= "unavailable") then resource = nil; end
\r
121 local host_session = hosts[host]
\r
122 if host_session and host_session.type == "local" then
\r
124 local user = host_session.sessions[node];
\r
126 local res = user.sessions[resource];
\r
128 -- if we get here, resource was not specified or was unavailable
\r
129 if stanza.name == "presence" then
\r
130 if stanza.attr.type ~= nil and stanza.attr.type ~= "unavailable" then
\r
131 if stanza.attr.type == "probe" then
\r
132 if is_authorized_to_see_presence(origin, node, host) then
\r
133 for k in pairs(user.sessions) do -- return presence for all resources
\r
134 if user.sessions[k].presence then
\r
135 local pres = user.sessions[k].presence;
\r
136 pres.attr.to = origin.full_jid;
\r
137 pres.attr.from = user.sessions[k].full_jid;
\r
138 send(origin, pres);
\r
139 pres.attr.to = nil;
\r
140 pres.attr.from = nil;
\r
144 send(origin, st.presence({from=user.."@"..host, to=origin.username.."@"..origin.host, type="unsubscribed"}));
\r
146 elseif stanza.attr.type == "subscribe" then
\r
148 elseif stanza.attr.type == "unsubscribe" then
\r
150 elseif stanza.attr.type == "subscribed" then
\r
152 elseif stanza.attr.type == "unsubscribed" then
\r
154 end -- discard any other type
\r
155 else -- sender is available or unavailable
\r
156 for k in pairs(user.sessions) do -- presence broadcast to all user resources
\r
157 if user.sessions[k].full_jid then
\r
158 stanza.attr.to = user.sessions[k].full_jid;
\r
159 send(user.sessions[k], stanza);
\r
163 elseif stanza.name == "message" then -- select a resource to recieve message
\r
164 for k in pairs(user.sessions) do
\r
165 if user.sessions[k].full_jid then
\r
166 res = user.sessions[k];
\r
170 -- TODO find resource with greatest priority
\r
173 -- TODO send IQ error
\r
176 -- User + resource is online...
\r
177 stanza.attr.to = res.full_jid;
\r
178 send(res, stanza); -- Yay \o/
\r
182 if user_exists(node, host) then
\r
183 if stanza.name == "presence" then
\r
184 if stanza.attr.type == "probe" and is_authorized_to_see_presence(origin, node, host) then -- FIXME what to do for not c2s?
\r
185 -- TODO send last recieved unavailable presence
\r
187 -- TODO send unavailable presence
\r
189 elseif stanza.name == "message" then
\r
190 -- TODO send message error, or store offline messages
\r
191 elseif stanza.name == "iq" then
\r
192 -- TODO send IQ error
\r
194 else -- user does not exist
\r
195 -- TODO we would get here for nodeless JIDs too. Do something fun maybe? Echo service? Let plugins use xmpp:server/resource addresses?
\r
196 if stanza.name == "presence" then
\r
197 if stanza.attr.type == "probe" then
\r
198 send(origin, st.presence({from = user.."@"..host, to = origin.username.."@"..origin.host, type = "unsubscribed"}));
\r
202 send(origin, st.error_reply(stanza, "cancel", "service-unavailable"));
\r
208 if host_session then
\r
211 -- Need to establish the connection
\r
214 stanza.attr.to = to; -- reset
\r
217 function handle_stanza_toremote(stanza)
\r
218 log("error", "Stanza bound for remote host, but s2s is not implemented");
\r
222 -- The code in this file should be self-explanatory, though the logic is horrible
223 -- for more info on that, see doc/stanza_routing.txt, which attempts to condense
224 -- the rules from the RFCs (mainly 3921)
226 require "core.servermanager"
228 local log = require "util.logger".init("stanzarouter")
230 local st = require "util.stanza";
231 local send = require "core.sessionmanager".send_to_session;
232 -- local send_s2s = require "core.s2smanager".send_to_host;
233 local user_exists = require "core.usermanager".user_exists;
235 local jid_split = require "util.jid".split;
238 function core_process_stanza(origin, stanza)
239 log("debug", "Received: "..tostring(stanza))
240 -- TODO verify validity of stanza (as well as JID validity)
241 if stanza.name == "iq" and not(#stanza.tags == 1 and stanza.tags[1].attr.xmlns) then
242 if stanza.attr.type == "set" or stanza.attr.type == "get" then
244 elseif #stanza.tags > 1 or not(stanza.attr.type == "error" or stanza.attr.type == "result") then
249 if origin.type == "c2s" and not origin.full_jid
250 and not(stanza.name == "iq" and stanza.tags[1].name == "bind"
251 and stanza.tags[1].attr.xmlns == "urn:ietf:params:xml:ns:xmpp-bind") then
252 error("Client MUST bind resource after auth");
255 local to = stanza.attr.to;
256 stanza.attr.from = origin.full_jid; -- quick fix to prevent impersonation (FIXME this would be incorrect when the origin is not c2s)
257 -- TODO also, stazas should be returned to their original state before the function ends
259 -- TODO presence subscriptions
261 core_handle_stanza(origin, stanza);
262 elseif hosts[to] and hosts[to].type == "local" then
263 core_handle_stanza(origin, stanza);
264 elseif stanza.name == "iq" and not select(3, jid_split(to)) then
265 core_handle_stanza(origin, stanza);
266 elseif origin.type == "c2s" then
267 core_route_stanza(origin, stanza);
271 -- This function handles stanzas which are not routed any further,
272 -- that is, they are handled by this server
273 function core_handle_stanza(origin, stanza)
275 if origin.type == "c2s" or origin.type == "c2s_unauthed" then
276 local session = origin;
278 if stanza.name == "presence" and origin.roster then
279 if stanza.attr.type == nil or stanza.attr.type == "available" or stanza.attr.type == "unavailable" then
280 for jid in pairs(origin.roster) do -- broadcast to all interested contacts
281 local subscription = origin.roster[jid].subscription;
282 if subscription == "both" or subscription == "from" then
283 stanza.attr.to = jid;
284 core_route_stanza(origin, stanza);
287 --[[local node, host = jid_split(stanza.attr.from);
288 for _, res in pairs(hosts[host].sessions[node].sessions) do -- broadcast to all resources
290 res = user.sessions[k];
294 if not origin.presence then -- presence probes on initial presence
295 local probe = st.presence({from = origin.full_jid, type = "probe"});
296 for jid in pairs(origin.roster) do
297 local subscription = origin.roster[jid].subscription;
298 if subscription == "both" or subscription == "to" then
300 core_route_stanza(origin, probe);
304 origin.presence = stanza;
305 stanza.attr.to = nil; -- reset it
307 -- TODO error, bad type
310 log("debug", "Routing stanza to local");
311 handle_stanza(session, stanza);
316 -- TODO: Does this function belong here?
317 function is_authorized_to_see_presence(origin, username, host)
318 local roster = datamanager.load(username, host, "roster") or {};
319 local item = roster[origin.username.."@"..origin.host];
320 return item and (item.subscription == "both" or item.subscription == "from");
323 function core_route_stanza(origin, stanza)
328 local to = stanza.attr.to;
329 local node, host, resource = jid_split(to);
331 if stanza.name == "presence" and stanza.attr.type == "probe" then resource = nil; end
333 local host_session = hosts[host]
334 if host_session and host_session.type == "local" then
336 local user = host_session.sessions[node];
338 local res = user.sessions[resource];
340 -- if we get here, resource was not specified or was unavailable
341 if stanza.name == "presence" then
342 if stanza.attr.type == "probe" then
343 if is_authorized_to_see_presence(origin, node, host) then
344 for k in pairs(user.sessions) do -- return presence for all resources
345 if user.sessions[k].presence then
346 local pres = user.sessions[k].presence;
347 pres.attr.to = origin.full_jid;
348 pres.attr.from = user.sessions[k].full_jid;
351 pres.attr.from = nil;
355 send(origin, st.presence({from = user.."@"..host, to = origin.username.."@"..origin.host, type = "unsubscribed"}));
358 for k in pairs(user.sessions) do -- presence broadcast to all user resources
359 if user.sessions[k].full_jid then
360 stanza.attr.to = user.sessions[k].full_jid;
361 send(user.sessions[k], stanza);
365 elseif stanza.name == "message" then -- select a resource to recieve message
366 for k in pairs(user.sessions) do
367 if user.sessions[k].full_jid then
368 res = user.sessions[k];
372 -- TODO find resource with greatest priority
375 -- TODO send IQ error
378 -- User + resource is online...
379 stanza.attr.to = res.full_jid;
380 send(res, stanza); -- Yay \o/
384 if user_exists(node, host) then
385 if stanza.name == "presence" then
386 if stanza.attr.type == "probe" and is_authorized_to_see_presence(origin, node, host) then -- FIXME what to do for not c2s?
387 -- TODO send last recieved unavailable presence
389 -- TODO send unavailable presence
391 elseif stanza.name == "message" then
392 -- TODO send message error, or store offline messages
393 elseif stanza.name == "iq" then
394 -- TODO send IQ error
396 else -- user does not exist
397 -- TODO we would get here for nodeless JIDs too. Do something fun maybe? Echo service? Let plugins use xmpp:server/resource addresses?
398 if stanza.name == "presence" then
399 if stanza.attr.type == "probe" then
400 send(origin, st.presence({from = user.."@"..host, to = origin.username.."@"..origin.host, type = "unsubscribed"}));
404 send(origin, st.error_reply(stanza, "cancel", "service-unavailable"));
410 send_s2s(origin.host, host, stanza);
412 stanza.attr.to = to; -- reset
415 function handle_stanza_toremote(stanza)
416 log("error", "Stanza bound for remote host, but s2s is not implemented");