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.

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