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.

242 lines
6.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
16 years ago
16 years ago
  1. # -*- coding: utf-8 -*-
  2. """
  3. * Copyright (C) 2009, Michael "Svedrin" Ziegler <diese-addy@funzt-halt.net>
  4. *
  5. * Mumble-Django is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This package is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. """
  15. import dbus
  16. import datetime
  17. from time import time
  18. # base = ice.stringToProxy( "Meta:tcp -h 127.0.0.1 -p 6502" );
  19. # srv = Murmur.ServerPrx.checkedCast( base );
  20. # met = Murmur.MetaPrx.checkedCast( base );
  21. class mmServer( object ):
  22. # channels = dict();
  23. # players = dict();
  24. # id = int();
  25. # rootName = str();
  26. def __init__( self, serverID, serverObj, rootName = '' ):
  27. self.dbusObj = serverObj;
  28. self.channels = dict();
  29. self.players = dict();
  30. self.id = serverID;
  31. self.rootName = rootName;
  32. links = dict();
  33. for theChan in serverObj.getChannels():
  34. # Channels - Fields: 0 = ID, 1 = Name, 2 = Parent-ID, 3 = Links
  35. if( theChan[2] == -1 ):
  36. # No parent
  37. self.channels[theChan[0]] = mmChannel( theChan );
  38. else:
  39. self.channels[theChan[0]] = mmChannel( theChan, self.channels[theChan[2]] );
  40. self.channels[theChan[0]].serverId = self.id;
  41. # process links - if the linked channels are known, link; else save their ids to link later
  42. for linked in theChan[3]:
  43. if linked in self.channels:
  44. self.channels[theChan[0]].linked.append( self.channels[linked] );
  45. else:
  46. if linked not in links:
  47. links[linked] = list();
  48. links[linked].append( self.channels[theChan[0]] );
  49. #print "Saving link: %s <- %s" % ( linked, self.channels[theChan[0]] );
  50. # check if earlier round trips saved channel ids to be linked to the current channel
  51. if theChan[0] in links:
  52. for targetChan in links[theChan[0]]:
  53. targetChan.linked.append( self.channels[theChan[0]] );
  54. if self.rootName:
  55. self.channels[0].name = self.rootName;
  56. for thePlayer in serverObj.getPlayers():
  57. # Players - Fields: 0 = UserID, 6 = ChannelID
  58. self.players[ thePlayer[0] ] = mmPlayer( thePlayer, self.channels[ thePlayer[6] ] );
  59. playerCount = property(
  60. lambda self: len( self.players ),
  61. None
  62. );
  63. def is_server( self ):
  64. return True;
  65. def is_channel( self ):
  66. return False;
  67. def is_player( self ):
  68. return False;
  69. def __str__( self ):
  70. return '<Server "%s" (%d)>' % ( self.rootName, self.id );
  71. def visit( self, callback, lvl = 0 ):
  72. if not callable( callback ):
  73. raise Exception, "a callback should be callable...";
  74. # call callback first on myself, then visit my root chan
  75. callback( self, lvl );
  76. self.channels[0].visit( callback, lvl + 1 );
  77. class mmChannel( object ):
  78. # channels = list();
  79. # subchans = list();
  80. # chanid = int();
  81. # name = str();
  82. # parent = mmChannel();
  83. # linked = list();
  84. # linkedIDs = list();
  85. def __init__( self, channelObj, parentChan = None ):
  86. self.players = list();
  87. self.subchans = list();
  88. self.linked = list();
  89. (self.chanid, self.name, parent, self.linkedIDs ) = channelObj;
  90. self.parent = parentChan;
  91. if self.parent is not None:
  92. self.parent.subchans.append( self );
  93. self.serverId = self.parent.serverId;
  94. def parentChannels( self ):
  95. if self.parent is None or self.parent.is_server() or self.parent.chanid == 0:
  96. return [];
  97. return self.parent.parentChannels() + [self.parent.name];
  98. def is_server( self ):
  99. return False;
  100. def is_channel( self ):
  101. return True;
  102. def is_player( self ):
  103. return False;
  104. playerCount = property(
  105. lambda self: len( self.players ) + sum( [ chan.playerCount for chan in self.subchans ] ),
  106. None
  107. );
  108. id = property( lambda self: "channel_%d"%self.chanid, None );
  109. def __str__( self ):
  110. return '<Channel "%s" (%d)>' % ( self.name, self.chanid );
  111. def visit( self, callback, lvl = 0 ):
  112. # call callback on myself, then visit my subchans, then my players
  113. callback( self, lvl );
  114. for sc in self.subchans:
  115. sc.visit( callback, lvl + 1 );
  116. for pl in self.players:
  117. pl.visit( callback, lvl + 1 );
  118. class mmPlayer( object ):
  119. # muted = bool;
  120. # deafened = bool;
  121. # suppressed = bool;
  122. # selfmuted = bool;
  123. # selfdeafened = bool;
  124. # channel = mmChannel();
  125. # dbaseid = int();
  126. # userid = int();
  127. # name = str();
  128. # onlinesince = time();
  129. # bytesPerSec = int();
  130. # mumbleuser = models.MumbleUser();
  131. def __init__( self, playerObj, playerChan ):
  132. ( self.userid, self.muted, self.deafened, self.suppressed, self.selfmuted, self.selfdeafened, chanID, self.dbaseid, self.name, onlinetime, self.bytesPerSec ) = playerObj;
  133. self.onlinesince = datetime.datetime.fromtimestamp( float( time() - onlinetime ) );
  134. self.channel = playerChan;
  135. self.channel.players.append( self );
  136. if self.isAuthed():
  137. from models import Mumble, MumbleUser
  138. srvInstance = Mumble.objects.get( srvid=self.channel.serverId );
  139. try:
  140. self.mumbleuser = MumbleUser.objects.get( mumbleid=self.dbaseid, server=srvInstance );
  141. except MumbleUser.DoesNotExist:
  142. self.mumbleuser = None;
  143. else:
  144. self.mumbleuser = None;
  145. def __str__( self ):
  146. return '<Player "%s" (%d, %d)>' % ( self.name, self.userid, self.dbaseid );
  147. def isAuthed( self ):
  148. return self.dbaseid != -1;
  149. isAdmin = property(
  150. lambda self: self.mumbleuser and self.mumbleuser.getAdmin(),
  151. None
  152. );
  153. def is_server( self ):
  154. return False;
  155. def is_channel( self ):
  156. return False;
  157. def is_player( self ):
  158. return True;
  159. # kept for compatibility to mmChannel (useful for traversal funcs)
  160. playerCount = property( lambda self: -1, None );
  161. id = property( lambda self: "player_%d"%self.userid, None );
  162. def visit( self, callback, lvl = 0 ):
  163. callback( self, lvl );
  164. class mmACL:
  165. def __init__( self, channelId, aclObj ):
  166. aclsrc, groupsrc, inherit = aclObj;
  167. self.channelId = channelId;
  168. self.acls = [];
  169. for line in aclsrc:
  170. acl = {};
  171. acl['applyHere'], acl['applySubs'], acl['inherited'], acl['playerid'], acl['group'], acl['allow'], acl['deny'] = line;
  172. self.acls.append( acl );
  173. self.groups = [];
  174. for line in groupsrc:
  175. group = {};
  176. group['name'], group['inherited'], group['inherit'], group['inheritable'], group['add'], group['remove'], group['members'] = line;
  177. self.groups.append( group );
  178. if group['name'] == "admin":
  179. self.admingroup = group;
  180. self.inherit = inherit;
  181. def pack( self ):
  182. return (
  183. self.channelId,
  184. [( acl['applyHere'], acl['applySubs'], acl['inherited'], acl['playerid'], acl['group'], acl['allow'], acl['deny'] ) for acl in self.acls ],
  185. [( group['name'], group['inherited'], group['inherit'], group['inheritable'], group['add'], group['remove'], group['members'] ) for group in self.groups ],
  186. self.inherit
  187. );