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