comparison src/plugins/plugin_xep_0045.py @ 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 d5befe7253aa
comparison
equal deleted inserted replaced
1989:757c512fe06c 1990:dfbe0bb056dc
80 host.bridge.addSignal("mucRoomJoined", ".plugin", signature='sa{sa{ss}}sss') # args: room_jid, occupants, user_nick, subject, profile 80 host.bridge.addSignal("mucRoomJoined", ".plugin", signature='sa{sa{ss}}sss') # args: room_jid, occupants, user_nick, subject, profile
81 host.bridge.addSignal("mucRoomLeft", ".plugin", signature='ss') # args: room_jid, profile 81 host.bridge.addSignal("mucRoomLeft", ".plugin", signature='ss') # args: room_jid, profile
82 host.bridge.addSignal("mucRoomUserChangedNick", ".plugin", signature='ssss') # args: room_jid, old_nick, new_nick, profile 82 host.bridge.addSignal("mucRoomUserChangedNick", ".plugin", signature='ssss') # args: room_jid, old_nick, new_nick, profile
83 host.bridge.addSignal("mucRoomNewSubject", ".plugin", signature='sss') # args: room_jid, subject, profile 83 host.bridge.addSignal("mucRoomNewSubject", ".plugin", signature='sss') # args: room_jid, subject, profile
84 self.__submit_conf_id = host.registerCallback(self._submitConfiguration, with_data=True) 84 self.__submit_conf_id = host.registerCallback(self._submitConfiguration, with_data=True)
85 self._room_join_id = host.registerCallback(self._UIRoomJoinCb, with_data=True)
85 host.importMenu((D_("MUC"), D_("configure")), self._configureRoomMenu, security_limit=0, help_string=D_("Configure Multi-User Chat room"), type_=C.MENU_ROOM) 86 host.importMenu((D_("MUC"), D_("configure")), self._configureRoomMenu, security_limit=0, help_string=D_("Configure Multi-User Chat room"), type_=C.MENU_ROOM)
86 try: 87 try:
87 self.txt_cmds = self.host.plugins[C.TEXT_CMDS] 88 self.txt_cmds = self.host.plugins[C.TEXT_CMDS]
88 except KeyError: 89 except KeyError:
89 log.info(_(u"Text commands not available")) 90 log.info(_(u"Text commands not available"))
135 except exceptions.NotFound: 136 except exceptions.NotFound:
136 return False 137 return False
137 else: 138 else:
138 return True 139 return True
139 140
141 def _UIRoomJoinCb(self, data, profile):
142 room_jid = jid.JID(data['index'])
143 client = self.host.getClient(profile)
144 self.join(client, room_jid)
145 return {}
146
140 def _passwordUICb(self, data, client, room_jid, nick): 147 def _passwordUICb(self, data, client, room_jid, nick):
141 """Called when the user has given room password (or cancelled)""" 148 """Called when the user has given room password (or cancelled)"""
142 if C.bool(data.get(C.XMLUI_DATA_CANCELLED, "false")): 149 if C.bool(data.get(C.XMLUI_DATA_CANCELLED, "false")):
143 log.info(u"room join for {} is cancelled".format(room_jid.userhost())) 150 log.info(u"room join for {} is cancelled".format(room_jid.userhost()))
144 raise failure.Failure(exceptions.CancelError(D_(u"Room joining cancelled by user"))) 151 raise failure.Failure(exceptions.CancelError(D_(u"Room joining cancelled by user")))
145 password = data[xml_tools.formEscape('password')] 152 password = data[xml_tools.formEscape('password')]
146 return client._muc_client.join(room_jid, nick, password).addCallbacks(self._joinCb, self._joinEb, (client, room_jid, nick), errbackArgs=(client, room_jid, nick, password)) 153 return client._muc_client.join(room_jid, nick, password).addCallbacks(self._joinCb, self._joinEb, (client, room_jid, nick), errbackArgs=(client, room_jid, nick, password))
154
155 def _showListUI(self, items, client, service):
156 xmlui = xml_tools.XMLUI(title=D_('Rooms in {}'.format(service.full())))
157 adv_list = xmlui.changeContainer('advanced_list', columns=1, selectable='single', callback_id=self._room_join_id)
158 items = sorted(items, key=lambda i: i.name.lower())
159 for item in items:
160 adv_list.setRowIndex(item.entity.full())
161 xmlui.addText(item.name)
162 adv_list.end()
163 self.host.actionNew({'xmlui': xmlui.toXml()}, profile=client.profile)
147 164
148 def _joinCb(self, room, client, room_jid, nick): 165 def _joinCb(self, room, client, room_jid, nick):
149 """Called when the user is in the requested room""" 166 """Called when the user is in the requested room"""
150 if room.locked: 167 if room.locked:
151 # FIXME: the current behaviour is to create an instant room 168 # FIXME: the current behaviour is to create an instant room
639 656
640 @command (group): title 657 @command (group): title
641 - title: new room subject 658 - title: new room subject
642 """ 659 """
643 return self.cmd_title(client, mess_data) 660 return self.cmd_title(client, mess_data)
661
662 def cmd_list(self, client, mess_data):
663 """list available rooms in a muc server
664
665 @command (all): [MUC_SERVICE]
666 - MUC_SERVICE: service to request
667 empty value will request room's service for a room,
668 or user's server default MUC service in a one2one chat
669 """
670 unparsed = mess_data["unparsed"].strip()
671 try:
672 service = jid.JID(unparsed)
673 except RuntimeError:
674 if mess_data['type'] == C.MESS_TYPE_GROUPCHAT:
675 room_jid = mess_data["to"]
676 service = jid.JID(room_jid.host)
677 elif client.muc_service is not None:
678 service = client.muc_service
679 else:
680 msg = D_(u"No known default MUC service".format(unparsed))
681 self.text_cmds.feedBack(client, msg, mess_data)
682 return False
683 except jid.InvalidFormat:
684 msg = D_(u"{} is not a valid JID!".format(unparsed))
685 self.text_cmds.feedBack(client, msg, mess_data)
686 return False
687 d = self.host.getDiscoItems(service, profile=client.profile)
688 d.addCallback(self._showListUI, client, service)
689
690 return False
644 691
645 def _whois(self, client, whois_msg, mess_data, target_jid): 692 def _whois(self, client, whois_msg, mess_data, target_jid):
646 """ Add MUC user information to whois """ 693 """ Add MUC user information to whois """
647 if mess_data['type'] != "groupchat": 694 if mess_data['type'] != "groupchat":
648 return 695 return