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.

353 lines
13 KiB

  1. # -*- coding: utf-8 -*-
  2. # kate: space-indent on; indent-width 4; replace-tabs on;
  3. """
  4. * Copyright © 2009, withgod <withgod@sourceforge.net>
  5. * 2009-2010, Michael "Svedrin" Ziegler <diese-addy@funzt-halt.net>
  6. *
  7. * Mumble-Django is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This package is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. """
  17. from PIL import Image
  18. from struct import pack, unpack
  19. from zlib import compress, decompress
  20. from mctl import MumbleCtlBase
  21. from utils import ObjectInfo
  22. import dbus
  23. from dbus.exceptions import DBusException
  24. def MumbleCtlDbus( connstring ):
  25. """ Choose the correct DBus handler (1.1.8 or legacy) to use. """
  26. meta = dbus.Interface( dbus.SystemBus().get_object( connstring, '/' ), 'net.sourceforge.mumble.Meta' )
  27. try:
  28. meta.getVersion()
  29. except DBusException:
  30. return MumbleCtlDbus_Legacy( connstring, meta )
  31. else:
  32. return MumbleCtlDbus_118( connstring, meta )
  33. class MumbleCtlDbus_118(MumbleCtlBase):
  34. method = "DBus"
  35. def __init__( self, connstring, meta ):
  36. self.dbus_base = connstring
  37. self.meta = meta
  38. def _getDbusMeta( self ):
  39. return self.meta
  40. def _getDbusServerObject( self, srvid):
  41. if srvid not in self.getBootedServers():
  42. raise SystemError, 'No murmur process with the given server ID (%d) is running and attached to system dbus under %s.' % ( srvid, self.meta )
  43. return dbus.Interface( dbus.SystemBus().get_object( self.dbus_base, '/%d' % srvid ), 'net.sourceforge.mumble.Murmur' )
  44. def getVersion( self ):
  45. return MumbleCtlDbus_118.convertDbusTypeToNative( self.meta.getVersion() )
  46. def getAllConf(self, srvid):
  47. conf = self.meta.getAllConf(dbus.Int32(srvid))
  48. info = {}
  49. for key in conf:
  50. if key == "playername":
  51. info['username'] = conf[key]
  52. else:
  53. info[str(key)] = conf[key]
  54. return info
  55. def getConf(self, srvid, key):
  56. if key == "username":
  57. key = "playername"
  58. return self.meta.getConf(dbus.Int32( srvid ), key)
  59. def setConf(self, srvid, key, value):
  60. if key == "username":
  61. key = "playername"
  62. self.meta.setConf(dbus.Int32( srvid ), key, value)
  63. def getDefaultConf(self):
  64. conf = self.meta.getDefaultConf()
  65. info = {}
  66. for key in conf:
  67. if key == "playername":
  68. info['username'] = conf[key]
  69. else:
  70. info[str(key)] = conf[key]
  71. return info
  72. def start( self, srvid ):
  73. self.meta.start( srvid )
  74. def stop( self, srvid ):
  75. self.meta.stop( srvid )
  76. def isBooted( self, srvid ):
  77. return bool( self.meta.isBooted( srvid ) )
  78. def deleteServer( self, srvid ):
  79. srvid = dbus.Int32( srvid )
  80. if self.meta.isBooted( srvid ):
  81. self.meta.stop( srvid )
  82. self.meta.deleteServer( srvid )
  83. def newServer(self):
  84. return self.meta.newServer()
  85. def registerPlayer(self, srvid, name, email, password):
  86. mumbleid = int( self._getDbusServerObject(srvid).registerPlayer(name) )
  87. self.setRegistration( srvid, mumbleid, name, email, password )
  88. return mumbleid
  89. def unregisterPlayer(self, srvid, mumbleid):
  90. self._getDbusServerObject(srvid).unregisterPlayer(dbus.Int32( mumbleid ))
  91. def getChannels(self, srvid):
  92. chans = self._getDbusServerObject(srvid).getChannels()
  93. ret = {}
  94. for channel in chans:
  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. def moveUser(self, srvid, sessionid, channelid):
  233. srv = self._getDbusServerObject(srvid)
  234. (session, ismute, isdeaf, suppressed, selfMute, selfDeaf, channel) = srv.getPlayerState(dbus.UInt32(sessionid))
  235. srv.setPlayerState((session, ismute, isdeaf, suppressed, selfMute, selfDeaf, channelid))
  236. def muteUser(self, srvid, sessionid, mute):
  237. srv = self._getDbusServerObject(srvid)
  238. (session, ismute, isdeaf, suppressed, selfMute, selfDeaf, channel) = srv.getPlayerState(dbus.UInt32(sessionid))
  239. srv.setPlayerState((session, mute, isdeaf, suppressed, selfMute, selfDeaf, channel))
  240. def deafenUser(self, srvid, sessionid, deaf):
  241. srv = self._getDbusServerObject(srvid)
  242. (session, ismute, isdeaf, suppressed, selfMute, selfDeaf, channel) = srv.getPlayerState(dbus.UInt32(sessionid))
  243. srv.setPlayerState((session, ismute, deaf, suppressed, selfMute, selfDeaf, channel))
  244. def moveChannel(self, srvid, channelid, parentid):
  245. srv = self._getDbusServerObject(srvid)
  246. (chanid, name, parent, links) = srv.getChannelState(dbus.UInt32(channelid))
  247. srv.setChannelState((chanid, name, parentid, links))
  248. @staticmethod
  249. def convertDbusTypeToNative(data):
  250. #i know dbus.* type is extends python native type.
  251. #but dbus.* type is not native type. it's not good transparent for using Ice/Dbus.
  252. ret = None
  253. if isinstance(data, tuple) or type(data) is data.__class__ is dbus.Array or data.__class__ is dbus.Struct:
  254. ret = []
  255. for x in data:
  256. ret.append(MumbleCtlDbus_118.convertDbusTypeToNative(x))
  257. elif data.__class__ is dbus.Dictionary:
  258. ret = {}
  259. for x in data.items():
  260. ret[MumbleCtlDbus_118.convertDbusTypeToNative(x[0])] = MumbleCtlDbus_118.convertDbusTypeToNative(x[1])
  261. else:
  262. if data.__class__ is dbus.Boolean:
  263. ret = bool(data)
  264. elif data.__class__ is dbus.String:
  265. ret = unicode(data)
  266. elif data.__class__ is dbus.Int32 or data.__class__ is dbus.UInt32:
  267. ret = int(data)
  268. elif data.__class__ is dbus.Byte:
  269. ret = int(data)
  270. return ret
  271. def getUptime(self, srvid):
  272. return None
  273. class MumbleCtlDbus_Legacy( MumbleCtlDbus_118 ):
  274. def getVersion( self ):
  275. return ( 1, 1, 4, u"1.1.4" )
  276. def setRegistration(self, srvid, mumbleid, name, email, password):
  277. return MumbleCtlDbus_118.convertDbusTypeToNative(
  278. self._getDbusServerObject(srvid).updateRegistration( ( dbus.Int32(mumbleid), name, email, password ) )
  279. )