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