diff src/tools/plugins/games.py @ 712:f610864eb7a5

plugins (MUC, tools, games): generalize the generation of a unique room name when joining a MUC and no room is specified: - method to get the MUC service moved to plugin_xep_0045 - joinMUC returns the muc name (given by user or generated) - getUniqueName can receive a specified service in argument
author souliane <souliane@mailoo.org>
date Sun, 17 Nov 2013 16:59:12 +0100
parents c9792d0be499
children ecc5a5b34ee1
line wrap: on
line diff
--- a/src/tools/plugins/games.py	Sun Nov 17 16:30:46 2013 +0100
+++ b/src/tools/plugins/games.py	Sun Nov 17 16:59:12 2013 +0100
@@ -44,9 +44,14 @@
         self.games = {}
         self.waiting_inv = {}  # Invitation waiting for people to join to launch a game
 
-    def prepareRoom(self, other_players, profile_key='@NONE@'):
+    def getUniqueName(self, muc_service="", profile_key='@DEFAULT@'):
+        room = self.host.plugins["XEP-0045"].getUniqueName(muc_service, profile_key=profile_key)
+        return "sat_%s_%s" % (self.name.lower(), room) if room != "" else ""
+
+    def prepareRoom(self, other_players, room_jid=None, profile_key='@NONE@'):
         """Prepare the room for a game: create it and invite players.
         @param other_players: list for other players JID userhosts
+        @param room_jid: JID of the room to reuse or None to create a new room
         """
         debug(_('Preparing room for %s game') % self.name)
         profile = self.host.memory.getProfileName(profile_key)
@@ -69,30 +74,25 @@
             for player in other_players:
                 self.host.plugins["XEP-0249"].invite(JID(player), room.occupantJID.userhostJID(), {"game": self.name}, profile)
 
-        def after_init(ignore):
-            room_name = "sat_%s_%s" % (self.name.lower(), self.host.plugins["XEP-0045"].getUniqueName(profile_key))
-            print "\n\n===> room_name:", room_name
-            muc_service = None
-            for service in self.host.memory.getServerServiceEntities("conference", "text", profile):
-                if not ".irc." in service.userhost():
-                    #FIXME:
-                    #This awfull ugly hack is here to avoid an issue with openfire: the irc gateway
-                    #use "conference/text" identity (instead of "conference/irc"), there is certainly a better way
-                    #to manage this, but this hack fill do it for test purpose
-                    muc_service = service
-                    break
-            if not muc_service:
-                error(_("Can't find a MUC service"))
-                return
-
-            d = self.host.plugins["XEP-0045"].join(JID("%s@%s" % (room_name, muc_service.userhost())), _jid.user, {}, profile)
+        def after_init(room_jid):
+            if room_jid is not None and room_jid != "":
+                # a room name has been specified...
+                if room_jid in self.host.plugins["XEP-0045"].clients[profile].joined_rooms:
+                    # and we're already in
+                    roomJoined(self.host.plugins["XEP-0045"].clients[profile].joined_rooms[room_jid])
+                    return
+            else:
+                room_jid = self.getUniqueName(profile_key=profile_key)
+                if room_jid == "":
+                    return
+            d = self.host.plugins["XEP-0045"].join(JID(room_jid), _jid.user, {}, profile)
             d.addCallback(roomJoined)
 
         client = self.host.getClient(profile)
         if not client:
             error(_('No client for this profile key: %s') % profile_key)
             return
-        client.client_initialized.addCallback(after_init)
+        client.client_initialized.addCallback(lambda ignore: after_init(room_jid))
 
     def userJoinedTrigger(self, room, user, profile):
         """This trigger is used to check if we are waiting for people in this room,