Mercurial > libervia-backend
annotate src/plugins/plugin_misc_radiocol.py @ 459:cf005701624b
copyleft date update
author | Goffi <goffi@goffi.org> |
---|---|
date | Tue, 06 Mar 2012 09:07:24 +0100 |
parents | fbe7c9118ce4 |
children | 794f60e2da27 |
rev | line source |
---|---|
450 | 1 #!/usr/bin/python |
2 # -*- coding: utf-8 -*- | |
3 | |
4 """ | |
5 SAT plugin for managing Radiocol | |
459 | 6 Copyright (C) 2009, 2010, 2011, 2012 Jérôme Poisson (goffi@goffi.org) |
450 | 7 |
8 This program is free software: you can redistribute it and/or modify | |
9 it under the terms of the GNU General Public License as published by | |
10 the Free Software Foundation, either version 3 of the License, or | |
11 (at your option) any later version. | |
12 | |
13 This program is distributed in the hope that it will be useful, | |
14 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 GNU General Public License for more details. | |
17 | |
18 You should have received a copy of the GNU General Public License | |
19 along with this program. If not, see <http://www.gnu.org/licenses/>. | |
20 """ | |
21 | |
22 from logging import debug, info, warning, error | |
23 from twisted.words.xish import domish | |
24 from twisted.internet import protocol, defer, threads, reactor | |
25 from twisted.words.protocols.jabber import client, jid, xmlstream | |
26 from twisted.words.protocols.jabber import error as jab_error | |
27 from twisted.words.protocols.jabber.xmlstream import IQ | |
28 import random | |
29 | |
30 from zope.interface import implements | |
31 | |
32 from wokkel import disco, iwokkel, data_form | |
33 from sat.tools.xml_tools import dataForm2xml | |
34 from sat.tools.games import TarotCard | |
35 | |
36 from time import time | |
455
72522263cbc9
plugin RadioCol: basic functionnality working approximately
Goffi <goffi@goffi.org>
parents:
453
diff
changeset
|
37 import os.path |
72522263cbc9
plugin RadioCol: basic functionnality working approximately
Goffi <goffi@goffi.org>
parents:
453
diff
changeset
|
38 from mutagen.oggvorbis import OggVorbis |
450 | 39 |
40 try: | |
41 from twisted.words.protocols.xmlstream import XMPPHandler | |
42 except ImportError: | |
43 from wokkel.subprotocols import XMPPHandler | |
44 | |
45 MESSAGE = '/message' | |
46 NC_RADIOCOL = 'http://www.goffi.org/protocol/radiocol' | |
47 RADIOC_TAG = 'radiocol' | |
48 RADIOC_REQUEST = MESSAGE + '/' + RADIOC_TAG + '[@xmlns="' + NC_RADIOCOL + '"]' | |
49 | |
50 PLUGIN_INFO = { | |
51 "name": "Radio collective plugin", | |
52 "import_name": "Radiocol", | |
53 "type": "Exp", | |
54 "protocols": [], | |
55 "dependencies": ["XEP-0045", "XEP-0249"], | |
56 "main": "Radiocol", | |
57 "handler": "yes", | |
58 "description": _("""Implementation of radio collective""") | |
59 } | |
60 | |
457
fbe7c9118ce4
plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents:
456
diff
changeset
|
61 QUEUE_LIMIT = 2 |
fbe7c9118ce4
plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents:
456
diff
changeset
|
62 |
450 | 63 |
64 class Radiocol(): | |
65 | |
66 def __init__(self, host): | |
67 info(_("Radio collective initialization")) | |
68 self.host = host | |
69 self.radios={} | |
455
72522263cbc9
plugin RadioCol: basic functionnality working approximately
Goffi <goffi@goffi.org>
parents:
453
diff
changeset
|
70 host.bridge.addMethod("radiocolLaunch", ".plugin", in_sign='ass', out_sign='', method=self.radiocolLaunch) |
72522263cbc9
plugin RadioCol: basic functionnality working approximately
Goffi <goffi@goffi.org>
parents:
453
diff
changeset
|
71 host.bridge.addMethod("radiocolCreate", ".plugin", in_sign='ss', out_sign='', method=self.radiocolCreate) |
72522263cbc9
plugin RadioCol: basic functionnality working approximately
Goffi <goffi@goffi.org>
parents:
453
diff
changeset
|
72 host.bridge.addMethod("radiocolSongAdded", ".plugin", in_sign='sss', out_sign='', method=self.radiocolSongAdded) |
72522263cbc9
plugin RadioCol: basic functionnality working approximately
Goffi <goffi@goffi.org>
parents:
453
diff
changeset
|
73 host.bridge.addSignal("radiocolStarted", ".plugin", signature='sss') #room_jid, referee, profile |
72522263cbc9
plugin RadioCol: basic functionnality working approximately
Goffi <goffi@goffi.org>
parents:
453
diff
changeset
|
74 host.bridge.addSignal("radiocolSongRejected", ".plugin", signature='sss') #room_jid, reason, profile |
72522263cbc9
plugin RadioCol: basic functionnality working approximately
Goffi <goffi@goffi.org>
parents:
453
diff
changeset
|
75 host.bridge.addSignal("radiocolPreload", ".plugin", signature='ssssss') #room_jid, filename, title, artist, album, profile |
457
fbe7c9118ce4
plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents:
456
diff
changeset
|
76 host.bridge.addSignal("radiocolPlay", ".plugin", signature='sss') #room_jid, filename, profile |
fbe7c9118ce4
plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents:
456
diff
changeset
|
77 host.bridge.addSignal("radiocolNoUpload", ".plugin", signature='ss') #room_jid, profile |
fbe7c9118ce4
plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents:
456
diff
changeset
|
78 host.bridge.addSignal("radiocolUploadOk", ".plugin", signature='ss') #room_jid, profile |
450 | 79 host.trigger.add("MUC user joined", self.userJoinedTrigger) |
80 | |
81 def createRadiocolElt(self, to_jid, type="normal"): | |
82 type = "normal" if to_jid.resource else "groupchat" | |
83 elt = domish.Element(('jabber:client','message')) | |
84 elt["to"] = to_jid.full() | |
85 elt["type"] = type | |
86 elt.addElement((NC_RADIOCOL, RADIOC_TAG)) | |
87 return elt | |
455
72522263cbc9
plugin RadioCol: basic functionnality working approximately
Goffi <goffi@goffi.org>
parents:
453
diff
changeset
|
88 |
450 | 89 def __create_started_elt(self): |
90 """Create a game_started domish element""" | |
91 started_elt = domish.Element(('','started')) | |
92 return started_elt | |
93 | |
457
fbe7c9118ce4
plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents:
456
diff
changeset
|
94 def __create_preload_elt(self, sender, filename, title, artist, album): |
fbe7c9118ce4
plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents:
456
diff
changeset
|
95 preload_elt = domish.Element(('','preload')) |
fbe7c9118ce4
plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents:
456
diff
changeset
|
96 preload_elt['sender'] = sender |
fbe7c9118ce4
plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents:
456
diff
changeset
|
97 preload_elt['filename'] = filename #XXX: the frontend should know the temporary directory where file is put |
fbe7c9118ce4
plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents:
456
diff
changeset
|
98 preload_elt['title'] = title |
fbe7c9118ce4
plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents:
456
diff
changeset
|
99 preload_elt['artist'] = artist |
fbe7c9118ce4
plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents:
456
diff
changeset
|
100 preload_elt['album'] = album |
fbe7c9118ce4
plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents:
456
diff
changeset
|
101 return preload_elt |
fbe7c9118ce4
plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents:
456
diff
changeset
|
102 |
fbe7c9118ce4
plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents:
456
diff
changeset
|
103 |
450 | 104 def userJoinedTrigger(self, room, user, profile): |
105 """This trigger is used to check if we are waiting people in this room, | |
106 and to create a game if everybody is here""" | |
456
ba6e1feda03e
plugin radiocol: fixed autocreation of radiocol for new comers
Goffi <goffi@goffi.org>
parents:
455
diff
changeset
|
107 room_jid = room.occupantJID.userhost() |
ba6e1feda03e
plugin radiocol: fixed autocreation of radiocol for new comers
Goffi <goffi@goffi.org>
parents:
455
diff
changeset
|
108 if self.radios.has_key(room_jid) and self.radios[room_jid]["referee"] == room.occupantJID.full(): |
450 | 109 #we are in a radiocol room, let's start the party ! |
456
ba6e1feda03e
plugin radiocol: fixed autocreation of radiocol for new comers
Goffi <goffi@goffi.org>
parents:
455
diff
changeset
|
110 mess = self.createRadiocolElt(jid.JID(room_jid+'/'+user.nick)) |
450 | 111 mess.firstChildElement().addChild(self.__create_started_elt()) |
112 self.host.profiles[profile].xmlstream.send(mess) | |
113 return True | |
114 | |
115 def radiocolLaunch(self, occupants, profile_key='@DEFAULT@'): | |
116 """Launch a game: helper method to create a room, invite occupants, and create the radiocol | |
117 @param occupants: list for occupants jid""" | |
118 debug(_('Launching radiocol')) | |
119 profile = self.host.memory.getProfileName(profile_key) | |
120 if not profile: | |
121 error(_("Unknown profile")) | |
122 return | |
123 | |
124 def radiocolRoomJoined(room): | |
125 print "radiocolRoomJoined" | |
126 _room_jid = room.occupantJID.userhostJID() | |
456
ba6e1feda03e
plugin radiocol: fixed autocreation of radiocol for new comers
Goffi <goffi@goffi.org>
parents:
455
diff
changeset
|
127 self.radiocolCreate(_room_jid.userhost(), profile_key=profile) |
450 | 128 for occupant in occupants: |
129 self.host.plugins["XEP-0249"].invite(jid.JID(occupant), room.occupantJID.userhostJID(), {"game":"Radiocol"}, profile) | |
130 | |
131 def after_init(ignore): | |
132 room_name = "sat_radiocol_%s" % self.host.plugins["XEP-0045"].getUniqueName(profile_key) | |
133 print "\n\n===> room_name:", room_name | |
134 muc_service = None | |
135 for service in self.host.memory.getServerServiceEntities("conference", "text", profile): | |
136 if not ".irc." in service.userhost(): | |
137 #FIXME: | |
138 #This awfull ugly hack is here to avoid an issue with openfire: the irc gateway | |
139 #use "conference/text" identity (instead of "conference/irc"), there is certainly a better way | |
140 #to manage this, but this hack fill do it for test purpose | |
141 muc_service = service | |
142 break | |
143 if not muc_service: | |
144 error(_("Can't find a MUC service")) | |
145 return | |
146 | |
147 _jid, xmlstream = self.host.getJidNStream(profile) | |
148 d = self.host.plugins["XEP-0045"].join(jid.JID("%s@%s" % (room_name, muc_service.userhost())), _jid.user, {}, profile) | |
149 d.addCallback(radiocolRoomJoined) | |
150 | |
151 client = self.host.getClient(profile) | |
152 if not client: | |
153 error(_('No client for this profile key: %s') % profile_key) | |
154 return | |
155 client.client_initialized.addCallback(after_init) | |
156 | |
157 def radiocolCreate(self, room_jid_param, profile_key='@DEFAULT@'): | |
158 """Create a new game | |
159 @param room_jid_param: jid of the room | |
160 @param profile_key: %(doc_profile_key)s""" | |
161 debug (_("Creating Radiocol")) | |
162 room_jid = jid.JID(room_jid_param) | |
163 profile = self.host.memory.getProfileName(profile_key) | |
164 if not profile: | |
165 error (_("profile %s is unknown") % profile_key) | |
166 return | |
167 if self.radios.has_key(room_jid): | |
168 warning (_("Radiocol already started in room %s") % room_jid.userhost()) | |
169 else: | |
170 room_nick = self.host.plugins["XEP-0045"].getRoomNick(room_jid.userhost(), profile) | |
171 if not room_nick: | |
172 error ('Internal error') | |
173 return | |
174 referee = room_jid.userhost() + '/' + room_nick | |
175 status = {} | |
176 occupants_data = {} | |
457
fbe7c9118ce4
plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents:
456
diff
changeset
|
177 self.radios[room_jid.userhost()] = {'referee':referee, 'queue':[], 'upload':True, 'playing': False, 'occupants_data':occupants_data} |
450 | 178 mess = self.createRadiocolElt(jid.JID(room_jid.userhost())) |
179 mess.firstChildElement().addChild(self.__create_started_elt()) | |
180 self.host.profiles[profile].xmlstream.send(mess) | |
181 | |
455
72522263cbc9
plugin RadioCol: basic functionnality working approximately
Goffi <goffi@goffi.org>
parents:
453
diff
changeset
|
182 def radiocolSongAdded(self, referee, song_path, profile): |
72522263cbc9
plugin RadioCol: basic functionnality working approximately
Goffi <goffi@goffi.org>
parents:
453
diff
changeset
|
183 """This method is called by libervia when a song has been uploaded |
72522263cbc9
plugin RadioCol: basic functionnality working approximately
Goffi <goffi@goffi.org>
parents:
453
diff
changeset
|
184 @param room_jid_param: jid of the room |
72522263cbc9
plugin RadioCol: basic functionnality working approximately
Goffi <goffi@goffi.org>
parents:
453
diff
changeset
|
185 @song_path: absolute path of the song added |
72522263cbc9
plugin RadioCol: basic functionnality working approximately
Goffi <goffi@goffi.org>
parents:
453
diff
changeset
|
186 @param profile_key: %(doc_profile_key)s""" |
72522263cbc9
plugin RadioCol: basic functionnality working approximately
Goffi <goffi@goffi.org>
parents:
453
diff
changeset
|
187 #XXX: this is a Q&D way for the proof of concept. In the future, the song should |
72522263cbc9
plugin RadioCol: basic functionnality working approximately
Goffi <goffi@goffi.org>
parents:
453
diff
changeset
|
188 # be streamed to the backend using XMPP file copy |
72522263cbc9
plugin RadioCol: basic functionnality working approximately
Goffi <goffi@goffi.org>
parents:
453
diff
changeset
|
189 # Here we cheat because we know we are on the same host, and we don't |
72522263cbc9
plugin RadioCol: basic functionnality working approximately
Goffi <goffi@goffi.org>
parents:
453
diff
changeset
|
190 # check data. Referee will have to parse the song himself to check it |
72522263cbc9
plugin RadioCol: basic functionnality working approximately
Goffi <goffi@goffi.org>
parents:
453
diff
changeset
|
191 client = self.host.getClient(profile) |
72522263cbc9
plugin RadioCol: basic functionnality working approximately
Goffi <goffi@goffi.org>
parents:
453
diff
changeset
|
192 if not client: |
72522263cbc9
plugin RadioCol: basic functionnality working approximately
Goffi <goffi@goffi.org>
parents:
453
diff
changeset
|
193 error(_("Can't access profile's data")) |
72522263cbc9
plugin RadioCol: basic functionnality working approximately
Goffi <goffi@goffi.org>
parents:
453
diff
changeset
|
194 return |
72522263cbc9
plugin RadioCol: basic functionnality working approximately
Goffi <goffi@goffi.org>
parents:
453
diff
changeset
|
195 try: |
72522263cbc9
plugin RadioCol: basic functionnality working approximately
Goffi <goffi@goffi.org>
parents:
453
diff
changeset
|
196 song = OggVorbis(song_path) |
72522263cbc9
plugin RadioCol: basic functionnality working approximately
Goffi <goffi@goffi.org>
parents:
453
diff
changeset
|
197 except OggVorbisHeaderError: |
72522263cbc9
plugin RadioCol: basic functionnality working approximately
Goffi <goffi@goffi.org>
parents:
453
diff
changeset
|
198 #this file is not ogg vorbis, we reject it |
457
fbe7c9118ce4
plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents:
456
diff
changeset
|
199 self.host.bridge.radiocolSongRejected(jid.JID(referee).userhost(), \ |
fbe7c9118ce4
plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents:
456
diff
changeset
|
200 "Uploaded file is not Ogg Vorbis song, only Ogg Vorbis songs are acceptable", profile) |
455
72522263cbc9
plugin RadioCol: basic functionnality working approximately
Goffi <goffi@goffi.org>
parents:
453
diff
changeset
|
201 """mess = self.createRadiocolElt(jid.JID(referee)) |
72522263cbc9
plugin RadioCol: basic functionnality working approximately
Goffi <goffi@goffi.org>
parents:
453
diff
changeset
|
202 reject_elt = mess.firstChildElement().addElement(('','song_rejected')) |
72522263cbc9
plugin RadioCol: basic functionnality working approximately
Goffi <goffi@goffi.org>
parents:
453
diff
changeset
|
203 reject_elt['sender'] = client.jid |
457
fbe7c9118ce4
plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents:
456
diff
changeset
|
204 reject_elt['reason'] = "Uploaded file is not Ogg Vorbis song, only Ogg Vorbis songs are acceptable" |
455
72522263cbc9
plugin RadioCol: basic functionnality working approximately
Goffi <goffi@goffi.org>
parents:
453
diff
changeset
|
205 #FIXME: add an error code |
72522263cbc9
plugin RadioCol: basic functionnality working approximately
Goffi <goffi@goffi.org>
parents:
453
diff
changeset
|
206 self.host.profiles[profile].xmlstream.send(mess)""" |
72522263cbc9
plugin RadioCol: basic functionnality working approximately
Goffi <goffi@goffi.org>
parents:
453
diff
changeset
|
207 return |
72522263cbc9
plugin RadioCol: basic functionnality working approximately
Goffi <goffi@goffi.org>
parents:
453
diff
changeset
|
208 title = song.get("title", ["Unknown"])[0] |
72522263cbc9
plugin RadioCol: basic functionnality working approximately
Goffi <goffi@goffi.org>
parents:
453
diff
changeset
|
209 artist = song.get("artist", ["Unknown"])[0] |
72522263cbc9
plugin RadioCol: basic functionnality working approximately
Goffi <goffi@goffi.org>
parents:
453
diff
changeset
|
210 album = song.get("album", ["Unknown"])[0] |
72522263cbc9
plugin RadioCol: basic functionnality working approximately
Goffi <goffi@goffi.org>
parents:
453
diff
changeset
|
211 length = song.info.length |
72522263cbc9
plugin RadioCol: basic functionnality working approximately
Goffi <goffi@goffi.org>
parents:
453
diff
changeset
|
212 mess = self.createRadiocolElt(jid.JID(referee)) |
72522263cbc9
plugin RadioCol: basic functionnality working approximately
Goffi <goffi@goffi.org>
parents:
453
diff
changeset
|
213 added_elt = mess.firstChildElement().addElement(('','song_added')) |
72522263cbc9
plugin RadioCol: basic functionnality working approximately
Goffi <goffi@goffi.org>
parents:
453
diff
changeset
|
214 added_elt['filename'] = os.path.basename(song_path) |
72522263cbc9
plugin RadioCol: basic functionnality working approximately
Goffi <goffi@goffi.org>
parents:
453
diff
changeset
|
215 added_elt['title'] = title |
72522263cbc9
plugin RadioCol: basic functionnality working approximately
Goffi <goffi@goffi.org>
parents:
453
diff
changeset
|
216 added_elt['artist'] = artist |
72522263cbc9
plugin RadioCol: basic functionnality working approximately
Goffi <goffi@goffi.org>
parents:
453
diff
changeset
|
217 added_elt['album'] = album |
72522263cbc9
plugin RadioCol: basic functionnality working approximately
Goffi <goffi@goffi.org>
parents:
453
diff
changeset
|
218 added_elt['length'] = str(length) |
72522263cbc9
plugin RadioCol: basic functionnality working approximately
Goffi <goffi@goffi.org>
parents:
453
diff
changeset
|
219 self.host.profiles[profile].xmlstream.send(mess) |
72522263cbc9
plugin RadioCol: basic functionnality working approximately
Goffi <goffi@goffi.org>
parents:
453
diff
changeset
|
220 return |
72522263cbc9
plugin RadioCol: basic functionnality working approximately
Goffi <goffi@goffi.org>
parents:
453
diff
changeset
|
221 |
457
fbe7c9118ce4
plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents:
456
diff
changeset
|
222 def playNext(self, room_jid, profile): |
fbe7c9118ce4
plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents:
456
diff
changeset
|
223 """"Play next sont in queue if exists, and put a timer |
fbe7c9118ce4
plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents:
456
diff
changeset
|
224 which trigger after the song has been played to play next one""" |
fbe7c9118ce4
plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents:
456
diff
changeset
|
225 #TODO: need to check that there are still peoples in the room |
fbe7c9118ce4
plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents:
456
diff
changeset
|
226 # and clean the datas/stop the playlist if it's not the case |
fbe7c9118ce4
plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents:
456
diff
changeset
|
227 radio_data = self.radios[room_jid.userhost()] |
fbe7c9118ce4
plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents:
456
diff
changeset
|
228 queue = radio_data['queue'] |
fbe7c9118ce4
plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents:
456
diff
changeset
|
229 if not queue: |
fbe7c9118ce4
plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents:
456
diff
changeset
|
230 #nothing left to play, we need to wait for uploads |
fbe7c9118ce4
plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents:
456
diff
changeset
|
231 radio_data['playing'] = False |
fbe7c9118ce4
plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents:
456
diff
changeset
|
232 return |
fbe7c9118ce4
plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents:
456
diff
changeset
|
233 |
fbe7c9118ce4
plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents:
456
diff
changeset
|
234 filename, length = queue.pop(0) |
fbe7c9118ce4
plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents:
456
diff
changeset
|
235 mess = self.createRadiocolElt(room_jid) |
fbe7c9118ce4
plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents:
456
diff
changeset
|
236 play_elt = mess.firstChildElement().addElement(('','play')) |
fbe7c9118ce4
plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents:
456
diff
changeset
|
237 play_elt['filename'] = filename |
fbe7c9118ce4
plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents:
456
diff
changeset
|
238 self.host.profiles[profile].xmlstream.send(mess) |
fbe7c9118ce4
plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents:
456
diff
changeset
|
239 |
fbe7c9118ce4
plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents:
456
diff
changeset
|
240 if not radio_data['upload'] and len(queue) < QUEUE_LIMIT: |
fbe7c9118ce4
plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents:
456
diff
changeset
|
241 #upload is blocked and we now have resources to get more, we reactivate it |
fbe7c9118ce4
plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents:
456
diff
changeset
|
242 mess = self.createRadiocolElt(room_jid) |
fbe7c9118ce4
plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents:
456
diff
changeset
|
243 no_upload_elt = mess.firstChildElement().addElement(('','upload_ok')) |
fbe7c9118ce4
plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents:
456
diff
changeset
|
244 self.host.profiles[profile].xmlstream.send(mess) |
fbe7c9118ce4
plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents:
456
diff
changeset
|
245 radio_data['upload'] = True |
fbe7c9118ce4
plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents:
456
diff
changeset
|
246 |
fbe7c9118ce4
plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents:
456
diff
changeset
|
247 print ("Playing next song in %s s" % length) |
fbe7c9118ce4
plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents:
456
diff
changeset
|
248 reactor.callLater(length, self.playNext, room_jid, profile) |
fbe7c9118ce4
plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents:
456
diff
changeset
|
249 |
455
72522263cbc9
plugin RadioCol: basic functionnality working approximately
Goffi <goffi@goffi.org>
parents:
453
diff
changeset
|
250 |
450 | 251 def radiocol_game_cmd(self, mess_elt, profile): |
457
fbe7c9118ce4
plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents:
456
diff
changeset
|
252 #FIXME: we should check sender (is it referee ?) here before accepting commands |
450 | 253 from_jid = jid.JID(mess_elt['from']) |
254 room_jid = jid.JID(from_jid.userhost()) | |
255 radio_elt = mess_elt.firstChildElement() | |
256 radio_data = self.radios[room_jid.userhost()] | |
257 occupants_data = radio_data['occupants_data'] | |
457
fbe7c9118ce4
plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents:
456
diff
changeset
|
258 queue = radio_data['queue'] |
450 | 259 |
260 for elt in radio_elt.elements(): | |
261 | |
262 if elt.name == 'started': #new game created | |
453
5731b038fc7f
plugin radiocol: removed occupants list in radiocolStarted signal
Goffi <goffi@goffi.org>
parents:
450
diff
changeset
|
263 self.host.bridge.radiocolStarted(room_jid.userhost(), from_jid.full(), profile) |
455
72522263cbc9
plugin RadioCol: basic functionnality working approximately
Goffi <goffi@goffi.org>
parents:
453
diff
changeset
|
264 elif elt.name == 'preload': #a song is in queue and must be preloaded |
72522263cbc9
plugin RadioCol: basic functionnality working approximately
Goffi <goffi@goffi.org>
parents:
453
diff
changeset
|
265 self.host.bridge.radiocolPreload(room_jid.userhost(), elt['filename'], elt['title'], elt['artist'], elt['album'], profile) |
457
fbe7c9118ce4
plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents:
456
diff
changeset
|
266 elif elt.name == 'play': |
fbe7c9118ce4
plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents:
456
diff
changeset
|
267 self.host.bridge.radiocolPlay(room_jid.userhost(), elt['filename'], profile) |
455
72522263cbc9
plugin RadioCol: basic functionnality working approximately
Goffi <goffi@goffi.org>
parents:
453
diff
changeset
|
268 elif elt.name == 'song_rejected': #a song has been refused |
457
fbe7c9118ce4
plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents:
456
diff
changeset
|
269 self.host.bridge.radiocolSongRejected(jid.JID(referee).userhost(), elt['reason'], profile) |
fbe7c9118ce4
plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents:
456
diff
changeset
|
270 elif elt.name == 'no_upload': |
fbe7c9118ce4
plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents:
456
diff
changeset
|
271 self.host.bridge.radiocolNoUpload(room_jid.userhost(), profile) |
fbe7c9118ce4
plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents:
456
diff
changeset
|
272 elif elt.name == 'upload_ok': |
fbe7c9118ce4
plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents:
456
diff
changeset
|
273 self.host.bridge.radiocolUploadOk(room_jid.userhost(), profile) |
fbe7c9118ce4
plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents:
456
diff
changeset
|
274 elif elt.name == 'song_added': #a song has been added |
fbe7c9118ce4
plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents:
456
diff
changeset
|
275 #FIXME: we are KISS for the proof of concept: every song is added, to a limit of 3 in queue. |
fbe7c9118ce4
plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents:
456
diff
changeset
|
276 # Need to manage some sort of rules to allow peoples to send songs |
fbe7c9118ce4
plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents:
456
diff
changeset
|
277 |
fbe7c9118ce4
plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents:
456
diff
changeset
|
278 if len(queue) >= QUEUE_LIMIT: |
fbe7c9118ce4
plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents:
456
diff
changeset
|
279 #there are already too many songs in queue, we reject this one |
fbe7c9118ce4
plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents:
456
diff
changeset
|
280 mess = self.createRadiocolElt(room_jid) |
fbe7c9118ce4
plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents:
456
diff
changeset
|
281 reject_elt = mess.firstChildElement().addElement(('','song_rejected')) |
fbe7c9118ce4
plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents:
456
diff
changeset
|
282 reject_elt['sender'] = from_jid.resource |
fbe7c9118ce4
plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents:
456
diff
changeset
|
283 reject_elt['reason'] = "Too many songs in queue" |
fbe7c9118ce4
plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents:
456
diff
changeset
|
284 #FIXME: add an error code |
fbe7c9118ce4
plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents:
456
diff
changeset
|
285 self.host.profiles[profile].xmlstream.send(mess) |
fbe7c9118ce4
plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents:
456
diff
changeset
|
286 return |
fbe7c9118ce4
plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents:
456
diff
changeset
|
287 |
fbe7c9118ce4
plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents:
456
diff
changeset
|
288 #The song is accepted and added in queue |
fbe7c9118ce4
plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents:
456
diff
changeset
|
289 queue.append((elt['filename'], float(elt['length']))) |
fbe7c9118ce4
plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents:
456
diff
changeset
|
290 |
fbe7c9118ce4
plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents:
456
diff
changeset
|
291 if len(queue) >= QUEUE_LIMIT: |
fbe7c9118ce4
plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents:
456
diff
changeset
|
292 #We are at the limit, we refuse new upload until next play |
fbe7c9118ce4
plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents:
456
diff
changeset
|
293 mess = self.createRadiocolElt(room_jid) |
fbe7c9118ce4
plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents:
456
diff
changeset
|
294 no_upload_elt = mess.firstChildElement().addElement(('','no_upload')) |
fbe7c9118ce4
plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents:
456
diff
changeset
|
295 #FIXME: add an error code |
fbe7c9118ce4
plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents:
456
diff
changeset
|
296 self.host.profiles[profile].xmlstream.send(mess) |
fbe7c9118ce4
plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents:
456
diff
changeset
|
297 radio_data['upload'] = False |
fbe7c9118ce4
plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents:
456
diff
changeset
|
298 |
fbe7c9118ce4
plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents:
456
diff
changeset
|
299 |
455
72522263cbc9
plugin RadioCol: basic functionnality working approximately
Goffi <goffi@goffi.org>
parents:
453
diff
changeset
|
300 mess = self.createRadiocolElt(room_jid) |
457
fbe7c9118ce4
plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents:
456
diff
changeset
|
301 preload_elt = self.__create_preload_elt(from_jid.resource, |
fbe7c9118ce4
plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents:
456
diff
changeset
|
302 elt['filename'], |
fbe7c9118ce4
plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents:
456
diff
changeset
|
303 elt['title'], |
fbe7c9118ce4
plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents:
456
diff
changeset
|
304 elt['artist'], |
fbe7c9118ce4
plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents:
456
diff
changeset
|
305 elt['album']) |
fbe7c9118ce4
plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents:
456
diff
changeset
|
306 mess.firstChildElement().addChild(preload_elt) |
455
72522263cbc9
plugin RadioCol: basic functionnality working approximately
Goffi <goffi@goffi.org>
parents:
453
diff
changeset
|
307 self.host.profiles[profile].xmlstream.send(mess) |
457
fbe7c9118ce4
plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents:
456
diff
changeset
|
308 if not radio_data['playing'] and len(queue) == 2: |
fbe7c9118ce4
plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents:
456
diff
changeset
|
309 #we have not started playing yet, and we have 2 songs in queue |
fbe7c9118ce4
plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents:
456
diff
changeset
|
310 #we can now start the party :) |
fbe7c9118ce4
plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents:
456
diff
changeset
|
311 radio_data['playing'] = True |
fbe7c9118ce4
plugin radiocol: working Radio Collective \o/
Goffi <goffi@goffi.org>
parents:
456
diff
changeset
|
312 self.playNext(room_jid, profile) |
450 | 313 else: |
314 error (_('Unmanaged game element: %s') % elt.name) | |
315 | |
316 def getHandler(self, profile): | |
317 return RadiocolHandler(self) | |
318 | |
319 class RadiocolHandler (XMPPHandler): | |
320 implements(iwokkel.IDisco) | |
321 | |
322 def __init__(self, plugin_parent): | |
323 self.plugin_parent = plugin_parent | |
324 self.host = plugin_parent.host | |
325 | |
326 def connectionInitialized(self): | |
327 self.xmlstream.addObserver(RADIOC_REQUEST, self.plugin_parent.radiocol_game_cmd, profile = self.parent.profile) | |
328 | |
329 def getDiscoInfo(self, requestor, target, nodeIdentifier=''): | |
330 return [disco.DiscoFeature(NC_RADIOCOL)] | |
331 | |
332 def getDiscoItems(self, requestor, target, nodeIdentifier=''): | |
333 return [] | |
334 |