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.

606 lines
21 KiB

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