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.

390 lines
12 KiB

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