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.

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