Software /
code /
prosody
Comparison
plugins/muc/muc.lib.lua @ 6096:84f9123637d4
plugins/muc/muc.lib: Refactor _to_occupant handlers
author | daurnimator <quae@daurnimator.com> |
---|---|
date | Fri, 21 Feb 2014 16:30:43 -0500 |
parent | 6095:7900ebc544ce |
child | 6097:538cdc3d8225 |
comparison
equal
deleted
inserted
replaced
6095:7900ebc544ce | 6096:84f9123637d4 |
---|---|
601 end | 601 end |
602 end | 602 end |
603 return true; | 603 return true; |
604 end | 604 end |
605 | 605 |
606 function room_mt:handle_private(origin, stanza) | 606 function room_mt:handle_iq_to_occupant(origin, stanza) |
607 local from, to = stanza.attr.from, stanza.attr.to; | |
608 local type = stanza.attr.type; | |
609 local id = stanza.attr.id; | |
610 if (type == "error" or type == "result") then | |
611 stanza.attr.from, stanza.attr.to, stanza.attr.id = deconstruct_stanza_id(self, stanza); | |
612 log("debug", "%s sent private iq stanza to %s (%s)", from, to, stanza.attr.to); | |
613 if stanza.attr.id then | |
614 self:_route_stanza(stanza); | |
615 end | |
616 stanza.attr.from, stanza.attr.to, stanza.attr.id = from, to, id; | |
617 return true; | |
618 else -- Type is "get" or "set" | |
619 local current_nick = self._jid_nick[from]; | |
620 if not current_nick then | |
621 origin.send(st.error_reply(stanza, "cancel", "not-acceptable")); | |
622 return true; | |
623 end | |
624 local o_data = self._occupants[to]; | |
625 if not o_data then -- recipient not in room | |
626 origin.send(st.error_reply(stanza, "cancel", "item-not-found", "Recipient not in room")); | |
627 return true; | |
628 end | |
629 stanza.attr.from, stanza.attr.to, stanza.attr.id = construct_stanza_id(self, stanza); | |
630 log("debug", "%s sent private iq stanza to %s (%s)", from, to, o_data.jid); | |
631 if stanza.tags[1].attr.xmlns == 'vcard-temp' then | |
632 stanza.attr.to = jid_bare(stanza.attr.to); | |
633 end | |
634 if stanza.attr.id then | |
635 self:_route_stanza(stanza); | |
636 end | |
637 stanza.attr.from, stanza.attr.to, stanza.attr.id = from, to, id; | |
638 return true; | |
639 end | |
640 end | |
641 | |
642 function room_mt:handle_message_to_occupant(origin, stanza) | |
607 local from, to = stanza.attr.from, stanza.attr.to; | 643 local from, to = stanza.attr.from, stanza.attr.to; |
608 local current_nick = self._jid_nick[from]; | 644 local current_nick = self._jid_nick[from]; |
609 local type = stanza.attr.type; | |
610 local o_data = self._occupants[to]; | |
611 if o_data then | |
612 log("debug", "%s sent private stanza to %s (%s)", from, to, o_data.jid); | |
613 if stanza.name == "iq" then | |
614 local id = stanza.attr.id; | |
615 if stanza.attr.type == "get" or stanza.attr.type == "set" then | |
616 stanza.attr.from, stanza.attr.to, stanza.attr.id = construct_stanza_id(self, stanza); | |
617 else | |
618 stanza.attr.from, stanza.attr.to, stanza.attr.id = deconstruct_stanza_id(self, stanza); | |
619 end | |
620 if type == 'get' and stanza.tags[1].attr.xmlns == 'vcard-temp' then | |
621 stanza.attr.to = jid_bare(stanza.attr.to); | |
622 end | |
623 if stanza.attr.id then | |
624 self:_route_stanza(stanza); | |
625 end | |
626 stanza.attr.from, stanza.attr.to, stanza.attr.id = from, to, id; | |
627 else -- message | |
628 stanza:tag("x", { xmlns = "http://jabber.org/protocol/muc#user" }):up(); | |
629 stanza.attr.from = current_nick; | |
630 for jid in pairs(o_data.sessions) do | |
631 stanza.attr.to = jid; | |
632 self:_route_stanza(stanza); | |
633 end | |
634 stanza.attr.from, stanza.attr.to = from, to; | |
635 end | |
636 elseif type ~= "error" and type ~= "result" then -- recipient not in room | |
637 origin.send(st.error_reply(stanza, "cancel", "item-not-found", "Recipient not in room")); | |
638 end | |
639 return true; | |
640 end | |
641 | |
642 function room_mt:handle_iq_to_occupant(origin, stanza) | |
643 local from, to = stanza.attr.from, stanza.attr.to; | |
644 local current_nick = self._jid_nick[from]; | |
645 if not current_nick then | |
646 local type = stanza.attr.type; | |
647 if (type == "error" or type == "result") then | |
648 local id = stanza.attr.id; | |
649 stanza.attr.from, stanza.attr.to, stanza.attr.id = deconstruct_stanza_id(self, stanza); | |
650 if stanza.attr.id then | |
651 self:_route_stanza(stanza); | |
652 end | |
653 stanza.attr.from, stanza.attr.to, stanza.attr.id = from, to, id; | |
654 else | |
655 origin.send(st.error_reply(stanza, "cancel", "not-acceptable")); | |
656 end | |
657 return true; | |
658 else | |
659 return self:handle_private(origin, stanza) | |
660 end | |
661 end | |
662 | |
663 function room_mt:handle_message_to_occupant(origin, stanza) | |
664 local current_nick = self._jid_nick[stanza.attr.from]; | |
665 local type = stanza.attr.type; | 645 local type = stanza.attr.type; |
666 if not current_nick then -- not in room | 646 if not current_nick then -- not in room |
667 if type ~= "error" then | 647 if type ~= "error" then |
668 origin.send(st.error_reply(stanza, "cancel", "not-acceptable")); | 648 origin.send(st.error_reply(stanza, "cancel", "not-acceptable")); |
669 end | 649 end |
672 if type == "groupchat" then -- groupchat messages not allowed in PM | 652 if type == "groupchat" then -- groupchat messages not allowed in PM |
673 origin.send(st.error_reply(stanza, "modify", "bad-request")); | 653 origin.send(st.error_reply(stanza, "modify", "bad-request")); |
674 return true; | 654 return true; |
675 elseif type == "error" and is_kickable_error(stanza) then | 655 elseif type == "error" and is_kickable_error(stanza) then |
676 log("debug", "%s kicked from %s for sending an error message", current_nick, self.jid); | 656 log("debug", "%s kicked from %s for sending an error message", current_nick, self.jid); |
677 return self:handle_to_occupant(origin, build_unavailable_presence_from_error(stanza)); -- send unavailable | 657 self:handle_to_occupant(origin, build_unavailable_presence_from_error(stanza)); -- send unavailable |
678 else -- private stanza | 658 return true; |
679 return self:handle_private(origin, stanza) | 659 end |
680 end | 660 |
661 local o_data = self._occupants[to]; | |
662 if not o_data then | |
663 origin.send(st.error_reply(stanza, "cancel", "item-not-found", "Recipient not in room")); | |
664 return true; | |
665 end | |
666 log("debug", "%s sent private message stanza to %s (%s)", from, to, o_data.jid); | |
667 stanza:tag("x", { xmlns = "http://jabber.org/protocol/muc#user" }):up(); | |
668 stanza.attr.from = current_nick; | |
669 for jid in pairs(o_data.sessions) do | |
670 stanza.attr.to = jid; | |
671 self:_route_stanza(stanza); | |
672 end | |
673 stanza.attr.from, stanza.attr.to = from, to; | |
674 return true; | |
681 end | 675 end |
682 | 676 |
683 function room_mt:handle_to_occupant(origin, stanza) -- PM, vCards, etc | 677 function room_mt:handle_to_occupant(origin, stanza) -- PM, vCards, etc |
684 local from, to = stanza.attr.from, stanza.attr.to; | 678 local from, to = stanza.attr.from, stanza.attr.to; |
685 local room = jid_bare(to); | 679 local room = jid_bare(to); |
686 local current_nick = self._jid_nick[from]; | 680 local current_nick = self._jid_nick[from]; |
687 local type = stanza.attr.type; | |
688 log("debug", "room: %s, current_nick: %s, stanza: %s", room or "nil", current_nick or "nil", stanza:top_tag()); | 681 log("debug", "room: %s, current_nick: %s, stanza: %s", room or "nil", current_nick or "nil", stanza:top_tag()); |
689 if (select(2, jid_split(from)) == muc_domain) then error("Presence from the MUC itself!!!"); end | 682 if (select(2, jid_split(from)) == muc_domain) then error("Presence from the MUC itself!!!"); end |
690 if stanza.name == "presence" then | 683 if stanza.name == "presence" then |
691 return self:handle_presence_to_occupant(origin, stanza) | 684 return self:handle_presence_to_occupant(origin, stanza) |
692 elseif stanza.name == "iq" then | 685 elseif stanza.name == "iq" then |