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.

351 lines
11 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 getACL(self, srvid, identifier):
  85. import Murmur
  86. acls = self._getIceServerObject(srvid).getACL(identifier)
  87. ret = []
  88. for x in acls:
  89. if isinstance(x, list):
  90. tmp = []
  91. for y in x:
  92. if y.__class__ is Murmur.ACL:
  93. tmp.append([y.applyHere, y.applySubs, y.inherited, y.playerid, self.setUnicodeFlag(y.group), y.allow, y.deny])
  94. elif y.__class__ is Murmur.Group:
  95. tmp.append([self.setUnicodeFlag(y.name), y.inherited, y.inherit, y.inheritable, y.add, y.remove, y.members])
  96. ret.append(tmp)
  97. else:
  98. ret.append(x)
  99. return ret
  100. def getDefaultConf(self):
  101. return self.setUnicodeFlag(self.meta.getDefaultConf())
  102. def getAllConf(self, srvid):
  103. return self.setUnicodeFlag(self._getIceServerObject(srvid).getAllConf())
  104. def newServer(self):
  105. return self.meta.newServer().id()
  106. def isBooted( self, srvid ):
  107. return bool( self._getIceServerObject(srvid).isRunning() );
  108. def start( self, srvid ):
  109. self._getIceServerObject(srvid).start();
  110. def stop( self, srvid ):
  111. self._getIceServerObject(srvid).stop();
  112. def deleteServer( self, srvid ):
  113. if self._getIceServerObject(srvid).isRunning():
  114. self._getIceServerObject(srvid).stop()
  115. self._getIceServerObject(srvid).delete()
  116. def setSuperUserPassword(self, srvid, value):
  117. self.meta.setSuperUserPassword( srvid, value.encode( "UTF-8" ) )
  118. def setConf(self, srvid, key, value):
  119. self._getIceServerObject(srvid).setConf( key, value.encode( "UTF-8" ) )
  120. def registerPlayer(self, srvid, name):
  121. return self._getIceServerObject(srvid).registerPlayer( name.encode( "UTF-8" ) )
  122. def unregisterPlayer(self, srvid, mumbleid):
  123. self._getIceServerObject(srvid).unregisterPlayer(mumbleid)
  124. def setRegistration(self, srvid, mumbleid, name, email, password):
  125. user = self._getIceServerObject(srvid).getRegistration(mumbleid)
  126. user.name = name.encode( "UTF-8" )
  127. user.email = email.encode( "UTF-8" )
  128. user.pw = password.encode( "UTF-8" )
  129. #print user
  130. # update*r*egistration r is lowercase...
  131. return self._getIceServerObject(srvid).updateregistration(user)
  132. def setACL(self, srvid, acl):
  133. import Murmur
  134. newacls = [];
  135. newgroups = [];
  136. for curr_acl in acl.acls:
  137. new_acl = Murmur.ACL();
  138. new_acl.applyHere = curr_acl['applyHere'];
  139. new_acl.applySubs = curr_acl['applySubs'];
  140. new_acl.inherited = curr_acl['inherited'];
  141. new_acl.playerid = curr_acl['playerid'];
  142. new_acl.group = curr_acl['group'].encode( "UTF-8" );
  143. new_acl.allow = curr_acl['allow'];
  144. new_acl.deny = curr_acl['deny'];
  145. newacls.append( new_acl );
  146. for curr_group in acl.groups:
  147. new_group = Murmur.Group()
  148. new_group.name = curr_group['name'].encode( "UTF-8" );
  149. new_group.inherited = curr_group['inherited'];
  150. new_group.inherit = curr_group['inherit'];
  151. new_group.inheritable = curr_group['inheritable'];
  152. new_group.add = curr_group['add'];
  153. new_group.remove = curr_group['remove'];
  154. new_group.members = curr_group['members'];
  155. newgroups.append( new_group );
  156. self._getIceServerObject(srvid).setACL( acl.channelId, newacls, newgroups, acl.inherit );
  157. def getTexture(self, srvid, mumbleid):
  158. texture = self._getIceServerObject(srvid).getTexture(mumbleid)
  159. if len(texture) == 0:
  160. raise ValueError( "No Texture has been set." );
  161. # this returns a list of bytes.
  162. decompressed = decompress( texture );
  163. # iterate over 4 byte chunks of the string
  164. imgdata = "";
  165. for idx in range( 0, len(decompressed), 4 ):
  166. # read 4 bytes = BGRA and convert to RGBA
  167. # manual wrote getTexture returns "Textures are stored as zlib compress()ed 600x60 32-bit RGBA data."
  168. # http://mumble.sourceforge.net/slice/Murmur/Server.html#getTexture
  169. # but return values BGRA X(
  170. bgra = unpack( "4B", decompressed[idx:idx+4] );
  171. imgdata += pack( "4B", bgra[2], bgra[1], bgra[0], bgra[3] );
  172. # return an 600x60 RGBA image object created from the data
  173. return Image.fromstring( "RGBA", ( 600, 60 ), imgdata );
  174. def setTexture(self, srvid, mumbleid, infile):
  175. # open image, convert to RGBA, and resize to 600x60
  176. img = Image.open( infile ).convert( "RGBA" ).transform( ( 600, 60 ), Image.EXTENT, ( 0, 0, 600, 60 ) );
  177. # iterate over the list and pack everything into a string
  178. bgrastring = "";
  179. for ent in list( img.getdata() ):
  180. # ent is in RGBA format, but Murmur wants BGRA (ARGB inverse), so stuff needs
  181. # to be reordered when passed to pack()
  182. bgrastring += pack( "4B", ent[2], ent[1], ent[0], ent[3] );
  183. # compress using zlib
  184. compressed = compress( bgrastring );
  185. # pack the original length in 4 byte big endian, and concat the compressed
  186. # data to it to emulate qCompress().
  187. texture = pack( ">L", len(bgrastring) ) + compressed;
  188. # finally call murmur and set the texture
  189. self._getIceServerObject(srvid).setTexture(mumbleid, texture)
  190. @staticmethod
  191. def setUnicodeFlag(data):
  192. ret = ''
  193. if isinstance(data, tuple) or isinstance(data, list) or isinstance(data, dict):
  194. ret = {}
  195. for key in data.keys():
  196. ret[MumbleCtlIce_118.setUnicodeFlag(key)] = MumbleCtlIce_118.setUnicodeFlag(data[key])
  197. else:
  198. ret = unicode(data, 'utf-8')
  199. return ret
  200. class MumbleCtlIce_120(MumbleCtlIce_118):
  201. def getRegisteredPlayers(self, srvid, filter = ''):
  202. users = self._getIceServerObject( srvid ).getRegisteredUsers( filter.encode( "UTF-8" ) )
  203. ret = []
  204. for user in users:
  205. ret.append([user.playerid, self.setUnicodeFlag(user.name), self.setUnicodeFlag(user.email), self.setUnicodeFlag(user.pw)])
  206. return ret
  207. def getChannels(self, srvid):
  208. chans = self._getIceServerObject(srvid).getChannels()
  209. ret = []
  210. for x in chans:
  211. chan = chans[x]
  212. ret.append([chan.id, self.setUnicodeFlag(chan.name), chan.parent, chan.links, chan.description])
  213. return ret
  214. def getPlayers(self, srvid):
  215. serv = self._getIceServerObject(srvid);
  216. users = serv.getUsers()
  217. ret = []
  218. for x in users:
  219. user = users[x]
  220. 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])
  221. return ret
  222. def registerPlayer(self, srvid, name):
  223. return self._getIceServerObject(srvid).registerUser( name.encode( "UTF-8" ) )
  224. def unregisterPlayer(self, srvid, mumbleid):
  225. self._getIceServerObject(srvid).unregisterUser(mumbleid)
  226. def setRegistration(self, srvid, mumbleid, name, email, password):
  227. user = self._getIceServerObject( srvid ).getRegistration( mumbleid )
  228. user['name'] = name.encode( "UTF-8" )
  229. user['email'] = email.encode( "UTF-8" )
  230. user['pw'] = password.encode( "UTF-8" )
  231. return self._getIceServerObject( srvid ).updateRegistration( mumbleid, user )
  232. def getACL(self, srvid, identifier):
  233. import Murmur
  234. acls = self._getIceServerObject(srvid).getACL(identifier)
  235. ret = []
  236. for x in acls:
  237. if isinstance(x, list):
  238. tmp = []
  239. for y in x:
  240. if y.__class__ is Murmur.ACL:
  241. tmp.append([y.applyHere, y.applySubs, y.inherited, y.userid, self.setUnicodeFlag(y.group), y.allow, y.deny])
  242. elif y.__class__ is Murmur.Group:
  243. tmp.append([self.setUnicodeFlag(y.name), y.inherited, y.inherit, y.inheritable, y.add, y.remove, y.members])
  244. ret.append(tmp)
  245. else:
  246. ret.append(x)
  247. return ret
  248. def setACL(self, srvid, acl):
  249. import Murmur
  250. newacls = [];
  251. newgroups = [];
  252. for curr_acl in acl.acls:
  253. new_acl = Murmur.ACL();
  254. new_acl.applyHere = curr_acl['applyHere'];
  255. new_acl.applySubs = curr_acl['applySubs'];
  256. new_acl.inherited = curr_acl['inherited'];
  257. new_acl.userid = curr_acl['playerid'];
  258. new_acl.group = curr_acl['group'].encode( "UTF-8" );
  259. new_acl.allow = curr_acl['allow'];
  260. new_acl.deny = curr_acl['deny'];
  261. newacls.append( new_acl );
  262. for curr_group in acl.groups:
  263. new_group = Murmur.Group()
  264. new_group.name = curr_group['name'].encode( "UTF-8" );
  265. new_group.inherited = curr_group['inherited'];
  266. new_group.inherit = curr_group['inherit'];
  267. new_group.inheritable = curr_group['inheritable'];
  268. new_group.add = curr_group['add'];
  269. new_group.remove = curr_group['remove'];
  270. new_group.members = curr_group['members'];
  271. newgroups.append( new_group );
  272. self._getIceServerObject(srvid).setACL( acl.channelId, newacls, newgroups, acl.inherit );