comparison src/plugins/plugin_misc_radiocol.py @ 587:952322b1d490

Remove trailing whitespaces.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Fri, 18 Jan 2013 17:55:34 +0100
parents 593331663b80
children beaf6bec2fcd
comparison
equal deleted inserted replaced
586:6a718ede8be1 587:952322b1d490
1 #!/usr/bin/python 1 #!/usr/bin/python
2 # -*- coding: utf-8 -*- 2 # -*- coding: utf-8 -*-
3 3
4 """ 4 """
5 SAT plugin for managing Radiocol 5 SAT plugin for managing Radiocol
6 Copyright (C) 2009, 2010, 2011, 2012, 2013 Jérôme Poisson (goffi@goffi.org) 6 Copyright (C) 2009, 2010, 2011, 2012, 2013 Jérôme Poisson (goffi@goffi.org)
7 7
8 This program is free software: you can redistribute it and/or modify 8 This program is free software: you can redistribute it and/or modify
9 it under the terms of the GNU Affero General Public License as published by 9 it under the terms of the GNU Affero General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or 10 the Free Software Foundation, either version 3 of the License, or
78 elt = domish.Element((None,'message')) 78 elt = domish.Element((None,'message'))
79 elt["to"] = to_jid.full() 79 elt["to"] = to_jid.full()
80 elt["type"] = type 80 elt["type"] = type
81 elt.addElement((NC_RADIOCOL, RADIOC_TAG)) 81 elt.addElement((NC_RADIOCOL, RADIOC_TAG))
82 return elt 82 return elt
83 83
84 def __create_started_elt(self): 84 def __create_started_elt(self):
85 """Create a game_started domish element""" 85 """Create a game_started domish element"""
86 started_elt = domish.Element((None,'started')) 86 started_elt = domish.Element((None,'started'))
87 return started_elt 87 return started_elt
88 88
120 print "radiocolRoomJoined" 120 print "radiocolRoomJoined"
121 _room_jid = room.occupantJID.userhostJID() 121 _room_jid = room.occupantJID.userhostJID()
122 self.radiocolCreate(_room_jid.userhost(), profile_key=profile) 122 self.radiocolCreate(_room_jid.userhost(), profile_key=profile)
123 for occupant in occupants: 123 for occupant in occupants:
124 self.host.plugins["XEP-0249"].invite(jid.JID(occupant), room.occupantJID.userhostJID(), {"game":"Radiocol"}, profile) 124 self.host.plugins["XEP-0249"].invite(jid.JID(occupant), room.occupantJID.userhostJID(), {"game":"Radiocol"}, profile)
125 125
126 def after_init(ignore): 126 def after_init(ignore):
127 room_name = "sat_radiocol_%s" % self.host.plugins["XEP-0045"].getUniqueName(profile_key) 127 room_name = "sat_radiocol_%s" % self.host.plugins["XEP-0045"].getUniqueName(profile_key)
128 print "\n\n===> room_name:", room_name 128 print "\n\n===> room_name:", room_name
129 muc_service = None 129 muc_service = None
130 for service in self.host.memory.getServerServiceEntities("conference", "text", profile): 130 for service in self.host.memory.getServerServiceEntities("conference", "text", profile):
131 if not ".irc." in service.userhost(): 131 if not ".irc." in service.userhost():
132 #FIXME: 132 #FIXME:
133 #This awfull ugly hack is here to avoid an issue with openfire: the irc gateway 133 #This awfull ugly hack is here to avoid an issue with openfire: the irc gateway
134 #use "conference/text" identity (instead of "conference/irc"), there is certainly a better way 134 #use "conference/text" identity (instead of "conference/irc"), there is certainly a better way
135 #to manage this, but this hack fill do it for test purpose 135 #to manage this, but this hack fill do it for test purpose
136 muc_service = service 136 muc_service = service
137 break 137 break
138 if not muc_service: 138 if not muc_service:
139 error(_("Can't find a MUC service")) 139 error(_("Can't find a MUC service"))
140 return 140 return
141 141
142 _jid, xmlstream = self.host.getJidNStream(profile) 142 _jid, xmlstream = self.host.getJidNStream(profile)
143 d = self.host.plugins["XEP-0045"].join(jid.JID("%s@%s" % (room_name, muc_service.userhost())), _jid.user, {}, profile) 143 d = self.host.plugins["XEP-0045"].join(jid.JID("%s@%s" % (room_name, muc_service.userhost())), _jid.user, {}, profile)
144 d.addCallback(radiocolRoomJoined) 144 d.addCallback(radiocolRoomJoined)
145 145
146 client = self.host.getClient(profile) 146 client = self.host.getClient(profile)
182 # Here we cheat because we know we are on the same host, and we don't 182 # Here we cheat because we know we are on the same host, and we don't
183 # check data. Referee will have to parse the song himself to check it 183 # check data. Referee will have to parse the song himself to check it
184 client = self.host.getClient(profile) 184 client = self.host.getClient(profile)
185 if not client: 185 if not client:
186 error(_("Can't access profile's data")) 186 error(_("Can't access profile's data"))
187 return 187 return
188 try: 188 try:
189 song = OggVorbis(song_path) 189 song = OggVorbis(song_path)
190 except OggVorbisHeaderError: 190 except OggVorbisHeaderError:
191 #this file is not ogg vorbis, we reject it 191 #this file is not ogg vorbis, we reject it
192 unlink(song_path) # FIXME: same host trick (see note above) 192 unlink(song_path) # FIXME: same host trick (see note above)
211 added_elt['album'] = album 211 added_elt['album'] = album
212 added_elt['length'] = str(length) 212 added_elt['length'] = str(length)
213 self.host.profiles[profile].xmlstream.send(mess) 213 self.host.profiles[profile].xmlstream.send(mess)
214 214
215 radio_data = self.radios[jid.JID(referee).userhost()] #FIXME: referee comes from Libervia's client side, it's unsecure 215 radio_data = self.radios[jid.JID(referee).userhost()] #FIXME: referee comes from Libervia's client side, it's unsecure
216 radio_data['to_delete'][filename] = song_path #FIXME: works only because of the same host trick, see the note under the docstring 216 radio_data['to_delete'][filename] = song_path #FIXME: works only because of the same host trick, see the note under the docstring
217 217
218 def playNext(self, room_jid, profile): 218 def playNext(self, room_jid, profile):
219 """"Play next sont in queue if exists, and put a timer 219 """"Play next sont in queue if exists, and put a timer
220 which trigger after the song has been played to play next one""" 220 which trigger after the song has been played to play next one"""
221 #TODO: need to check that there are still peoples in the room 221 #TODO: need to check that there are still peoples in the room
246 try: 246 try:
247 file_to_delete = radio_data['to_delete'][filename] 247 file_to_delete = radio_data['to_delete'][filename]
248 except KeyError: 248 except KeyError:
249 error(_("INTERNAL ERROR: can't find full path of the song to delete")) 249 error(_("INTERNAL ERROR: can't find full path of the song to delete"))
250 return 250 return
251 251
252 #we wait more than the song length to delete the file, to manage poorly reactive networks/clients 252 #we wait more than the song length to delete the file, to manage poorly reactive networks/clients
253 reactor.callLater(length + 90, unlink, file_to_delete) #FIXME: same host trick (see above) 253 reactor.callLater(length + 90, unlink, file_to_delete) #FIXME: same host trick (see above)
254 254
255 255
256 def radiocol_game_cmd(self, mess_elt, profile): 256 def radiocol_game_cmd(self, mess_elt, profile):
257 #FIXME: we should check sender (is it referee ?) here before accepting commands 257 #FIXME: we should check sender (is it referee ?) here before accepting commands
258 from_jid = jid.JID(mess_elt['from']) 258 from_jid = jid.JID(mess_elt['from'])
259 room_jid = jid.JID(from_jid.userhost()) 259 room_jid = jid.JID(from_jid.userhost())
260 radio_elt = mess_elt.firstChildElement() 260 radio_elt = mess_elt.firstChildElement()
261 radio_data = self.radios[room_jid.userhost()] 261 radio_data = self.radios[room_jid.userhost()]
262 occupants_data = radio_data['occupants_data'] 262 occupants_data = radio_data['occupants_data']
263 queue = radio_data['queue'] 263 queue = radio_data['queue']
264 264
265 for elt in radio_elt.elements(): 265 for elt in radio_elt.elements():
266 266
267 if elt.name == 'started': #new game created 267 if elt.name == 'started': #new game created
268 self.host.bridge.radiocolStarted(room_jid.userhost(), from_jid.full(), profile) 268 self.host.bridge.radiocolStarted(room_jid.userhost(), from_jid.full(), profile)
269 elif elt.name == 'preload': #a song is in queue and must be preloaded 269 elif elt.name == 'preload': #a song is in queue and must be preloaded
270 self.host.bridge.radiocolPreload(room_jid.userhost(), elt['filename'], elt['title'], elt['artist'], elt['album'], profile) 270 self.host.bridge.radiocolPreload(room_jid.userhost(), elt['filename'], elt['title'], elt['artist'], elt['album'], profile)
271 elif elt.name == 'play': 271 elif elt.name == 'play':
277 elif elt.name == 'upload_ok': 277 elif elt.name == 'upload_ok':
278 self.host.bridge.radiocolUploadOk(room_jid.userhost(), profile) 278 self.host.bridge.radiocolUploadOk(room_jid.userhost(), profile)
279 elif elt.name == 'song_added': #a song has been added 279 elif elt.name == 'song_added': #a song has been added
280 #FIXME: we are KISS for the proof of concept: every song is added, to a limit of 3 in queue. 280 #FIXME: we are KISS for the proof of concept: every song is added, to a limit of 3 in queue.
281 # Need to manage some sort of rules to allow peoples to send songs 281 # Need to manage some sort of rules to allow peoples to send songs
282 282
283 if len(queue) >= QUEUE_LIMIT: 283 if len(queue) >= QUEUE_LIMIT:
284 #there are already too many songs in queue, we reject this one 284 #there are already too many songs in queue, we reject this one
285 mess = self.createRadiocolElt(room_jid) 285 mess = self.createRadiocolElt(room_jid)
286 reject_elt = mess.firstChildElement().addElement(('','song_rejected')) 286 reject_elt = mess.firstChildElement().addElement(('','song_rejected'))
287 reject_elt['sender'] = from_jid.resource 287 reject_elt['sender'] = from_jid.resource
288 reject_elt['reason'] = "Too many songs in queue" 288 reject_elt['reason'] = "Too many songs in queue"
289 #FIXME: add an error code 289 #FIXME: add an error code
290 self.host.profiles[profile].xmlstream.send(mess) 290 self.host.profiles[profile].xmlstream.send(mess)
291 return 291 return
292 292
293 #The song is accepted and added in queue 293 #The song is accepted and added in queue
294 queue.append((elt['filename'], float(elt['length']))) 294 queue.append((elt['filename'], float(elt['length'])))
295 295
296 if len(queue) >= QUEUE_LIMIT: 296 if len(queue) >= QUEUE_LIMIT:
297 #We are at the limit, we refuse new upload until next play 297 #We are at the limit, we refuse new upload until next play
315 #we can now start the party :) 315 #we can now start the party :)
316 radio_data['playing'] = True 316 radio_data['playing'] = True
317 self.playNext(room_jid, profile) 317 self.playNext(room_jid, profile)
318 else: 318 else:
319 error (_('Unmanaged game element: %s') % elt.name) 319 error (_('Unmanaged game element: %s') % elt.name)
320 320
321 def getHandler(self, profile): 321 def getHandler(self, profile):
322 return RadiocolHandler(self) 322 return RadiocolHandler(self)
323 323
324 class RadiocolHandler (XMPPHandler): 324 class RadiocolHandler (XMPPHandler):
325 implements(iwokkel.IDisco) 325 implements(iwokkel.IDisco)
326 326
327 def __init__(self, plugin_parent): 327 def __init__(self, plugin_parent):
328 self.plugin_parent = plugin_parent 328 self.plugin_parent = plugin_parent
329 self.host = plugin_parent.host 329 self.host = plugin_parent.host
330 330
331 def connectionInitialized(self): 331 def connectionInitialized(self):