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