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.

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