From f24a7679e571fb376c5889d9344bc1e5df537623 Mon Sep 17 00:00:00 2001 From: jinyu <96060102+jinyu2022@users.noreply.github.com> Date: Fri, 15 Nov 2024 17:53:08 +0800 Subject: [PATCH] enhancement: Improve JID handling and logging in MUC plugin; update status code return type --- slixmpp/__init__.py | 6 ++++++ slixmpp/plugins/xep_0045/muc.py | 28 ++++++++++++++++++++++++---- slixmpp/plugins/xep_0045/stanza.py | 8 +++++--- 3 files changed, 35 insertions(+), 7 deletions(-) diff --git a/slixmpp/__init__.py b/slixmpp/__init__.py index 5841cdf7..9b79c7c0 100644 --- a/slixmpp/__init__.py +++ b/slixmpp/__init__.py @@ -27,3 +27,9 @@ from slixmpp.clientxmpp import ClientXMPP from slixmpp.componentxmpp import ComponentXMPP from slixmpp.version import __version__, __version_info__ + +__all__ = [ + 'Message', 'Presence', 'Iq', 'JID', 'InvalidJID', 'ET', 'ElementBase', + 'register_stanza_plugin', 'XMLStream', 'BaseXMPP', 'ClientXMPP', 'ComponentXMPP', + '__version__', '__version_info__' +] diff --git a/slixmpp/plugins/xep_0045/muc.py b/slixmpp/plugins/xep_0045/muc.py index ea0e06a5..dd113dc5 100644 --- a/slixmpp/plugins/xep_0045/muc.py +++ b/slixmpp/plugins/xep_0045/muc.py @@ -650,13 +650,23 @@ class XEP_0045(BasePlugin): :param room: Room to check. :param jid: FULL JID to check. """ + bare_match = False for nick in self.rooms[room]: entry = self.rooms[room][nick] if not entry.get('jid'): continue - if entry is not None and entry['jid'] == jid.full: + + if entry['jid'] == jid.full: return True - return False + elif JID(entry['jid']) == jid.bare: + bare_match = True + + if bare_match: + logging.info( + "Could not retrieve full JID, falling back to bare JID for %s in %s", + jid, room + ) + return bare_match def get_nick(self, room: JID, jid: JID) -> Optional[str]: """Get the nickname of a specific JID in a room. @@ -664,13 +674,23 @@ class XEP_0045(BasePlugin): :param room: Room to inspect. :param jid: FULL JID whose nick to return. """ + bare_match = None for nick in self.rooms[room]: entry = self.rooms[room][nick] if not entry.get('jid'): continue - if entry is not None and entry['jid'] == jid.full: + + if entry['jid'] == jid.full: return nick - return None + elif JID(entry['jid']) == jid.bare: + bare_match = nick + + if bare_match: + logging.info( + "Could not retrieve full JID, falling back to bare JID for %s in %s", + jid, room + ) + return bare_match def get_joined_rooms(self) -> List[JID]: """Get the list of rooms we sent a join presence to diff --git a/slixmpp/plugins/xep_0045/stanza.py b/slixmpp/plugins/xep_0045/stanza.py index 6c3d2244..832019e4 100644 --- a/slixmpp/plugins/xep_0045/stanza.py +++ b/slixmpp/plugins/xep_0045/stanza.py @@ -28,7 +28,7 @@ class MUCBase(ElementBase): plugin_attrib = 'muc' interfaces = {'affiliation', 'role', 'jid', 'nick', 'room', 'status_codes'} - def get_status_codes(self) -> Set[str]: + def get_status_codes(self) -> Set[int]: status = self.xml.findall(f'{{{NS_USER}}}status') return {int(status.attrib['code']) for status in status} @@ -275,7 +275,8 @@ class MUCUserItem(ElementBase): jid = self.xml.attrib.get('jid', None) if jid: return JID(jid) - return jid + else: + return None class MUCActor(ElementBase): @@ -288,7 +289,8 @@ class MUCActor(ElementBase): jid = self.xml.attrib.get('jid', None) if jid: return JID(jid) - return jid + else: + return None class MUCDestroy(ElementBase):