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.

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