diff src/plugins/plugin_xep_0045.py @ 1986:fbd313cfd40b

plugin xep-0045: ask for password when getting a "not-allowed" error
author Goffi <goffi@goffi.org>
date Fri, 01 Jul 2016 00:00:09 +0200
parents de4fac507dc4
children 757c512fe06c
line wrap: on
line diff
--- a/src/plugins/plugin_xep_0045.py	Wed Jun 29 13:51:39 2016 +0200
+++ b/src/plugins/plugin_xep_0045.py	Fri Jul 01 00:00:09 2016 +0200
@@ -23,6 +23,7 @@
 log = getLogger(__name__)
 from twisted.internet import defer
 from twisted.words.protocols.jabber import jid
+from twisted.python import failure
 from dateutil.tz import tzutc
 
 from sat.core import exceptions
@@ -40,7 +41,7 @@
 
 
 PLUGIN_INFO = {
-    "name": "XEP 0045 Plugin",
+    "name": "XEP-0045 Plugin",
     "import_name": "XEP-0045",
     "type": "XEP",
     "protocols": ["XEP-0045"],
@@ -136,7 +137,15 @@
         else:
             return True
 
-    def _joinCb(self, room, client):
+    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")):
+            log.info(u"room join for {} is cancelled".format(room_jid.userhost()))
+            raise failure.Failure(exceptions.CancelError(D_(u"Room joining cancelled by user")))
+        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 _joinCb(self, room, client, room_jid, nick):
         """Called when the user is in the requested room"""
         if room.locked:
             # FIXME: the current behaviour is to create an instant room
@@ -157,7 +166,16 @@
             if condition == 'conflict':
                 # we have a nickname conflict, we try again with "_" suffixed to current nickname
                 nick += '_'
-                return client._muc_client.join(room_jid, nick, password).addCallbacks(self._joinCb, self._joinEb, callbackKeywords={client: client}, errbackArgs=[client, room_jid, nick, 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))
+            elif condition == 'not-allowed':
+                # room is restricted, we need a password
+                password_ui = xml_tools.XMLUI("form", title=D_(u'Room {} is restricted').format(room_jid.userhost()), submit_id='')
+                password_ui.addText(D_("This room is restricted, please enter the password"))
+                password_ui.addPassword('password')
+                d = xml_tools.deferXMLUI(self.host, password_ui, profile=client.profile)
+                d.addCallback(self._passwordUICb, client, room_jid, nick)
+                return d
+
             msg_suffix = ' with condition "{}"'.format(failure.value.condition)
 
         mess = D_(u"Error while joining the room {room}{suffix}".format(
@@ -387,7 +405,7 @@
 
         password = options["password"] if "password" in options else None
 
-        return client._muc_client.join(room_jid, nick, password).addCallbacks(self._joinCb, self._joinEb, callbackKeywords={'client': client}, errbackArgs=[client, room_jid, nick, 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 _nick(self, room_jid_s, nick, profile_key=C.PROF_KEY_NONE):
         client = self.host.getClient(profile_key)