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.

183 lines
5.1 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
  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. from os.path import join
  19. from django.utils.http import urlquote
  20. class mmChannel( object ):
  21. # channels = list();
  22. # subchans = list();
  23. # chanid = int();
  24. # name = str();
  25. # parent = mmChannel();
  26. # linked = list();
  27. # linkedIDs = list();
  28. def __init__( self, server, channelObj, parentChan = None ):
  29. self.server = server;
  30. self.players = list();
  31. self.subchans = list();
  32. self.linked = list();
  33. (self.chanid, self.name, parent, self.linkedIDs ) = channelObj;
  34. self.parent = parentChan;
  35. if self.parent is not None:
  36. self.parent.subchans.append( self );
  37. def parentChannels( self ):
  38. if self.parent is None or self.parent.is_server or self.parent.chanid == 0:
  39. return [];
  40. return self.parent.parentChannels() + [self.parent.name];
  41. is_server = False;
  42. is_channel = True;
  43. is_player = False;
  44. playerCount = property(
  45. lambda self: len( self.players ) + sum( [ chan.playerCount for chan in self.subchans ] ),
  46. None
  47. );
  48. id = property( lambda self: "channel_%d"%self.chanid, None );
  49. def __str__( self ):
  50. return '<Channel "%s" (%d)>' % ( self.name, self.chanid );
  51. def visit( self, callback, lvl = 0 ):
  52. # call callback on myself, then visit my subchans, then my players
  53. callback( self, lvl );
  54. for sc in self.subchans:
  55. sc.visit( callback, lvl + 1 );
  56. for pl in self.players:
  57. pl.visit( callback, lvl + 1 );
  58. def getURL( self, forUser = None ):
  59. # mumble://username@host:port/parentchans/self.name
  60. userstr = "";
  61. if forUser is not None:
  62. userstr = "%s@" % forUser.name;
  63. # create list of all my parents and myself
  64. chanlist = self.parentChannels() + [self.name];
  65. # urlencode channel names
  66. chanlist = [ urlquote( chan ) for chan in chanlist ];
  67. # create a path by joining the channel names
  68. chanpath = join( *chanlist );
  69. return "mumble://%s%s:%d/%s" % ( userstr, self.server.addr, self.server.port, chanpath );
  70. url = property( getURL, None );
  71. class mmPlayer( object ):
  72. # muted = bool;
  73. # deafened = bool;
  74. # suppressed = bool;
  75. # selfmuted = bool;
  76. # selfdeafened = bool;
  77. # channel = mmChannel();
  78. # dbaseid = int();
  79. # userid = int();
  80. # name = str();
  81. # onlinesince = time();
  82. # bytesPerSec = int();
  83. # mumbleuser = models.MumbleUser();
  84. def __init__( self, srvInstance, playerObj, playerChan ):
  85. ( self.userid, self.muted, self.deafened, self.suppressed, self.selfmuted, self.selfdeafened, chanID, self.dbaseid, self.name, onlinetime, self.bytesPerSec ) = playerObj;
  86. self.onlinesince = datetime.datetime.fromtimestamp( float( time() - onlinetime ) );
  87. self.channel = playerChan;
  88. self.channel.players.append( self );
  89. if self.isAuthed():
  90. from models import Mumble, MumbleUser
  91. try:
  92. self.mumbleuser = MumbleUser.objects.get( mumbleid=self.dbaseid, server=srvInstance );
  93. except MumbleUser.DoesNotExist:
  94. self.mumbleuser = None;
  95. else:
  96. self.mumbleuser = None;
  97. def __str__( self ):
  98. return '<Player "%s" (%d, %d)>' % ( self.name, self.userid, self.dbaseid );
  99. def isAuthed( self ):
  100. return self.dbaseid != -1;
  101. isAdmin = property(
  102. lambda self: self.mumbleuser and self.mumbleuser.getAdmin(),
  103. None
  104. );
  105. is_server = False;
  106. is_channel = False;
  107. is_player = True;
  108. # kept for compatibility to mmChannel (useful for traversal funcs)
  109. playerCount = property( lambda self: -1, None );
  110. id = property( lambda self: "player_%d"%self.userid, None );
  111. def visit( self, callback, lvl = 0 ):
  112. callback( self, lvl );
  113. class mmACL:
  114. def __init__( self, channelId, aclObj ):
  115. aclsrc, groupsrc, inherit = aclObj;
  116. self.channelId = channelId;
  117. self.acls = [];
  118. for line in aclsrc:
  119. acl = {};
  120. acl['applyHere'], acl['applySubs'], acl['inherited'], acl['playerid'], acl['group'], acl['allow'], acl['deny'] = line;
  121. self.acls.append( acl );
  122. self.groups = [];
  123. for line in groupsrc:
  124. group = {};
  125. group['name'], group['inherited'], group['inherit'], group['inheritable'], group['add'], group['remove'], group['members'] = line;
  126. self.groups.append( group );
  127. if group['name'] == "admin":
  128. self.admingroup = group;
  129. self.inherit = inherit;
  130. def pack( self ):
  131. return (
  132. self.channelId,
  133. [( acl['applyHere'], acl['applySubs'], acl['inherited'], acl['playerid'], acl['group'], acl['allow'], acl['deny'] ) for acl in self.acls ],
  134. [( group['name'], group['inherited'], group['inherit'], group['inheritable'], group['add'], group['remove'], group['members'] ) for group in self.groups ],
  135. self.inherit
  136. );