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.

303 lines
9.7 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. from utils import ObjectInfo
  21. import dbus
  22. from dbus.exceptions import DBusException
  23. def MumbleCtlDbus( connstring ):
  24. """ Choose the correct DBus handler (1.1.8 or legacy) to use. """
  25. meta = dbus.Interface( dbus.SystemBus().get_object( connstring, '/' ), 'net.sourceforge.mumble.Meta' );
  26. try:
  27. version = meta.getVersion();
  28. except DBusException:
  29. return MumbleCtlDbus_Legacy( connstring, meta );
  30. else:
  31. return MumbleCtlDbus_118( connstring, meta );
  32. class MumbleCtlDbus_118(MumbleCtlBase):
  33. method = "DBus";
  34. def __init__( self, connstring, meta ):
  35. self.dbus_base = connstring;
  36. self.meta = meta;
  37. def _getDbusMeta( self ):
  38. return self.meta;
  39. def _getDbusServerObject( self, srvid):
  40. if srvid not in self.getBootedServers():
  41. raise SystemError, 'No murmur process with the given server ID (%d) is running and attached to system dbus under %s.' % ( srvid, self.meta );
  42. return dbus.Interface( dbus.SystemBus().get_object( self.dbus_base, '/%d' % srvid ), 'net.sourceforge.mumble.Murmur' );
  43. def getVersion( self ):
  44. return MumbleCtlDbus_118.convertDbusTypeToNative( self.meta.getVersion() )
  45. def getAllConf(self, srvid):
  46. return MumbleCtlDbus_118.convertDbusTypeToNative(self.meta.getAllConf(dbus.Int32(srvid)))
  47. def setConf(self, srvid, key, value):
  48. self.meta.setConf(dbus.Int32( srvid ), key, value)
  49. def getDefaultConf(self):
  50. return MumbleCtlDbus_118.convertDbusTypeToNative(self.meta.getDefaultConf())
  51. def start( self, srvid ):
  52. self.meta.start( srvid );
  53. def stop( self, srvid ):
  54. self.meta.stop( srvid );
  55. def isBooted( self, srvid ):
  56. return bool( self.meta.isBooted( srvid ) );
  57. def deleteServer( self, srvid ):
  58. srvid = dbus.Int32( srvid )
  59. if self.meta.isBooted( srvid ):
  60. self.meta.stop( srvid )
  61. self.meta.deleteServer( srvid )
  62. def newServer(self):
  63. return self.meta.newServer()
  64. def registerPlayer(self, srvid, name, email, password):
  65. mumbleid = int( self._getDbusServerObject(srvid).registerPlayer(name) );
  66. self.setRegistration( srvid, mumbleid, name, email, password );
  67. return mumbleid;
  68. def unregisterPlayer(self, srvid, mumbleid):
  69. self._getDbusServerObject(srvid).unregisterPlayer(dbus.Int32( mumbleid ))
  70. def getChannels(self, srvid):
  71. chans = self._getDbusServerObject(srvid).getChannels()
  72. ret = {};
  73. for channel in chans:
  74. ret[ channel[0] ] = ObjectInfo(
  75. id = int(channel[0]),
  76. name = str(channel[1]),
  77. parent = int(channel[2]),
  78. links = [ int(lnk) for lnk in channel[3] ],
  79. );
  80. return ret;
  81. def getPlayers(self, srvid):
  82. players = self._getDbusServerObject(srvid).getPlayers();
  83. ret = {};
  84. for playerObj in players:
  85. ret[ int(playerObj[0]) ] = ObjectInfo(
  86. session = int( playerObj[0] ),
  87. mute = bool( playerObj[1] ),
  88. deaf = bool( playerObj[2] ),
  89. suppress = bool( playerObj[3] ),
  90. selfMute = bool( playerObj[4] ),
  91. selfDeaf = bool( playerObj[5] ),
  92. channel = int( playerObj[6] ),
  93. userid = int( playerObj[7] ),
  94. name = str( playerObj[8] ),
  95. onlinesecs = int( playerObj[9] ),
  96. bytespersec = int( playerObj[10] )
  97. );
  98. return ret;
  99. def getRegisteredPlayers(self, srvid, filter = ''):
  100. users = self._getDbusServerObject(srvid).getRegisteredPlayers( filter );
  101. ret = {};
  102. for user in users:
  103. ret[int(user[0])] = ObjectInfo(
  104. userid = int( user[0] ),
  105. name = unicode( user[1] ),
  106. email = unicode( user[2] ),
  107. pw = unicode( user[3] )
  108. );
  109. return ret
  110. def getACL(self, srvid, channelid):
  111. raw_acls, raw_groups, raw_inherit = self._getDbusServerObject(srvid).getACL(channelid)
  112. acls = [ ObjectInfo(
  113. applyHere = bool(rule[0]),
  114. applySubs = bool(rule[1]),
  115. inherited = bool(rule[2]),
  116. userid = int(rule[3]),
  117. group = str(rule[4]),
  118. allow = int(rule[5]),
  119. deny = int(rule[6]),
  120. )
  121. for rule in raw_acls
  122. ];
  123. groups = [ ObjectInfo(
  124. name = str(group[0]),
  125. inherited = bool(group[1]),
  126. inherit = bool(group[2]),
  127. inheritable = bool(group[3]),
  128. add = [ int(usrid) for usrid in group[4] ],
  129. remove = [ int(usrid) for usrid in group[5] ],
  130. members = [ int(usrid) for usrid in group[6] ],
  131. )
  132. for group in raw_groups
  133. ];
  134. return acls, groups, bool(raw_inherit);
  135. def setACL(self, srvid, channelid, acls, groups, inherit):
  136. # Pack acl ObjectInfo into a tuple and send that over dbus
  137. dbus_acls = [
  138. ( rule.applyHere, rule.applySubs, rule.inherited, rule.userid, rule.group, rule.allow, rule.deny )
  139. for rule in acls
  140. ];
  141. dbus_groups = [
  142. ( group.name, group.inherited, group.inherit, group.inheritable, group.add, group.remove, group.members )
  143. for group in groups
  144. ];
  145. return self._getDbusServerObject(srvid).setACL( channelid, dbus_acls, dbus_groups, inherit );
  146. def getBootedServers(self):
  147. return MumbleCtlDbus_118.convertDbusTypeToNative(self.meta.getBootedServers())
  148. def getAllServers(self):
  149. return MumbleCtlDbus_118.convertDbusTypeToNative(self.meta.getAllServers())
  150. def setSuperUserPassword(self, srvid, value):
  151. self.meta.setSuperUserPassword(dbus.Int32(srvid), value)
  152. def getRegistration(self, srvid, mumbleid):
  153. user = MumbleCtlDbus_118.convertDbusTypeToNative(self._getDbusServerObject(srvid).getRegistration(dbus.Int32(mumbleid)))
  154. return {
  155. 'name': user[1],
  156. 'email': user[2],
  157. };
  158. def setRegistration(self, srvid, mumbleid, name, email, password):
  159. return MumbleCtlDbus_118.convertDbusTypeToNative(
  160. self._getDbusServerObject(srvid).setRegistration(dbus.Int32(mumbleid), name, email, password)
  161. )
  162. def getTexture(self, srvid, mumbleid):
  163. texture = self._getDbusServerObject(srvid).getTexture(dbus.Int32(mumbleid));
  164. if len(texture) == 0:
  165. raise ValueError( "No Texture has been set." );
  166. # this returns a list of bytes.
  167. # first 4 bytes: Length of uncompressed string, rest: compressed data
  168. orig_len = ( texture[0] << 24 ) | ( texture[1] << 16 ) | ( texture[2] << 8 ) | ( texture[3] );
  169. # convert rest to string and run decompress
  170. bytestr = "";
  171. for byte in texture[4:]:
  172. bytestr += pack( "B", int(byte) );
  173. decompressed = decompress( bytestr );
  174. # iterate over 4 byte chunks of the string
  175. imgdata = "";
  176. for idx in range( 0, orig_len, 4 ):
  177. # read 4 bytes = BGRA and convert to RGBA
  178. bgra = unpack( "4B", decompressed[idx:idx+4] );
  179. imgdata += pack( "4B", bgra[2], bgra[1], bgra[0], bgra[3] );
  180. # return an 600x60 RGBA image object created from the data
  181. return Image.fromstring( "RGBA", ( 600, 60 ), imgdata);
  182. def setTexture(self, srvid, mumbleid, infile):
  183. # open image, convert to RGBA, and resize to 600x60
  184. img = Image.open( infile ).convert( "RGBA" ).transform( ( 600, 60 ), Image.EXTENT, ( 0, 0, 600, 60 ) );
  185. # iterate over the list and pack everything into a string
  186. bgrastring = "";
  187. for ent in list( img.getdata() ):
  188. # ent is in RGBA format, but Murmur wants BGRA (ARGB inverse), so stuff needs
  189. # to be reordered when passed to pack()
  190. bgrastring += pack( "4B", ent[2], ent[1], ent[0], ent[3] );
  191. # compress using zlib
  192. compressed = compress( bgrastring );
  193. # pack the original length in 4 byte big endian, and concat the compressed
  194. # data to it to emulate qCompress().
  195. texture = pack( ">L", len(bgrastring) ) + compressed;
  196. # finally call murmur and set the texture
  197. self._getDbusServerObject(srvid).setTexture(dbus.Int32( mumbleid ), texture)
  198. def verifyPassword( self, srvid, username, password ):
  199. player = self.getRegisteredPlayers( srvid, username );
  200. if not player:
  201. return -2;
  202. ok = MumbleCtlDbus_118.convertDbusTypeToNative(
  203. self._getDbusServerObject(srvid).verifyPassword( dbus.Int32( player[0].userid ), password )
  204. );
  205. if ok:
  206. return player[0].userid;
  207. else:
  208. return -1;
  209. @staticmethod
  210. def convertDbusTypeToNative(data):
  211. #i know dbus.* type is extends python native type.
  212. #but dbus.* type is not native type. it's not good transparent for using Ice/Dbus.
  213. ret = None
  214. if isinstance(data, tuple) or type(data) is data.__class__ is dbus.Array or data.__class__ is dbus.Struct:
  215. ret = []
  216. for x in data:
  217. ret.append(MumbleCtlDbus_118.convertDbusTypeToNative(x))
  218. elif data.__class__ is dbus.Dictionary:
  219. ret = {}
  220. for x in data.items():
  221. ret[MumbleCtlDbus_118.convertDbusTypeToNative(x[0])] = MumbleCtlDbus_118.convertDbusTypeToNative(x[1])
  222. else:
  223. if data.__class__ is dbus.Boolean:
  224. ret = bool(data)
  225. elif data.__class__ is dbus.String:
  226. ret = unicode(data)
  227. elif data.__class__ is dbus.Int32 or data.__class__ is dbus.UInt32:
  228. ret = int(data)
  229. elif data.__class__ is dbus.Byte:
  230. ret = byte(data)
  231. return ret
  232. class MumbleCtlDbus_Legacy( MumbleCtlDbus_118 ):
  233. def getVersion( self ):
  234. return ( 1, 1, 4, u"1.1.4" );
  235. def setRegistration(self, srvid, mumbleid, name, email, password):
  236. return MumbleCtlDbus_118.convertDbusTypeToNative(
  237. self._getDbusServerObject(srvid).updateRegistration( ( dbus.Int32(mumbleid), name, email, password ) )
  238. )