changeset 2844:fa1cc09df195

plugin XEP-0045: added getRoomOccupants/mucOccupantsGet method to retrieve occupants of a joined room.
author Goffi <goffi@goffi.org>
date Sat, 09 Mar 2019 16:24:17 +0100
parents a425c1ca51d0
children 42380a4f6433
files sat/plugins/plugin_xep_0045.py
diffstat 1 files changed, 21 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/sat/plugins/plugin_xep_0045.py	Sat Mar 09 16:24:15 2019 +0100
+++ b/sat/plugins/plugin_xep_0045.py	Sat Mar 09 16:24:17 2019 +0100
@@ -91,6 +91,7 @@
         host.bridge.addMethod("mucNick", ".plugin", in_sign='sss', out_sign='', method=self._nick)
         host.bridge.addMethod("mucNickGet", ".plugin", in_sign='ss', out_sign='s', method=self._getRoomNick)
         host.bridge.addMethod("mucLeave", ".plugin", in_sign='ss', out_sign='', method=self._leave, async=True)
+        host.bridge.addMethod("mucOccupantsGet", ".plugin", in_sign='ss', out_sign='a{sa{ss}}', method=self._getRoomOccupants)
         host.bridge.addMethod("mucSubject", ".plugin", in_sign='sss', out_sign='', method=self._subject)
         host.bridge.addMethod("mucGetRoomsJoined", ".plugin", in_sign='s', out_sign='a(sa{sa{ss}}ss)', method=self._getRoomsJoined)
         host.bridge.addMethod("mucGetUniqueRoomName", ".plugin", in_sign='ss', out_sign='s', method=self._getUniqueName)
@@ -164,6 +165,17 @@
                 return False
         return True
 
+    def getRoom(self, client, room_jid):
+        """Retrieve Room instance from its jid
+
+        @param room_jid(jid.JID): jid of the room
+        @raise exceptions.NotFound: the room has not been joined
+        """
+        try:
+            return client._muc_client.joined_rooms[room_jid]
+        except KeyError:
+            raise exceptions.NotFound(_(u"This room has not been joined"))
+
     def checkRoomJoined(self, client, room_jid):
         """Check that given room has been joined in current session
 
@@ -261,6 +273,15 @@
         """Get occupants of a room in a form suitable for bridge"""
         return {u.nick: {k:unicode(getattr(u,k) or '') for k in OCCUPANT_KEYS} for u in room.roster.values()}
 
+    def _getRoomOccupants(self, room_jid_s, profile_key):
+        client = self.host.getClient(profile_key)
+        room_jid = jid.JID(room_jid_s)
+        return self.getRoomOccupants(client, room_jid)
+
+    def getRoomOccupants(self, client, room_jid):
+        room = self.getRoom(client, room_jid)
+        return self._getOccupants(room)
+
     def _getRoomsJoined(self, profile_key=C.PROF_KEY_NONE):
         client = self.host.getClient(profile_key)
         return self.getRoomsJoined(client)