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.

243 lines
7.3 KiB

16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
  1. """ This file is part of the mumble-django application.
  2. Copyright (C) 2009, Michael "Svedrin" Ziegler <diese-addy@funzt-halt.net>
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions
  6. are met:
  7. - Redistributions of source code must retain the above copyright notice,
  8. this list of conditions and the following disclaimer.
  9. - Redistributions in binary form must reproduce the above copyright notice,
  10. this list of conditions and the following disclaimer in the documentation
  11. and/or other materials provided with the distribution.
  12. - Neither the name of the Mumble Developers nor the names of its
  13. contributors may be used to endorse or promote products derived from this
  14. software without specific prior written permission.
  15. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  16. ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  17. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  18. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
  19. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  20. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  22. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  23. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  24. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. """
  27. import dbus
  28. import datetime
  29. from time import time
  30. # base = ice.stringToProxy( "Meta:tcp -h 127.0.0.1 -p 6502" );
  31. # srv = Murmur.ServerPrx.checkedCast( base );
  32. # met = Murmur.MetaPrx.checkedCast( base );
  33. class mmServer( object ):
  34. # channels = dict();
  35. # players = dict();
  36. # id = int();
  37. # rootName = str();
  38. def __init__( self, serverID, serverObj, rootName = '' ):
  39. if not isinstance( serverObj, dbus.proxies.ProxyObject ):
  40. raise Exception, "mmServer: I need the object returned by dbus.get_object!"
  41. self.dbusObj = serverObj;
  42. self.channels = dict();
  43. self.players = dict();
  44. self.id = serverID;
  45. self.rootName = rootName;
  46. links = dict();
  47. for theChan in serverObj.getChannels():
  48. # Channels - Fields: 0 = ID, 1 = Name, 2 = Parent-ID, 3 = Links
  49. if( theChan[2] == -1 ):
  50. # No parent
  51. self.channels[theChan[0]] = mmChannel( theChan );
  52. else:
  53. self.channels[theChan[0]] = mmChannel( theChan, self.channels[theChan[2]] );
  54. self.channels[theChan[0]].serverId = self.id;
  55. # process links - if the linked channels are known, link; else save their ids to link later
  56. for linked in theChan[3]:
  57. if linked in self.channels:
  58. self.channels[theChan[0]].linked.append( self.channels[linked] );
  59. else:
  60. if linked not in links:
  61. links[linked] = list();
  62. links[linked].append( self.channels[theChan[0]] );
  63. #print "Saving link: %s <- %s" % ( linked, self.channels[theChan[0]] );
  64. # check if earlier round trips saved channel ids to be linked to the current channel
  65. if theChan[0] in links:
  66. for targetChan in links[theChan[0]]:
  67. targetChan.linked.append( self.channels[theChan[0]] );
  68. if self.rootName:
  69. self.channels[0].name = self.rootName;
  70. for thePlayer in serverObj.getPlayers():
  71. # Players - Fields: 0 = UserID, 6 = ChannelID
  72. self.players[ thePlayer[0] ] = mmPlayer( thePlayer, self.channels[ thePlayer[6] ] );
  73. playerCount = property(
  74. lambda self: len( self.players ),
  75. None
  76. );
  77. def is_server( self ):
  78. return True;
  79. def is_channel( self ):
  80. return False;
  81. def is_player( self ):
  82. return False;
  83. def __str__( self ):
  84. return '<Server "%s" (%d)>' % ( self.rootName, self.id );
  85. def visit( self, callback, lvl = 0 ):
  86. if not callable( callback ):
  87. raise Exception, "a callback should be callable...";
  88. # call callback first on myself, then visit my root chan
  89. callback( self, lvl );
  90. self.channels[0].visit( callback, lvl + 1 );
  91. class mmChannel( object ):
  92. # channels = list();
  93. # subchans = list();
  94. # id = int();
  95. # name = str();
  96. # parent = mmChannel();
  97. # linked = list();
  98. # linkedIDs = list();
  99. def __init__( self, channelObj, parentChan = None ):
  100. self.players = list();
  101. self.subchans = list();
  102. self.linked = list();
  103. (self.id, self.name, parent, self.linkedIDs ) = channelObj;
  104. self.parent = parentChan;
  105. if self.parent is not None:
  106. self.parent.subchans.append( self );
  107. self.serverId = self.parent.serverId;
  108. def parentChannels( self ):
  109. if self.parent is None or self.parent.is_server() or self.parent.id == 0:
  110. return [];
  111. return self.parent.parentChannels() + [self.parent.name];
  112. def is_server( self ):
  113. return False;
  114. def is_channel( self ):
  115. return True;
  116. def is_player( self ):
  117. return False;
  118. playerCount = property(
  119. lambda self: len( self.players ) + sum( [ chan.playerCount for chan in self.subchans ] ),
  120. None
  121. );
  122. def __str__( self ):
  123. return '<Channel "%s" (%d)>' % ( self.name, self.id );
  124. def visit( self, callback, lvl = 0 ):
  125. # call callback on myself, then visit my subchans, then my players
  126. callback( self, lvl );
  127. for sc in self.subchans:
  128. sc.visit( callback, lvl + 1 );
  129. for pl in self.players:
  130. pl.visit( callback, lvl + 1 );
  131. class mmPlayer( object ):
  132. # muted = bool;
  133. # deafened = bool;
  134. # suppressed = bool;
  135. # selfmuted = bool;
  136. # selfdeafened = bool;
  137. # channel = mmChannel();
  138. # dbaseid = int();
  139. # userid = int();
  140. # name = str();
  141. # onlinesince = time();
  142. # bytesPerSec = int();
  143. # mumbleuser = models.MumbleUser();
  144. def __init__( self, playerObj, playerChan ):
  145. ( self.userid, self.muted, self.deafened, self.suppressed, self.selfmuted, self.selfdeafened, chanID, self.dbaseid, self.name, onlinetime, self.bytesPerSec ) = playerObj;
  146. self.onlinesince = datetime.datetime.fromtimestamp( float( time() - onlinetime ) );
  147. self.channel = playerChan;
  148. self.channel.players.append( self );
  149. if self.isAuthed():
  150. from models import Mumble, MumbleUser
  151. srvInstance = Mumble.objects.get( srvid=self.channel.serverId );
  152. try:
  153. self.mumbleuser = MumbleUser.objects.get( mumbleid=self.dbaseid, server=srvInstance );
  154. except MumbleUser.DoesNotExist:
  155. self.mumbleuser = None;
  156. else:
  157. self.mumbleuser = None;
  158. def __str__( self ):
  159. return '<Player "%s" (%d, %d)>' % ( self.name, self.userid, self.dbaseid );
  160. def isAuthed( self ):
  161. return self.dbaseid != -1;
  162. def is_server( self ):
  163. return False;
  164. def is_channel( self ):
  165. return False;
  166. def is_player( self ):
  167. return True;
  168. # kept for compatibility to mmChannel (useful for traversal funcs)
  169. playerCount = property(
  170. lambda self: -1,
  171. None
  172. );
  173. def visit( self, callback, lvl = 0 ):
  174. callback( self, lvl );
  175. if __name__ == '__main__':
  176. # connect to dbus
  177. bus = dbus.SystemBus();
  178. # get our murmur servers
  179. dbus_base = 'net.sourceforge.mumble.murmur';
  180. murmur = bus.get_object( dbus_base, '/' );
  181. # example callback
  182. def travrz( obj, lvl ):
  183. print lvl*'-', str(obj);
  184. # show each server
  185. for srv in murmur.getBootedServers():
  186. theSrv = bus.get_object( dbus_base, '/%d' % srv );
  187. srvobj = mmServer( srv, theSrv, 'teh %d srvz root' % srv );
  188. srvobj.visit( travrz );