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.

221 lines
7.6 KiB

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