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.7 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
  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. self.dbusObj = serverObj;
  40. self.channels = dict();
  41. self.players = dict();
  42. self.id = serverID;
  43. self.rootName = rootName;
  44. links = dict();
  45. for theChan in serverObj.getChannels():
  46. # Channels - Fields: 0 = ID, 1 = Name, 2 = Parent-ID, 3 = Links
  47. if( theChan[2] == -1 ):
  48. # No parent
  49. self.channels[theChan[0]] = mmChannel( theChan );
  50. else:
  51. self.channels[theChan[0]] = mmChannel( theChan, self.channels[theChan[2]] );
  52. self.channels[theChan[0]].serverId = self.id;
  53. # process links - if the linked channels are known, link; else save their ids to link later
  54. for linked in theChan[3]:
  55. if linked in self.channels:
  56. self.channels[theChan[0]].linked.append( self.channels[linked] );
  57. else:
  58. if linked not in links:
  59. links[linked] = list();
  60. links[linked].append( self.channels[theChan[0]] );
  61. #print "Saving link: %s <- %s" % ( linked, self.channels[theChan[0]] );
  62. # check if earlier round trips saved channel ids to be linked to the current channel
  63. if theChan[0] in links:
  64. for targetChan in links[theChan[0]]:
  65. targetChan.linked.append( self.channels[theChan[0]] );
  66. if self.rootName:
  67. self.channels[0].name = self.rootName;
  68. for thePlayer in serverObj.getPlayers():
  69. # Players - Fields: 0 = UserID, 6 = ChannelID
  70. self.players[ thePlayer[0] ] = mmPlayer( thePlayer, self.channels[ thePlayer[6] ] );
  71. playerCount = property(
  72. lambda self: len( self.players ),
  73. None
  74. );
  75. def is_server( self ):
  76. return True;
  77. def is_channel( self ):
  78. return False;
  79. def is_player( self ):
  80. return False;
  81. def __str__( self ):
  82. return '<Server "%s" (%d)>' % ( self.rootName, self.id );
  83. def visit( self, callback, lvl = 0 ):
  84. if not callable( callback ):
  85. raise Exception, "a callback should be callable...";
  86. # call callback first on myself, then visit my root chan
  87. callback( self, lvl );
  88. self.channels[0].visit( callback, lvl + 1 );
  89. class mmChannel( object ):
  90. # channels = list();
  91. # subchans = list();
  92. # id = int();
  93. # name = str();
  94. # parent = mmChannel();
  95. # linked = list();
  96. # linkedIDs = list();
  97. def __init__( self, channelObj, parentChan = None ):
  98. self.players = list();
  99. self.subchans = list();
  100. self.linked = list();
  101. (self.id, self.name, parent, self.linkedIDs ) = channelObj;
  102. self.parent = parentChan;
  103. if self.parent is not None:
  104. self.parent.subchans.append( self );
  105. self.serverId = self.parent.serverId;
  106. def parentChannels( self ):
  107. if self.parent is None or self.parent.is_server() or self.parent.id == 0:
  108. return [];
  109. return self.parent.parentChannels() + [self.parent.name];
  110. def is_server( self ):
  111. return False;
  112. def is_channel( self ):
  113. return True;
  114. def is_player( self ):
  115. return False;
  116. playerCount = property(
  117. lambda self: len( self.players ) + sum( [ chan.playerCount for chan in self.subchans ] ),
  118. None
  119. );
  120. def __str__( self ):
  121. return '<Channel "%s" (%d)>' % ( self.name, self.id );
  122. def visit( self, callback, lvl = 0 ):
  123. # call callback on myself, then visit my subchans, then my players
  124. callback( self, lvl );
  125. for sc in self.subchans:
  126. sc.visit( callback, lvl + 1 );
  127. for pl in self.players:
  128. pl.visit( callback, lvl + 1 );
  129. class mmPlayer( object ):
  130. # muted = bool;
  131. # deafened = bool;
  132. # suppressed = bool;
  133. # selfmuted = bool;
  134. # selfdeafened = bool;
  135. # channel = mmChannel();
  136. # dbaseid = int();
  137. # userid = int();
  138. # name = str();
  139. # onlinesince = time();
  140. # bytesPerSec = int();
  141. # mumbleuser = models.MumbleUser();
  142. def __init__( self, playerObj, playerChan ):
  143. ( self.userid, self.muted, self.deafened, self.suppressed, self.selfmuted, self.selfdeafened, chanID, self.dbaseid, self.name, onlinetime, self.bytesPerSec ) = playerObj;
  144. self.onlinesince = datetime.datetime.fromtimestamp( float( time() - onlinetime ) );
  145. self.channel = playerChan;
  146. self.channel.players.append( self );
  147. if self.isAuthed():
  148. from models import Mumble, MumbleUser
  149. srvInstance = Mumble.objects.get( srvid=self.channel.serverId );
  150. try:
  151. self.mumbleuser = MumbleUser.objects.get( mumbleid=self.dbaseid, server=srvInstance );
  152. except MumbleUser.DoesNotExist:
  153. self.mumbleuser = None;
  154. else:
  155. self.mumbleuser = None;
  156. def __str__( self ):
  157. return '<Player "%s" (%d, %d)>' % ( self.name, self.userid, self.dbaseid );
  158. def isAuthed( self ):
  159. return self.dbaseid != -1;
  160. def is_server( self ):
  161. return False;
  162. def is_channel( self ):
  163. return False;
  164. def is_player( self ):
  165. return True;
  166. # kept for compatibility to mmChannel (useful for traversal funcs)
  167. playerCount = property(
  168. lambda self: -1,
  169. None
  170. );
  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. );