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.

395 lines
12 KiB

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