Software /
code /
prosody
Comparison
core/stanza_router.lua @ 139:8f17ba74823c
Reverting stanza_router to non-conflicted version
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Thu, 23 Oct 2008 18:33:10 +0100 |
parent | 138:cb0dd442ca63 |
child | 140:a0fc73d5f48c |
comparison
equal
deleted
inserted
replaced
138:cb0dd442ca63 | 139:8f17ba74823c |
---|---|
1 <<<<<<< local | |
2 | 1 |
3 -- The code in this file should be self-explanatory, though the logic is horrible | 2 -- The code in this file should be self-explanatory, though the logic is horrible |
4 -- for more info on that, see doc/stanza_routing.txt, which attempts to condense | 3 -- for more info on that, see doc/stanza_routing.txt, which attempts to condense |
5 -- the rules from the RFCs (mainly 3921) | 4 -- the rules from the RFCs (mainly 3921) |
6 | 5 |
12 local send = require "core.sessionmanager".send_to_session; | 11 local send = require "core.sessionmanager".send_to_session; |
13 -- local send_s2s = require "core.s2smanager".send_to_host; | 12 -- local send_s2s = require "core.s2smanager".send_to_host; |
14 local user_exists = require "core.usermanager".user_exists; | 13 local user_exists = require "core.usermanager".user_exists; |
15 | 14 |
16 local jid_split = require "util.jid".split; | 15 local jid_split = require "util.jid".split; |
17 local print, debug = print, debug; | 16 local print = print; |
18 | 17 |
19 function core_process_stanza(origin, stanza) | 18 function core_process_stanza(origin, stanza) |
20 log("debug", "Received: "..tostring(stanza)) | 19 log("debug", "Received: "..tostring(stanza)) |
21 -- TODO verify validity of stanza (as well as JID validity) | 20 -- TODO verify validity of stanza (as well as JID validity) |
22 if stanza.name == "iq" and not(#stanza.tags == 1 and stanza.tags[1].attr.xmlns) then | 21 if stanza.name == "iq" and not(#stanza.tags == 1 and stanza.tags[1].attr.xmlns) then |
63 if subscription == "both" or subscription == "from" then | 62 if subscription == "both" or subscription == "from" then |
64 stanza.attr.to = jid; | 63 stanza.attr.to = jid; |
65 core_route_stanza(origin, stanza); | 64 core_route_stanza(origin, stanza); |
66 end | 65 end |
67 end | 66 end |
68 local node, host = jid_split(stanza.attr.from); | 67 --[[local node, host = jid_split(stanza.attr.from); |
69 for _, res in pairs(hosts[host].sessions[node].sessions) do -- broadcast to all resources and from resources | 68 for _, res in pairs(hosts[host].sessions[node].sessions) do -- broadcast to all resources |
70 if res ~= origin then | 69 if res.full_jid then |
71 if res.full_jid then -- to resource. FIXME is this check correct? Maybe it should be res.presence | 70 res = user.sessions[k]; |
72 stanza.attr.to = res.full_jid; | 71 break; |
73 core_route_stanza(origin, stanza); | 72 end |
74 end | 73 end]] |
75 if res.presence then -- from all resources for which we have presence | |
76 res.presence.attr.to = origin.full_jid; | |
77 core_route_stanza(res, res.presence); | |
78 res.presence.attr.to = nil; | |
79 end | |
80 end | |
81 end | |
82 if not origin.presence then -- presence probes on initial presence | 74 if not origin.presence then -- presence probes on initial presence |
83 local probe = st.presence({from = origin.full_jid, type = "probe"}); | 75 local probe = st.presence({from = origin.full_jid, type = "probe"}); |
84 for jid in pairs(origin.roster) do | 76 for jid in pairs(origin.roster) do |
85 local subscription = origin.roster[jid].subscription; | 77 local subscription = origin.roster[jid].subscription; |
86 if subscription == "both" or subscription == "to" then | 78 if subscription == "both" or subscription == "to" then |
87 probe.attr.to = jid; | 79 probe.attr.to = jid; |
88 core_route_stanza(origin, probe); | 80 core_route_stanza(origin, probe); |
89 end | 81 end |
90 end | 82 end |
91 -- TODO resend subscription requests | |
92 end | 83 end |
93 origin.presence = stanza; | 84 origin.presence = stanza; |
94 stanza.attr.to = nil; -- reset it | 85 stanza.attr.to = nil; -- reset it |
95 else | 86 else |
96 -- TODO error, bad type | 87 -- TODO error, bad type |
100 handle_stanza(session, stanza); | 91 handle_stanza(session, stanza); |
101 end | 92 end |
102 end | 93 end |
103 end | 94 end |
104 | 95 |
105 function is_authorized_to_see_presence(origin, username, host) | |
106 local roster = datamanager.load(username, host, "roster") or {}; | |
107 local item = roster[origin.username.."@"..origin.host]; | |
108 return item and (item.subscription == "both" or item.subscription == "from"); | |
109 end | |
110 | |
111 function core_route_stanza(origin, stanza) | |
112 -- Hooks | |
113 --- ...later | |
114 | |
115 -- Deliver | |
116 local to = stanza.attr.to; | |
117 local node, host, resource = jid_split(to); | |
118 | |
119 if stanza.name == "presence" and (stanza.attr.type ~= nil and stanza.attr.type ~= "unavailable") then resource = nil; end | |
120 | |
121 local host_session = hosts[host] | |
122 if host_session and host_session.type == "local" then | |
123 -- Local host | |
124 local user = host_session.sessions[node]; | |
125 if user then | |
126 local res = user.sessions[resource]; | |
127 if not res then | |
128 -- if we get here, resource was not specified or was unavailable | |
129 if stanza.name == "presence" then | |
130 if stanza.attr.type ~= nil and stanza.attr.type ~= "unavailable" then | |
131 if stanza.attr.type == "probe" then | |
132 if is_authorized_to_see_presence(origin, node, host) then | |
133 for k in pairs(user.sessions) do -- return presence for all resources | |
134 if user.sessions[k].presence then | |
135 local pres = user.sessions[k].presence; | |
136 pres.attr.to = origin.full_jid; | |
137 pres.attr.from = user.sessions[k].full_jid; | |
138 send(origin, pres); | |
139 pres.attr.to = nil; | |
140 pres.attr.from = nil; | |
141 end | |
142 end | |
143 else | |
144 send(origin, st.presence({from=user.."@"..host, to=origin.username.."@"..origin.host, type="unsubscribed"})); | |
145 end | |
146 elseif stanza.attr.type == "subscribe" then | |
147 -- TODO | |
148 elseif stanza.attr.type == "unsubscribe" then | |
149 -- TODO | |
150 elseif stanza.attr.type == "subscribed" then | |
151 -- TODO | |
152 elseif stanza.attr.type == "unsubscribed" then | |
153 -- TODO | |
154 end -- discard any other type | |
155 else -- sender is available or unavailable | |
156 for k in pairs(user.sessions) do -- presence broadcast to all user resources | |
157 if user.sessions[k].full_jid then | |
158 stanza.attr.to = user.sessions[k].full_jid; | |
159 send(user.sessions[k], stanza); | |
160 end | |
161 end | |
162 end | |
163 elseif stanza.name == "message" then -- select a resource to recieve message | |
164 for k in pairs(user.sessions) do | |
165 if user.sessions[k].full_jid then | |
166 res = user.sessions[k]; | |
167 break; | |
168 end | |
169 end | |
170 -- TODO find resource with greatest priority | |
171 send(res, stanza); | |
172 else | |
173 -- TODO send IQ error | |
174 end | |
175 else | |
176 -- User + resource is online... | |
177 stanza.attr.to = res.full_jid; | |
178 send(res, stanza); -- Yay \o/ | |
179 end | |
180 else | |
181 -- user not online | |
182 if user_exists(node, host) then | |
183 if stanza.name == "presence" then | |
184 if stanza.attr.type == "probe" and is_authorized_to_see_presence(origin, node, host) then -- FIXME what to do for not c2s? | |
185 -- TODO send last recieved unavailable presence | |
186 else | |
187 -- TODO send unavailable presence | |
188 end | |
189 elseif stanza.name == "message" then | |
190 -- TODO send message error, or store offline messages | |
191 elseif stanza.name == "iq" then | |
192 -- TODO send IQ error | |
193 end | |
194 else -- user does not exist | |
195 -- TODO we would get here for nodeless JIDs too. Do something fun maybe? Echo service? Let plugins use xmpp:server/resource addresses? | |
196 if stanza.name == "presence" then | |
197 if stanza.attr.type == "probe" then | |
198 send(origin, st.presence({from = user.."@"..host, to = origin.username.."@"..origin.host, type = "unsubscribed"})); | |
199 end | |
200 -- else ignore | |
201 else | |
202 send(origin, st.error_reply(stanza, "cancel", "service-unavailable")); | |
203 end | |
204 end | |
205 end | |
206 else | |
207 -- Remote host | |
208 if host_session then | |
209 -- Send to session | |
210 else | |
211 -- Need to establish the connection | |
212 end | |
213 end | |
214 stanza.attr.to = to; -- reset | |
215 end | |
216 | |
217 function handle_stanza_toremote(stanza) | |
218 log("error", "Stanza bound for remote host, but s2s is not implemented"); | |
219 end | |
220 ======= | |
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) | 96 function is_authorized_to_see_presence(origin, username, host) |
318 local roster = datamanager.load(username, host, "roster") or {}; | 97 local roster = datamanager.load(username, host, "roster") or {}; |
319 local item = roster[origin.username.."@"..origin.host]; | 98 local item = roster[origin.username.."@"..origin.host]; |
320 return item and (item.subscription == "both" or item.subscription == "from"); | 99 return item and (item.subscription == "both" or item.subscription == "from"); |
321 end | 100 end |
405 end | 184 end |
406 end | 185 end |
407 end | 186 end |
408 else | 187 else |
409 -- Remote host | 188 -- Remote host |
410 send_s2s(origin.host, host, stanza); | 189 if host_session then |
190 -- Send to session | |
191 else | |
192 -- Need to establish the connection | |
193 end | |
411 end | 194 end |
412 stanza.attr.to = to; -- reset | 195 stanza.attr.to = to; -- reset |
413 end | 196 end |
414 | 197 |
415 function handle_stanza_toremote(stanza) | 198 function handle_stanza_toremote(stanza) |
416 log("error", "Stanza bound for remote host, but s2s is not implemented"); | 199 log("error", "Stanza bound for remote host, but s2s is not implemented"); |
417 end | 200 end |
418 >>>>>>> other |