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.

209 lines
6.0 KiB

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