changeset 1990:dfbe0bb056dc

plugin XEP-0045: added /list text command: /list display a basic (for now) listing of available rooms in current or specified MUC service
author Goffi <goffi@goffi.org>
date Fri, 01 Jul 2016 00:00:36 +0200
parents 757c512fe06c
children dbe025b03eba
files src/plugins/plugin_xep_0045.py
diffstat 1 files changed, 47 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/src/plugins/plugin_xep_0045.py	Fri Jul 01 00:00:23 2016 +0200
+++ b/src/plugins/plugin_xep_0045.py	Fri Jul 01 00:00:36 2016 +0200
@@ -82,6 +82,7 @@
         host.bridge.addSignal("mucRoomUserChangedNick", ".plugin", signature='ssss')  # args: room_jid, old_nick, new_nick, profile
         host.bridge.addSignal("mucRoomNewSubject", ".plugin", signature='sss')  # args: room_jid, subject, profile
         self.__submit_conf_id = host.registerCallback(self._submitConfiguration, with_data=True)
+        self._room_join_id = host.registerCallback(self._UIRoomJoinCb, with_data=True)
         host.importMenu((D_("MUC"), D_("configure")), self._configureRoomMenu, security_limit=0, help_string=D_("Configure Multi-User Chat room"), type_=C.MENU_ROOM)
         try:
             self.txt_cmds = self.host.plugins[C.TEXT_CMDS]
@@ -137,6 +138,12 @@
         else:
             return True
 
+    def _UIRoomJoinCb(self, data, profile):
+        room_jid = jid.JID(data['index'])
+        client = self.host.getClient(profile)
+        self.join(client, room_jid)
+        return {}
+
     def _passwordUICb(self, data, client, room_jid, nick):
         """Called when the user has given room password (or cancelled)"""
         if C.bool(data.get(C.XMLUI_DATA_CANCELLED, "false")):
@@ -145,6 +152,16 @@
         password = data[xml_tools.formEscape('password')]
         return client._muc_client.join(room_jid, nick, password).addCallbacks(self._joinCb, self._joinEb, (client, room_jid, nick), errbackArgs=(client, room_jid, nick, password))
 
+    def _showListUI(self, items, client, service):
+        xmlui = xml_tools.XMLUI(title=D_('Rooms in {}'.format(service.full())))
+        adv_list = xmlui.changeContainer('advanced_list', columns=1, selectable='single', callback_id=self._room_join_id)
+        items = sorted(items, key=lambda i: i.name.lower())
+        for item in items:
+            adv_list.setRowIndex(item.entity.full())
+            xmlui.addText(item.name)
+        adv_list.end()
+        self.host.actionNew({'xmlui': xmlui.toXml()}, profile=client.profile)
+
     def _joinCb(self, room, client, room_jid, nick):
         """Called when the user is in the requested room"""
         if room.locked:
@@ -642,6 +659,36 @@
         """
         return self.cmd_title(client, mess_data)
 
+    def cmd_list(self, client, mess_data):
+        """list available rooms in a muc server
+
+        @command (all): [MUC_SERVICE]
+            - MUC_SERVICE: service to request
+               empty value will request room's service for a room,
+               or user's server default MUC service in a one2one chat
+        """
+        unparsed = mess_data["unparsed"].strip()
+        try:
+            service = jid.JID(unparsed)
+        except RuntimeError:
+            if mess_data['type'] == C.MESS_TYPE_GROUPCHAT:
+                room_jid = mess_data["to"]
+                service = jid.JID(room_jid.host)
+            elif client.muc_service is not None:
+                service = client.muc_service
+            else:
+                msg = D_(u"No known default MUC service".format(unparsed))
+                self.text_cmds.feedBack(client, msg, mess_data)
+                return False
+        except jid.InvalidFormat:
+            msg = D_(u"{} is not a valid JID!".format(unparsed))
+            self.text_cmds.feedBack(client, msg, mess_data)
+            return False
+        d = self.host.getDiscoItems(service, profile=client.profile)
+        d.addCallback(self._showListUI, client, service)
+
+        return False
+
     def _whois(self, client, whois_msg, mess_data, target_jid):
         """ Add MUC user information to whois """
         if mess_data['type'] != "groupchat":