Software / code / prosody
Comparison
core/stanza_router.lua @ 138:cb0dd442ca63
Making the best attempt out of a bad merge from waqas
| author | Matthew Wild <mwild1@gmail.com> |
|---|---|
| date | Thu, 23 Oct 2008 18:24:55 +0100 |
| parent | 137:9a05dfb35dd5 |
| parent | 127:93f3c6b94c75 |
| child | 139:8f17ba74823c |
comparison
equal
deleted
inserted
replaced
| 137:9a05dfb35dd5 | 138:cb0dd442ca63 |
|---|---|
| 1 <<<<<<< local | |
| 1 | 2 |
| 2 -- The code in this file should be self-explanatory, though the logic is horrible | 3 -- 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 -- for more info on that, see doc/stanza_routing.txt, which attempts to condense |
| 4 -- the rules from the RFCs (mainly 3921) | 5 -- the rules from the RFCs (mainly 3921) |
| 5 | 6 |
| 214 end | 215 end |
| 215 | 216 |
| 216 function handle_stanza_toremote(stanza) | 217 function handle_stanza_toremote(stanza) |
| 217 log("error", "Stanza bound for remote host, but s2s is not implemented"); | 218 log("error", "Stanza bound for remote host, but s2s is not implemented"); |
| 218 end | 219 end |
| 220 ======= | |
| 221 | |
| 222 -- The code in this file should be self-explanatory, though the logic is horrible | |
| 223 -- for more info on that, see doc/stanza_routing.txt, which attempts to condense | |
| 224 -- the rules from the RFCs (mainly 3921) | |
| 225 | |
| 226 require "core.servermanager" | |
| 227 | |
| 228 local log = require "util.logger".init("stanzarouter") | |
| 229 | |
| 230 local st = require "util.stanza"; | |
| 231 local send = require "core.sessionmanager".send_to_session; | |
| 232 -- local send_s2s = require "core.s2smanager".send_to_host; | |
| 233 local user_exists = require "core.usermanager".user_exists; | |
| 234 | |
| 235 local jid_split = require "util.jid".split; | |
| 236 local print = print; | |
| 237 | |
| 238 function core_process_stanza(origin, stanza) | |
| 239 log("debug", "Received: "..tostring(stanza)) | |
| 240 -- TODO verify validity of stanza (as well as JID validity) | |
| 241 if stanza.name == "iq" and not(#stanza.tags == 1 and stanza.tags[1].attr.xmlns) then | |
| 242 if stanza.attr.type == "set" or stanza.attr.type == "get" then | |
| 243 error("Invalid IQ"); | |
| 244 elseif #stanza.tags > 1 or not(stanza.attr.type == "error" or stanza.attr.type == "result") then | |
| 245 error("Invalid IQ"); | |
| 246 end | |
| 247 end | |
| 248 | |
| 249 if origin.type == "c2s" and not origin.full_jid | |
| 250 and not(stanza.name == "iq" and stanza.tags[1].name == "bind" | |
| 251 and stanza.tags[1].attr.xmlns == "urn:ietf:params:xml:ns:xmpp-bind") then | |
| 252 error("Client MUST bind resource after auth"); | |
| 253 end | |
| 254 | |
| 255 local to = stanza.attr.to; | |
| 256 stanza.attr.from = origin.full_jid; -- quick fix to prevent impersonation (FIXME this would be incorrect when the origin is not c2s) | |
| 257 -- TODO also, stazas should be returned to their original state before the function ends | |
| 258 | |
| 259 -- TODO presence subscriptions | |
| 260 if not to then | |
| 261 core_handle_stanza(origin, stanza); | |
| 262 elseif hosts[to] and hosts[to].type == "local" then | |
| 263 core_handle_stanza(origin, stanza); | |
| 264 elseif stanza.name == "iq" and not select(3, jid_split(to)) then | |
| 265 core_handle_stanza(origin, stanza); | |
| 266 elseif origin.type == "c2s" then | |
| 267 core_route_stanza(origin, stanza); | |
| 268 end | |
| 269 end | |
| 270 | |
| 271 -- This function handles stanzas which are not routed any further, | |
| 272 -- that is, they are handled by this server | |
| 273 function core_handle_stanza(origin, stanza) | |
| 274 -- Handlers | |
| 275 if origin.type == "c2s" or origin.type == "c2s_unauthed" then | |
| 276 local session = origin; | |
| 277 | |
| 278 if stanza.name == "presence" and origin.roster then | |
| 279 if stanza.attr.type == nil or stanza.attr.type == "available" or stanza.attr.type == "unavailable" then | |
| 280 for jid in pairs(origin.roster) do -- broadcast to all interested contacts | |
| 281 local subscription = origin.roster[jid].subscription; | |
| 282 if subscription == "both" or subscription == "from" then | |
| 283 stanza.attr.to = jid; | |
| 284 core_route_stanza(origin, stanza); | |
| 285 end | |
| 286 end | |
| 287 --[[local node, host = jid_split(stanza.attr.from); | |
| 288 for _, res in pairs(hosts[host].sessions[node].sessions) do -- broadcast to all resources | |
| 289 if res.full_jid then | |
| 290 res = user.sessions[k]; | |
| 291 break; | |
| 292 end | |
| 293 end]] | |
| 294 if not origin.presence then -- presence probes on initial presence | |
| 295 local probe = st.presence({from = origin.full_jid, type = "probe"}); | |
| 296 for jid in pairs(origin.roster) do | |
| 297 local subscription = origin.roster[jid].subscription; | |
| 298 if subscription == "both" or subscription == "to" then | |
| 299 probe.attr.to = jid; | |
| 300 core_route_stanza(origin, probe); | |
| 301 end | |
| 302 end | |
| 303 end | |
| 304 origin.presence = stanza; | |
| 305 stanza.attr.to = nil; -- reset it | |
| 306 else | |
| 307 -- TODO error, bad type | |
| 308 end | |
| 309 else | |
| 310 log("debug", "Routing stanza to local"); | |
| 311 handle_stanza(session, stanza); | |
| 312 end | |
| 313 end | |
| 314 end | |
| 315 | |
| 316 -- TODO: Does this function belong here? | |
| 317 function is_authorized_to_see_presence(origin, username, host) | |
| 318 local roster = datamanager.load(username, host, "roster") or {}; | |
| 319 local item = roster[origin.username.."@"..origin.host]; | |
| 320 return item and (item.subscription == "both" or item.subscription == "from"); | |
| 321 end | |
| 322 | |
| 323 function core_route_stanza(origin, stanza) | |
| 324 -- Hooks | |
| 325 --- ...later | |
| 326 | |
| 327 -- Deliver | |
| 328 local to = stanza.attr.to; | |
| 329 local node, host, resource = jid_split(to); | |
| 330 | |
| 331 if stanza.name == "presence" and stanza.attr.type == "probe" then resource = nil; end | |
| 332 | |
| 333 local host_session = hosts[host] | |
| 334 if host_session and host_session.type == "local" then | |
| 335 -- Local host | |
| 336 local user = host_session.sessions[node]; | |
| 337 if user then | |
| 338 local res = user.sessions[resource]; | |
| 339 if not res then | |
| 340 -- if we get here, resource was not specified or was unavailable | |
| 341 if stanza.name == "presence" then | |
| 342 if stanza.attr.type == "probe" then | |
| 343 if is_authorized_to_see_presence(origin, node, host) then | |
| 344 for k in pairs(user.sessions) do -- return presence for all resources | |
| 345 if user.sessions[k].presence then | |
| 346 local pres = user.sessions[k].presence; | |
| 347 pres.attr.to = origin.full_jid; | |
| 348 pres.attr.from = user.sessions[k].full_jid; | |
| 349 send(origin, pres); | |
| 350 pres.attr.to = nil; | |
| 351 pres.attr.from = nil; | |
| 352 end | |
| 353 end | |
| 354 else | |
| 355 send(origin, st.presence({from = user.."@"..host, to = origin.username.."@"..origin.host, type = "unsubscribed"})); | |
| 356 end | |
| 357 else | |
| 358 for k in pairs(user.sessions) do -- presence broadcast to all user resources | |
| 359 if user.sessions[k].full_jid then | |
| 360 stanza.attr.to = user.sessions[k].full_jid; | |
| 361 send(user.sessions[k], stanza); | |
| 362 end | |
| 363 end | |
| 364 end | |
| 365 elseif stanza.name == "message" then -- select a resource to recieve message | |
| 366 for k in pairs(user.sessions) do | |
| 367 if user.sessions[k].full_jid then | |
| 368 res = user.sessions[k]; | |
| 369 break; | |
| 370 end | |
| 371 end | |
| 372 -- TODO find resource with greatest priority | |
| 373 send(res, stanza); | |
| 374 else | |
| 375 -- TODO send IQ error | |
| 376 end | |
| 377 else | |
| 378 -- User + resource is online... | |
| 379 stanza.attr.to = res.full_jid; | |
| 380 send(res, stanza); -- Yay \o/ | |
| 381 end | |
| 382 else | |
| 383 -- user not online | |
| 384 if user_exists(node, host) then | |
| 385 if stanza.name == "presence" then | |
| 386 if stanza.attr.type == "probe" and is_authorized_to_see_presence(origin, node, host) then -- FIXME what to do for not c2s? | |
| 387 -- TODO send last recieved unavailable presence | |
| 388 else | |
| 389 -- TODO send unavailable presence | |
| 390 end | |
| 391 elseif stanza.name == "message" then | |
| 392 -- TODO send message error, or store offline messages | |
| 393 elseif stanza.name == "iq" then | |
| 394 -- TODO send IQ error | |
| 395 end | |
| 396 else -- user does not exist | |
| 397 -- TODO we would get here for nodeless JIDs too. Do something fun maybe? Echo service? Let plugins use xmpp:server/resource addresses? | |
| 398 if stanza.name == "presence" then | |
| 399 if stanza.attr.type == "probe" then | |
| 400 send(origin, st.presence({from = user.."@"..host, to = origin.username.."@"..origin.host, type = "unsubscribed"})); | |
| 401 end | |
| 402 -- else ignore | |
| 403 else | |
| 404 send(origin, st.error_reply(stanza, "cancel", "service-unavailable")); | |
| 405 end | |
| 406 end | |
| 407 end | |
| 408 else | |
| 409 -- Remote host | |
| 410 send_s2s(origin.host, host, stanza); | |
| 411 end | |
| 412 stanza.attr.to = to; -- reset | |
| 413 end | |
| 414 | |
| 415 function handle_stanza_toremote(stanza) | |
| 416 log("error", "Stanza bound for remote host, but s2s is not implemented"); | |
| 417 end | |
| 418 >>>>>>> other |