Mercurial > prosody-hg
comparison plugins/muc/muc.lib.lua @ 7410:45f543c82893
MUC: Split out handling of the room-creating presence into its own method
| author | Kim Alvefur <zash@zash.se> |
|---|---|
| date | Thu, 28 Apr 2016 22:57:02 +0200 |
| parents | 9a3ce6da3256 |
| children | f385cd6127b2 |
comparison
equal
deleted
inserted
replaced
| 7409:9a3ce6da3256 | 7410:45f543c82893 |
|---|---|
| 382 event.origin.send(reply:tag("x", {xmlns = "http://jabber.org/protocol/muc"})); | 382 event.origin.send(reply:tag("x", {xmlns = "http://jabber.org/protocol/muc"})); |
| 383 return true; | 383 return true; |
| 384 end | 384 end |
| 385 end, -10); | 385 end, -10); |
| 386 | 386 |
| 387 function room_mt:handle_first_presence(origin, stanza) | |
| 388 local real_jid = stanza.attr.from; | |
| 389 local dest_jid = stanza.attr.to; | |
| 390 local bare_jid = jid_bare(real_jid); | |
| 391 if module:fire_event("muc-room-pre-create", { | |
| 392 room = self; | |
| 393 origin = origin; | |
| 394 stanza = stanza; | |
| 395 }) then return true; end | |
| 396 local is_first_dest_session = true; | |
| 397 local dest_occupant = self:new_occupant(bare_jid, dest_jid); | |
| 398 | |
| 399 -- TODO Handle this case sensibly | |
| 400 if not stanza:get_child("x", "http://jabber.org/protocol/muc") then | |
| 401 module:log("debug", "Room creation without <x>, possibly desynced"); | |
| 402 end | |
| 403 | |
| 404 if module:fire_event("muc-occupant-pre-join", { | |
| 405 room = self; | |
| 406 origin = origin; | |
| 407 stanza = stanza; | |
| 408 is_first_session = is_first_dest_session; | |
| 409 is_new_room = true; | |
| 410 occupant = dest_occupant; | |
| 411 }) then return true; end | |
| 412 | |
| 413 dest_occupant:set_session(real_jid, stanza); | |
| 414 local dest_x = st.stanza("x", {xmlns = "http://jabber.org/protocol/muc#user";}); | |
| 415 dest_x:tag("status", {code = "201"}):up(); | |
| 416 if self:get_whois() == "anyone" then | |
| 417 dest_x:tag("status", {code = "100"}):up(); | |
| 418 end | |
| 419 self:save_occupant(dest_occupant); | |
| 420 | |
| 421 self:publicise_occupant_status(dest_occupant, dest_x); | |
| 422 | |
| 423 module:fire_event("muc-occupant-joined", { | |
| 424 room = self; | |
| 425 nick = dest_occupant.nick; | |
| 426 occupant = dest_occupant; | |
| 427 stanza = stanza; | |
| 428 origin = origin; | |
| 429 }); | |
| 430 module:fire_event("muc-occupant-session-new", { | |
| 431 room = self; | |
| 432 nick = dest_occupant.nick; | |
| 433 occupant = dest_occupant; | |
| 434 stanza = stanza; | |
| 435 origin = origin; | |
| 436 jid = real_jid; | |
| 437 }); | |
| 438 return true; | |
| 439 end | |
| 440 | |
| 387 function room_mt:handle_normal_presence(origin, stanza) | 441 function room_mt:handle_normal_presence(origin, stanza) |
| 388 local type = stanza.attr.type; | 442 local type = stanza.attr.type; |
| 389 local real_jid = stanza.attr.from; | 443 local real_jid = stanza.attr.from; |
| 390 local bare_jid = jid_bare(real_jid); | 444 local bare_jid = jid_bare(real_jid); |
| 391 local orig_occupant, dest_occupant; | 445 local orig_occupant = self:get_occupant_by_real_jid(real_jid); |
| 392 local is_new_room = next(self._affiliations) == nil; | 446 if type == "unavailable" and orig_occupant == nil then return true; end -- Unavailable from someone not in the room |
| 393 if is_new_room then | |
| 394 if type == "unavailable" then return true; end -- Unavailable from someone not in the room | |
| 395 if module:fire_event("muc-room-pre-create", { | |
| 396 room = self; | |
| 397 origin = origin; | |
| 398 stanza = stanza; | |
| 399 }) then return true; end | |
| 400 else | |
| 401 orig_occupant = self:get_occupant_by_real_jid(real_jid); | |
| 402 if type == "unavailable" and orig_occupant == nil then return true; end -- Unavailable from someone not in the room | |
| 403 end | |
| 404 local is_first_dest_session; | 447 local is_first_dest_session; |
| 448 local dest_occupant; | |
| 405 if type == "unavailable" then -- luacheck: ignore 542 | 449 if type == "unavailable" then -- luacheck: ignore 542 |
| 406 -- FIXME Why the empty if branch? | 450 -- FIXME Why the empty if branch? |
| 407 -- dest_occupant = nil | 451 -- dest_occupant = nil |
| 408 elseif orig_occupant and orig_occupant.nick == stanza.attr.to then -- Just a presence update | 452 elseif orig_occupant and orig_occupant.nick == stanza.attr.to then -- Just a presence update |
| 409 log("debug", "presence update for %s from session %s", orig_occupant.nick, real_jid); | 453 log("debug", "presence update for %s from session %s", orig_occupant.nick, real_jid); |
| 441 is_first_session = is_first_dest_session; | 485 is_first_session = is_first_dest_session; |
| 442 is_last_session = is_last_orig_session; | 486 is_last_session = is_last_orig_session; |
| 443 }; | 487 }; |
| 444 if orig_occupant == nil then | 488 if orig_occupant == nil then |
| 445 event_name = "muc-occupant-pre-join"; | 489 event_name = "muc-occupant-pre-join"; |
| 446 event.is_new_room = is_new_room; | |
| 447 event.occupant = dest_occupant; | 490 event.occupant = dest_occupant; |
| 448 elseif dest_occupant == nil then | 491 elseif dest_occupant == nil then |
| 449 event_name = "muc-occupant-pre-leave"; | 492 event_name = "muc-occupant-pre-leave"; |
| 450 event.occupant = orig_occupant; | 493 event.occupant = orig_occupant; |
| 451 else | 494 else |
| 521 end | 564 end |
| 522 | 565 |
| 523 if dest_occupant ~= nil then | 566 if dest_occupant ~= nil then |
| 524 dest_occupant:set_session(real_jid, stanza); | 567 dest_occupant:set_session(real_jid, stanza); |
| 525 local dest_x = st.stanza("x", {xmlns = "http://jabber.org/protocol/muc#user";}); | 568 local dest_x = st.stanza("x", {xmlns = "http://jabber.org/protocol/muc#user";}); |
| 526 if is_new_room then | |
| 527 dest_x:tag("status", {code = "201"}):up(); | |
| 528 end | |
| 529 if orig_occupant == nil and self:get_whois() == "anyone" then | 569 if orig_occupant == nil and self:get_whois() == "anyone" then |
| 530 dest_x:tag("status", {code = "100"}):up(); | 570 dest_x:tag("status", {code = "100"}):up(); |
| 531 end | 571 end |
| 532 self:save_occupant(dest_occupant); | 572 self:save_occupant(dest_occupant); |
| 533 | 573 |
