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.

254 lines
7.9 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
16 years ago
16 years ago
16 years ago
16 years ago
  1. # -*- coding: utf-8 -*-
  2. """ This file is part of the mumble-django application.
  3. Copyright (C) 2009, Michael "Svedrin" Ziegler <diese-addy@funzt-halt.net>
  4. All rights reserved.
  5. Redistribution and use in source and binary forms, with or without
  6. modification, are permitted provided that the following conditions
  7. are met:
  8. - Redistributions of source code must retain the above copyright notice,
  9. this list of conditions and the following disclaimer.
  10. - Redistributions in binary form must reproduce the above copyright notice,
  11. this list of conditions and the following disclaimer in the documentation
  12. and/or other materials provided with the distribution.
  13. - Neither the name of the Mumble Developers nor the names of its
  14. contributors may be used to endorse or promote products derived from this
  15. software without specific prior written permission.
  16. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  17. ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  18. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  19. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
  20. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  21. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  22. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  23. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  24. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  25. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  26. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. """
  28. import dbus
  29. import datetime
  30. from time import time
  31. # base = ice.stringToProxy( "Meta:tcp -h 127.0.0.1 -p 6502" );
  32. # srv = Murmur.ServerPrx.checkedCast( base );
  33. # met = Murmur.MetaPrx.checkedCast( base );
  34. class mmServer( object ):
  35. # channels = dict();
  36. # players = dict();
  37. # id = int();
  38. # rootName = str();
  39. def __init__( self, serverID, serverObj, rootName = '' ):
  40. self.dbusObj = serverObj;
  41. self.channels = dict();
  42. self.players = dict();
  43. self.id = serverID;
  44. self.rootName = rootName;
  45. links = dict();
  46. for theChan in serverObj.getChannels():
  47. # Channels - Fields: 0 = ID, 1 = Name, 2 = Parent-ID, 3 = Links
  48. if( theChan[2] == -1 ):
  49. # No parent
  50. self.channels[theChan[0]] = mmChannel( theChan );
  51. else:
  52. self.channels[theChan[0]] = mmChannel( theChan, self.channels[theChan[2]] );
  53. self.channels[theChan[0]].serverId = self.id;
  54. # process links - if the linked channels are known, link; else save their ids to link later
  55. for linked in theChan[3]:
  56. if linked in self.channels:
  57. self.channels[theChan[0]].linked.append( self.channels[linked] );
  58. else:
  59. if linked not in links:
  60. links[linked] = list();
  61. links[linked].append( self.channels[theChan[0]] );
  62. #print "Saving link: %s <- %s" % ( linked, self.channels[theChan[0]] );
  63. # check if earlier round trips saved channel ids to be linked to the current channel
  64. if theChan[0] in links:
  65. for targetChan in links[theChan[0]]:
  66. targetChan.linked.append( self.channels[theChan[0]] );
  67. if self.rootName:
  68. self.channels[0].name = self.rootName;
  69. for thePlayer in serverObj.getPlayers():
  70. # Players - Fields: 0 = UserID, 6 = ChannelID
  71. self.players[ thePlayer[0] ] = mmPlayer( thePlayer, self.channels[ thePlayer[6] ] );
  72. playerCount = property(
  73. lambda self: len( self.players ),
  74. None
  75. );
  76. def is_server( self ):
  77. return True;
  78. def is_channel( self ):
  79. return False;
  80. def is_player( self ):
  81. return False;
  82. def __str__( self ):
  83. return '<Server "%s" (%d)>' % ( self.rootName, self.id );
  84. def visit( self, callback, lvl = 0 ):
  85. if not callable( callback ):
  86. raise Exception, "a callback should be callable...";
  87. # call callback first on myself, then visit my root chan
  88. callback( self, lvl );
  89. self.channels[0].visit( callback, lvl + 1 );
  90. class mmChannel( object ):
  91. # channels = list();
  92. # subchans = list();
  93. # chanid = int();
  94. # name = str();
  95. # parent = mmChannel();
  96. # linked = list();
  97. # linkedIDs = list();
  98. def __init__( self, channelObj, parentChan = None ):
  99. self.players = list();
  100. self.subchans = list();
  101. self.linked = list();
  102. (self.chanid, self.name, parent, self.linkedIDs ) = channelObj;
  103. self.parent = parentChan;
  104. if self.parent is not None:
  105. self.parent.subchans.append( self );
  106. self.serverId = self.parent.serverId;
  107. def parentChannels( self ):
  108. if self.parent is None or self.parent.is_server() or self.parent.id == 0:
  109. return [];
  110. return self.parent.parentChannels() + [self.parent.name];
  111. def is_server( self ):
  112. return False;
  113. def is_channel( self ):
  114. return True;
  115. def is_player( self ):
  116. return False;
  117. playerCount = property(
  118. lambda self: len( self.players ) + sum( [ chan.playerCount for chan in self.subchans ] ),
  119. None
  120. );
  121. id = property( lambda self: "channel_%d"%self.chanid, None );
  122. def __str__( self ):
  123. return '<Channel "%s" (%d)>' % ( self.name, self.chanid );
  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( lambda self: -1, None );
  170. id = property( lambda self: "player_%d"%self.id, None );
  171. def visit( self, callback, lvl = 0 ):
  172. callback( self, lvl );
  173. class mmACL:
  174. def __init__( self, channelId, aclObj ):
  175. aclsrc, groupsrc, inherit = aclObj;
  176. self.channelId = channelId;
  177. self.acls = [];
  178. for line in aclsrc:
  179. acl = {};
  180. acl['applyHere'], acl['applySubs'], acl['inherited'], acl['playerid'], acl['group'], acl['allow'], acl['deny'] = line;
  181. self.acls.append( acl );
  182. self.groups = [];
  183. for line in groupsrc:
  184. group = {};
  185. group['name'], group['inherited'], group['inherit'], group['inheritable'], group['add'], group['remove'], group['members'] = line;
  186. self.groups.append( group );
  187. if group['name'] == "admin":
  188. self.admingroup = group;
  189. self.inherit = inherit;
  190. def pack( self ):
  191. return (
  192. self.channelId,
  193. [( acl['applyHere'], acl['applySubs'], acl['inherited'], acl['playerid'], acl['group'], acl['allow'], acl['deny'] ) for acl in self.acls ],
  194. [( group['name'], group['inherited'], group['inherit'], group['inheritable'], group['add'], group['remove'], group['members'] ) for group in self.groups ],
  195. self.inherit
  196. );