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.

433 lines
13 KiB

15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 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. self._getIceServerObject(srvid).setConf( key, value.encode( "UTF-8" ) )
  165. @protectDjangoErrPage
  166. def registerPlayer(self, srvid, name, email, password):
  167. mumbleid = self._getIceServerObject(srvid).registerPlayer( name.encode( "UTF-8" ) )
  168. self.setRegistration( srvid, mumbleid, name, email, password );
  169. return mumbleid;
  170. @protectDjangoErrPage
  171. def unregisterPlayer(self, srvid, mumbleid):
  172. self._getIceServerObject(srvid).unregisterPlayer(mumbleid)
  173. @protectDjangoErrPage
  174. def getRegistration(self, srvid, mumbleid):
  175. user = self._getIceServerObject(srvid).getRegistration(mumbleid)
  176. return ObjectInfo(
  177. userid = mumbleid,
  178. name = user.name,
  179. email = user.email,
  180. pw = '',
  181. );
  182. @protectDjangoErrPage
  183. def setRegistration(self, srvid, mumbleid, name, email, password):
  184. import Murmur
  185. user = Murmur.Player()
  186. user.playerid = mumbleid;
  187. user.name = name.encode( "UTF-8" )
  188. user.email = email.encode( "UTF-8" )
  189. user.pw = password.encode( "UTF-8" )
  190. # update*r*egistration r is lowercase...
  191. return self._getIceServerObject(srvid).updateregistration(user)
  192. @protectDjangoErrPage
  193. def getACL(self, srvid, channelid):
  194. # need to convert acls to say "userid" instead of "playerid". meh.
  195. raw_acls, raw_groups, raw_inherit = self._getIceServerObject(srvid).getACL(channelid)
  196. acls = [ ObjectInfo(
  197. applyHere = rule.applyHere,
  198. applySubs = rule.applySubs,
  199. inherited = rule.inherited,
  200. userid = rule.playerid,
  201. group = rule.group,
  202. allow = rule.allow,
  203. deny = rule.deny,
  204. )
  205. for rule in raw_acls
  206. ];
  207. return acls, raw_groups, raw_inherit;
  208. @protectDjangoErrPage
  209. def setACL(self, srvid, channelid, acls, groups, inherit):
  210. import Murmur
  211. ice_acls = [];
  212. for rule in acls:
  213. ice_rule = Murmur.ACL();
  214. ice_rule.applyHere = rule.applyHere;
  215. ice_rule.applySubs = rule.applySubs;
  216. ice_rule.inherited = rule.inherited;
  217. ice_rule.playerid = rule.userid;
  218. ice_rule.group = rule.group;
  219. ice_rule.allow = rule.allow;
  220. ice_rule.deny = rule.deny;
  221. ice_acls.append(ice_rule);
  222. return self._getIceServerObject(srvid).setACL( channelid, ice_acls, groups, inherit );
  223. @protectDjangoErrPage
  224. def getTexture(self, srvid, mumbleid):
  225. texture = self._getIceServerObject(srvid).getTexture(mumbleid)
  226. if len(texture) == 0:
  227. raise ValueError( "No Texture has been set." );
  228. # this returns a list of bytes.
  229. decompressed = decompress( texture );
  230. # iterate over 4 byte chunks of the string
  231. imgdata = "";
  232. for idx in range( 0, len(decompressed), 4 ):
  233. # read 4 bytes = BGRA and convert to RGBA
  234. # manual wrote getTexture returns "Textures are stored as zlib compress()ed 600x60 32-bit RGBA data."
  235. # http://mumble.sourceforge.net/slice/Murmur/Server.html#getTexture
  236. # but return values BGRA X(
  237. bgra = unpack( "4B", decompressed[idx:idx+4] );
  238. imgdata += pack( "4B", bgra[2], bgra[1], bgra[0], bgra[3] );
  239. # return an 600x60 RGBA image object created from the data
  240. return Image.fromstring( "RGBA", ( 600, 60 ), imgdata );
  241. @protectDjangoErrPage
  242. def setTexture(self, srvid, mumbleid, infile):
  243. # open image, convert to RGBA, and resize to 600x60
  244. img = Image.open( infile ).convert( "RGBA" ).transform( ( 600, 60 ), Image.EXTENT, ( 0, 0, 600, 60 ) );
  245. # iterate over the list and pack everything into a string
  246. bgrastring = "";
  247. for ent in list( img.getdata() ):
  248. # ent is in RGBA format, but Murmur wants BGRA (ARGB inverse), so stuff needs
  249. # to be reordered when passed to pack()
  250. bgrastring += pack( "4B", ent[2], ent[1], ent[0], ent[3] );
  251. # compress using zlib
  252. compressed = compress( bgrastring );
  253. # pack the original length in 4 byte big endian, and concat the compressed
  254. # data to it to emulate qCompress().
  255. texture = pack( ">L", len(bgrastring) ) + compressed;
  256. # finally call murmur and set the texture
  257. self._getIceServerObject(srvid).setTexture(mumbleid, texture)
  258. @protectDjangoErrPage
  259. def verifyPassword(self, srvid, username, password):
  260. return self._getIceServerObject(srvid).verifyPassword(username, password);
  261. @staticmethod
  262. def setUnicodeFlag(data):
  263. ret = ''
  264. if isinstance(data, tuple) or isinstance(data, list) or isinstance(data, dict):
  265. ret = {}
  266. for key in data.keys():
  267. ret[MumbleCtlIce_118.setUnicodeFlag(key)] = MumbleCtlIce_118.setUnicodeFlag(data[key])
  268. else:
  269. ret = unicode(data, 'utf-8')
  270. return ret
  271. class MumbleCtlIce_120(MumbleCtlIce_118):
  272. @protectDjangoErrPage
  273. def getRegisteredPlayers(self, srvid, filter = ''):
  274. users = self._getIceServerObject( srvid ).getRegisteredUsers( filter.encode( "UTF-8" ) )
  275. ret = {};
  276. for id in users:
  277. ret[id] = ObjectInfo(
  278. userid = id,
  279. name = unicode( users[id], "utf8" ),
  280. email = '',
  281. pw = ''
  282. );
  283. return ret
  284. @protectDjangoErrPage
  285. def getPlayers(self, srvid):
  286. return self._getIceServerObject(srvid).getUsers();
  287. @protectDjangoErrPage
  288. def registerPlayer(self, srvid, name, email, password):
  289. # To get the real values of these ENUM entries, try
  290. # Murmur.UserInfo.UserX.value
  291. import Murmur
  292. user = {
  293. Murmur.UserInfo.UserName: name.encode( "UTF-8" ),
  294. Murmur.UserInfo.UserEmail: email.encode( "UTF-8" ),
  295. Murmur.UserInfo.UserPassword: password.encode( "UTF-8" ),
  296. };
  297. return self._getIceServerObject(srvid).registerUser( user );
  298. @protectDjangoErrPage
  299. def unregisterPlayer(self, srvid, mumbleid):
  300. self._getIceServerObject(srvid).unregisterUser(mumbleid)
  301. @protectDjangoErrPage
  302. def getRegistration(self, srvid, mumbleid):
  303. reg = self._getIceServerObject( srvid ).getRegistration( mumbleid )
  304. user = ObjectInfo( userid=mumbleid, name="", email="", comment="", hash="", pw="" );
  305. import Murmur
  306. if Murmur.UserInfo.UserName in reg: user.name = reg[Murmur.UserInfo.UserName];
  307. if Murmur.UserInfo.UserEmail in reg: user.email = reg[Murmur.UserInfo.UserEmail];
  308. if Murmur.UserInfo.UserComment in reg: user.comment = reg[Murmur.UserInfo.UserComment];
  309. if Murmur.UserInfo.UserHash in reg: user.hash = reg[Murmur.UserInfo.UserHash];
  310. return user;
  311. @protectDjangoErrPage
  312. def setRegistration(self, srvid, mumbleid, name, email, password):
  313. import Murmur
  314. user = {
  315. Murmur.UserInfo.UserName: name.encode( "UTF-8" ),
  316. Murmur.UserInfo.UserEmail: email.encode( "UTF-8" ),
  317. Murmur.UserInfo.UserPassword: password.encode( "UTF-8" ),
  318. };
  319. return self._getIceServerObject( srvid ).updateRegistration( mumbleid, user )
  320. @protectDjangoErrPage
  321. def getAllConf(self, srvid):
  322. conf = self.setUnicodeFlag(self._getIceServerObject(srvid).getAllConf())
  323. info = {};
  324. for key in conf:
  325. if key == "playername" and conf[key]:
  326. # Buggy database transition from 1.1.8 -> 1.2.0
  327. # Store username as "username" field and set playername field to empty
  328. info['username'] = conf[key];
  329. self.setConf( srvid, "playername", "" );
  330. self.setConf( srvid, "username", conf[key] );
  331. else:
  332. info[str(key)] = conf[key];
  333. return info;
  334. @protectDjangoErrPage
  335. def getConf(self, srvid, key):
  336. return self._getIceServerObject(srvid).getConf( key )
  337. @protectDjangoErrPage
  338. def setConf(self, srvid, key, value):
  339. self._getIceServerObject(srvid).setConf( key, value.encode( "UTF-8" ) )
  340. @protectDjangoErrPage
  341. def getACL(self, srvid, channelid):
  342. return self._getIceServerObject(srvid).getACL(channelid)
  343. @protectDjangoErrPage
  344. def setACL(self, srvid, channelid, acls, groups, inherit):
  345. return self._getIceServerObject(srvid).setACL( channelid, acls, groups, inherit );