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