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.

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