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.

330 lines
10 KiB

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