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.

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