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.

159 lines
6.2 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. meta = None
  23. dbus_base='net.sourceforge.mumble.murmur'
  24. def __init__(self):
  25. # Prior to saving the model, connect to murmur via dbus and update its settings.
  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 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 deleteServer( self, srvid ):
  41. srvid = dbus.Int32( srvid )
  42. if self.meta.isBooted( srvid ):
  43. self.meta.stop( srvid )
  44. self.meta.deleteServer( srvid )
  45. def newServer(self):
  46. return self.meta.newServer()
  47. def registerPlayer(self, srvid, name):
  48. return MumbleCtlDbus.converDbusTypeToNative(self._getDbusServerObject(srvid).registerPlayer(name))
  49. def unregisterPlayer(self, srvid, mumbleid):
  50. self._getDbusServerObject(srvid).unregisterPlayer(dbus.Int32( mumbleid ))
  51. def getChannels(self, srvid):
  52. return MumbleCtlDbus.converDbusTypeToNative(self._getDbusServerObject(srvid).getChannels())
  53. def getPlayers(self, srvid):
  54. return MumbleCtlDbus.converDbusTypeToNative(self._getDbusServerObject(srvid).getPlayers())
  55. def getRegisteredPlayers(self, srvid):
  56. return MumbleCtlDbus.converDbusTypeToNative(self._getDbusServerObject(srvid).getRegisteredPlayers(''))
  57. def getACL(self, srvid, identifier):
  58. return MumbleCtlDbus.converDbusTypeToNative(self._getDbusServerObject(srvid).getACL(identifier))
  59. def setACL(self, srvid, acl):
  60. self._getDbusServerObject(srvid).setACL(*acl.pack())
  61. def getBootedServers(self):
  62. return MumbleCtlDbus.converDbusTypeToNative(self.meta.getBootedServers())
  63. def getAllServers(self):
  64. return MumbleCtlDbus.converDbusTypeToNative(self.meta.getAllServers())
  65. def setSuperUserPassword(self, srvid, value):
  66. self.meta.setSuperUserPassword(dbus.Int32(srvid), value)
  67. def setRegistration(self, srvid, mumbleid, name, email, password):
  68. return MumbleCtlDbus.converDbusTypeToNative(self._getDbusServerObject(srvid).setRegistration(dbus.Int32(mumbleid), dbus.String(name), dbus.String(email), dbus.String(password)))
  69. def getTexture(self, srvid, mumbleid):
  70. texture = self._getDbusServerObject(srvid).getTexture(dbus.Int32(mumbleid));
  71. if len(texture) == 0:
  72. raise ValueError( "No Texture has been set." );
  73. # this returns a list of bytes.
  74. # first 4 bytes: Length of uncompressed string, rest: compressed data
  75. orig_len = ( texture[0] << 24 ) | ( texture[1] << 16 ) | ( texture[2] << 8 ) | ( texture[3] );
  76. # convert rest to string and run decompress
  77. bytestr = "";
  78. for byte in texture[4:]:
  79. bytestr += pack( "B", int(byte) );
  80. decompressed = decompress( bytestr );
  81. # iterate over 4 byte chunks of the string
  82. imgdata = "";
  83. for idx in range( 0, orig_len, 4 ):
  84. # read 4 bytes = BGRA and convert to RGBA
  85. bgra = unpack( "4B", decompressed[idx:idx+4] );
  86. imgdata += pack( "4B", bgra[2], bgra[1], bgra[0], bgra[3] );
  87. # return an 600x60 RGBA image object created from the data
  88. return Image.fromstring( "RGBA", ( 600, 60 ), imgdata);
  89. def setTexture(self, srvid, mumbleid, infile):
  90. # open image, convert to RGBA, and resize to 600x60
  91. img = Image.open( infile ).convert( "RGBA" ).transform( ( 600, 60 ), Image.EXTENT, ( 0, 0, 600, 60 ) );
  92. # iterate over the list and pack everything into a string
  93. bgrastring = "";
  94. for ent in list( img.getdata() ):
  95. # ent is in RGBA format, but Murmur wants BGRA (ARGB inverse), so stuff needs
  96. # to be reordered when passed to pack()
  97. bgrastring += pack( "4B", ent[2], ent[1], ent[0], ent[3] );
  98. # compress using zlib
  99. compressed = compress( bgrastring );
  100. # pack the original length in 4 byte big endian, and concat the compressed
  101. # data to it to emulate qCompress().
  102. texture = pack( ">L", len(bgrastring) ) + compressed;
  103. # finally call murmur and set the texture
  104. self._getDbusServerObject(srvid).setTexture(dbus.Int32( mumbleid ), texture)
  105. @staticmethod
  106. def converDbusTypeToNative(data):
  107. #i know dbus.* type is extends python native type.
  108. #but dbus.* type is not native type. it's not good transparent for using Ice/Dbus.
  109. ret = None
  110. if isinstance(data, tuple) or type(data) is data.__class__ is dbus.Array or data.__class__ is dbus.Struct:
  111. ret = []
  112. for x in data:
  113. ret.append(MumbleCtlDbus.converDbusTypeToNative(x))
  114. elif data.__class__ is dbus.Dictionary:
  115. ret = {}
  116. for x in data.items():
  117. ret[MumbleCtlDbus.converDbusTypeToNative(x[0])] = MumbleCtlDbus.converDbusTypeToNative(x[1])
  118. else:
  119. if data.__class__ is dbus.Boolean:
  120. ret = bool(data)
  121. elif data.__class__ is dbus.String:
  122. ret = unicode(data)
  123. elif data.__class__ is dbus.Int32 or data.__class__ is dbus.UInt32:
  124. ret = int(data)
  125. elif data.__class__ is dbus.Byte:
  126. ret = byte(data)
  127. return ret