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.

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