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.

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