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.

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