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.

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