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.

415 lines
12 KiB

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