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.

354 lines
11 KiB

16 years ago
16 years ago
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 protectDjangoErrPage( func ):
  24. """ Catch and reraise Ice exceptions to prevent the Django page from failing.
  25. Since I need to "import Murmur", Django would try to read a murmur.py file
  26. which doesn't exist, and thereby produce an IndexError exception. This method
  27. erases the exception's traceback, preventing Django from trying to read any
  28. non-existant files and borking.
  29. """
  30. def protection_wrapper( *args, **kwargs ):
  31. try:
  32. return func( *args, **kwargs );
  33. except Ice.Exception, e:
  34. raise e;
  35. return protection_wrapper;
  36. @protectDjangoErrPage
  37. def MumbleCtlIce( connstring ):
  38. """ Choose the correct Ice handler to use (1.1.8 or 1.2.0), and make sure the
  39. Murmur version matches the slice Version.
  40. """
  41. version = settings.SLICE_VERSION;
  42. slice = settings.SLICE;
  43. if not slice:
  44. slice = join(
  45. settings.MUMBLE_DJANGO_ROOT, 'pyweb', 'mumble',
  46. 'Murmur_%d-%d-%d.ice' % (version[0], version[1], version[2] )
  47. );
  48. Ice.loadSlice( slice )
  49. ice = Ice.initialize()
  50. import Murmur
  51. prx = ice.stringToProxy( connstring.encode("utf-8") )
  52. meta = Murmur.MetaPrx.checkedCast(prx)
  53. murmurversion = meta.getVersion();
  54. if murmurversion[0] != version[0] or murmurversion[1] != version[1] or murmurversion[2] != version[2]:
  55. raise TypeError(
  56. "Detected Murmur version %d.%d.%d, for which I am not configured." % ( murmurversion[0], murmurversion[1], murmurversion[2] )
  57. );
  58. if murmurversion[0] == murmurversion[1] == 1 and murmurversion[2] <= 8:
  59. return MumbleCtlIce_118( connstring, meta );
  60. elif murmurversion[0] == 1 and murmurversion[1] == 2 and murmurversion[2] == 0:
  61. return MumbleCtlIce_120( connstring, meta );
  62. class MumbleCtlIce_118(MumbleCtlBase):
  63. method = "ICE";
  64. def __init__( self, connstring, meta ):
  65. self.proxy = connstring;
  66. self.meta = meta;
  67. @protectDjangoErrPage
  68. def _getIceServerObject(self, srvid):
  69. return self.meta.getServer(srvid);
  70. @protectDjangoErrPage
  71. def getBootedServers(self):
  72. ret = []
  73. for x in self.meta.getBootedServers():
  74. ret.append(x.id())
  75. return ret
  76. @protectDjangoErrPage
  77. def getVersion( self ):
  78. return self.meta.getVersion();
  79. @protectDjangoErrPage
  80. def getAllServers(self):
  81. ret = []
  82. for x in self.meta.getAllServers():
  83. ret.append(x.id())
  84. return ret
  85. @protectDjangoErrPage
  86. def getRegisteredPlayers(self, srvid, filter = ''):
  87. users = self._getIceServerObject(srvid).getRegisteredPlayers( filter.encode( "UTF-8" ) )
  88. ret = []
  89. for user in users:
  90. ret.append([user.playerid, self.setUnicodeFlag(user.name), self.setUnicodeFlag(user.email), self.setUnicodeFlag(user.pw)])
  91. return ret
  92. @protectDjangoErrPage
  93. def getChannels(self, srvid):
  94. return self._getIceServerObject(srvid).getChannels();
  95. @protectDjangoErrPage
  96. def getPlayers(self, srvid):
  97. return self._getIceServerObject(srvid).getPlayers()
  98. @protectDjangoErrPage
  99. def getDefaultConf(self):
  100. return self.setUnicodeFlag(self.meta.getDefaultConf())
  101. @protectDjangoErrPage
  102. def getAllConf(self, srvid):
  103. return self.setUnicodeFlag(self._getIceServerObject(srvid).getAllConf())
  104. @protectDjangoErrPage
  105. def newServer(self):
  106. return self.meta.newServer().id()
  107. @protectDjangoErrPage
  108. def isBooted( self, srvid ):
  109. return bool( self._getIceServerObject(srvid).isRunning() );
  110. @protectDjangoErrPage
  111. def start( self, srvid ):
  112. self._getIceServerObject(srvid).start();
  113. @protectDjangoErrPage
  114. def stop( self, srvid ):
  115. self._getIceServerObject(srvid).stop();
  116. @protectDjangoErrPage
  117. def deleteServer( self, srvid ):
  118. if self._getIceServerObject(srvid).isRunning():
  119. self._getIceServerObject(srvid).stop()
  120. self._getIceServerObject(srvid).delete()
  121. @protectDjangoErrPage
  122. def setSuperUserPassword(self, srvid, value):
  123. self._getIceServerObject(srvid).setSuperuserPassword( value.encode( "UTF-8" ) )
  124. @protectDjangoErrPage
  125. def setConf(self, srvid, key, value):
  126. self._getIceServerObject(srvid).setConf( key, value.encode( "UTF-8" ) )
  127. @protectDjangoErrPage
  128. def registerPlayer(self, srvid, name, email, password):
  129. mumbleid = self._getIceServerObject(srvid).registerPlayer( name.encode( "UTF-8" ) )
  130. self.setRegistration( srvid, mumbleid, name, email, password );
  131. return mumbleid;
  132. @protectDjangoErrPage
  133. def unregisterPlayer(self, srvid, mumbleid):
  134. self._getIceServerObject(srvid).unregisterPlayer(mumbleid)
  135. @protectDjangoErrPage
  136. def getRegistration(self, srvid, mumbleid):
  137. user = self._getIceServerObject(srvid).getRegistration(mumbleid)
  138. return {
  139. 'name': user.name,
  140. 'email': user.email,
  141. };
  142. @protectDjangoErrPage
  143. def setRegistration(self, srvid, mumbleid, name, email, password):
  144. user = self._getIceServerObject(srvid).getRegistration(mumbleid)
  145. user.name = name.encode( "UTF-8" )
  146. user.email = email.encode( "UTF-8" )
  147. user.pw = password.encode( "UTF-8" )
  148. # update*r*egistration r is lowercase...
  149. return self._getIceServerObject(srvid).updateregistration(user)
  150. @protectDjangoErrPage
  151. def getACL(self, srvid, channelid):
  152. # need to convert acls to say "userid" instead of "playerid". meh.
  153. raw_acls, raw_groups, raw_inherit = self._getIceServerObject(srvid).getACL(channelid)
  154. acls = [ ObjectInfo(
  155. applyHere = rule.applyHere,
  156. applySubs = rule.applySubs,
  157. inherited = rule.inherited,
  158. userid = rule.playerid,
  159. group = rule.group,
  160. allow = rule.allow,
  161. deny = rule.deny,
  162. )
  163. for rule in raw_acls
  164. ];
  165. return acls, raw_groups, raw_inherit;
  166. @protectDjangoErrPage
  167. def setACL(self, srvid, channelid, acls, groups, inherit):
  168. import Murmur
  169. ice_acls = [];
  170. for rule in acls:
  171. ice_rule = Murmur.ACL();
  172. ice_rule.applyHere = rule.applyHere;
  173. ice_rule.applySubs = rule.applySubs;
  174. ice_rule.inherited = rule.inherited;
  175. ice_rule.playerid = rule.userid;
  176. ice_rule.group = rule.group;
  177. ice_rule.allow = rule.allow;
  178. ice_rule.deny = rule.deny;
  179. ice_acls.append(ice_rule);
  180. return self._getIceServerObject(srvid).setACL( channelid, ice_acls, groups, inherit );
  181. @protectDjangoErrPage
  182. def getTexture(self, srvid, mumbleid):
  183. texture = self._getIceServerObject(srvid).getTexture(mumbleid)
  184. if len(texture) == 0:
  185. raise ValueError( "No Texture has been set." );
  186. # this returns a list of bytes.
  187. decompressed = decompress( texture );
  188. # iterate over 4 byte chunks of the string
  189. imgdata = "";
  190. for idx in range( 0, len(decompressed), 4 ):
  191. # read 4 bytes = BGRA and convert to RGBA
  192. # manual wrote getTexture returns "Textures are stored as zlib compress()ed 600x60 32-bit RGBA data."
  193. # http://mumble.sourceforge.net/slice/Murmur/Server.html#getTexture
  194. # but return values BGRA X(
  195. bgra = unpack( "4B", decompressed[idx:idx+4] );
  196. imgdata += pack( "4B", bgra[2], bgra[1], bgra[0], bgra[3] );
  197. # return an 600x60 RGBA image object created from the data
  198. return Image.fromstring( "RGBA", ( 600, 60 ), imgdata );
  199. @protectDjangoErrPage
  200. def setTexture(self, srvid, mumbleid, infile):
  201. # open image, convert to RGBA, and resize to 600x60
  202. img = Image.open( infile ).convert( "RGBA" ).transform( ( 600, 60 ), Image.EXTENT, ( 0, 0, 600, 60 ) );
  203. # iterate over the list and pack everything into a string
  204. bgrastring = "";
  205. for ent in list( img.getdata() ):
  206. # ent is in RGBA format, but Murmur wants BGRA (ARGB inverse), so stuff needs
  207. # to be reordered when passed to pack()
  208. bgrastring += pack( "4B", ent[2], ent[1], ent[0], ent[3] );
  209. # compress using zlib
  210. compressed = compress( bgrastring );
  211. # pack the original length in 4 byte big endian, and concat the compressed
  212. # data to it to emulate qCompress().
  213. texture = pack( ">L", len(bgrastring) ) + compressed;
  214. # finally call murmur and set the texture
  215. self._getIceServerObject(srvid).setTexture(mumbleid, texture)
  216. @protectDjangoErrPage
  217. def verifyPassword(self, srvid, username, password):
  218. return self._getIceServerObject(srvid).verifyPassword(username, password);
  219. @staticmethod
  220. def setUnicodeFlag(data):
  221. ret = ''
  222. if isinstance(data, tuple) or isinstance(data, list) or isinstance(data, dict):
  223. ret = {}
  224. for key in data.keys():
  225. ret[MumbleCtlIce_118.setUnicodeFlag(key)] = MumbleCtlIce_118.setUnicodeFlag(data[key])
  226. else:
  227. ret = unicode(data, 'utf-8')
  228. return ret
  229. class MumbleCtlIce_120(MumbleCtlIce_118):
  230. @protectDjangoErrPage
  231. def getRegisteredPlayers(self, srvid, filter = ''):
  232. users = self._getIceServerObject( srvid ).getRegisteredUsers( filter.encode( "UTF-8" ) )
  233. ret = []
  234. for id in users:
  235. ret.append( [ id, self.setUnicodeFlag( users[id] ) ] );
  236. return ret
  237. @protectDjangoErrPage
  238. def getPlayers(self, srvid):
  239. return self._getIceServerObject(srvid).getUsers();
  240. @protectDjangoErrPage
  241. def registerPlayer(self, srvid, name, email, password):
  242. # To get the real values of these ENUM entries, try
  243. # Murmur.UserInfo.UserX.value
  244. from Murmur import UserInfo
  245. user = {
  246. UserInfo.UserName: name.encode( "UTF-8" ),
  247. UserInfo.UserEmail: email.encode( "UTF-8" ),
  248. UserInfo.UserPassword: password.encode( "UTF-8" ),
  249. };
  250. return self._getIceServerObject(srvid).registerUser( user );
  251. @protectDjangoErrPage
  252. def unregisterPlayer(self, srvid, mumbleid):
  253. self._getIceServerObject(srvid).unregisterUser(mumbleid)
  254. @protectDjangoErrPage
  255. def getRegistration(self, srvid, mumbleid):
  256. from Murmur import UserInfo
  257. reg = self._getIceServerObject( srvid ).getRegistration( mumbleid )
  258. user = {};
  259. if UserInfo.UserName in reg:
  260. user['name'] = reg[UserInfo.UserName];
  261. if UserInfo.UserEmail in reg:
  262. user['email'] = reg[UserInfo.UserEmail];
  263. if UserInfo.UserComment in reg:
  264. user['comment'] = reg[UserInfo.UserComment];
  265. if UserInfo.UserHash in reg:
  266. user['hash'] = reg[UserInfo.UserHash];
  267. return user;
  268. @protectDjangoErrPage
  269. def setRegistration(self, srvid, mumbleid, name, email, password):
  270. from Murmur import UserInfo
  271. user = {
  272. UserInfo.UserName: name.encode( "UTF-8" ),
  273. UserInfo.UserEmail: email.encode( "UTF-8" ),
  274. UserInfo.UserPassword: password.encode( "UTF-8" ),
  275. };
  276. return self._getIceServerObject( srvid ).updateRegistration( mumbleid, user )
  277. @protectDjangoErrPage
  278. def getACL(self, srvid, channelid):
  279. return self._getIceServerObject(srvid).getACL(channelid)
  280. @protectDjangoErrPage
  281. def setACL(self, srvid, channelid, acls, groups, inherit):
  282. return self._getIceServerObject(srvid).setACL( channelid, acls, groups, inherit );