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.

225 lines
6.4 KiB

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