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.

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