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