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.

255 lines
8.4 KiB

15 years ago
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 mctl import MumbleCtlBase
  20. from utils import ObjectInfo
  21. import dbus
  22. from dbus.exceptions import DBusException
  23. def MumbleCtlDbus( connstring ):
  24. """ Choose the correct DBus handler (1.1.8 or legacy) to use. """
  25. meta = dbus.Interface( dbus.SystemBus().get_object( connstring, '/' ), 'net.sourceforge.mumble.Meta' );
  26. try:
  27. version = meta.getVersion();
  28. except DBusException:
  29. return MumbleCtlDbus_Legacy( connstring, meta );
  30. else:
  31. return MumbleCtlDbus_118( connstring, meta );
  32. class MumbleCtlDbus_118(MumbleCtlBase):
  33. method = "DBus";
  34. def __init__( self, connstring, meta ):
  35. self.dbus_base = connstring;
  36. self.meta = meta;
  37. def _getDbusMeta( self ):
  38. return self.meta;
  39. def _getDbusServerObject( self, srvid):
  40. if srvid not in self.getBootedServers():
  41. raise SystemError, 'No murmur process with the given server ID (%d) is running and attached to system dbus under %s.' % ( srvid, self.meta );
  42. return dbus.Interface( dbus.SystemBus().get_object( self.dbus_base, '/%d' % srvid ), 'net.sourceforge.mumble.Murmur' );
  43. def getVersion( self ):
  44. return MumbleCtlDbus_118.convertDbusTypeToNative( self.meta.getVersion() )
  45. def getAllConf(self, srvid):
  46. return MumbleCtlDbus_118.convertDbusTypeToNative(self.meta.getAllConf(dbus.Int32(srvid)))
  47. def setConf(self, srvid, key, value):
  48. self.meta.setConf(dbus.Int32( srvid ), key, value)
  49. def getDefaultConf(self):
  50. return MumbleCtlDbus_118.convertDbusTypeToNative(self.meta.getDefaultConf())
  51. def start( self, srvid ):
  52. self.meta.start( srvid );
  53. def stop( self, srvid ):
  54. self.meta.stop( srvid );
  55. def isBooted( self, srvid ):
  56. return bool( self.meta.isBooted( srvid ) );
  57. def deleteServer( self, srvid ):
  58. srvid = dbus.Int32( srvid )
  59. if self.meta.isBooted( srvid ):
  60. self.meta.stop( srvid )
  61. self.meta.deleteServer( srvid )
  62. def newServer(self):
  63. return self.meta.newServer()
  64. def registerPlayer(self, srvid, name, email, password):
  65. mumbleid = MumbleCtlDbus_118.convertDbusTypeToNative( self._getDbusServerObject(srvid).registerPlayer(name) );
  66. self.setRegistration( srvid, mumbleid, name, email, password );
  67. return mumbleid;
  68. def unregisterPlayer(self, srvid, mumbleid):
  69. self._getDbusServerObject(srvid).unregisterPlayer(dbus.Int32( mumbleid ))
  70. def getChannels(self, srvid):
  71. chans = MumbleCtlDbus_118.convertDbusTypeToNative(self._getDbusServerObject(srvid).getChannels())
  72. ret = {};
  73. for channel in chans:
  74. ret[ channel[0] ] = ObjectInfo(
  75. id = channel[0],
  76. name = channel[1],
  77. parent = channel[2],
  78. links = channel[3],
  79. );
  80. return ret;
  81. def getPlayers(self, srvid):
  82. players = MumbleCtlDbus_118.convertDbusTypeToNative(self._getDbusServerObject(srvid).getPlayers());
  83. ret = {};
  84. for playerObj in players:
  85. ret[ playerObj[0] ] = ObjectInfo(
  86. session = playerObj[0],
  87. mute = playerObj[1],
  88. deaf = playerObj[2],
  89. suppress = playerObj[3],
  90. selfMute = playerObj[4],
  91. selfDeaf = playerObj[5],
  92. channel = playerObj[6],
  93. userid = playerObj[7],
  94. name = playerObj[8],
  95. onlinesecs = playerObj[9],
  96. bytespersec = playerObj[10]
  97. );
  98. return ret;
  99. def getRegisteredPlayers(self, srvid, filter = ''):
  100. return MumbleCtlDbus_118.convertDbusTypeToNative(self._getDbusServerObject(srvid).getRegisteredPlayers( filter ) )
  101. def getACL(self, srvid, channelid):
  102. return MumbleCtlDbus_118.convertDbusTypeToNative(self._getDbusServerObject(srvid).getACL(channelid))
  103. def setACL(self, srvid, acl):
  104. self._getDbusServerObject(srvid).setACL(*acl.pack())
  105. def getBootedServers(self):
  106. return MumbleCtlDbus_118.convertDbusTypeToNative(self.meta.getBootedServers())
  107. def getAllServers(self):
  108. return MumbleCtlDbus_118.convertDbusTypeToNative(self.meta.getAllServers())
  109. def setSuperUserPassword(self, srvid, value):
  110. self.meta.setSuperUserPassword(dbus.Int32(srvid), value)
  111. def getRegistration(self, srvid, mumbleid):
  112. user = MumbleCtlDbus_118.convertDbusTypeToNative(self._getDbusServerObject(srvid).getRegistration(dbus.Int32(mumbleid)))
  113. return {
  114. 'name': user[1],
  115. 'email': user[2],
  116. };
  117. def setRegistration(self, srvid, mumbleid, name, email, password):
  118. return MumbleCtlDbus_118.convertDbusTypeToNative(
  119. self._getDbusServerObject(srvid).setRegistration(dbus.Int32(mumbleid), name, email, password)
  120. )
  121. def getTexture(self, srvid, mumbleid):
  122. texture = self._getDbusServerObject(srvid).getTexture(dbus.Int32(mumbleid));
  123. if len(texture) == 0:
  124. raise ValueError( "No Texture has been set." );
  125. # this returns a list of bytes.
  126. # first 4 bytes: Length of uncompressed string, rest: compressed data
  127. orig_len = ( texture[0] << 24 ) | ( texture[1] << 16 ) | ( texture[2] << 8 ) | ( texture[3] );
  128. # convert rest to string and run decompress
  129. bytestr = "";
  130. for byte in texture[4:]:
  131. bytestr += pack( "B", int(byte) );
  132. decompressed = decompress( bytestr );
  133. # iterate over 4 byte chunks of the string
  134. imgdata = "";
  135. for idx in range( 0, orig_len, 4 ):
  136. # read 4 bytes = BGRA and convert to RGBA
  137. bgra = unpack( "4B", decompressed[idx:idx+4] );
  138. imgdata += pack( "4B", bgra[2], bgra[1], bgra[0], bgra[3] );
  139. # return an 600x60 RGBA image object created from the data
  140. return Image.fromstring( "RGBA", ( 600, 60 ), imgdata);
  141. def setTexture(self, srvid, mumbleid, infile):
  142. # open image, convert to RGBA, and resize to 600x60
  143. img = Image.open( infile ).convert( "RGBA" ).transform( ( 600, 60 ), Image.EXTENT, ( 0, 0, 600, 60 ) );
  144. # iterate over the list and pack everything into a string
  145. bgrastring = "";
  146. for ent in list( img.getdata() ):
  147. # ent is in RGBA format, but Murmur wants BGRA (ARGB inverse), so stuff needs
  148. # to be reordered when passed to pack()
  149. bgrastring += pack( "4B", ent[2], ent[1], ent[0], ent[3] );
  150. # compress using zlib
  151. compressed = compress( bgrastring );
  152. # pack the original length in 4 byte big endian, and concat the compressed
  153. # data to it to emulate qCompress().
  154. texture = pack( ">L", len(bgrastring) ) + compressed;
  155. # finally call murmur and set the texture
  156. self._getDbusServerObject(srvid).setTexture(dbus.Int32( mumbleid ), texture)
  157. def verifyPassword( self, srvid, username, password ):
  158. player = self.getRegisteredPlayers( srvid, username );
  159. if not player:
  160. return -2;
  161. ok = MumbleCtlDbus_118.convertDbusTypeToNative(
  162. self._getDbusServerObject(srvid).verifyPassword( dbus.Int32( player[0][0] ), password )
  163. );
  164. if ok:
  165. return player[0][0];
  166. else:
  167. return -1;
  168. @staticmethod
  169. def convertDbusTypeToNative(data):
  170. #i know dbus.* type is extends python native type.
  171. #but dbus.* type is not native type. it's not good transparent for using Ice/Dbus.
  172. ret = None
  173. if isinstance(data, tuple) or type(data) is data.__class__ is dbus.Array or data.__class__ is dbus.Struct:
  174. ret = []
  175. for x in data:
  176. ret.append(MumbleCtlDbus_118.convertDbusTypeToNative(x))
  177. elif data.__class__ is dbus.Dictionary:
  178. ret = {}
  179. for x in data.items():
  180. ret[MumbleCtlDbus_118.convertDbusTypeToNative(x[0])] = MumbleCtlDbus_118.convertDbusTypeToNative(x[1])
  181. else:
  182. if data.__class__ is dbus.Boolean:
  183. ret = bool(data)
  184. elif data.__class__ is dbus.String:
  185. ret = unicode(data)
  186. elif data.__class__ is dbus.Int32 or data.__class__ is dbus.UInt32:
  187. ret = int(data)
  188. elif data.__class__ is dbus.Byte:
  189. ret = byte(data)
  190. return ret
  191. class MumbleCtlDbus_Legacy( MumbleCtlDbus_118 ):
  192. def getVersion( self ):
  193. return ( 1, 1, 4, u"1.1.4" );
  194. def setRegistration(self, srvid, mumbleid, name, email, password):
  195. return MumbleCtlDbus_118.convertDbusTypeToNative(
  196. self._getDbusServerObject(srvid).updateRegistration( ( dbus.Int32(mumbleid), name, email, password ) )
  197. )