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.

364 lines
10 KiB

  1. # -*- coding: utf-8 -*-
  2. # mumble-django contributed by withgod@sourceforge.net
  3. #zope.interface is good but don't standard interface library
  4. #abc is better but 2.6 higher.
  5. #import abc
  6. #from django.conf import settings
  7. class MumbleCtlBase ():
  8. ''' abstract Ctrol Object '''
  9. def getAllConf(self, srvid):
  10. pass
  11. def setConf(self, srvid, key, value):
  12. pass
  13. def getDefaultConf(self):
  14. pass
  15. def getValue(self, srvid, key):
  16. pass
  17. def newServer(self):
  18. pass
  19. def setSuperUserPassword(self, srvid, value):
  20. pass
  21. def start(self, srvid):
  22. pass
  23. def stop(self, srvid):
  24. pass
  25. def isBooted(self, srvid):
  26. pass
  27. def deleteServer(self, srvid):
  28. pass
  29. def getUsers(self, srvid):
  30. pass
  31. def getPlayers(self, srvid):
  32. pass
  33. def getRegisteredPlayers(self, srvid):
  34. pass
  35. def getChannels(self, srvid):
  36. pass
  37. def registerPlayer(self, srvid, name):
  38. pass
  39. def setRegistration(self, srvid, mumbleid, name, email, password):
  40. pass
  41. def unregisterPlayer(self, srvid, mumbleid):
  42. pass
  43. def getBootedServers(self):
  44. pass
  45. def getAllServers(self):
  46. pass
  47. def getACL(self, srvid, identifier):
  48. pass
  49. def setACL(self, srvid, acl):
  50. pass
  51. @staticmethod
  52. def newInstance():
  53. # if dbus
  54. #if settings.DAOTYPE == 'dbus':
  55. #ret = MumbleCtlDbus()
  56. ret = MumbleCtlIce()
  57. #else:
  58. # ret = MumbleCtlIce()
  59. return ret
  60. import Ice
  61. class MumbleCtlIce(MumbleCtlBase):
  62. proxy = 'Meta:tcp -h 127.0.0.1 -p 6502'
  63. slice = '/usr/share/slice/Murmur.ice'
  64. meta = None
  65. def __init__(self):
  66. self.meta = self._getIceMeta()
  67. def _getIceMeta(self):
  68. Ice.loadSlice(self.slice)
  69. ice = Ice.initialize()
  70. import Murmur
  71. prx = ice.stringToProxy(self.proxy)
  72. return Murmur.MetaPrx.checkedCast(prx)
  73. def _getIceServerObject(self, srvid):
  74. return self.meta.getServer(srvid);
  75. def getBootedServers(self):
  76. ret = []
  77. for x in self.meta.getBootedServers():
  78. ret.append(x.id())
  79. return ret
  80. def getAllServers(self):
  81. ret = []
  82. for x in self.meta.getAllServers():
  83. ret.append(x.id())
  84. return ret
  85. def getRegisteredPlayers(self, srvid):
  86. users = self._getIceServerObject(srvid).getRegisteredPlayers('')
  87. ret = []
  88. for user in users:
  89. ret.append([user.playerid, unicode(user.name), unicode(user.email), unicode(user.pw)])
  90. return ret
  91. def getChannels(self, srvid):
  92. chans = self._getIceServerObject(srvid).getChannels()
  93. ret = []
  94. for x in chans:
  95. chan = chans[x]
  96. ret.append([chan.id, unicode(chan.name), chan.parent, chan.links])
  97. return ret
  98. def getPlayers(self, srvid):
  99. users = self._getIceServerObject(srvid).getPlayers()
  100. ret = []
  101. for x in users:
  102. user = users[x]
  103. ret.append([user.session, user.mute, user.deaf, user.suppressed, user.selfMute, user.selfDeaf, user.channel, user.playerid, unicode(user.name), user.onlinesecs, user.bytespersec])
  104. return ret
  105. def getACL(self, srvid, identifier):
  106. import Murmur
  107. acls = self._getIceServerObject(srvid).getACL(identifier)
  108. ret = []
  109. for x in acls:
  110. if isinstance(x, list):
  111. tmp = []
  112. for y in x:
  113. if y.__class__ is Murmur.ACL:
  114. tmp.append([y.applyHere, y.applySubs, y.inherited, y.playerid, unicode(y.group), y.allow, y.deny])
  115. elif y.__class__ is Murmur.Group:
  116. tmp.append([unicode(y.name), y.inherited, y.inherit, y.inheritable, y.add, y.remove, y.members])
  117. ret.append(tmp)
  118. else:
  119. ret.append(x)
  120. return ret
  121. def getDefaultConf(self):
  122. return MumbleCtlIce.setUnicodeFlag(self.meta.getDefaultConf())
  123. def getAllConf(self, srvid):
  124. return MumbleCtlIce.setUnicodeFlag(self._getIceServerObject(srvid).getAllConf())
  125. def newServer(self):
  126. return self.meta.newServer().id()
  127. def deleteServer( self, srvid ):
  128. if self._getIceServerObject(srvid).isRunning():
  129. self._getIceServerObject(srvid).stop()
  130. self._getIceServerObject(srvid).delete()
  131. def setSuperUserPassword(self, srvid, value):
  132. self.meta.setSuperUserPassword(srvid, value)
  133. def setConf(self, srvid, key, value):
  134. #print "%s server %s/%s" % (srvid, key, value)
  135. self._getIceServerObject(srvid).setConf(key, value)
  136. def registerPlayer(self, srvid, name):
  137. return self._getIceServerObject(srvid).registerPlayer(name)
  138. def unregisterPlayer(self, srvid, mumbleid):
  139. self._getIceServerObject(srvid).unregisterPlayer(mumbleid)
  140. def setRegistration(self, srvid, mumbleid, name, email, password):
  141. user = self._getIceServerObject(srvid).getRegistration(mumbleid)
  142. user.name = name
  143. user.email = email
  144. user.pw = password
  145. #print user
  146. # update*r*egistration r is lowercase...
  147. return self._getIceServerObject(srvid).updateregistration(user)
  148. def setACL(self, srvid, acl):
  149. print "xxxx"
  150. print srvid
  151. print acl
  152. print "xxxx"
  153. #self._getIceServerObject(srvid).setACL(*acl.pack())
  154. @staticmethod
  155. def setUnicodeFlag(data):
  156. ret = {}
  157. for key in data.keys():
  158. ret[unicode(key)] = unicode(data[key])
  159. return ret
  160. import dbus
  161. class MumbleCtlDbus(MumbleCtlBase):
  162. meta = None
  163. dbus_base='net.sourceforge.mumble.murmur'
  164. def __init__(self):
  165. # Prior to saving the model, connect to murmur via dbus and update its settings.
  166. self.meta = self._getDbusMeta();
  167. def _getDbusMeta( self ):
  168. return dbus.Interface( dbus.SystemBus().get_object( self.dbus_base, '/' ), 'net.sourceforge.mumble.Meta' );
  169. def _getDbusServerObject( self, srvid):
  170. "Connects to DBus and returns an mmServer object representing this Murmur instance."
  171. if srvid not in self.getBootedServers():
  172. raise Exception, 'No murmur process with the given server ID (%d) is running and attached to system dbus under %s.' % ( srvid, self.meta );
  173. return dbus.Interface( dbus.SystemBus().get_object( self.dbus_base, '/%d' % srvid ), 'net.sourceforge.mumble.Murmur' );
  174. def getAllConf(self, srvid):
  175. return MumbleCtlDbus.converDbusTypeToNative(self.meta.getAllConf(dbus.Int32(srvid)))
  176. def setConf(self, srvid, key, value):
  177. self.meta.setConf(dbus.Int32( srvid ), key, value)
  178. def getDefaultConf(self):
  179. return MumbleCtlDbus.converDbusTypeToNative(self.meta.getDefaultConf())
  180. def deleteServer( self, srvid ):
  181. srvid = dbus.Int32( srvid )
  182. if self.meta.isBooted( srvid ):
  183. self.meta.stop( srvid )
  184. self.meta.deleteServer( srvid )
  185. def newServer(self):
  186. return self.meta.newServer()
  187. def registerPlayer(self, srvid, name):
  188. return MumbleCtlDbus.converDbusTypeToNative(self._getDbusServerObject(srvid).registerPlayer(name))
  189. def unregisterPlayer(self, srvid, mumbleid):
  190. self._getDbusServerObject(srvid).unregisterPlayer(dbus.Int32( mumbleid ))
  191. def getChannels(self, srvid):
  192. return MumbleCtlDbus.converDbusTypeToNative(self._getDbusServerObject(srvid).getChannels())
  193. def getPlayers(self, srvid):
  194. return MumbleCtlDbus.converDbusTypeToNative(self._getDbusServerObject(srvid).getPlayers())
  195. def getRegisteredPlayers(self, srvid):
  196. return MumbleCtlDbus.converDbusTypeToNative(self._getDbusServerObject(srvid).getRegisteredPlayers(''))
  197. def getACL(self, srvid, identifier):
  198. return MumbleCtlDbus.converDbusTypeToNative(self._getDbusServerObject(srvid).getACL(identifier))
  199. def setACL(self, srvid, acl):
  200. self._getDbusServerObject(srvid).setACL(*acl.pack())
  201. def getBootedServers(self):
  202. return MumbleCtlDbus.converDbusTypeToNative(self.meta.getBootedServers())
  203. def getAllServers(self):
  204. return MumbleCtlDbus.converDbusTypeToNative(self.meta.getAllServers())
  205. def setSuperUserPassword(self, srvid, value):
  206. self.meta.setSuperUserPassword(dbus.Int32(srvid), value)
  207. def setRegistration(self, srvid, mumbleid, name, email, password):
  208. return MumbleCtlDbus.converDbusTypeToNative(self._getDbusServerObject(srvid).setRegistration(dbus.Int32(mumbleid), dbus.String(name), dbus.String(email), dbus.String(password)))
  209. @staticmethod
  210. def converDbusTypeToNative(data):
  211. #i know dbus.* type is extends python native type.
  212. #but dbus.* type is not native type. it's not good transparent for using Ice/Dbus.
  213. ret = None
  214. if isinstance(data, tuple) or type(data) is data.__class__ is dbus.Array or data.__class__ is dbus.Struct:
  215. ret = []
  216. for x in data:
  217. ret.append(MumbleCtlDbus.converDbusTypeToNative(x))
  218. elif data.__class__ is dbus.Dictionary:
  219. ret = {}
  220. for x in data.items():
  221. ret[MumbleCtlDbus.converDbusTypeToNative(x[0])] = MumbleCtlDbus.converDbusTypeToNative(x[1])
  222. else:
  223. if data.__class__ is dbus.Boolean:
  224. ret = bool(data)
  225. elif data.__class__ is dbus.String:
  226. ret = unicode(data)
  227. elif data.__class__ is dbus.Int32 or data.__class__ is dbus.UInt32:
  228. ret = int(data)
  229. return ret
  230. if __name__ == "__main__":
  231. import sys
  232. x = int(sys.argv[1])
  233. dbusCtl = MumbleCtlDbus()
  234. iceCtl = MumbleCtlIce()
  235. '''
  236. print "--- Dbus test start"
  237. #ctl = MumbleCtlBase.newInstance()
  238. print dbusCtl
  239. print dbusCtl.meta
  240. print "booted server", dbusCtl.getBootedServers()
  241. print "chans"
  242. print dbusCtl.getChannels(x)
  243. print "users"
  244. print dbusCtl.getPlayers(x)
  245. print "getACL", dbusCtl.getACL(x, 0)
  246. print "getAllServers()"
  247. print dbusCtl.getAllServers()
  248. print "getDefaultConf()"
  249. print dbusCtl.getDefaultConf()
  250. print "getAllConf(x)"
  251. print dbusCtl.getAllConf(x)
  252. print "--Dbus end--"
  253. print "--- Ice test start"
  254. print iceCtl
  255. print iceCtl.meta
  256. print "booted server", iceCtl.getBootedServers()
  257. print "chans"
  258. print iceCtl.getChannels(x)
  259. print "users"
  260. print iceCtl.getPlayers(x)
  261. print "getACL", iceCtl.getACL(x, 0)
  262. print "getAllServers()"
  263. print iceCtl.getAllServers()
  264. print "getDefaultConf()"
  265. print iceCtl.getDefaultConf()
  266. print "getAllConf(x)"
  267. print iceCtl.getAllConf(x)
  268. print "--- Ice test end"
  269. '''
  270. print "equal test ---"
  271. print "getBootedServers [%s]" % (dbusCtl.getBootedServers() == iceCtl.getBootedServers())
  272. print "getChannels [%s]" % (dbusCtl.getChannels(x) == iceCtl.getChannels(x))
  273. print "getPlayers [%s]" % (dbusCtl.getPlayers(x) == iceCtl.getPlayers(x))
  274. print "getACL(x, 0) [%s]" % (dbusCtl.getACL(x, 0) == iceCtl.getACL(x, 0))
  275. print "getAllServers [%s]" % (dbusCtl.getAllServers() == iceCtl.getAllServers())
  276. print "getDefaultConf [%s]" % (dbusCtl.getDefaultConf() == iceCtl.getDefaultConf())
  277. print "getAllConf(x) [%s]" % (dbusCtl.getAllConf(x) == iceCtl.getAllConf(x))
  278. print "getRegisteredPlayers(x) [%s]" % (dbusCtl.getRegisteredPlayers(x) == iceCtl.getRegisteredPlayers(x))
  279. #print iceCtl.getACL(x, 0)