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.

173 lines
6.4 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 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 setRegistration(self, srvid, mumbleid, name, email, password):
  76. return MumbleCtlDbus.converDbusTypeToNative(self._getDbusServerObject(srvid).setRegistration(dbus.Int32(mumbleid), name, email, password))
  77. def getTexture(self, srvid, mumbleid):
  78. texture = self._getDbusServerObject(srvid).getTexture(dbus.Int32(mumbleid));
  79. if len(texture) == 0:
  80. raise ValueError( "No Texture has been set." );
  81. # this returns a list of bytes.
  82. # first 4 bytes: Length of uncompressed string, rest: compressed data
  83. orig_len = ( texture[0] << 24 ) | ( texture[1] << 16 ) | ( texture[2] << 8 ) | ( texture[3] );
  84. # convert rest to string and run decompress
  85. bytestr = "";
  86. for byte in texture[4:]:
  87. bytestr += pack( "B", int(byte) );
  88. decompressed = decompress( bytestr );
  89. # iterate over 4 byte chunks of the string
  90. imgdata = "";
  91. for idx in range( 0, orig_len, 4 ):
  92. # read 4 bytes = BGRA and convert to RGBA
  93. bgra = unpack( "4B", decompressed[idx:idx+4] );
  94. imgdata += pack( "4B", bgra[2], bgra[1], bgra[0], bgra[3] );
  95. # return an 600x60 RGBA image object created from the data
  96. return Image.fromstring( "RGBA", ( 600, 60 ), imgdata);
  97. def setTexture(self, srvid, mumbleid, infile):
  98. # open image, convert to RGBA, and resize to 600x60
  99. img = Image.open( infile ).convert( "RGBA" ).transform( ( 600, 60 ), Image.EXTENT, ( 0, 0, 600, 60 ) );
  100. # iterate over the list and pack everything into a string
  101. bgrastring = "";
  102. for ent in list( img.getdata() ):
  103. # ent is in RGBA format, but Murmur wants BGRA (ARGB inverse), so stuff needs
  104. # to be reordered when passed to pack()
  105. bgrastring += pack( "4B", ent[2], ent[1], ent[0], ent[3] );
  106. # compress using zlib
  107. compressed = compress( bgrastring );
  108. # pack the original length in 4 byte big endian, and concat the compressed
  109. # data to it to emulate qCompress().
  110. texture = pack( ">L", len(bgrastring) ) + compressed;
  111. # finally call murmur and set the texture
  112. self._getDbusServerObject(srvid).setTexture(dbus.Int32( mumbleid ), texture)
  113. @staticmethod
  114. def converDbusTypeToNative(data):
  115. #i know dbus.* type is extends python native type.
  116. #but dbus.* type is not native type. it's not good transparent for using Ice/Dbus.
  117. ret = None
  118. if isinstance(data, tuple) or type(data) is data.__class__ is dbus.Array or data.__class__ is dbus.Struct:
  119. ret = []
  120. for x in data:
  121. ret.append(MumbleCtlDbus.converDbusTypeToNative(x))
  122. elif data.__class__ is dbus.Dictionary:
  123. ret = {}
  124. for x in data.items():
  125. ret[MumbleCtlDbus.converDbusTypeToNative(x[0])] = MumbleCtlDbus.converDbusTypeToNative(x[1])
  126. else:
  127. if data.__class__ is dbus.Boolean:
  128. ret = bool(data)
  129. elif data.__class__ is dbus.String:
  130. ret = unicode(data)
  131. elif data.__class__ is dbus.Int32 or data.__class__ is dbus.UInt32:
  132. ret = int(data)
  133. elif data.__class__ is dbus.Byte:
  134. ret = byte(data)
  135. return ret