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.

472 lines
14 KiB

15 years ago
16 years ago
16 years ago
15 years ago
16 years ago
  1. # -*- coding: utf-8 -*-
  2. """
  3. * Copyright © 2009, withgod <withgod@sourceforge.net>
  4. * 2009-2010, 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 exists
  17. from PIL import Image
  18. from struct import pack, unpack
  19. from zlib import compress, decompress
  20. from mctl import MumbleCtlBase
  21. from utils import ObjectInfo
  22. import Ice, IcePy, tempfile
  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. """ Call the original function and catch Ice exceptions. """
  32. try:
  33. return func( *args, **kwargs );
  34. except Ice.Exception, e:
  35. raise e;
  36. protection_wrapper.innerfunc = func
  37. return protection_wrapper;
  38. @protectDjangoErrPage
  39. def MumbleCtlIce( connstring, slicefile=None, icesecret=None ):
  40. """ Choose the correct Ice handler to use (1.1.8 or 1.2.x), and make sure the
  41. Murmur version matches the slice Version.
  42. Optional parameters are the path to the slice file and the Ice secret
  43. necessary to authenticate to Murmur.
  44. The path can be omitted only if running Murmur 1.2.3 or later, which
  45. exports a getSlice method to retrieve the Slice from.
  46. """
  47. prop = Ice.createProperties([])
  48. prop.setProperty("Ice.ImplicitContext", "Shared")
  49. idd = Ice.InitializationData()
  50. idd.properties = prop
  51. ice = Ice.initialize(idd)
  52. if icesecret:
  53. ice.getImplicitContext().put( "secret", icesecret.encode("utf-8") )
  54. prx = ice.stringToProxy( connstring.encode("utf-8") )
  55. try:
  56. import Murmur
  57. except ImportError:
  58. # Try loading the Slice from Murmur directly via its getSlice method.
  59. # See scripts/testdynamic.py in Mumble's Git repository.
  60. try:
  61. slice = IcePy.Operation( 'getSlice',
  62. Ice.OperationMode.Idempotent, Ice.OperationMode.Idempotent,
  63. True, (), (), (), IcePy._t_string, ()
  64. ).invoke(prx, ((), None))
  65. except Ice.OperationNotExistException:
  66. if not slicefile:
  67. raise EnvironmentError(
  68. "You didn't configure a slice file. Please set the SLICE variable in settings.py." )
  69. if not exists( slicefile ):
  70. raise EnvironmentError(
  71. "The slice file does not exist: '%s' - please check the settings." % slicefile )
  72. if " " in slicefile:
  73. raise EnvironmentError(
  74. "You have a space char in your Slice path. This will confuse Ice, please check." )
  75. if not slicefile.endswith( ".ice" ):
  76. raise EnvironmentError( "The slice file name MUST end with '.ice'." )
  77. try:
  78. Ice.loadSlice( slicefile )
  79. except RuntimeError:
  80. raise RuntimeError( "Slice preprocessing failed. Please check your server's error log." )
  81. else:
  82. slicetemp = tempfile.NamedTemporaryFile( suffix='.ice' )
  83. try:
  84. slicetemp.write( slice )
  85. slicetemp.flush()
  86. Ice.loadSlice( slicetemp.name )
  87. except RuntimeError:
  88. raise RuntimeError( "Slice preprocessing failed. Please check your server's error log." )
  89. finally:
  90. slicetemp.close()
  91. import Murmur
  92. meta = Murmur.MetaPrx.checkedCast(prx)
  93. murmurversion = meta.getVersion()[:3]
  94. if murmurversion == (1, 1, 8):
  95. return MumbleCtlIce_118( connstring, meta );
  96. elif murmurversion[:2] == (1, 2):
  97. return MumbleCtlIce_120( connstring, meta );
  98. class MumbleCtlIce_118(MumbleCtlBase):
  99. method = "ICE";
  100. def __init__( self, connstring, meta ):
  101. self.proxy = connstring;
  102. self.meta = meta;
  103. @protectDjangoErrPage
  104. def _getIceServerObject(self, srvid):
  105. return self.meta.getServer(srvid);
  106. @protectDjangoErrPage
  107. def getBootedServers(self):
  108. ret = []
  109. for x in self.meta.getBootedServers():
  110. ret.append(x.id())
  111. return ret
  112. @protectDjangoErrPage
  113. def getVersion( self ):
  114. return self.meta.getVersion();
  115. @protectDjangoErrPage
  116. def getAllServers(self):
  117. ret = []
  118. for x in self.meta.getAllServers():
  119. ret.append(x.id())
  120. return ret
  121. @protectDjangoErrPage
  122. def getRegisteredPlayers(self, srvid, filter = ''):
  123. users = self._getIceServerObject(srvid).getRegisteredPlayers( filter.encode( "UTF-8" ) )
  124. ret = {};
  125. for user in users:
  126. ret[user.playerid] = ObjectInfo(
  127. userid = int( user.playerid ),
  128. name = unicode( user.name, "utf8" ),
  129. email = unicode( user.email, "utf8" ),
  130. pw = unicode( user.pw, "utf8" )
  131. );
  132. return ret
  133. @protectDjangoErrPage
  134. def getChannels(self, srvid):
  135. return self._getIceServerObject(srvid).getChannels();
  136. @protectDjangoErrPage
  137. def getPlayers(self, srvid):
  138. users = self._getIceServerObject(srvid).getPlayers()
  139. ret = {};
  140. for useridx in users:
  141. user = users[useridx];
  142. ret[ user.session ] = ObjectInfo(
  143. session = user.session,
  144. userid = user.playerid,
  145. mute = user.mute,
  146. deaf = user.deaf,
  147. suppress = user.suppressed,
  148. selfMute = user.selfMute,
  149. selfDeaf = user.selfDeaf,
  150. channel = user.channel,
  151. name = user.name,
  152. onlinesecs = user.onlinesecs,
  153. bytespersec = user.bytespersec
  154. );
  155. return ret;
  156. @protectDjangoErrPage
  157. def getDefaultConf(self):
  158. return self.setUnicodeFlag(self.meta.getDefaultConf())
  159. @protectDjangoErrPage
  160. def getAllConf(self, srvid):
  161. conf = self.setUnicodeFlag(self._getIceServerObject(srvid).getAllConf())
  162. info = {};
  163. for key in conf:
  164. if key == "playername":
  165. info['username'] = conf[key];
  166. else:
  167. info[str(key)] = conf[key];
  168. return info;
  169. @protectDjangoErrPage
  170. def newServer(self):
  171. return self.meta.newServer().id()
  172. @protectDjangoErrPage
  173. def isBooted( self, srvid ):
  174. return bool( self._getIceServerObject(srvid).isRunning() );
  175. @protectDjangoErrPage
  176. def start( self, srvid ):
  177. self._getIceServerObject(srvid).start();
  178. @protectDjangoErrPage
  179. def stop( self, srvid ):
  180. self._getIceServerObject(srvid).stop();
  181. @protectDjangoErrPage
  182. def deleteServer( self, srvid ):
  183. if self._getIceServerObject(srvid).isRunning():
  184. self._getIceServerObject(srvid).stop()
  185. self._getIceServerObject(srvid).delete()
  186. @protectDjangoErrPage
  187. def setSuperUserPassword(self, srvid, value):
  188. self._getIceServerObject(srvid).setSuperuserPassword( value.encode( "UTF-8" ) )
  189. @protectDjangoErrPage
  190. def getConf(self, srvid, key):
  191. if key == "username":
  192. key = "playername";
  193. return self._getIceServerObject(srvid).getConf( key )
  194. @protectDjangoErrPage
  195. def setConf(self, srvid, key, value):
  196. if key == "username":
  197. key = "playername";
  198. if value is None:
  199. value = ''
  200. self._getIceServerObject(srvid).setConf( key, value.encode( "UTF-8" ) )
  201. @protectDjangoErrPage
  202. def registerPlayer(self, srvid, name, email, password):
  203. mumbleid = self._getIceServerObject(srvid).registerPlayer( name.encode( "UTF-8" ) )
  204. self.setRegistration( srvid, mumbleid, name, email, password );
  205. return mumbleid;
  206. @protectDjangoErrPage
  207. def unregisterPlayer(self, srvid, mumbleid):
  208. self._getIceServerObject(srvid).unregisterPlayer(mumbleid)
  209. @protectDjangoErrPage
  210. def getRegistration(self, srvid, mumbleid):
  211. user = self._getIceServerObject(srvid).getRegistration(mumbleid)
  212. return ObjectInfo(
  213. userid = mumbleid,
  214. name = user.name,
  215. email = user.email,
  216. pw = '',
  217. );
  218. @protectDjangoErrPage
  219. def setRegistration(self, srvid, mumbleid, name, email, password):
  220. import Murmur
  221. user = Murmur.Player()
  222. user.playerid = mumbleid;
  223. user.name = name.encode( "UTF-8" )
  224. user.email = email.encode( "UTF-8" )
  225. user.pw = password.encode( "UTF-8" )
  226. # update*r*egistration r is lowercase...
  227. return self._getIceServerObject(srvid).updateregistration(user)
  228. @protectDjangoErrPage
  229. def getACL(self, srvid, channelid):
  230. # need to convert acls to say "userid" instead of "playerid". meh.
  231. raw_acls, raw_groups, raw_inherit = self._getIceServerObject(srvid).getACL(channelid)
  232. acls = [ ObjectInfo(
  233. applyHere = rule.applyHere,
  234. applySubs = rule.applySubs,
  235. inherited = rule.inherited,
  236. userid = rule.playerid,
  237. group = rule.group,
  238. allow = rule.allow,
  239. deny = rule.deny,
  240. )
  241. for rule in raw_acls
  242. ];
  243. return acls, raw_groups, raw_inherit;
  244. @protectDjangoErrPage
  245. def setACL(self, srvid, channelid, acls, groups, inherit):
  246. import Murmur
  247. ice_acls = [];
  248. for rule in acls:
  249. ice_rule = Murmur.ACL();
  250. ice_rule.applyHere = rule.applyHere;
  251. ice_rule.applySubs = rule.applySubs;
  252. ice_rule.inherited = rule.inherited;
  253. ice_rule.playerid = rule.userid;
  254. ice_rule.group = rule.group;
  255. ice_rule.allow = rule.allow;
  256. ice_rule.deny = rule.deny;
  257. ice_acls.append(ice_rule);
  258. return self._getIceServerObject(srvid).setACL( channelid, ice_acls, groups, inherit );
  259. @protectDjangoErrPage
  260. def getTexture(self, srvid, mumbleid):
  261. texture = self._getIceServerObject(srvid).getTexture(mumbleid)
  262. if len(texture) == 0:
  263. raise ValueError( "No Texture has been set." );
  264. # this returns a list of bytes.
  265. decompressed = decompress( texture );
  266. # iterate over 4 byte chunks of the string
  267. imgdata = "";
  268. for idx in range( 0, len(decompressed), 4 ):
  269. # read 4 bytes = BGRA and convert to RGBA
  270. # manual wrote getTexture returns "Textures are stored as zlib compress()ed 600x60 32-bit RGBA data."
  271. # http://mumble.sourceforge.net/slice/Murmur/Server.html#getTexture
  272. # but return values BGRA X(
  273. bgra = unpack( "4B", decompressed[idx:idx+4] );
  274. imgdata += pack( "4B", bgra[2], bgra[1], bgra[0], bgra[3] );
  275. # return an 600x60 RGBA image object created from the data
  276. return Image.fromstring( "RGBA", ( 600, 60 ), imgdata );
  277. @protectDjangoErrPage
  278. def setTexture(self, srvid, mumbleid, infile):
  279. # open image, convert to RGBA, and resize to 600x60
  280. img = Image.open( infile ).convert( "RGBA" ).transform( ( 600, 60 ), Image.EXTENT, ( 0, 0, 600, 60 ) );
  281. # iterate over the list and pack everything into a string
  282. bgrastring = "";
  283. for ent in list( img.getdata() ):
  284. # ent is in RGBA format, but Murmur wants BGRA (ARGB inverse), so stuff needs
  285. # to be reordered when passed to pack()
  286. bgrastring += pack( "4B", ent[2], ent[1], ent[0], ent[3] );
  287. # compress using zlib
  288. compressed = compress( bgrastring );
  289. # pack the original length in 4 byte big endian, and concat the compressed
  290. # data to it to emulate qCompress().
  291. texture = pack( ">L", len(bgrastring) ) + compressed;
  292. # finally call murmur and set the texture
  293. self._getIceServerObject(srvid).setTexture(mumbleid, texture)
  294. @protectDjangoErrPage
  295. def verifyPassword(self, srvid, username, password):
  296. return self._getIceServerObject(srvid).verifyPassword(username, password);
  297. @staticmethod
  298. def setUnicodeFlag(data):
  299. ret = ''
  300. if isinstance(data, tuple) or isinstance(data, list) or isinstance(data, dict):
  301. ret = {}
  302. for key in data.keys():
  303. ret[MumbleCtlIce_118.setUnicodeFlag(key)] = MumbleCtlIce_118.setUnicodeFlag(data[key])
  304. else:
  305. ret = unicode(data, 'utf-8')
  306. return ret
  307. class MumbleCtlIce_120(MumbleCtlIce_118):
  308. @protectDjangoErrPage
  309. def getRegisteredPlayers(self, srvid, filter = ''):
  310. users = self._getIceServerObject( srvid ).getRegisteredUsers( filter.encode( "UTF-8" ) )
  311. ret = {};
  312. for id in users:
  313. ret[id] = ObjectInfo(
  314. userid = id,
  315. name = unicode( users[id], "utf8" ),
  316. email = '',
  317. pw = ''
  318. );
  319. return ret
  320. @protectDjangoErrPage
  321. def getPlayers(self, srvid):
  322. return self._getIceServerObject(srvid).getUsers();
  323. @protectDjangoErrPage
  324. def registerPlayer(self, srvid, name, email, password):
  325. # To get the real values of these ENUM entries, try
  326. # Murmur.UserInfo.UserX.value
  327. import Murmur
  328. user = {
  329. Murmur.UserInfo.UserName: name.encode( "UTF-8" ),
  330. Murmur.UserInfo.UserEmail: email.encode( "UTF-8" ),
  331. Murmur.UserInfo.UserPassword: password.encode( "UTF-8" ),
  332. };
  333. return self._getIceServerObject(srvid).registerUser( user );
  334. @protectDjangoErrPage
  335. def unregisterPlayer(self, srvid, mumbleid):
  336. self._getIceServerObject(srvid).unregisterUser(mumbleid)
  337. @protectDjangoErrPage
  338. def getRegistration(self, srvid, mumbleid):
  339. reg = self._getIceServerObject( srvid ).getRegistration( mumbleid )
  340. user = ObjectInfo( userid=mumbleid, name="", email="", comment="", hash="", pw="" );
  341. import Murmur
  342. if Murmur.UserInfo.UserName in reg: user.name = reg[Murmur.UserInfo.UserName];
  343. if Murmur.UserInfo.UserEmail in reg: user.email = reg[Murmur.UserInfo.UserEmail];
  344. if Murmur.UserInfo.UserComment in reg: user.comment = reg[Murmur.UserInfo.UserComment];
  345. if Murmur.UserInfo.UserHash in reg: user.hash = reg[Murmur.UserInfo.UserHash];
  346. return user;
  347. @protectDjangoErrPage
  348. def setRegistration(self, srvid, mumbleid, name, email, password):
  349. import Murmur
  350. user = {
  351. Murmur.UserInfo.UserName: name.encode( "UTF-8" ),
  352. Murmur.UserInfo.UserEmail: email.encode( "UTF-8" ),
  353. Murmur.UserInfo.UserPassword: password.encode( "UTF-8" ),
  354. };
  355. return self._getIceServerObject( srvid ).updateRegistration( mumbleid, user )
  356. @protectDjangoErrPage
  357. def getAllConf(self, srvid):
  358. conf = self.setUnicodeFlag(self._getIceServerObject(srvid).getAllConf())
  359. info = {};
  360. for key in conf:
  361. if key == "playername" and conf[key]:
  362. # Buggy database transition from 1.1.8 -> 1.2.0
  363. # Store username as "username" field and set playername field to empty
  364. info['username'] = conf[key];
  365. self.setConf( srvid, "playername", "" );
  366. self.setConf( srvid, "username", conf[key] );
  367. else:
  368. info[str(key)] = conf[key];
  369. return info;
  370. @protectDjangoErrPage
  371. def getConf(self, srvid, key):
  372. return self._getIceServerObject(srvid).getConf( key )
  373. @protectDjangoErrPage
  374. def setConf(self, srvid, key, value):
  375. if value is None:
  376. value = ''
  377. self._getIceServerObject(srvid).setConf( key, value.encode( "UTF-8" ) )
  378. @protectDjangoErrPage
  379. def getACL(self, srvid, channelid):
  380. return self._getIceServerObject(srvid).getACL(channelid)
  381. @protectDjangoErrPage
  382. def setACL(self, srvid, channelid, acls, groups, inherit):
  383. return self._getIceServerObject(srvid).setACL( channelid, acls, groups, inherit );