Software /
code /
prosody
Annotate
core/stanza_router.lua @ 105:b099f0f80775
Removed an unnecessary line
author | Waqas Hussain <waqas20@gmail.com> |
---|---|
date | Tue, 14 Oct 2008 09:56:14 +0500 |
parent | 83:79608fc8f98d |
child | 106:f2a3d204a76a |
rev | line source |
---|---|
30 | 1 |
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) | |
5 | |
6 require "core.servermanager" | |
7 | |
8 local log = require "util.logger".init("stanzarouter") | |
9 | |
71
fbb4ef1da82e
Added: Local stanza routing
Waqas Hussain <waqas20@gmail.com>
parents:
68
diff
changeset
|
10 local st = require "util.stanza"; |
fbb4ef1da82e
Added: Local stanza routing
Waqas Hussain <waqas20@gmail.com>
parents:
68
diff
changeset
|
11 local send = require "core.sessionmanager".send_to_session; |
fbb4ef1da82e
Added: Local stanza routing
Waqas Hussain <waqas20@gmail.com>
parents:
68
diff
changeset
|
12 |
105
b099f0f80775
Removed an unnecessary line
Waqas Hussain <waqas20@gmail.com>
parents:
83
diff
changeset
|
13 local jid_split = require "util.jid".split; |
30 | 14 |
15 function core_process_stanza(origin, stanza) | |
66 | 16 log("debug", "Received: "..tostring(stanza)) |
73 | 17 -- TODO verify validity of stanza (as well as JID validity) |
83
79608fc8f98d
Fixed routing for IQs to bare JIDs, and added a simple IQ validity check
Waqas Hussain <waqas20@gmail.com>
parents:
78
diff
changeset
|
18 if stanza.name == "iq" and not(#stanza.tags == 1 and stanza.tags[1].attr.xmlns) then |
79608fc8f98d
Fixed routing for IQs to bare JIDs, and added a simple IQ validity check
Waqas Hussain <waqas20@gmail.com>
parents:
78
diff
changeset
|
19 error("Invalid IQ"); |
79608fc8f98d
Fixed routing for IQs to bare JIDs, and added a simple IQ validity check
Waqas Hussain <waqas20@gmail.com>
parents:
78
diff
changeset
|
20 end |
78
972e31cc91e8
Fized: Added check to ensure that resource binding is done after auth.
Waqas Hussain <waqas20@gmail.com>
parents:
73
diff
changeset
|
21 |
972e31cc91e8
Fized: Added check to ensure that resource binding is done after auth.
Waqas Hussain <waqas20@gmail.com>
parents:
73
diff
changeset
|
22 if origin.type == "c2s" and not origin.full_jid |
83
79608fc8f98d
Fixed routing for IQs to bare JIDs, and added a simple IQ validity check
Waqas Hussain <waqas20@gmail.com>
parents:
78
diff
changeset
|
23 and not(stanza.name == "iq" and stanza.tags[1].name == "bind" |
78
972e31cc91e8
Fized: Added check to ensure that resource binding is done after auth.
Waqas Hussain <waqas20@gmail.com>
parents:
73
diff
changeset
|
24 and stanza.tags[1].attr.xmlns == "urn:ietf:params:xml:ns:xmpp-bind") then |
972e31cc91e8
Fized: Added check to ensure that resource binding is done after auth.
Waqas Hussain <waqas20@gmail.com>
parents:
73
diff
changeset
|
25 error("Client MUST bind resource after auth"); |
972e31cc91e8
Fized: Added check to ensure that resource binding is done after auth.
Waqas Hussain <waqas20@gmail.com>
parents:
73
diff
changeset
|
26 end |
972e31cc91e8
Fized: Added check to ensure that resource binding is done after auth.
Waqas Hussain <waqas20@gmail.com>
parents:
73
diff
changeset
|
27 |
71
fbb4ef1da82e
Added: Local stanza routing
Waqas Hussain <waqas20@gmail.com>
parents:
68
diff
changeset
|
28 |
30 | 29 local to = stanza.attr.to; |
71
fbb4ef1da82e
Added: Local stanza routing
Waqas Hussain <waqas20@gmail.com>
parents:
68
diff
changeset
|
30 stanza.attr.from = origin.full_jid -- quick fix to prevent impersonation |
30 | 31 |
32 if not to or (hosts[to] and hosts[to].type == "local") then | |
33 core_handle_stanza(origin, stanza); | |
83
79608fc8f98d
Fixed routing for IQs to bare JIDs, and added a simple IQ validity check
Waqas Hussain <waqas20@gmail.com>
parents:
78
diff
changeset
|
34 elseif to and stanza.name == "iq" and not select(3, jid_split(to)) then |
79608fc8f98d
Fixed routing for IQs to bare JIDs, and added a simple IQ validity check
Waqas Hussain <waqas20@gmail.com>
parents:
78
diff
changeset
|
35 core_handle_stanza(origin, stanza); |
30 | 36 elseif origin.type == "c2s" then |
37 core_route_stanza(origin, stanza); | |
38 end | |
39 end | |
40 | |
41 function core_handle_stanza(origin, stanza) | |
42 -- Handlers | |
43 if origin.type == "c2s" or origin.type == "c2s_unauthed" then | |
44 local session = origin; | |
45 stanza.attr.from = session.full_jid; | |
46 | |
47 log("debug", "Routing stanza"); | |
48 -- Stanza has no to attribute | |
49 --local to_node, to_host, to_resource = jid_split(stanza.attr.to); | |
50 --if not to_host then error("Invalid destination JID: "..string.format("{ %q, %q, %q } == %q", to_node or "", to_host or "", to_resource or "", stanza.attr.to or "nil")); end | |
51 | |
52 -- Stanza is to this server, or a user on this server | |
53 log("debug", "Routing stanza to local"); | |
54 handle_stanza(session, stanza); | |
71
fbb4ef1da82e
Added: Local stanza routing
Waqas Hussain <waqas20@gmail.com>
parents:
68
diff
changeset
|
55 end |
30 | 56 end |
57 | |
58 function core_route_stanza(origin, stanza) | |
59 -- Hooks | |
68
ceb7a55676a4
Beginnings of real stanza routing
Matthew Wild <mwild1@gmail.com>
parents:
66
diff
changeset
|
60 --- ...later |
ceb7a55676a4
Beginnings of real stanza routing
Matthew Wild <mwild1@gmail.com>
parents:
66
diff
changeset
|
61 |
30 | 62 -- Deliver |
68
ceb7a55676a4
Beginnings of real stanza routing
Matthew Wild <mwild1@gmail.com>
parents:
66
diff
changeset
|
63 local node, host, resource = jid_split(stanza.attr.to); |
ceb7a55676a4
Beginnings of real stanza routing
Matthew Wild <mwild1@gmail.com>
parents:
66
diff
changeset
|
64 local host_session = hosts[host] |
ceb7a55676a4
Beginnings of real stanza routing
Matthew Wild <mwild1@gmail.com>
parents:
66
diff
changeset
|
65 if host_session and host_session.type == "local" then |
ceb7a55676a4
Beginnings of real stanza routing
Matthew Wild <mwild1@gmail.com>
parents:
66
diff
changeset
|
66 -- Local host |
71
fbb4ef1da82e
Added: Local stanza routing
Waqas Hussain <waqas20@gmail.com>
parents:
68
diff
changeset
|
67 local user = host_session.sessions[node]; |
fbb4ef1da82e
Added: Local stanza routing
Waqas Hussain <waqas20@gmail.com>
parents:
68
diff
changeset
|
68 if user then |
72 | 69 local res = user.sessions[resource]; |
71
fbb4ef1da82e
Added: Local stanza routing
Waqas Hussain <waqas20@gmail.com>
parents:
68
diff
changeset
|
70 -- TODO do something about presence broadcast |
fbb4ef1da82e
Added: Local stanza routing
Waqas Hussain <waqas20@gmail.com>
parents:
68
diff
changeset
|
71 if not res then |
fbb4ef1da82e
Added: Local stanza routing
Waqas Hussain <waqas20@gmail.com>
parents:
68
diff
changeset
|
72 -- if we get here, resource was not specified or was unavailable |
fbb4ef1da82e
Added: Local stanza routing
Waqas Hussain <waqas20@gmail.com>
parents:
68
diff
changeset
|
73 for k in pairs(user.sessions) do |
fbb4ef1da82e
Added: Local stanza routing
Waqas Hussain <waqas20@gmail.com>
parents:
68
diff
changeset
|
74 res = user.sessions[k]; |
fbb4ef1da82e
Added: Local stanza routing
Waqas Hussain <waqas20@gmail.com>
parents:
68
diff
changeset
|
75 break; |
fbb4ef1da82e
Added: Local stanza routing
Waqas Hussain <waqas20@gmail.com>
parents:
68
diff
changeset
|
76 end |
fbb4ef1da82e
Added: Local stanza routing
Waqas Hussain <waqas20@gmail.com>
parents:
68
diff
changeset
|
77 -- TODO find resource with greatest priority |
fbb4ef1da82e
Added: Local stanza routing
Waqas Hussain <waqas20@gmail.com>
parents:
68
diff
changeset
|
78 end |
fbb4ef1da82e
Added: Local stanza routing
Waqas Hussain <waqas20@gmail.com>
parents:
68
diff
changeset
|
79 stanza.attr.to = res.full_jid; |
fbb4ef1da82e
Added: Local stanza routing
Waqas Hussain <waqas20@gmail.com>
parents:
68
diff
changeset
|
80 send(res, stanza); -- Yay \o/ |
fbb4ef1da82e
Added: Local stanza routing
Waqas Hussain <waqas20@gmail.com>
parents:
68
diff
changeset
|
81 else |
fbb4ef1da82e
Added: Local stanza routing
Waqas Hussain <waqas20@gmail.com>
parents:
68
diff
changeset
|
82 -- user not found |
fbb4ef1da82e
Added: Local stanza routing
Waqas Hussain <waqas20@gmail.com>
parents:
68
diff
changeset
|
83 send(origin, st.error_reply(stanza, "cancel", "service-unavailable")); |
fbb4ef1da82e
Added: Local stanza routing
Waqas Hussain <waqas20@gmail.com>
parents:
68
diff
changeset
|
84 end |
68
ceb7a55676a4
Beginnings of real stanza routing
Matthew Wild <mwild1@gmail.com>
parents:
66
diff
changeset
|
85 else |
ceb7a55676a4
Beginnings of real stanza routing
Matthew Wild <mwild1@gmail.com>
parents:
66
diff
changeset
|
86 -- Remote host |
ceb7a55676a4
Beginnings of real stanza routing
Matthew Wild <mwild1@gmail.com>
parents:
66
diff
changeset
|
87 if host_session then |
ceb7a55676a4
Beginnings of real stanza routing
Matthew Wild <mwild1@gmail.com>
parents:
66
diff
changeset
|
88 -- Send to session |
ceb7a55676a4
Beginnings of real stanza routing
Matthew Wild <mwild1@gmail.com>
parents:
66
diff
changeset
|
89 else |
ceb7a55676a4
Beginnings of real stanza routing
Matthew Wild <mwild1@gmail.com>
parents:
66
diff
changeset
|
90 -- Need to establish the connection |
ceb7a55676a4
Beginnings of real stanza routing
Matthew Wild <mwild1@gmail.com>
parents:
66
diff
changeset
|
91 end |
ceb7a55676a4
Beginnings of real stanza routing
Matthew Wild <mwild1@gmail.com>
parents:
66
diff
changeset
|
92 end |
30 | 93 end |
94 | |
95 function handle_stanza_nodest(stanza) | |
96 if stanza.name == "iq" then | |
97 handle_stanza_iq_no_to(session, stanza); | |
98 elseif stanza.name == "presence" then | |
99 -- Broadcast to this user's contacts | |
100 handle_stanza_presence_broadcast(session, stanza); | |
101 -- also, if it is initial presence, send out presence probes | |
102 if not session.last_presence then | |
103 handle_stanza_presence_probe_broadcast(session, stanza); | |
104 end | |
105 session.last_presence = stanza; | |
106 elseif stanza.name == "message" then | |
107 -- Treat as if message was sent to bare JID of the sender | |
108 handle_stanza_to_local_user(stanza); | |
109 end | |
110 end | |
111 | |
112 function handle_stanza_tolocal(stanza) | |
113 local node, host, resource = jid.split(stanza.attr.to); | |
114 if host and hosts[host] and hosts[host].type == "local" then | |
115 -- Is a local host, handle internally | |
116 if node then | |
117 -- Is a local user, send to their session | |
118 log("debug", "Routing stanza to %s@%s", node, host); | |
119 if not session.username then return; end --FIXME: Correct response when trying to use unauthed stream is what? | |
120 handle_stanza_to_local_user(stanza); | |
121 else | |
122 -- Is sent to this server, let's handle it... | |
123 log("debug", "Routing stanza to %s", host); | |
124 handle_stanza_to_server(stanza, session); | |
125 end | |
126 end | |
127 end | |
128 | |
129 function handle_stanza_toremote(stanza) | |
130 log("error", "Stanza bound for remote host, but s2s is not implemented"); | |
131 end | |
132 | |
133 | |
134 --[[ | |
135 local function route_c2s_stanza(session, stanza) | |
136 stanza.attr.from = session.full_jid; | |
137 if not stanza.attr.to and session.username then | |
138 -- Has no 'to' attribute, handle internally | |
139 if stanza.name == "iq" then | |
140 handle_stanza_iq_no_to(session, stanza); | |
141 elseif stanza.name == "presence" then | |
142 -- Broadcast to this user's contacts | |
143 handle_stanza_presence_broadcast(session, stanza); | |
144 -- also, if it is initial presence, send out presence probes | |
145 if not session.last_presence then | |
146 handle_stanza_presence_probe_broadcast(session, stanza); | |
147 end | |
148 session.last_presence = stanza; | |
149 elseif stanza.name == "message" then | |
150 -- Treat as if message was sent to bare JID of the sender | |
151 handle_stanza_to_local_user(stanza); | |
152 end | |
153 end | |
154 local node, host, resource = jid.split(stanza.attr.to); | |
155 if host and hosts[host] and hosts[host].type == "local" then | |
156 -- Is a local host, handle internally | |
157 if node then | |
158 -- Is a local user, send to their session | |
159 if not session.username then return; end --FIXME: Correct response when trying to use unauthed stream is what? | |
160 handle_stanza_to_local_user(stanza); | |
161 else | |
162 -- Is sent to this server, let's handle it... | |
163 handle_stanza_to_server(stanza, session); | |
164 end | |
165 else | |
166 -- Is not for us or a local user, route accordingly | |
167 route_s2s_stanza(stanza); | |
168 end | |
169 end | |
170 | |
171 function handle_stanza_no_to(session, stanza) | |
172 if not stanza.attr.id then log("warn", "<iq> without id attribute is invalid"); end | |
173 local xmlns = (stanza.tags[1].attr and stanza.tags[1].attr.xmlns); | |
174 if stanza.attr.type == "get" or stanza.attr.type == "set" then | |
175 if iq_handlers[xmlns] then | |
176 if iq_handlers[xmlns](stanza) then return; end; -- If handler returns true, it handled it | |
177 end | |
178 -- Oh, handler didn't handle it. Need to send service-unavailable now. | |
179 log("warn", "Unhandled namespace: "..xmlns); | |
180 session:send(format("<iq type='error' id='%s'><error type='cancel'><service-unavailable/></error></iq>", stanza.attr.id)); | |
181 return; -- All done! | |
182 end | |
183 end | |
184 | |
185 function handle_stanza_to_local_user(stanza) | |
186 if stanza.name == "message" then | |
187 handle_stanza_message_to_local_user(stanza); | |
188 elseif stanza.name == "presence" then | |
189 handle_stanza_presence_to_local_user(stanza); | |
190 elseif stanza.name == "iq" then | |
191 handle_stanza_iq_to_local_user(stanza); | |
192 end | |
193 end | |
194 | |
195 function handle_stanza_message_to_local_user(stanza) | |
196 local node, host, resource = stanza.to.node, stanza.to.host, stanza.to.resource; | |
197 local destuser = hosts[host].sessions[node]; | |
198 if destuser then | |
199 if resource and destuser[resource] then | |
200 destuser[resource]:send(stanza); | |
201 else | |
202 -- Bare JID, or resource offline | |
203 local best_session; | |
204 for resource, session in pairs(destuser.sessions) do | |
205 if not best_session then best_session = session; | |
206 elseif session.priority >= best_session.priority and session.priority >= 0 then | |
207 best_session = session; | |
208 end | |
209 end | |
210 if not best_session then | |
211 offlinemessage.new(node, host, stanza); | |
212 else | |
213 print("resource '"..resource.."' was not online, have chosen to send to '"..best_session.username.."@"..best_session.host.."/"..best_session.resource.."'"); | |
214 destuser[best_session]:send(stanza); | |
215 end | |
216 end | |
217 else | |
218 -- User is offline | |
219 offlinemessage.new(node, host, stanza); | |
220 end | |
221 end | |
222 | |
223 function handle_stanza_presence_to_local_user(stanza) | |
224 local node, host, resource = stanza.to.node, stanza.to.host, stanza.to.resource; | |
225 local destuser = hosts[host].sessions[node]; | |
226 if destuser then | |
227 if resource then | |
228 if destuser[resource] then | |
229 destuser[resource]:send(stanza); | |
230 else | |
231 return; | |
232 end | |
233 else | |
234 -- Broadcast to all user's resources | |
235 for resource, session in pairs(destuser.sessions) do | |
236 session:send(stanza); | |
237 end | |
238 end | |
239 end | |
240 end | |
241 | |
242 function handle_stanza_iq_to_local_user(stanza) | |
243 | |
244 end | |
245 | |
246 function foo() | |
247 local node, host, resource = stanza.to.node, stanza.to.host, stanza.to.resource; | |
248 local destuser = hosts[host].sessions[node]; | |
249 if destuser and destuser.sessions then | |
250 -- User online | |
251 if resource and destuser.sessions[resource] then | |
252 stanza.to:send(stanza); | |
253 else | |
254 --User is online, but specified resource isn't (or no resource specified) | |
255 local best_session; | |
256 for resource, session in pairs(destuser.sessions) do | |
257 if not best_session then best_session = session; | |
258 elseif session.priority >= best_session.priority and session.priority >= 0 then | |
259 best_session = session; | |
260 end | |
261 end | |
262 if not best_session then | |
263 offlinemessage.new(node, host, stanza); | |
264 else | |
265 print("resource '"..resource.."' was not online, have chosen to send to '"..best_session.username.."@"..best_session.host.."/"..best_session.resource.."'"); | |
266 resource = best_session.resource; | |
267 end | |
268 end | |
269 if destuser.sessions[resource] == session then | |
270 log("warn", "core", "Attempt to send stanza to self, dropping..."); | |
271 else | |
272 print("...sending...", tostring(stanza)); | |
273 --destuser.sessions[resource].conn.write(tostring(data)); | |
274 print(" to conn ", destuser.sessions[resource].conn); | |
275 destuser.sessions[resource].conn.write(tostring(stanza)); | |
276 print("...sent") | |
277 end | |
278 elseif stanza.name == "message" then | |
279 print(" ...will be stored offline"); | |
280 offlinemessage.new(node, host, stanza); | |
281 elseif stanza.name == "iq" then | |
282 print(" ...is an iq"); | |
283 stanza.from:send(st.reply(stanza) | |
284 :tag("error", { type = "cancel" }) | |
285 :tag("service-unavailable", { xmlns = "urn:ietf:params:xml:ns:xmpp-stanzas" })); | |
286 end | |
287 end | |
288 | |
289 -- Broadcast a presence stanza to all of a user's contacts | |
290 function handle_stanza_presence_broadcast(session, stanza) | |
291 if session.roster then | |
292 local initial_presence = not session.last_presence; | |
293 session.last_presence = stanza; | |
294 | |
295 -- Broadcast presence and probes | |
296 local broadcast = st.presence({ from = session.full_jid, type = stanza.attr.type }); | |
297 | |
298 for child in stanza:childtags() do | |
299 broadcast:add_child(child); | |
300 end | |
301 for contact_jid in pairs(session.roster) do | |
302 broadcast.attr.to = contact_jid; | |
303 send_to(contact_jid, broadcast); | |
304 if initial_presence then | |
305 local node, host = jid.split(contact_jid); | |
306 if hosts[host] and hosts[host].type == "local" then | |
307 local contact = hosts[host].sessions[node] | |
308 if contact then | |
309 local pres = st.presence { to = session.full_jid }; | |
310 for resource, contact_session in pairs(contact.sessions) do | |
311 if contact_session.last_presence then | |
312 pres.tags = contact_session.last_presence.tags; | |
313 pres.attr.from = contact_session.full_jid; | |
314 send(pres); | |
315 end | |
316 end | |
317 end | |
318 --FIXME: Do we send unavailable if they are offline? | |
319 else | |
320 probe.attr.to = contact; | |
321 send_to(contact, probe); | |
322 end | |
323 end | |
324 end | |
325 | |
326 -- Probe for our contacts' presence | |
327 end | |
328 end | |
329 | |
330 -- Broadcast presence probes to all of a user's contacts | |
331 function handle_stanza_presence_probe_broadcast(session, stanza) | |
332 end | |
333 | |
334 -- | |
335 function handle_stanza_to_server(stanza) | |
336 end | |
337 | |
338 function handle_stanza_iq_no_to(session, stanza) | |
339 end | |
340 ]] |