Mercurial > libervia-backend
annotate src/plugins/plugin_misc_radiocol.py @ 455:72522263cbc9
plugin RadioCol: basic functionnality working approximately
author | Goffi <goffi@goffi.org> |
---|---|
date | Mon, 23 Jan 2012 00:14:33 +0100 |
parents | 5731b038fc7f |
children | ba6e1feda03e |
rev | line source |
---|---|
450 | 1 #!/usr/bin/python |
2 # -*- coding: utf-8 -*- | |
3 | |
4 """ | |
5 SAT plugin for managing Radiocol | |
6 Copyright (C) 2009, 2010, 2011 Jérôme Poisson (goffi@goffi.org) | |
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 | |
61 | |
62 class Radiocol(): | |
63 | |
64 def __init__(self, host): | |
65 info(_("Radio collective initialization")) | |
66 self.host = host | |
67 self.radios={} | |
455
72522263cbc9
plugin RadioCol: basic functionnality working approximately
Goffi <goffi@goffi.org>
parents:
453
diff
changeset
|
68 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
|
69 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
|
70 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
|
71 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
|
72 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
|
73 host.bridge.addSignal("radiocolPreload", ".plugin", signature='ssssss') #room_jid, filename, title, artist, album, profile |
450 | 74 host.trigger.add("MUC user joined", self.userJoinedTrigger) |
75 | |
76 def createRadiocolElt(self, to_jid, type="normal"): | |
77 type = "normal" if to_jid.resource else "groupchat" | |
78 elt = domish.Element(('jabber:client','message')) | |
79 elt["to"] = to_jid.full() | |
80 elt["type"] = type | |
81 elt.addElement((NC_RADIOCOL, RADIOC_TAG)) | |
82 return elt | |
455
72522263cbc9
plugin RadioCol: basic functionnality working approximately
Goffi <goffi@goffi.org>
parents:
453
diff
changeset
|
83 |
450 | 84 def __create_started_elt(self): |
85 """Create a game_started domish element""" | |
86 started_elt = domish.Element(('','started')) | |
87 return started_elt | |
88 | |
89 def userJoinedTrigger(self, room, user, profile): | |
90 """This trigger is used to check if we are waiting people in this room, | |
91 and to create a game if everybody is here""" | |
92 room_jid = room.occupantJID.userhostJID() | |
93 if self.radios.has_key(room_jid): | |
94 #we are in a radiocol room, let's start the party ! | |
95 mess = self.createRadiocolElt(jid.JID(room_jid.userhost()+'/'+occupant)) | |
96 mess.firstChildElement().addChild(self.__create_started_elt()) | |
97 self.host.profiles[profile].xmlstream.send(mess) | |
98 return True | |
99 | |
100 def radiocolLaunch(self, occupants, profile_key='@DEFAULT@'): | |
101 """Launch a game: helper method to create a room, invite occupants, and create the radiocol | |
102 @param occupants: list for occupants jid""" | |
103 debug(_('Launching radiocol')) | |
104 profile = self.host.memory.getProfileName(profile_key) | |
105 if not profile: | |
106 error(_("Unknown profile")) | |
107 return | |
108 | |
109 def radiocolRoomJoined(room): | |
110 print "radiocolRoomJoined" | |
111 _room_jid = room.occupantJID.userhostJID() | |
112 for occupant in occupants: | |
113 self.host.plugins["XEP-0249"].invite(jid.JID(occupant), room.occupantJID.userhostJID(), {"game":"Radiocol"}, profile) | |
114 self.radiocolCreate(_room_jid.userhost(), profile_key=profile) | |
115 | |
116 def after_init(ignore): | |
117 room_name = "sat_radiocol_%s" % self.host.plugins["XEP-0045"].getUniqueName(profile_key) | |
118 print "\n\n===> room_name:", room_name | |
119 muc_service = None | |
120 for service in self.host.memory.getServerServiceEntities("conference", "text", profile): | |
121 if not ".irc." in service.userhost(): | |
122 #FIXME: | |
123 #This awfull ugly hack is here to avoid an issue with openfire: the irc gateway | |
124 #use "conference/text" identity (instead of "conference/irc"), there is certainly a better way | |
125 #to manage this, but this hack fill do it for test purpose | |
126 muc_service = service | |
127 break | |
128 if not muc_service: | |
129 error(_("Can't find a MUC service")) | |
130 return | |
131 | |
132 _jid, xmlstream = self.host.getJidNStream(profile) | |
133 d = self.host.plugins["XEP-0045"].join(jid.JID("%s@%s" % (room_name, muc_service.userhost())), _jid.user, {}, profile) | |
134 d.addCallback(radiocolRoomJoined) | |
135 | |
136 client = self.host.getClient(profile) | |
137 if not client: | |
138 error(_('No client for this profile key: %s') % profile_key) | |
139 return | |
140 client.client_initialized.addCallback(after_init) | |
141 | |
142 def radiocolCreate(self, room_jid_param, profile_key='@DEFAULT@'): | |
143 """Create a new game | |
144 @param room_jid_param: jid of the room | |
145 @param profile_key: %(doc_profile_key)s""" | |
146 debug (_("Creating Radiocol")) | |
147 room_jid = jid.JID(room_jid_param) | |
148 profile = self.host.memory.getProfileName(profile_key) | |
149 if not profile: | |
150 error (_("profile %s is unknown") % profile_key) | |
151 return | |
152 if self.radios.has_key(room_jid): | |
153 warning (_("Radiocol already started in room %s") % room_jid.userhost()) | |
154 else: | |
155 room_nick = self.host.plugins["XEP-0045"].getRoomNick(room_jid.userhost(), profile) | |
156 if not room_nick: | |
157 error ('Internal error') | |
158 return | |
159 referee = room_jid.userhost() + '/' + room_nick | |
160 status = {} | |
161 occupants_data = {} | |
162 self.radios[room_jid.userhost()] = {'referee':referee, 'occupants_data':occupants_data} | |
163 mess = self.createRadiocolElt(jid.JID(room_jid.userhost())) | |
164 mess.firstChildElement().addChild(self.__create_started_elt()) | |
165 self.host.profiles[profile].xmlstream.send(mess) | |
166 | |
455
72522263cbc9
plugin RadioCol: basic functionnality working approximately
Goffi <goffi@goffi.org>
parents:
453
diff
changeset
|
167 def radiocolSongAdded(self, referee, song_path, profile): |
72522263cbc9
plugin RadioCol: basic functionnality working approximately
Goffi <goffi@goffi.org>
parents:
453
diff
changeset
|
168 """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
|
169 @param room_jid_param: jid of the room |
72522263cbc9
plugin RadioCol: basic functionnality working approximately
Goffi <goffi@goffi.org>
parents:
453
diff
changeset
|
170 @song_path: absolute path of the song added |
72522263cbc9
plugin RadioCol: basic functionnality working approximately
Goffi <goffi@goffi.org>
parents:
453
diff
changeset
|
171 @param profile_key: %(doc_profile_key)s""" |
72522263cbc9
plugin RadioCol: basic functionnality working approximately
Goffi <goffi@goffi.org>
parents:
453
diff
changeset
|
172 #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
|
173 # be streamed to the backend using XMPP file copy |
72522263cbc9
plugin RadioCol: basic functionnality working approximately
Goffi <goffi@goffi.org>
parents:
453
diff
changeset
|
174 # 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
|
175 # 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
|
176 client = self.host.getClient(profile) |
72522263cbc9
plugin RadioCol: basic functionnality working approximately
Goffi <goffi@goffi.org>
parents:
453
diff
changeset
|
177 if not client: |
72522263cbc9
plugin RadioCol: basic functionnality working approximately
Goffi <goffi@goffi.org>
parents:
453
diff
changeset
|
178 error(_("Can't access profile's data")) |
72522263cbc9
plugin RadioCol: basic functionnality working approximately
Goffi <goffi@goffi.org>
parents:
453
diff
changeset
|
179 return |
72522263cbc9
plugin RadioCol: basic functionnality working approximately
Goffi <goffi@goffi.org>
parents:
453
diff
changeset
|
180 try: |
72522263cbc9
plugin RadioCol: basic functionnality working approximately
Goffi <goffi@goffi.org>
parents:
453
diff
changeset
|
181 song = OggVorbis(song_path) |
72522263cbc9
plugin RadioCol: basic functionnality working approximately
Goffi <goffi@goffi.org>
parents:
453
diff
changeset
|
182 except OggVorbisHeaderError: |
72522263cbc9
plugin RadioCol: basic functionnality working approximately
Goffi <goffi@goffi.org>
parents:
453
diff
changeset
|
183 #this file is not ogg vorbis, we reject it |
72522263cbc9
plugin RadioCol: basic functionnality working approximately
Goffi <goffi@goffi.org>
parents:
453
diff
changeset
|
184 import pdb |
72522263cbc9
plugin RadioCol: basic functionnality working approximately
Goffi <goffi@goffi.org>
parents:
453
diff
changeset
|
185 pdb.set_trace() |
72522263cbc9
plugin RadioCol: basic functionnality working approximately
Goffi <goffi@goffi.org>
parents:
453
diff
changeset
|
186 """mess = self.createRadiocolElt(jid.JID(referee)) |
72522263cbc9
plugin RadioCol: basic functionnality working approximately
Goffi <goffi@goffi.org>
parents:
453
diff
changeset
|
187 reject_elt = mess.firstChildElement().addElement(('','song_rejected')) |
72522263cbc9
plugin RadioCol: basic functionnality working approximately
Goffi <goffi@goffi.org>
parents:
453
diff
changeset
|
188 reject_elt['sender'] = client.jid |
72522263cbc9
plugin RadioCol: basic functionnality working approximately
Goffi <goffi@goffi.org>
parents:
453
diff
changeset
|
189 reject_elt['reason'] = _("Uploaded file is not Ogg Vorbis song, only Ogg Vorbis songs are acceptable") |
72522263cbc9
plugin RadioCol: basic functionnality working approximately
Goffi <goffi@goffi.org>
parents:
453
diff
changeset
|
190 #FIXME: add an error code |
72522263cbc9
plugin RadioCol: basic functionnality working approximately
Goffi <goffi@goffi.org>
parents:
453
diff
changeset
|
191 self.host.profiles[profile].xmlstream.send(mess)""" |
72522263cbc9
plugin RadioCol: basic functionnality working approximately
Goffi <goffi@goffi.org>
parents:
453
diff
changeset
|
192 return |
72522263cbc9
plugin RadioCol: basic functionnality working approximately
Goffi <goffi@goffi.org>
parents:
453
diff
changeset
|
193 title = song.get("title", ["Unknown"])[0] |
72522263cbc9
plugin RadioCol: basic functionnality working approximately
Goffi <goffi@goffi.org>
parents:
453
diff
changeset
|
194 artist = song.get("artist", ["Unknown"])[0] |
72522263cbc9
plugin RadioCol: basic functionnality working approximately
Goffi <goffi@goffi.org>
parents:
453
diff
changeset
|
195 album = song.get("album", ["Unknown"])[0] |
72522263cbc9
plugin RadioCol: basic functionnality working approximately
Goffi <goffi@goffi.org>
parents:
453
diff
changeset
|
196 length = song.info.length |
72522263cbc9
plugin RadioCol: basic functionnality working approximately
Goffi <goffi@goffi.org>
parents:
453
diff
changeset
|
197 mess = self.createRadiocolElt(jid.JID(referee)) |
72522263cbc9
plugin RadioCol: basic functionnality working approximately
Goffi <goffi@goffi.org>
parents:
453
diff
changeset
|
198 added_elt = mess.firstChildElement().addElement(('','song_added')) |
72522263cbc9
plugin RadioCol: basic functionnality working approximately
Goffi <goffi@goffi.org>
parents:
453
diff
changeset
|
199 added_elt['filename'] = os.path.basename(song_path) |
72522263cbc9
plugin RadioCol: basic functionnality working approximately
Goffi <goffi@goffi.org>
parents:
453
diff
changeset
|
200 added_elt['title'] = title |
72522263cbc9
plugin RadioCol: basic functionnality working approximately
Goffi <goffi@goffi.org>
parents:
453
diff
changeset
|
201 added_elt['artist'] = artist |
72522263cbc9
plugin RadioCol: basic functionnality working approximately
Goffi <goffi@goffi.org>
parents:
453
diff
changeset
|
202 added_elt['album'] = album |
72522263cbc9
plugin RadioCol: basic functionnality working approximately
Goffi <goffi@goffi.org>
parents:
453
diff
changeset
|
203 added_elt['length'] = str(length) |
72522263cbc9
plugin RadioCol: basic functionnality working approximately
Goffi <goffi@goffi.org>
parents:
453
diff
changeset
|
204 self.host.profiles[profile].xmlstream.send(mess) |
72522263cbc9
plugin RadioCol: basic functionnality working approximately
Goffi <goffi@goffi.org>
parents:
453
diff
changeset
|
205 return |
72522263cbc9
plugin RadioCol: basic functionnality working approximately
Goffi <goffi@goffi.org>
parents:
453
diff
changeset
|
206 |
72522263cbc9
plugin RadioCol: basic functionnality working approximately
Goffi <goffi@goffi.org>
parents:
453
diff
changeset
|
207 |
72522263cbc9
plugin RadioCol: basic functionnality working approximately
Goffi <goffi@goffi.org>
parents:
453
diff
changeset
|
208 |
450 | 209 def radiocol_game_cmd(self, mess_elt, profile): |
210 from_jid = jid.JID(mess_elt['from']) | |
211 room_jid = jid.JID(from_jid.userhost()) | |
212 radio_elt = mess_elt.firstChildElement() | |
213 radio_data = self.radios[room_jid.userhost()] | |
214 occupants_data = radio_data['occupants_data'] | |
215 | |
216 for elt in radio_elt.elements(): | |
217 | |
218 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
|
219 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
|
220 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
|
221 self.host.bridge.radiocolPreload(room_jid.userhost(), elt['filename'], elt['title'], elt['artist'], elt['album'], profile) |
72522263cbc9
plugin RadioCol: basic functionnality working approximately
Goffi <goffi@goffi.org>
parents:
453
diff
changeset
|
222 elif elt.name == 'song_rejected': #a song has been refused |
72522263cbc9
plugin RadioCol: basic functionnality working approximately
Goffi <goffi@goffi.org>
parents:
453
diff
changeset
|
223 import pdb |
72522263cbc9
plugin RadioCol: basic functionnality working approximately
Goffi <goffi@goffi.org>
parents:
453
diff
changeset
|
224 pdb.set_trace() |
72522263cbc9
plugin RadioCol: basic functionnality working approximately
Goffi <goffi@goffi.org>
parents:
453
diff
changeset
|
225 elif elt.name == 'song_added': #a song has been refused |
72522263cbc9
plugin RadioCol: basic functionnality working approximately
Goffi <goffi@goffi.org>
parents:
453
diff
changeset
|
226 #FIXME: we are KISS for the PoC: every song is added. Need to manage some sort of rules to allow peoples to send songs |
72522263cbc9
plugin RadioCol: basic functionnality working approximately
Goffi <goffi@goffi.org>
parents:
453
diff
changeset
|
227 mess = self.createRadiocolElt(room_jid) |
72522263cbc9
plugin RadioCol: basic functionnality working approximately
Goffi <goffi@goffi.org>
parents:
453
diff
changeset
|
228 preload_elt = mess.firstChildElement().addElement(('','preload')) |
72522263cbc9
plugin RadioCol: basic functionnality working approximately
Goffi <goffi@goffi.org>
parents:
453
diff
changeset
|
229 preload_elt['sender'] = from_jid.resource |
72522263cbc9
plugin RadioCol: basic functionnality working approximately
Goffi <goffi@goffi.org>
parents:
453
diff
changeset
|
230 preload_elt['filename'] = elt['filename'] #XXX: the frontend should know the temporary directory where file is put |
72522263cbc9
plugin RadioCol: basic functionnality working approximately
Goffi <goffi@goffi.org>
parents:
453
diff
changeset
|
231 preload_elt['title'] = elt['title'] |
72522263cbc9
plugin RadioCol: basic functionnality working approximately
Goffi <goffi@goffi.org>
parents:
453
diff
changeset
|
232 preload_elt['artist'] = elt['artist'] |
72522263cbc9
plugin RadioCol: basic functionnality working approximately
Goffi <goffi@goffi.org>
parents:
453
diff
changeset
|
233 preload_elt['album'] = elt['album'] |
72522263cbc9
plugin RadioCol: basic functionnality working approximately
Goffi <goffi@goffi.org>
parents:
453
diff
changeset
|
234 preload_elt['length'] = elt['length'] |
72522263cbc9
plugin RadioCol: basic functionnality working approximately
Goffi <goffi@goffi.org>
parents:
453
diff
changeset
|
235 self.host.profiles[profile].xmlstream.send(mess) |
450 | 236 else: |
237 error (_('Unmanaged game element: %s') % elt.name) | |
238 | |
239 def getHandler(self, profile): | |
240 return RadiocolHandler(self) | |
241 | |
242 class RadiocolHandler (XMPPHandler): | |
243 implements(iwokkel.IDisco) | |
244 | |
245 def __init__(self, plugin_parent): | |
246 self.plugin_parent = plugin_parent | |
247 self.host = plugin_parent.host | |
248 | |
249 def connectionInitialized(self): | |
250 self.xmlstream.addObserver(RADIOC_REQUEST, self.plugin_parent.radiocol_game_cmd, profile = self.parent.profile) | |
251 | |
252 def getDiscoInfo(self, requestor, target, nodeIdentifier=''): | |
253 return [disco.DiscoFeature(NC_RADIOCOL)] | |
254 | |
255 def getDiscoItems(self, requestor, target, nodeIdentifier=''): | |
256 return [] | |
257 |