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.

331 lines
10 KiB

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