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.

436 lines
13 KiB

15 years ago
16 years ago
16 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 mctl import MumbleCtlBase
  21. from utils import ObjectInfo
  22. import Ice
  23. def protectDjangoErrPage( func ):
  24. """ Catch and reraise Ice exceptions to prevent the Django page from failing.
  25. Since I need to "import Murmur", Django would try to read a murmur.py file
  26. which doesn't exist, and thereby produce an IndexError exception. This method
  27. erases the exception's traceback, preventing Django from trying to read any
  28. non-existant files and borking.
  29. """
  30. def protection_wrapper( *args, **kwargs ):
  31. """ Call the original function and catch Ice exceptions. """
  32. try:
  33. return func( *args, **kwargs );
  34. except Ice.Exception, e:
  35. raise e;
  36. protection_wrapper.innerfunc = func
  37. return protection_wrapper;
  38. @protectDjangoErrPage
  39. def MumbleCtlIce( connstring, slicefile ):
  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 slicefile:
  47. raise EnvironmentError( "You didn't configure a slice file. Please set the SLICE variable in settings.py." )
  48. if not exists( slicefile ):
  49. raise EnvironmentError( "The slice file does not exist: '%s' - please check the settings." % slicefile )
  50. if " " in slicefile:
  51. raise EnvironmentError( "You have a space char in your Slice path. This will confuse Ice, please check." )
  52. if not slicefile.endswith( ".ice" ):
  53. raise EnvironmentError( "The slice file name MUST end with '.ice'." )
  54. Ice.loadSlice( slicefile )
  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 getConf(self, srvid, key):
  157. if key == "username":
  158. key = "playername";
  159. return self._getIceServerObject(srvid).getConf( key )
  160. @protectDjangoErrPage
  161. def setConf(self, srvid, key, value):
  162. if key == "username":
  163. key = "playername";
  164. if value is None:
  165. value = ''
  166. self._getIceServerObject(srvid).setConf( key, value.encode( "UTF-8" ) )
  167. @protectDjangoErrPage
  168. def registerPlayer(self, srvid, name, email, password):
  169. mumbleid = self._getIceServerObject(srvid).registerPlayer( name.encode( "UTF-8" ) )
  170. self.setRegistration( srvid, mumbleid, name, email, password );
  171. return mumbleid;
  172. @protectDjangoErrPage
  173. def unregisterPlayer(self, srvid, mumbleid):
  174. self._getIceServerObject(srvid).unregisterPlayer(mumbleid)
  175. @protectDjangoErrPage
  176. def getRegistration(self, srvid, mumbleid):
  177. user = self._getIceServerObject(srvid).getRegistration(mumbleid)
  178. return ObjectInfo(
  179. userid = mumbleid,
  180. name = user.name,
  181. email = user.email,
  182. pw = '',
  183. );
  184. @protectDjangoErrPage
  185. def setRegistration(self, srvid, mumbleid, name, email, password):
  186. import Murmur
  187. user = Murmur.Player()
  188. user.playerid = mumbleid;
  189. user.name = name.encode( "UTF-8" )
  190. user.email = email.encode( "UTF-8" )
  191. user.pw = password.encode( "UTF-8" )
  192. # update*r*egistration r is lowercase...
  193. return self._getIceServerObject(srvid).updateregistration(user)
  194. @protectDjangoErrPage
  195. def getACL(self, srvid, channelid):
  196. # need to convert acls to say "userid" instead of "playerid". meh.
  197. raw_acls, raw_groups, raw_inherit = self._getIceServerObject(srvid).getACL(channelid)
  198. acls = [ ObjectInfo(
  199. applyHere = rule.applyHere,
  200. applySubs = rule.applySubs,
  201. inherited = rule.inherited,
  202. userid = rule.playerid,
  203. group = rule.group,
  204. allow = rule.allow,
  205. deny = rule.deny,
  206. )
  207. for rule in raw_acls
  208. ];
  209. return acls, raw_groups, raw_inherit;
  210. @protectDjangoErrPage
  211. def setACL(self, srvid, channelid, acls, groups, inherit):
  212. import Murmur
  213. ice_acls = [];
  214. for rule in acls:
  215. ice_rule = Murmur.ACL();
  216. ice_rule.applyHere = rule.applyHere;
  217. ice_rule.applySubs = rule.applySubs;
  218. ice_rule.inherited = rule.inherited;
  219. ice_rule.playerid = rule.userid;
  220. ice_rule.group = rule.group;
  221. ice_rule.allow = rule.allow;
  222. ice_rule.deny = rule.deny;
  223. ice_acls.append(ice_rule);
  224. return self._getIceServerObject(srvid).setACL( channelid, ice_acls, groups, inherit );
  225. @protectDjangoErrPage
  226. def getTexture(self, srvid, mumbleid):
  227. texture = self._getIceServerObject(srvid).getTexture(mumbleid)
  228. if len(texture) == 0:
  229. raise ValueError( "No Texture has been set." );
  230. # this returns a list of bytes.
  231. decompressed = decompress( texture );
  232. # iterate over 4 byte chunks of the string
  233. imgdata = "";
  234. for idx in range( 0, len(decompressed), 4 ):
  235. # read 4 bytes = BGRA and convert to RGBA
  236. # manual wrote getTexture returns "Textures are stored as zlib compress()ed 600x60 32-bit RGBA data."
  237. # http://mumble.sourceforge.net/slice/Murmur/Server.html#getTexture
  238. # but return values BGRA X(
  239. bgra = unpack( "4B", decompressed[idx:idx+4] );
  240. imgdata += pack( "4B", bgra[2], bgra[1], bgra[0], bgra[3] );
  241. # return an 600x60 RGBA image object created from the data
  242. return Image.fromstring( "RGBA", ( 600, 60 ), imgdata );
  243. @protectDjangoErrPage
  244. def setTexture(self, srvid, mumbleid, infile):
  245. # open image, convert to RGBA, and resize to 600x60
  246. img = Image.open( infile ).convert( "RGBA" ).transform( ( 600, 60 ), Image.EXTENT, ( 0, 0, 600, 60 ) );
  247. # iterate over the list and pack everything into a string
  248. bgrastring = "";
  249. for ent in list( img.getdata() ):
  250. # ent is in RGBA format, but Murmur wants BGRA (ARGB inverse), so stuff needs
  251. # to be reordered when passed to pack()
  252. bgrastring += pack( "4B", ent[2], ent[1], ent[0], ent[3] );
  253. # compress using zlib
  254. compressed = compress( bgrastring );
  255. # pack the original length in 4 byte big endian, and concat the compressed
  256. # data to it to emulate qCompress().
  257. texture = pack( ">L", len(bgrastring) ) + compressed;
  258. # finally call murmur and set the texture
  259. self._getIceServerObject(srvid).setTexture(mumbleid, texture)
  260. @protectDjangoErrPage
  261. def verifyPassword(self, srvid, username, password):
  262. return self._getIceServerObject(srvid).verifyPassword(username, password);
  263. @staticmethod
  264. def setUnicodeFlag(data):
  265. ret = ''
  266. if isinstance(data, tuple) or isinstance(data, list) or isinstance(data, dict):
  267. ret = {}
  268. for key in data.keys():
  269. ret[MumbleCtlIce_118.setUnicodeFlag(key)] = MumbleCtlIce_118.setUnicodeFlag(data[key])
  270. else:
  271. ret = unicode(data, 'utf-8')
  272. return ret
  273. class MumbleCtlIce_120(MumbleCtlIce_118):
  274. @protectDjangoErrPage
  275. def getRegisteredPlayers(self, srvid, filter = ''):
  276. users = self._getIceServerObject( srvid ).getRegisteredUsers( filter.encode( "UTF-8" ) )
  277. ret = {};
  278. for id in users:
  279. ret[id] = ObjectInfo(
  280. userid = id,
  281. name = unicode( users[id], "utf8" ),
  282. email = '',
  283. pw = ''
  284. );
  285. return ret
  286. @protectDjangoErrPage
  287. def getPlayers(self, srvid):
  288. return self._getIceServerObject(srvid).getUsers();
  289. @protectDjangoErrPage
  290. def registerPlayer(self, srvid, name, email, password):
  291. # To get the real values of these ENUM entries, try
  292. # Murmur.UserInfo.UserX.value
  293. import Murmur
  294. user = {
  295. Murmur.UserInfo.UserName: name.encode( "UTF-8" ),
  296. Murmur.UserInfo.UserEmail: email.encode( "UTF-8" ),
  297. Murmur.UserInfo.UserPassword: password.encode( "UTF-8" ),
  298. };
  299. return self._getIceServerObject(srvid).registerUser( user );
  300. @protectDjangoErrPage
  301. def unregisterPlayer(self, srvid, mumbleid):
  302. self._getIceServerObject(srvid).unregisterUser(mumbleid)
  303. @protectDjangoErrPage
  304. def getRegistration(self, srvid, mumbleid):
  305. reg = self._getIceServerObject( srvid ).getRegistration( mumbleid )
  306. user = ObjectInfo( userid=mumbleid, name="", email="", comment="", hash="", pw="" );
  307. import Murmur
  308. if Murmur.UserInfo.UserName in reg: user.name = reg[Murmur.UserInfo.UserName];
  309. if Murmur.UserInfo.UserEmail in reg: user.email = reg[Murmur.UserInfo.UserEmail];
  310. if Murmur.UserInfo.UserComment in reg: user.comment = reg[Murmur.UserInfo.UserComment];
  311. if Murmur.UserInfo.UserHash in reg: user.hash = reg[Murmur.UserInfo.UserHash];
  312. return user;
  313. @protectDjangoErrPage
  314. def setRegistration(self, srvid, mumbleid, name, email, password):
  315. import Murmur
  316. user = {
  317. Murmur.UserInfo.UserName: name.encode( "UTF-8" ),
  318. Murmur.UserInfo.UserEmail: email.encode( "UTF-8" ),
  319. Murmur.UserInfo.UserPassword: password.encode( "UTF-8" ),
  320. };
  321. return self._getIceServerObject( srvid ).updateRegistration( mumbleid, user )
  322. @protectDjangoErrPage
  323. def getAllConf(self, srvid):
  324. conf = self.setUnicodeFlag(self._getIceServerObject(srvid).getAllConf())
  325. info = {};
  326. for key in conf:
  327. if key == "playername" and conf[key]:
  328. # Buggy database transition from 1.1.8 -> 1.2.0
  329. # Store username as "username" field and set playername field to empty
  330. info['username'] = conf[key];
  331. self.setConf( srvid, "playername", "" );
  332. self.setConf( srvid, "username", conf[key] );
  333. else:
  334. info[str(key)] = conf[key];
  335. return info;
  336. @protectDjangoErrPage
  337. def getConf(self, srvid, key):
  338. return self._getIceServerObject(srvid).getConf( key )
  339. @protectDjangoErrPage
  340. def setConf(self, srvid, key, value):
  341. if value is None:
  342. value = ''
  343. self._getIceServerObject(srvid).setConf( key, value.encode( "UTF-8" ) )
  344. @protectDjangoErrPage
  345. def getACL(self, srvid, channelid):
  346. return self._getIceServerObject(srvid).getACL(channelid)
  347. @protectDjangoErrPage
  348. def setACL(self, srvid, channelid, acls, groups, inherit):
  349. return self._getIceServerObject(srvid).setACL( channelid, acls, groups, inherit );