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.

232 lines
7.0 KiB

  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 PIL import Image
  17. from struct import pack, unpack
  18. from zlib import compress, decompress
  19. from django.conf import settings
  20. from mctl import MumbleCtlBase
  21. import Ice
  22. class MumbleCtlIce(MumbleCtlBase):
  23. proxy = 'Meta:tcp -h 127.0.0.1 -p 6502'
  24. slice = settings.SLICE
  25. meta = None
  26. def __init__(self):
  27. self.meta = self._getIceMeta()
  28. def _getIceMeta(self):
  29. Ice.loadSlice(self.slice)
  30. ice = Ice.initialize()
  31. import Murmur
  32. prx = ice.stringToProxy(self.proxy)
  33. return Murmur.MetaPrx.checkedCast(prx)
  34. def _getIceServerObject(self, srvid):
  35. return self.meta.getServer(srvid);
  36. def getBootedServers(self):
  37. ret = []
  38. for x in self.meta.getBootedServers():
  39. ret.append(x.id())
  40. return ret
  41. def getAllServers(self):
  42. ret = []
  43. for x in self.meta.getAllServers():
  44. ret.append(x.id())
  45. return ret
  46. def getRegisteredPlayers(self, srvid):
  47. users = self._getIceServerObject(srvid).getRegisteredPlayers('')
  48. ret = []
  49. for user in users:
  50. ret.append([user.playerid, MumbleCtlIce.setUnicodeFlag(user.name), MumbleCtlIce.setUnicodeFlag(user.email), MumbleCtlIce.setUnicodeFlag(user.pw)])
  51. return ret
  52. def getChannels(self, srvid):
  53. chans = self._getIceServerObject(srvid).getChannels()
  54. ret = []
  55. for x in chans:
  56. chan = chans[x]
  57. ret.append([chan.id, MumbleCtlIce.setUnicodeFlag(chan.name), chan.parent, chan.links])
  58. return ret
  59. def getPlayers(self, srvid):
  60. users = self._getIceServerObject(srvid).getPlayers()
  61. ret = []
  62. for x in users:
  63. user = users[x]
  64. ret.append([user.session, user.mute, user.deaf, user.suppressed, user.selfMute, user.selfDeaf, user.channel, user.playerid, MumbleCtlIce.setUnicodeFlag(user.name), user.onlinesecs, user.bytespersec])
  65. return ret
  66. def getACL(self, srvid, identifier):
  67. import Murmur
  68. acls = self._getIceServerObject(srvid).getACL(identifier)
  69. ret = []
  70. for x in acls:
  71. if isinstance(x, list):
  72. tmp = []
  73. for y in x:
  74. if y.__class__ is Murmur.ACL:
  75. tmp.append([y.applyHere, y.applySubs, y.inherited, y.playerid, MumbleCtlIce.setUnicodeFlag(y.group), y.allow, y.deny])
  76. elif y.__class__ is Murmur.Group:
  77. tmp.append([MumbleCtlIce.setUnicodeFlag(y.name), y.inherited, y.inherit, y.inheritable, y.add, y.remove, y.members])
  78. ret.append(tmp)
  79. else:
  80. ret.append(x)
  81. return ret
  82. def getDefaultConf(self):
  83. return MumbleCtlIce.setUnicodeFlag(self.meta.getDefaultConf())
  84. def getAllConf(self, srvid):
  85. return MumbleCtlIce.setUnicodeFlag(self._getIceServerObject(srvid).getAllConf())
  86. def newServer(self):
  87. return self.meta.newServer().id()
  88. def deleteServer( self, srvid ):
  89. if self._getIceServerObject(srvid).isRunning():
  90. self._getIceServerObject(srvid).stop()
  91. self._getIceServerObject(srvid).delete()
  92. def setSuperUserPassword(self, srvid, value):
  93. self.meta.setSuperUserPassword(srvid, value)
  94. def setConf(self, srvid, key, value):
  95. value = value.encode("utf-8")
  96. #print "%s server %s=%s (%s/%s)" % (srvid, key, value, type(key), type(value))
  97. self._getIceServerObject(srvid).setConf(key, value)
  98. def registerPlayer(self, srvid, name):
  99. return self._getIceServerObject(srvid).registerPlayer(name)
  100. def unregisterPlayer(self, srvid, mumbleid):
  101. self._getIceServerObject(srvid).unregisterPlayer(mumbleid)
  102. def setRegistration(self, srvid, mumbleid, name, email, password):
  103. user = self._getIceServerObject(srvid).getRegistration(mumbleid)
  104. user.name = name
  105. user.email = email
  106. user.pw = password
  107. #print user
  108. # update*r*egistration r is lowercase...
  109. return self._getIceServerObject(srvid).updateregistration(user)
  110. def setACL(self, srvid, acl):
  111. '''
  112. print "xxxx"
  113. print srvid
  114. print acl
  115. print "--"
  116. print acl.pack()
  117. print "xxxx"
  118. '''
  119. import Murmur
  120. tmp = acl.pack()
  121. id = tmp[0]
  122. _acls = tmp[1]
  123. acls = []
  124. _groups = tmp[2]
  125. groups = []
  126. inherit = tmp[3]
  127. for x in _acls:
  128. acl = Murmur.ACL()
  129. acl.applyHere = x[0]
  130. acl.applySubs = x[1]
  131. acl.inherited = x[2]
  132. acl.playerid = x[3]
  133. acl.group = x[4]
  134. acl.allow = x[5]
  135. acl.deny = x[6]
  136. acls.append(acl)
  137. for x in _groups:
  138. group = Murmur.Group()
  139. group.name = x[0]
  140. group.inherited = x[1]
  141. group.inherit = x[2]
  142. group.inheritable = x[3]
  143. group.add = x[4]
  144. group.remove = x[5]
  145. group.members = x[6]
  146. groups.append(group)
  147. self._getIceServerObject(srvid).setACL(id, acls, groups, inherit)
  148. def getTexture(self, srvid, mumbleid):
  149. texture = self._getIceServerObject(srvid).getTexture(mumbleid)
  150. if len(texture) == 0:
  151. raise ValueError( "No Texture has been set." );
  152. # this returns a list of bytes.
  153. decompressed = decompress( texture );
  154. # iterate over 4 byte chunks of the string
  155. imgdata = "";
  156. for idx in range( 0, len(decompressed), 4 ):
  157. # read 4 bytes = BGRA and convert to RGBA
  158. # manual wrote getTexture returns "Textures are stored as zlib compress()ed 600x60 32-bit RGBA data."
  159. # http://mumble.sourceforge.net/slice/Murmur/Server.html#getTexture
  160. # but return values BGRA X(
  161. bgra = unpack( "4B", decompressed[idx:idx+4] );
  162. imgdata += pack( "4B", bgra[2], bgra[1], bgra[0], bgra[3] );
  163. # return an 600x60 RGBA image object created from the data
  164. return Image.fromstring( "RGBA", ( 600, 60 ), imgdata);
  165. def setTexture(self, srvid, mumbleid, infile):
  166. # open image, convert to RGBA, and resize to 600x60
  167. img = Image.open( infile ).convert( "RGBA" ).transform( ( 600, 60 ), Image.EXTENT, ( 0, 0, 600, 60 ) );
  168. # iterate over the list and pack everything into a string
  169. bgrastring = "";
  170. for ent in list( img.getdata() ):
  171. # ent is in RGBA format, but Murmur wants BGRA (ARGB inverse), so stuff needs
  172. # to be reordered when passed to pack()
  173. bgrastring += pack( "4B", ent[2], ent[1], ent[0], ent[3] );
  174. # compress using zlib
  175. compressed = compress( bgrastring );
  176. # pack the original length in 4 byte big endian, and concat the compressed
  177. # data to it to emulate qCompress().
  178. texture = pack( ">L", len(bgrastring) ) + compressed;
  179. # finally call murmur and set the texture
  180. self._getIceServerObject(srvid).setTexture(mumbleid, texture)
  181. @staticmethod
  182. def setUnicodeFlag(data):
  183. ret = ''
  184. if isinstance(data, tuple) or isinstance(data, list) or isinstance(data, dict):
  185. ret = {}
  186. for key in data.keys():
  187. ret[MumbleCtlIce.setUnicodeFlag(key)] = MumbleCtlIce.setUnicodeFlag(data[key])
  188. else:
  189. ret = unicode(data, 'utf-8')
  190. return ret