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.

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