From 8b700a6d48ac1413a5f2107a6e36a09777b08957 Mon Sep 17 00:00:00 2001 From: souliane Date: Wed, 15 Jan 2014 23:09:39 +0100 Subject: [PATCH] plugins radiocol_tarot: do not process received messages for MUC users that are actually not playing the game + fix for testing purpose --- src/plugins/plugin_misc_radiocol.py | 18 ++++++++++++++---- src/plugins/plugin_misc_tarot.py | 7 ++++++- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/src/plugins/plugin_misc_radiocol.py b/src/plugins/plugin_misc_radiocol.py index c61a9431..dac01173 100755 --- a/src/plugins/plugin_misc_radiocol.py +++ b/src/plugins/plugin_misc_radiocol.py @@ -111,7 +111,14 @@ class Radiocol(object): } radio_data = self.games[jid.JID(referee).userhost()] # FIXME: referee comes from Libervia's client side, it's unsecure radio_data['to_delete'][attrs['filename']] = song_path # FIXME: works only because of the same host trick, see the note under the docstring - return threads.deferToThread(self.send, jid.JID(referee), ('', 'song_added'), attrs, profile=profile) + + # XXX: avoid deferToThread which is causing testing troubles. When using deferToThread, + # the callbacks that are added (by the test) to the Deferred instance returned by this + # method are not run. And if you run d.callback again, you get a AlreadyCalledError. + d = defer.Deferred() + d.addCallback(self.send, ('', 'song_added'), attrs, profile=profile) + d.callback(jid.JID(referee)) + return d def playNext(self, room_jid, profile): """"Play next song in queue if exists, and put a timer @@ -130,7 +137,6 @@ class Radiocol(object): #nothing left to play, we need to wait for uploads radio_data['playing'] = False return - song = queue.pop(0) filename, length = song['filename'], float(song['length']) self.send(room_jid, ('', 'play'), {'filename': filename}, profile=profile) @@ -169,6 +175,8 @@ class Radiocol(object): def room_game_cmd(self, mess_elt, profile): from_jid = jid.JID(mess_elt['from']) room_jid = jid.JID(from_jid.userhost()) + nick = self.host.plugins["XEP-0045"].getRoomNick(room_jid.userhost(), profile) + radio_elt = mess_elt.firstChildElement() radio_data = self.games[room_jid.userhost()] if 'queue' in radio_data: @@ -176,11 +184,14 @@ class Radiocol(object): from_referee = self.isReferee(room_jid.userhost(), from_jid.resource) to_referee = self.isReferee(room_jid.userhost(), jid.JID(mess_elt['to']).user) + is_player = self.isPlayer(room_jid.userhost(), nick) for elt in radio_elt.elements(): if not from_referee and not (to_referee and elt.name == 'song_added'): continue # sender must be referee, expect when a song is submitted + if not is_player and (elt.name not in ('started', 'players')): + continue # user is in the room but not playing - if elt.name == 'started' or elt.name == 'players': # new game created + if elt.name in ('started', 'players'): # new game created and/or players list updated players = [] for player in elt.elements(): players.append(unicode(player)) @@ -199,7 +210,6 @@ class Radiocol(object): elif elt.name == 'song_added': # a song has been added #FIXME: we are KISS for the proof of concept: every song is added, to a limit of 3 in queue. # Need to manage some sort of rules to allow peoples to send songs - if len(queue) >= QUEUE_LIMIT: #there are already too many songs in queue, we reject this one #FIXME: add an error code diff --git a/src/plugins/plugin_misc_tarot.py b/src/plugins/plugin_misc_tarot.py index 53593ea3..af47746a 100755 --- a/src/plugins/plugin_misc_tarot.py +++ b/src/plugins/plugin_misc_tarot.py @@ -447,14 +447,19 @@ class Tarot(object): """ from_jid = jid.JID(mess_elt['from']) room_jid = jid.JID(from_jid.userhost()) + nick = self.host.plugins["XEP-0045"].getRoomNick(room_jid.userhost(), profile) + game_elt = mess_elt.firstChildElement() game_data = self.games[room_jid.userhost()] + is_player = self.isPlayer(room_jid.userhost(), nick) if 'players_data' in game_data: players_data = game_data['players_data'] for elt in game_elt.elements(): + if not is_player and (elt.name not in ('started', 'players')): + continue # user is in the room but not playing - if elt.name == 'started' or elt.name == 'players': # new game created + if elt.name in ('started', 'players'): # new game created and/or players list updated players = [] for player in elt.elements(): players.append(unicode(player))