Forked mumble-django project from https://bitbucket.org/Svedrin/mumble-django
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

369 lines
12 KiB

16 years ago
16 years ago
  1. # -*- coding: utf-8 -*-
  2. """
  3. * Copyright (C) 2009, withgod <withgod@sourceforge.net>
  4. * Michael "Svedrin" Ziegler <diese-addy@funzt-halt.net>
  5. *
  6. * Mumble-Django is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This package is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. """
  16. from os.path import join
  17. from PIL import Image
  18. from struct import pack, unpack
  19. from zlib import compress, decompress
  20. from django.conf import settings
  21. from mctl import MumbleCtlBase
  22. import Ice
  23. def MumbleCtlIce( connstring ):
  24. version = settings.SLICE_VERSION;
  25. slice = settings.SLICE;
  26. if not slice:
  27. slice = join(
  28. settings.MUMBLE_DJANGO_ROOT, 'pyweb', 'mumble',
  29. 'Murmur_%d-%d-%d.ice' % (version[0], version[1], version[2] )
  30. );
  31. Ice.loadSlice( slice )
  32. ice = Ice.initialize()
  33. import Murmur
  34. prx = ice.stringToProxy( connstring.encode("utf-8") )
  35. meta = Murmur.MetaPrx.checkedCast(prx)
  36. murmurversion = meta.getVersion();
  37. if murmurversion[0] != version[0] or murmurversion[1] != version[1] or murmurversion[2] != version[2]:
  38. raise TypeError(
  39. "Detected Murmur version %d.%d.%d, for which I am not configured." % ( murmurversion[0], murmurversion[1], murmurversion[2] )
  40. );
  41. if murmurversion[0] == murmurversion[1] == 1 and murmurversion[2] <= 8:
  42. return MumbleCtlIce_118( connstring, meta );
  43. elif murmurversion[0] == 1 and murmurversion[1] == 2 and murmurversion[2] == 0:
  44. return MumbleCtlIce_120( connstring, meta );
  45. class MumbleCtlIce_118(MumbleCtlBase):
  46. method = "ICE";
  47. def __init__( self, connstring, meta ):
  48. self.proxy = connstring;
  49. self.meta = meta;
  50. def _getIceServerObject(self, srvid):
  51. return self.meta.getServer(srvid);
  52. def getBootedServers(self):
  53. ret = []
  54. for x in self.meta.getBootedServers():
  55. ret.append(x.id())
  56. return ret
  57. def getVersion( self ):
  58. return self.meta.getVersion();
  59. def getAllServers(self):
  60. ret = []
  61. for x in self.meta.getAllServers():
  62. ret.append(x.id())
  63. return ret
  64. def getRegisteredPlayers(self, srvid, filter = ''):
  65. users = self._getIceServerObject(srvid).getRegisteredPlayers( filter.encode( "UTF-8" ) )
  66. ret = []
  67. for user in users:
  68. ret.append([user.playerid, self.setUnicodeFlag(user.name), self.setUnicodeFlag(user.email), self.setUnicodeFlag(user.pw)])
  69. return ret
  70. def getChannels(self, srvid):
  71. chans = self._getIceServerObject(srvid).getChannels()
  72. ret = []
  73. for x in chans:
  74. chan = chans[x]
  75. ret.append([chan.id, self.setUnicodeFlag(chan.name), chan.parent, chan.links])
  76. return ret
  77. def getPlayers(self, srvid):
  78. users = self._getIceServerObject(srvid).getPlayers()
  79. ret = []
  80. for x in users:
  81. user = users[x]
  82. ret.append([user.session, user.mute, user.deaf, user.suppressed, user.selfMute, user.selfDeaf, user.channel, user.playerid, self.setUnicodeFlag(user.name), user.onlinesecs, user.bytespersec])
  83. return ret
  84. def getDefaultConf(self):
  85. return self.setUnicodeFlag(self.meta.getDefaultConf())
  86. def getAllConf(self, srvid):
  87. return self.setUnicodeFlag(self._getIceServerObject(srvid).getAllConf())
  88. def newServer(self):
  89. return self.meta.newServer().id()
  90. def isBooted( self, srvid ):
  91. return bool( self._getIceServerObject(srvid).isRunning() );
  92. def start( self, srvid ):
  93. self._getIceServerObject(srvid).start();
  94. def stop( self, srvid ):
  95. self._getIceServerObject(srvid).stop();
  96. def deleteServer( self, srvid ):
  97. if self._getIceServerObject(srvid).isRunning():
  98. self._getIceServerObject(srvid).stop()
  99. self._getIceServerObject(srvid).delete()
  100. def setSuperUserPassword(self, srvid, value):
  101. self._getIceServerObject(srvid).setSuperuserPassword( value.encode( "UTF-8" ) )
  102. def setConf(self, srvid, key, value):
  103. self._getIceServerObject(srvid).setConf( key, value.encode( "UTF-8" ) )
  104. def registerPlayer(self, srvid, name, email, password):
  105. mumbleid = self._getIceServerObject(srvid).registerPlayer( name.encode( "UTF-8" ) )
  106. self.setRegistration( srvid, mumbleid, name, email, password );
  107. return mumbleid;
  108. def unregisterPlayer(self, srvid, mumbleid):
  109. self._getIceServerObject(srvid).unregisterPlayer(mumbleid)
  110. def getRegistration(self, srvid, mumbleid):
  111. user = self._getIceServerObject(srvid).getRegistration(mumbleid)
  112. return {
  113. 'name': user.name,
  114. 'email': user.email,
  115. };
  116. def setRegistration(self, srvid, mumbleid, name, email, password):
  117. user = self._getIceServerObject(srvid).getRegistration(mumbleid)
  118. user.name = name.encode( "UTF-8" )
  119. user.email = email.encode( "UTF-8" )
  120. user.pw = password.encode( "UTF-8" )
  121. # update*r*egistration r is lowercase...
  122. return self._getIceServerObject(srvid).updateregistration(user)
  123. def getACL(self, srvid, channelid):
  124. import Murmur
  125. acls = self._getIceServerObject(srvid).getACL(channelid)
  126. ret = []
  127. for x in acls:
  128. if isinstance(x, list):
  129. tmp = []
  130. for y in x:
  131. if y.__class__ is Murmur.ACL:
  132. tmp.append([y.applyHere, y.applySubs, y.inherited, y.playerid, self.setUnicodeFlag(y.group), y.allow, y.deny])
  133. elif y.__class__ is Murmur.Group:
  134. tmp.append([self.setUnicodeFlag(y.name), y.inherited, y.inherit, y.inheritable, y.add, y.remove, y.members])
  135. ret.append(tmp)
  136. else:
  137. ret.append(x)
  138. return ret
  139. def setACL(self, srvid, acl):
  140. import Murmur
  141. newacls = [];
  142. newgroups = [];
  143. for curr_acl in acl.acls:
  144. new_acl = Murmur.ACL();
  145. new_acl.applyHere = curr_acl['applyHere'];
  146. new_acl.applySubs = curr_acl['applySubs'];
  147. new_acl.inherited = curr_acl['inherited'];
  148. new_acl.playerid = curr_acl['playerid'];
  149. new_acl.group = curr_acl['group'].encode( "UTF-8" );
  150. new_acl.allow = curr_acl['allow'];
  151. new_acl.deny = curr_acl['deny'];
  152. newacls.append( new_acl );
  153. for curr_group in acl.groups:
  154. new_group = Murmur.Group()
  155. new_group.name = curr_group['name'].encode( "UTF-8" );
  156. new_group.inherited = curr_group['inherited'];
  157. new_group.inherit = curr_group['inherit'];
  158. new_group.inheritable = curr_group['inheritable'];
  159. new_group.add = curr_group['add'];
  160. new_group.remove = curr_group['remove'];
  161. new_group.members = curr_group['members'];
  162. newgroups.append( new_group );
  163. self._getIceServerObject(srvid).setACL( acl.channelId, newacls, newgroups, acl.inherit );
  164. def getTexture(self, srvid, mumbleid):
  165. texture = self._getIceServerObject(srvid).getTexture(mumbleid)
  166. if len(texture) == 0:
  167. raise ValueError( "No Texture has been set." );
  168. # this returns a list of bytes.
  169. decompressed = decompress( texture );
  170. # iterate over 4 byte chunks of the string
  171. imgdata = "";
  172. for idx in range( 0, len(decompressed), 4 ):
  173. # read 4 bytes = BGRA and convert to RGBA
  174. # manual wrote getTexture returns "Textures are stored as zlib compress()ed 600x60 32-bit RGBA data."
  175. # http://mumble.sourceforge.net/slice/Murmur/Server.html#getTexture
  176. # but return values BGRA X(
  177. bgra = unpack( "4B", decompressed[idx:idx+4] );
  178. imgdata += pack( "4B", bgra[2], bgra[1], bgra[0], bgra[3] );
  179. # return an 600x60 RGBA image object created from the data
  180. return Image.fromstring( "RGBA", ( 600, 60 ), imgdata );
  181. def setTexture(self, srvid, mumbleid, infile):
  182. # open image, convert to RGBA, and resize to 600x60
  183. img = Image.open( infile ).convert( "RGBA" ).transform( ( 600, 60 ), Image.EXTENT, ( 0, 0, 600, 60 ) );
  184. # iterate over the list and pack everything into a string
  185. bgrastring = "";
  186. for ent in list( img.getdata() ):
  187. # ent is in RGBA format, but Murmur wants BGRA (ARGB inverse), so stuff needs
  188. # to be reordered when passed to pack()
  189. bgrastring += pack( "4B", ent[2], ent[1], ent[0], ent[3] );
  190. # compress using zlib
  191. compressed = compress( bgrastring );
  192. # pack the original length in 4 byte big endian, and concat the compressed
  193. # data to it to emulate qCompress().
  194. texture = pack( ">L", len(bgrastring) ) + compressed;
  195. # finally call murmur and set the texture
  196. self._getIceServerObject(srvid).setTexture(mumbleid, texture)
  197. @staticmethod
  198. def setUnicodeFlag(data):
  199. ret = ''
  200. if isinstance(data, tuple) or isinstance(data, list) or isinstance(data, dict):
  201. ret = {}
  202. for key in data.keys():
  203. ret[MumbleCtlIce_118.setUnicodeFlag(key)] = MumbleCtlIce_118.setUnicodeFlag(data[key])
  204. else:
  205. ret = unicode(data, 'utf-8')
  206. return ret
  207. class MumbleCtlIce_120(MumbleCtlIce_118):
  208. def getRegisteredPlayers(self, srvid, filter = ''):
  209. users = self._getIceServerObject( srvid ).getRegisteredUsers( filter.encode( "UTF-8" ) )
  210. ret = []
  211. for user in users:
  212. ret.append([user.playerid, self.setUnicodeFlag(user.name), self.setUnicodeFlag(user.email), self.setUnicodeFlag(user.pw)])
  213. return ret
  214. def getChannels(self, srvid):
  215. chans = self._getIceServerObject(srvid).getChannels()
  216. ret = []
  217. for x in chans:
  218. chan = chans[x]
  219. ret.append([chan.id, self.setUnicodeFlag(chan.name), chan.parent, chan.links, chan.description])
  220. return ret
  221. def getPlayers(self, srvid):
  222. serv = self._getIceServerObject(srvid);
  223. users = serv.getUsers()
  224. ret = []
  225. for x in users:
  226. user = users[x]
  227. ret.append([user.session, user.mute, user.deaf, user.suppressed, user.selfMute, user.selfDeaf, user.channel, user.userid, self.setUnicodeFlag(user.name), user.onlinesecs, user.bytespersec])
  228. return ret
  229. def registerPlayer(self, srvid, name, email, password):
  230. user = {
  231. 'name': name.encode( "UTF-8" ),
  232. 'email': email.encode( "UTF-8" ),
  233. 'pw': password.encode( "UTF-8" ),
  234. };
  235. return self._getIceServerObject(srvid).registerUser( user );
  236. def unregisterPlayer(self, srvid, mumbleid):
  237. self._getIceServerObject(srvid).unregisterUser(mumbleid)
  238. def getRegistration(self, srvid, mumbleid):
  239. return self._getIceServerObject( srvid ).getRegistration( mumbleid )
  240. def setRegistration(self, srvid, mumbleid, name, email, password):
  241. user = self._getIceServerObject( srvid ).getRegistration( mumbleid )
  242. user['name'] = name.encode( "UTF-8" )
  243. user['email'] = email.encode( "UTF-8" )
  244. user['pw'] = password.encode( "UTF-8" )
  245. return self._getIceServerObject( srvid ).updateRegistration( mumbleid, user )
  246. def getACL(self, srvid, channelid):
  247. import Murmur
  248. acls = self._getIceServerObject(srvid).getACL(channelid)
  249. ret = []
  250. for x in acls:
  251. if isinstance(x, list):
  252. tmp = []
  253. for y in x:
  254. if y.__class__ is Murmur.ACL:
  255. tmp.append([y.applyHere, y.applySubs, y.inherited, y.userid, self.setUnicodeFlag(y.group), y.allow, y.deny])
  256. elif y.__class__ is Murmur.Group:
  257. tmp.append([self.setUnicodeFlag(y.name), y.inherited, y.inherit, y.inheritable, y.add, y.remove, y.members])
  258. ret.append(tmp)
  259. else:
  260. ret.append(x)
  261. return ret
  262. def setACL(self, srvid, acl):
  263. import Murmur
  264. newacls = [];
  265. newgroups = [];
  266. for curr_acl in acl.acls:
  267. new_acl = Murmur.ACL();
  268. new_acl.applyHere = curr_acl['applyHere'];
  269. new_acl.applySubs = curr_acl['applySubs'];
  270. new_acl.inherited = curr_acl['inherited'];
  271. new_acl.userid = curr_acl['playerid'];
  272. new_acl.group = curr_acl['group'].encode( "UTF-8" );
  273. new_acl.allow = curr_acl['allow'];
  274. new_acl.deny = curr_acl['deny'];
  275. newacls.append( new_acl );
  276. for curr_group in acl.groups:
  277. new_group = Murmur.Group()
  278. new_group.name = curr_group['name'].encode( "UTF-8" );
  279. new_group.inherited = curr_group['inherited'];
  280. new_group.inherit = curr_group['inherit'];
  281. new_group.inheritable = curr_group['inheritable'];
  282. new_group.add = curr_group['add'];
  283. new_group.remove = curr_group['remove'];
  284. new_group.members = curr_group['members'];
  285. newgroups.append( new_group );
  286. self._getIceServerObject(srvid).setACL( acl.channelId, newacls, newgroups, acl.inherit );