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.

423 lines
13 KiB

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