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