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.

171 lines
6.5 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):
  64. return MumbleCtlDbus.converDbusTypeToNative(self._getDbusServerObject(srvid).getRegisteredPlayers(''))
  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), dbus.String(name), dbus.String(email), dbus.String(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