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.

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