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.

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