From ee82c6717dcbc50ef5bc288d2ffb307339006b2f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maxime=20=E2=80=9Cpep=E2=80=9D=20Buquet?= Date: Tue, 1 Mar 2022 00:34:18 +0100 Subject: impromptu: Ensure a room is empty before joining MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Maxime “pep” Buquet --- poezio/core/core.py | 42 +++++++++++++++++++++++++++++++++--------- 1 file changed, 33 insertions(+), 9 deletions(-) diff --git a/poezio/core/core.py b/poezio/core/core.py index a1fdde0d..05008e02 100644 --- a/poezio/core/core.py +++ b/poezio/core/core.py @@ -1004,19 +1004,43 @@ class Core: ) return - nick = self.own_nick - localpart = utils.pronounceable() - room_str = '{!s}@{!s}'.format(localpart, default_muc) - try: - room = JID(room_str) - except InvalidJID: + # Retries generating a name until we find a non-existing room. + # Abort otherwise. + retries = 3 + while retries > 0: + localpart = utils.pronounceable() + room_str = '{!s}@{!s}'.format(localpart, default_muc + try: + room = JID(room_str) + except InvalidJID: + self.information( + 'The generated XMPP address is invalid: {!s}'.format(room_str), + 'Error' + ) + return None + + try: + iq = await self.xmpp['xep_0030'].get_info( + jid=room, + cached=False, + ) + except IqTimeout: + pass + except IqError as exn: + if exn.etype == 'cancel' and exn.condition == 'item-not-found': + log.debug('Found empty room for /impromptu') + break + + retries = retries - 1 + + if retries == 0: self.information( - 'The generated XMPP address is invalid: {!s}'.format(room_str), - 'Error' + 'Couldn\'t generate a room name that isn\'t already used.', + 'Error', ) return None - self.open_new_room(room, nick).join() + self.open_new_room(room, self.own_nick).join() async def join_callback(_presence): iq = self._impromptu_room_form(room) -- cgit v1.2.3