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.

231 lines
7.0 KiB

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