plugin XEP-0045, quick_frontend + primitivus (chat): cleaning of XEP-0045 (first pass):

- bridge methods/signals now all start with "muc" to follow new convention
    - internal method use client instead of profile to follow new convention
    - removed excetpions from plugin XEP-0045 in favor of core.exceptions, NotReady added
    - cleaned/simplified several part of the code. checkClient removed as it is not needed anymore
    - self.clients map removed, muc data are now stored directly in client
    - getRoomEntityNick and getRoomNicksOfUsers are removed as they don't look sane.
        /!\ This break all room game plugins for the moment
    - use of uuid4 instead of uuid1 for getUniqueName, as host ID and current time are used for uuid1
This commit is contained in:
Goffi 2016-06-27 21:45:11 +02:00
commit 04515e5013

View file

@ -48,6 +48,8 @@ PLUGIN_INFO = {
}
# FIXME: this plugin is broken, need to be fixed
class RoomGame(object):
"""This class is used to help launching a MUC game.
@ -123,7 +125,7 @@ class RoomGame(object):
host.trigger.add("MUC user joined", self.userJoinedTrigger)
host.trigger.add("MUC user left", self.userLeftTrigger)
def _createOrInvite(self, room, other_players, profile):
def _createOrInvite(self, room_jid, other_players, profile):
"""
This is called only when someone explicitly wants to play.
@ -131,22 +133,24 @@ class RoomGame(object):
also its creation could be postponed until all the expected players
join the room (in that case it will be created from userJoinedTrigger).
@param room (wokkel.muc.Room): the room
@param other_players (list[jid.JID]): list of the other players JID (bare)
@param other_players (list[jid.JID]): list of the other players JID (bare)
"""
# FIXME: broken !
raise NotImplementedError("To be fixed")
client = self.host.getClient(profile)
user_jid = self.host.getJidNStream(profile)[0]
room_jid = room.occupantJID.userhostJID()
nick = self.host.plugins["XEP-0045"].getRoomNick(room_jid, profile)
nick = self.host.plugins["XEP-0045"].getRoomNick(client, room_jid)
nicks = [nick]
if self._gameExists(room_jid):
if not self._checkJoinAuth(room_jid, user_jid, nick):
return
nicks.extend(self._invitePlayers(room, other_players, nick, profile))
nicks.extend(self._invitePlayers(room_jid, other_players, nick, profile))
self._updatePlayers(room_jid, nicks, True, profile)
else:
self._initGame(room_jid, nick)
(auth, waiting, missing) = self._checkWaitAuth(room, other_players)
(auth, waiting, missing) = self._checkWaitAuth(room_jid, other_players)
nicks.extend(waiting)
nicks.extend(self._invitePlayers(room, missing, nick, profile))
nicks.extend(self._invitePlayers(room_jid, missing, nick, profile))
if auth:
self.createGame(room_jid, nicks, profile)
else:
@ -300,15 +304,15 @@ class RoomGame(object):
"""
return []
def _invitePlayers(self, room, other_players, nick, profile):
def _invitePlayers(self, room_jid, other_players, nick, profile):
"""Invite players to a room, associated game may exist or not.
@param room (wokkel.muc.Room): the room
@param other_players (list[jid.JID]): list of the players to invite
@param nick (unicode): nick of the user who send the invitation
@return: list[unicode] of room nicks for invited players who are already in the room
"""
room_jid = room.occupantJID.userhostJID()
raise NotImplementedError("Need to be fixed !")
# FIXME: this is broken and unsecure !
if not self._checkInviteAuth(room_jid, nick):
return []
# TODO: remove invitation waiting for too long, using the time data
@ -316,7 +320,7 @@ class RoomGame(object):
nicks = []
for player_jid in [player.userhostJID() for player in other_players]:
# TODO: find a way to make it secure
other_nick = self.host.plugins["XEP-0045"].getRoomNickOfUser(room, player_jid, secure=self.testing)
other_nick = self.host.plugins["XEP-0045"].getRoomEntityNick(room_jid, player_jid, secure=self.testing)
if other_nick is None:
self.host.plugins["XEP-0249"].invite(player_jid, room_jid, {"game": self.name}, profile)
else:
@ -397,8 +401,9 @@ class RoomGame(object):
@param profile_key (unicode): %(doc_profile_key)s
@return: jid.JID (unique name for a new room to be created)
"""
client = self.host.getClient(profile_key)
# FIXME: jid.JID must be used instead of strings
room = self.host.plugins["XEP-0045"].getUniqueName(muc_service, profile_key=profile_key)
room = self.host.plugins["XEP-0045"].getUniqueName(client, muc_service)
return jid.JID("sat_%s_%s" % (self.name.lower(), room.userhost()))
def _prepareRoom(self, other_players=None, room_jid_s='', profile_key=C.PROF_KEY_NONE):
@ -413,6 +418,8 @@ class RoomGame(object):
@param room_jid (jid.JID): JID of the room, or None to generate a unique name
@param profile_key (unicode): %(doc_profile_key)s
"""
# FIXME: need to be refactored
client = self.host.getClient(profile_key)
log.debug(_(u'Preparing room for %s game') % self.name)
profile = self.host.memory.getProfileName(profile_key)
if not profile:
@ -421,21 +428,17 @@ class RoomGame(object):
if other_players is None:
other_players = []
def roomJoined(room):
"""@param room: instance of wokkel.muc.Room"""
self._createOrInvite(room, other_players, profile)
# Create/join the given room, or a unique generated one if no room is specified.
if room_jid is None:
room_jid = self.getUniqueName(profile_key=profile_key)
else:
if room_jid in self.host.plugins["XEP-0045"].clients[profile].joined_rooms:
roomJoined(self.host.plugins["XEP-0045"].clients[profile].joined_rooms[room_jid])
return defer.succeed(None)
self.host.plugins["XEP-0045"].checkRoomJoined(client, room_jid)
self._createOrInvite(client, room_jid, other_players)
return defer.succeed(None)
user_jid = self.host.getJidNStream(profile)[0]
d = self.host.plugins["XEP-0045"].join(room_jid, user_jid.user, {}, profile)
return d.addCallback(roomJoined)
return d.addCallback(lambda dummy: self._createOrInvite(client, room_jid, other_players))
def userJoinedTrigger(self, room, user, profile):
"""This trigger is used to check if the new user can take part of a game, create the game if we were waiting for him or just update the players list.