Backed out changeset 37b3e9ed8918 (again)
[prosody.git] / core / stanza_router.lua
1 <<<<<<< local\r
2 \r
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
6 \r
7 require "core.servermanager"\r
8 \r
9 local log = require "util.logger".init("stanzarouter")\r
10 \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
15 \r
16 local jid_split = require "util.jid".split;\r
17 local print, debug = print, debug;\r
18 \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
27                 end\r
28         end\r
29 \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
34         end\r
35 \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
39         \r
40         -- TODO presence subscriptions\r
41         if not to then\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
49         end\r
50 end\r
51 \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
55         -- Handlers\r
56         if origin.type == "c2s" or origin.type == "c2s_unauthed" then\r
57                 local session = origin;\r
58                 \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
66                                         end\r
67                                 end\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
74                                                 end\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
79                                                 end\r
80                                         end\r
81                                 end\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
89                                                 end\r
90                                         end\r
91                                         -- TODO resend subscription requests\r
92                                 end\r
93                                 origin.presence = stanza;\r
94                                 stanza.attr.to = nil; -- reset it\r
95                         else\r
96                                 -- TODO error, bad type\r
97                         end\r
98                 else\r
99                         log("debug", "Routing stanza to local");\r
100                         handle_stanza(session, stanza);\r
101                 end\r
102         end\r
103 end\r
104 \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
109 end\r
110 \r
111 function core_route_stanza(origin, stanza)\r
112         -- Hooks\r
113         --- ...later\r
114         \r
115         -- Deliver\r
116         local to = stanza.attr.to;\r
117         local node, host, resource = jid_split(to);\r
118 \r
119         if stanza.name == "presence" and (stanza.attr.type ~= nil and stanza.attr.type ~= "unavailable") then resource = nil; end\r
120 \r
121         local host_session = hosts[host]\r
122         if host_session and host_session.type == "local" then\r
123                 -- Local host\r
124                 local user = host_session.sessions[node];\r
125                 if user then\r
126                         local res = user.sessions[resource];\r
127                         if not res then\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
141                                                                         end\r
142                                                                 end\r
143                                                         else\r
144                                                                 send(origin, st.presence({from=user.."@"..host, to=origin.username.."@"..origin.host, type="unsubscribed"}));\r
145                                                         end\r
146                                                 elseif stanza.attr.type == "subscribe" then\r
147                                                         -- TODO\r
148                                                 elseif stanza.attr.type == "unsubscribe" then\r
149                                                         -- TODO\r
150                                                 elseif stanza.attr.type == "subscribed" then\r
151                                                         -- TODO\r
152                                                 elseif stanza.attr.type == "unsubscribed" then\r
153                                                         -- TODO\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
160                                                         end\r
161                                                 end\r
162                                         end\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
167                                                         break;\r
168                                                 end\r
169                                         end\r
170                                         -- TODO find resource with greatest priority\r
171                                         send(res, stanza);\r
172                                 else\r
173                                         -- TODO send IQ error\r
174                                 end\r
175                         else\r
176                                 -- User + resource is online...\r
177                                 stanza.attr.to = res.full_jid;\r
178                                 send(res, stanza); -- Yay \o/\r
179                         end\r
180                 else\r
181                         -- user not online\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
186                                         else\r
187                                                 -- TODO send unavailable presence\r
188                                         end\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
193                                 end\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
199                                         end\r
200                                         -- else ignore\r
201                                 else\r
202                                         send(origin, st.error_reply(stanza, "cancel", "service-unavailable"));\r
203                                 end\r
204                         end\r
205                 end\r
206         else\r
207                 -- Remote host\r
208                 if host_session then\r
209                         -- Send to session\r
210                 else\r
211                         -- Need to establish the connection\r
212                 end\r
213         end\r
214         stanza.attr.to = to; -- reset\r
215 end\r
216 \r
217 function handle_stanza_toremote(stanza)\r
218         log("error", "Stanza bound for remote host, but s2s is not implemented");\r
219 end\r
220 =======\r
221
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)
225
226 require "core.servermanager"
227
228 local log = require "util.logger".init("stanzarouter")
229
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;
234
235 local jid_split = require "util.jid".split;
236 local print = print;
237
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
243                         error("Invalid IQ");
244                 elseif #stanza.tags > 1 or not(stanza.attr.type == "error" or stanza.attr.type == "result") then
245                         error("Invalid IQ");
246                 end
247         end
248
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");
253         end
254
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
258         
259         -- TODO presence subscriptions
260         if not to then
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);
268         end
269 end
270
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)
274         -- Handlers
275         if origin.type == "c2s" or origin.type == "c2s_unauthed" then
276                 local session = origin;
277                 
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);
285                                         end
286                                 end
287                                 --[[local node, host = jid_split(stanza.attr.from);
288                                 for _, res in pairs(hosts[host].sessions[node].sessions) do -- broadcast to all resources
289                                         if res.full_jid then
290                                                 res = user.sessions[k];
291                                                 break;
292                                         end
293                                 end]]
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
299                                                         probe.attr.to = jid;
300                                                         core_route_stanza(origin, probe);
301                                                 end
302                                         end
303                                 end
304                                 origin.presence = stanza;
305                                 stanza.attr.to = nil; -- reset it
306                         else
307                                 -- TODO error, bad type
308                         end
309                 else
310                         log("debug", "Routing stanza to local");
311                         handle_stanza(session, stanza);
312                 end
313         end
314 end
315
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");
321 end
322
323 function core_route_stanza(origin, stanza)
324         -- Hooks
325         --- ...later
326         
327         -- Deliver
328         local to = stanza.attr.to;
329         local node, host, resource = jid_split(to);
330
331         if stanza.name == "presence" and stanza.attr.type == "probe" then resource = nil; end
332
333         local host_session = hosts[host]
334         if host_session and host_session.type == "local" then
335                 -- Local host
336                 local user = host_session.sessions[node];
337                 if user then
338                         local res = user.sessions[resource];
339                         if not res then
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;
349                                                                         send(origin, pres);
350                                                                         pres.attr.to = nil;
351                                                                         pres.attr.from = nil;
352                                                                 end
353                                                         end
354                                                 else
355                                                         send(origin, st.presence({from = user.."@"..host, to = origin.username.."@"..origin.host, type = "unsubscribed"}));
356                                                 end
357                                         else
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);
362                                                         end
363                                                 end
364                                         end
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];
369                                                         break;
370                                                 end
371                                         end
372                                         -- TODO find resource with greatest priority
373                                         send(res, stanza);
374                                 else
375                                         -- TODO send IQ error
376                                 end
377                         else
378                                 -- User + resource is online...
379                                 stanza.attr.to = res.full_jid;
380                                 send(res, stanza); -- Yay \o/
381                         end
382                 else
383                         -- user not online
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
388                                         else
389                                                 -- TODO send unavailable presence
390                                         end
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
395                                 end
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"}));
401                                         end
402                                         -- else ignore
403                                 else
404                                         send(origin, st.error_reply(stanza, "cancel", "service-unavailable"));
405                                 end
406                         end
407                 end
408         else
409                 -- Remote host
410                 send_s2s(origin.host, host, stanza);
411         end
412         stanza.attr.to = to; -- reset
413 end
414
415 function handle_stanza_toremote(stanza)
416         log("error", "Stanza bound for remote host, but s2s is not implemented");
417 end
418 >>>>>>> other\r