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.

605 lines
18 KiB

15 years ago
15 years ago
15 years ago
15 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 time import time
  17. from functools import wraps
  18. from StringIO import StringIO
  19. from os.path import exists, join
  20. from os import unlink, name as os_name
  21. from PIL import Image
  22. from struct import pack, unpack
  23. from zlib import compress, decompress, error
  24. from mctl import MumbleCtlBase
  25. from utils import ObjectInfo
  26. import Ice, IcePy, tempfile
  27. def loadSlice( slicefile ):
  28. """ Load the slice file with the correct include dir set, if possible. """
  29. icepath = Ice.getSliceDir()
  30. if not icepath:
  31. # last resort, let's hope to christ this works (won't for >=1.2.3)
  32. Ice.loadSlice( slicefile )
  33. else:
  34. Ice.loadSlice( '', ['-I' + icepath, slicefile ] )
  35. def protectDjangoErrPage( func ):
  36. """ Catch and reraise Ice exceptions to prevent the Django page from failing.
  37. Since I need to "import Murmur", Django would try to read a murmur.py file
  38. which doesn't exist, and thereby produce an IndexError exception. This method
  39. erases the exception's traceback, preventing Django from trying to read any
  40. non-existant files and borking.
  41. """
  42. @wraps(func)
  43. def protection_wrapper( self, *args, **kwargs ):
  44. """ Call the original function and catch Ice exceptions. """
  45. try:
  46. return func( self, *args, **kwargs );
  47. except Ice.Exception, err:
  48. raise err;
  49. protection_wrapper.innerfunc = func
  50. return protection_wrapper;
  51. @protectDjangoErrPage
  52. def MumbleCtlIce( connstring, slicefile=None, icesecret=None ):
  53. """ Choose the correct Ice handler to use (1.1.8 or 1.2.x), and make sure the
  54. Murmur version matches the slice Version.
  55. Optional parameters are the path to the slice file and the Ice secret
  56. necessary to authenticate to Murmur.
  57. The path can be omitted only if running Murmur 1.2.3 or later, which
  58. exports a getSlice method to retrieve the Slice from.
  59. """
  60. prop = Ice.createProperties([])
  61. prop.setProperty("Ice.ImplicitContext", "Shared")
  62. idd = Ice.InitializationData()
  63. idd.properties = prop
  64. ice = Ice.initialize(idd)
  65. if icesecret:
  66. ice.getImplicitContext().put( "secret", icesecret.encode("utf-8") )
  67. prx = ice.stringToProxy( connstring.encode("utf-8") )
  68. try:
  69. prx.ice_ping()
  70. except Ice.Exception:
  71. raise EnvironmentError( "Murmur does not appear to be listening on this address (Ice ping failed)." )
  72. try:
  73. import Murmur
  74. except ImportError:
  75. # Try loading the Slice from Murmur directly via its getSlice method.
  76. # See scripts/testdynamic.py in Mumble's Git repository.
  77. try:
  78. slice = IcePy.Operation( 'getSlice',
  79. Ice.OperationMode.Idempotent, Ice.OperationMode.Idempotent,
  80. True, (), (), (), IcePy._t_string, ()
  81. ).invoke(prx, ((), None))
  82. except (TypeError, Ice.OperationNotExistException):
  83. if not slicefile:
  84. raise EnvironmentError(
  85. "You didn't configure a slice file. Please set the SLICE variable in settings.py." )
  86. if not exists( slicefile ):
  87. raise EnvironmentError(
  88. "The slice file does not exist: '%s' - please check the settings." % slicefile )
  89. if " " in slicefile:
  90. raise EnvironmentError(
  91. "You have a space char in your Slice path. This will confuse Ice, please check." )
  92. if not slicefile.endswith( ".ice" ):
  93. raise EnvironmentError( "The slice file name MUST end with '.ice'." )
  94. try:
  95. loadSlice( slicefile )
  96. except RuntimeError:
  97. raise RuntimeError( "Slice preprocessing failed. Please check your server's error log." )
  98. else:
  99. if os_name == "nt":
  100. # It weren't Windows if it didn't need to be treated differently. *sigh*
  101. temppath = join( tempfile.gettempdir(), "Murmur.ice" )
  102. slicetemp = open( temppath, "w+b" )
  103. try:
  104. slicetemp.write( slice )
  105. finally:
  106. slicetemp.close()
  107. try:
  108. loadSlice( temppath )
  109. except RuntimeError:
  110. raise RuntimeError( "Slice preprocessing failed. Please check your server's error log." )
  111. finally:
  112. unlink(temppath)
  113. else:
  114. slicetemp = tempfile.NamedTemporaryFile( suffix='.ice' )
  115. try:
  116. slicetemp.write( slice )
  117. slicetemp.flush()
  118. loadSlice( slicetemp.name )
  119. except RuntimeError:
  120. raise RuntimeError( "Slice preprocessing failed. Please check your server's error log." )
  121. finally:
  122. slicetemp.close()
  123. import Murmur
  124. meta = Murmur.MetaPrx.checkedCast(prx)
  125. murmurversion = meta.getVersion()[:3]
  126. if murmurversion == (1, 1, 8):
  127. return MumbleCtlIce_118( connstring, meta );
  128. elif murmurversion[:2] == (1, 2):
  129. if murmurversion[2] < 2:
  130. return MumbleCtlIce_120( connstring, meta );
  131. elif murmurversion[2] == 2:
  132. return MumbleCtlIce_122( connstring, meta );
  133. elif murmurversion[2] == 3:
  134. return MumbleCtlIce_123( connstring, meta );
  135. raise NotImplementedError( "No ctl object available for Murmur version %d.%d.%d" % tuple(murmurversion) )
  136. class MumbleCtlIce_118(MumbleCtlBase):
  137. method = "ICE";
  138. def __init__( self, connstring, meta ):
  139. self.proxy = connstring;
  140. self.meta = meta;
  141. @protectDjangoErrPage
  142. def _getIceServerObject(self, srvid):
  143. return self.meta.getServer(srvid);
  144. @protectDjangoErrPage
  145. def getBootedServers(self):
  146. ret = []
  147. for x in self.meta.getBootedServers():
  148. ret.append(x.id())
  149. return ret
  150. @protectDjangoErrPage
  151. def getVersion( self ):
  152. return self.meta.getVersion();
  153. @protectDjangoErrPage
  154. def getAllServers(self):
  155. ret = []
  156. for x in self.meta.getAllServers():
  157. ret.append(x.id())
  158. return ret
  159. @protectDjangoErrPage
  160. def getRegisteredPlayers(self, srvid, filter = ''):
  161. users = self._getIceServerObject(srvid).getRegisteredPlayers( filter.encode( "UTF-8" ) )
  162. ret = {};
  163. for user in users:
  164. ret[user.playerid] = ObjectInfo(
  165. userid = int( user.playerid ),
  166. name = unicode( user.name, "utf8" ),
  167. email = unicode( user.email, "utf8" ),
  168. pw = unicode( user.pw, "utf8" )
  169. );
  170. return ret
  171. @protectDjangoErrPage
  172. def getChannels(self, srvid):
  173. return self._getIceServerObject(srvid).getChannels();
  174. @protectDjangoErrPage
  175. def getPlayers(self, srvid):
  176. users = self._getIceServerObject(srvid).getPlayers()
  177. ret = {};
  178. for useridx in users:
  179. user = users[useridx];
  180. ret[ user.session ] = ObjectInfo(
  181. session = user.session,
  182. userid = user.playerid,
  183. mute = user.mute,
  184. deaf = user.deaf,
  185. suppress = user.suppressed,
  186. selfMute = user.selfMute,
  187. selfDeaf = user.selfDeaf,
  188. channel = user.channel,
  189. name = user.name,
  190. onlinesecs = user.onlinesecs,
  191. bytespersec = user.bytespersec
  192. );
  193. return ret;
  194. @protectDjangoErrPage
  195. def getDefaultConf(self):
  196. return self.setUnicodeFlag(self.meta.getDefaultConf())
  197. @protectDjangoErrPage
  198. def getAllConf(self, srvid):
  199. conf = self.setUnicodeFlag(self._getIceServerObject(srvid).getAllConf())
  200. info = {};
  201. for key in conf:
  202. if key == "playername":
  203. info['username'] = conf[key];
  204. else:
  205. info[str(key)] = conf[key];
  206. return info;
  207. @protectDjangoErrPage
  208. def newServer(self):
  209. return self.meta.newServer().id()
  210. @protectDjangoErrPage
  211. def isBooted( self, srvid ):
  212. return bool( self._getIceServerObject(srvid).isRunning() );
  213. @protectDjangoErrPage
  214. def start( self, srvid ):
  215. self._getIceServerObject(srvid).start();
  216. @protectDjangoErrPage
  217. def stop( self, srvid ):
  218. self._getIceServerObject(srvid).stop();
  219. @protectDjangoErrPage
  220. def deleteServer( self, srvid ):
  221. if self._getIceServerObject(srvid).isRunning():
  222. self._getIceServerObject(srvid).stop()
  223. self._getIceServerObject(srvid).delete()
  224. @protectDjangoErrPage
  225. def setSuperUserPassword(self, srvid, value):
  226. self._getIceServerObject(srvid).setSuperuserPassword( value.encode( "UTF-8" ) )
  227. @protectDjangoErrPage
  228. def getConf(self, srvid, key):
  229. if key == "username":
  230. key = "playername";
  231. return self._getIceServerObject(srvid).getConf( key )
  232. @protectDjangoErrPage
  233. def setConf(self, srvid, key, value):
  234. if key == "username":
  235. key = "playername";
  236. if value is None:
  237. value = ''
  238. self._getIceServerObject(srvid).setConf( key, value.encode( "UTF-8" ) )
  239. @protectDjangoErrPage
  240. def registerPlayer(self, srvid, name, email, password):
  241. mumbleid = self._getIceServerObject(srvid).registerPlayer( name.encode( "UTF-8" ) )
  242. self.setRegistration( srvid, mumbleid, name, email, password );
  243. return mumbleid;
  244. @protectDjangoErrPage
  245. def unregisterPlayer(self, srvid, mumbleid):
  246. self._getIceServerObject(srvid).unregisterPlayer(mumbleid)
  247. @protectDjangoErrPage
  248. def getRegistration(self, srvid, mumbleid):
  249. user = self._getIceServerObject(srvid).getRegistration(mumbleid)
  250. return ObjectInfo(
  251. userid = mumbleid,
  252. name = user.name,
  253. email = user.email,
  254. pw = '',
  255. );
  256. @protectDjangoErrPage
  257. def setRegistration(self, srvid, mumbleid, name, email, password):
  258. import Murmur
  259. user = Murmur.Player()
  260. user.playerid = mumbleid;
  261. user.name = name.encode( "UTF-8" )
  262. user.email = email.encode( "UTF-8" )
  263. user.pw = password.encode( "UTF-8" )
  264. # update*r*egistration r is lowercase...
  265. return self._getIceServerObject(srvid).updateregistration(user)
  266. @protectDjangoErrPage
  267. def getACL(self, srvid, channelid):
  268. # need to convert acls to say "userid" instead of "playerid". meh.
  269. raw_acls, raw_groups, raw_inherit = self._getIceServerObject(srvid).getACL(channelid)
  270. acls = [ ObjectInfo(
  271. applyHere = rule.applyHere,
  272. applySubs = rule.applySubs,
  273. inherited = rule.inherited,
  274. userid = rule.playerid,
  275. group = rule.group,
  276. allow = rule.allow,
  277. deny = rule.deny,
  278. )
  279. for rule in raw_acls
  280. ];
  281. return acls, raw_groups, raw_inherit;
  282. @protectDjangoErrPage
  283. def setACL(self, srvid, channelid, acls, groups, inherit):
  284. import Murmur
  285. ice_acls = [];
  286. for rule in acls:
  287. ice_rule = Murmur.ACL();
  288. ice_rule.applyHere = rule.applyHere;
  289. ice_rule.applySubs = rule.applySubs;
  290. ice_rule.inherited = rule.inherited;
  291. ice_rule.playerid = rule.userid;
  292. ice_rule.group = rule.group;
  293. ice_rule.allow = rule.allow;
  294. ice_rule.deny = rule.deny;
  295. ice_acls.append(ice_rule);
  296. return self._getIceServerObject(srvid).setACL( channelid, ice_acls, groups, inherit );
  297. @protectDjangoErrPage
  298. def getTexture(self, srvid, mumbleid):
  299. texture = self._getIceServerObject(srvid).getTexture(mumbleid)
  300. if len(texture) == 0:
  301. raise ValueError( "No Texture has been set." );
  302. # this returns a list of bytes.
  303. try:
  304. decompressed = decompress( texture );
  305. except error, err:
  306. raise ValueError( err )
  307. # iterate over 4 byte chunks of the string
  308. imgdata = "";
  309. for idx in range( 0, len(decompressed), 4 ):
  310. # read 4 bytes = BGRA and convert to RGBA
  311. # manual wrote getTexture returns "Textures are stored as zlib compress()ed 600x60 32-bit RGBA data."
  312. # http://mumble.sourceforge.net/slice/Murmur/Server.html#getTexture
  313. # but return values BGRA X(
  314. bgra = unpack( "4B", decompressed[idx:idx+4] );
  315. imgdata += pack( "4B", bgra[2], bgra[1], bgra[0], bgra[3] );
  316. # return an 600x60 RGBA image object created from the data
  317. return Image.fromstring( "RGBA", ( 600, 60 ), imgdata );
  318. @protectDjangoErrPage
  319. def setTexture(self, srvid, mumbleid, infile):
  320. # open image, convert to RGBA, and resize to 600x60
  321. img = infile.convert( "RGBA" ).transform( ( 600, 60 ), Image.EXTENT, ( 0, 0, 600, 60 ) );
  322. # iterate over the list and pack everything into a string
  323. bgrastring = "";
  324. for ent in list( img.getdata() ):
  325. # ent is in RGBA format, but Murmur wants BGRA (ARGB inverse), so stuff needs
  326. # to be reordered when passed to pack()
  327. bgrastring += pack( "4B", ent[2], ent[1], ent[0], ent[3] );
  328. # compress using zlib
  329. compressed = compress( bgrastring );
  330. # pack the original length in 4 byte big endian, and concat the compressed
  331. # data to it to emulate qCompress().
  332. texture = pack( ">L", len(bgrastring) ) + compressed;
  333. # finally call murmur and set the texture
  334. self._getIceServerObject(srvid).setTexture(mumbleid, texture)
  335. @protectDjangoErrPage
  336. def verifyPassword(self, srvid, username, password):
  337. return self._getIceServerObject(srvid).verifyPassword(username, password);
  338. @staticmethod
  339. def setUnicodeFlag(data):
  340. ret = ''
  341. if isinstance(data, tuple) or isinstance(data, list) or isinstance(data, dict):
  342. ret = {}
  343. for key in data.keys():
  344. ret[MumbleCtlIce_118.setUnicodeFlag(key)] = MumbleCtlIce_118.setUnicodeFlag(data[key])
  345. else:
  346. ret = unicode(data, 'utf-8')
  347. return ret
  348. class MumbleCtlIce_120(MumbleCtlIce_118):
  349. @protectDjangoErrPage
  350. def getRegisteredPlayers(self, srvid, filter = ''):
  351. users = self._getIceServerObject( srvid ).getRegisteredUsers( filter.encode( "UTF-8" ) )
  352. ret = {};
  353. for id in users:
  354. ret[id] = ObjectInfo(
  355. userid = id,
  356. name = unicode( users[id], "utf8" ),
  357. email = '',
  358. pw = ''
  359. );
  360. return ret
  361. @protectDjangoErrPage
  362. def getPlayers(self, srvid):
  363. userdata = self._getIceServerObject(srvid).getUsers();
  364. for key in userdata:
  365. if isinstance( userdata[key], str ):
  366. userdata[key] = userdata[key].decode( "UTF-8" )
  367. return userdata
  368. @protectDjangoErrPage
  369. def getState(self, srvid, sessionid):
  370. userdata = self._getIceServerObject(srvid).getState(sessionid);
  371. for key in userdata.__dict__:
  372. attr = getattr( userdata, key )
  373. if isinstance( attr, str ):
  374. setattr( userdata, key, attr.decode( "UTF-8" ) )
  375. return userdata
  376. @protectDjangoErrPage
  377. def registerPlayer(self, srvid, name, email, password):
  378. # To get the real values of these ENUM entries, try
  379. # Murmur.UserInfo.UserX.value
  380. import Murmur
  381. user = {
  382. Murmur.UserInfo.UserName: name.encode( "UTF-8" ),
  383. Murmur.UserInfo.UserEmail: email.encode( "UTF-8" ),
  384. Murmur.UserInfo.UserPassword: password.encode( "UTF-8" ),
  385. };
  386. return self._getIceServerObject(srvid).registerUser( user );
  387. @protectDjangoErrPage
  388. def unregisterPlayer(self, srvid, mumbleid):
  389. self._getIceServerObject(srvid).unregisterUser(mumbleid)
  390. @protectDjangoErrPage
  391. def getRegistration(self, srvid, mumbleid):
  392. reg = self._getIceServerObject( srvid ).getRegistration( mumbleid )
  393. user = ObjectInfo( userid=mumbleid, name="", email="", comment="", hash="", pw="" );
  394. import Murmur
  395. if Murmur.UserInfo.UserName in reg: user.name = reg[Murmur.UserInfo.UserName];
  396. if Murmur.UserInfo.UserEmail in reg: user.email = reg[Murmur.UserInfo.UserEmail];
  397. if Murmur.UserInfo.UserComment in reg: user.comment = reg[Murmur.UserInfo.UserComment];
  398. if Murmur.UserInfo.UserHash in reg: user.hash = reg[Murmur.UserInfo.UserHash];
  399. return user;
  400. @protectDjangoErrPage
  401. def setRegistration(self, srvid, mumbleid, name, email, password):
  402. import Murmur
  403. user = {
  404. Murmur.UserInfo.UserName: name.encode( "UTF-8" ),
  405. Murmur.UserInfo.UserEmail: email.encode( "UTF-8" ),
  406. Murmur.UserInfo.UserPassword: password.encode( "UTF-8" ),
  407. };
  408. return self._getIceServerObject( srvid ).updateRegistration( mumbleid, user )
  409. @protectDjangoErrPage
  410. def getAllConf(self, srvid):
  411. conf = self.setUnicodeFlag(self._getIceServerObject(srvid).getAllConf())
  412. info = {};
  413. for key in conf:
  414. if key == "playername" and conf[key]:
  415. # Buggy database transition from 1.1.8 -> 1.2.0
  416. # Store username as "username" field and set playername field to empty
  417. info['username'] = conf[key];
  418. self.setConf( srvid, "playername", "" );
  419. self.setConf( srvid, "username", conf[key] );
  420. else:
  421. info[str(key)] = conf[key];
  422. return info;
  423. @protectDjangoErrPage
  424. def getConf(self, srvid, key):
  425. return self._getIceServerObject(srvid).getConf( key )
  426. @protectDjangoErrPage
  427. def setConf(self, srvid, key, value):
  428. if value is None:
  429. value = ''
  430. self._getIceServerObject(srvid).setConf( key, value.encode( "UTF-8" ) )
  431. @protectDjangoErrPage
  432. def getACL(self, srvid, channelid):
  433. return self._getIceServerObject(srvid).getACL(channelid)
  434. @protectDjangoErrPage
  435. def setACL(self, srvid, channelid, acls, groups, inherit):
  436. return self._getIceServerObject(srvid).setACL( channelid, acls, groups, inherit );
  437. @protectDjangoErrPage
  438. def getBans(self, srvid):
  439. return self._getIceServerObject(srvid).getBans();
  440. @protectDjangoErrPage
  441. def setBans(self, srvid, bans):
  442. return self._getIceServerObject(srvid).setBans(bans);
  443. @protectDjangoErrPage
  444. def addBanForSession(self, srvid, sessionid, **kwargs):
  445. session = self.getState(srvid, sessionid);
  446. if "bits" not in kwargs:
  447. kwargs["bits"] = 128;
  448. if "start" not in kwargs:
  449. kwargs["start"] = int(time());
  450. if "duration" not in kwargs:
  451. kwargs["duration"] = 3600;
  452. return self.addBan(srvid, address=session.address, **kwargs);
  453. @protectDjangoErrPage
  454. def addBan(self, srvid, **kwargs):
  455. for key in kwargs:
  456. if isinstance( kwargs[key], unicode ):
  457. kwargs[key] = kwargs[key].encode("UTF-8")
  458. from Murmur import Ban
  459. srvbans = self.getBans(srvid);
  460. srvbans.append( Ban( **kwargs ) );
  461. return self.setBans(srvid, srvbans);
  462. @protectDjangoErrPage
  463. def kickUser(self, srvid, userid, reason=""):
  464. return self._getIceServerObject(srvid).kickUser( userid, reason.encode("UTF-8") );
  465. class MumbleCtlIce_122(MumbleCtlIce_120):
  466. @protectDjangoErrPage
  467. def getTexture(self, srvid, mumbleid):
  468. raise ValueError( "This method is buggy in 1.2.2, sorry dude." );
  469. @protectDjangoErrPage
  470. def setTexture(self, srvid, mumbleid, infile):
  471. buf = StringIO()
  472. infile.save( buf, "PNG" )
  473. buf.seek(0)
  474. self._getIceServerObject(srvid).setTexture(mumbleid, buf.read())
  475. class MumbleCtlIce_123(MumbleCtlIce_120):
  476. @protectDjangoErrPage
  477. def getRawTexture(self, srvid, mumbleid):
  478. return self._getIceServerObject(srvid).getTexture(mumbleid)
  479. @protectDjangoErrPage
  480. def getTexture(self, srvid, mumbleid):
  481. texture = self.getRawTexture(srvid, mumbleid)
  482. if len(texture) == 0:
  483. raise ValueError( "No Texture has been set." );
  484. from StringIO import StringIO
  485. try:
  486. return Image.open( StringIO( texture ) )
  487. except IOError, err:
  488. raise ValueError( err )
  489. @protectDjangoErrPage
  490. def setTexture(self, srvid, mumbleid, infile):
  491. buf = StringIO()
  492. infile.save( buf, "PNG" )
  493. buf.seek(0)
  494. self._getIceServerObject(srvid).setTexture(mumbleid, buf.read())