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.

245 lines
7.1 KiB

16 years ago
15 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
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
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 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. doc="The number of players in this channel."
  59. );
  60. id = property(
  61. lambda self: "channel_%d"%self.chanid,
  62. doc="A string ready to be used in an id property of an HTML tag."
  63. );
  64. top_or_not_empty = property(
  65. lambda self: self.parent is None or self.parent.chanid == 0 or self.playerCount > 0,
  66. doc="True if this channel needs to be shown because it is root, a child of root, or has players."
  67. );
  68. show = property( lambda self: settings.SHOW_EMPTY_SUBCHANS or self.top_or_not_empty );
  69. def __str__( self ):
  70. return '<Channel "%s" (%d)>' % ( self.name, self.chanid );
  71. def sort( self ):
  72. """Sort my subchannels and players, and then iterate over them and sort them recursively."""
  73. self.subchans.sort( cmp_names );
  74. self.players.sort( cmp_names );
  75. for sc in self.subchans:
  76. sc.sort();
  77. def visit( self, callback, lvl = 0 ):
  78. """Call callback on myself, then visit my subchans, then my players."""
  79. callback( self, lvl );
  80. for sc in self.subchans:
  81. sc.visit( callback, lvl + 1 );
  82. for pl in self.players:
  83. pl.visit( callback, lvl + 1 );
  84. def getURL( self, forUser = None ):
  85. """
  86. Create an URL to connect to this channel. The URL is of the form
  87. mumble://username@host:port/parentchans/self.name
  88. """
  89. userstr = "";
  90. if forUser is not None:
  91. userstr = "%s@" % forUser.name;
  92. # create list of all my parents and myself
  93. chanlist = self.parentChannels() + [self.name];
  94. # urlencode channel names
  95. chanlist = [ urlquote( chan ) for chan in chanlist ];
  96. # create a path by joining the channel names
  97. chanpath = join( *chanlist );
  98. if self.server.port != settings.MUMBLE_DEFAULT_PORT:
  99. return "mumble://%s%s:%d/%s" % ( userstr, self.server.addr, self.server.port, chanpath );
  100. return "mumble://%s%s/%s" % ( userstr, self.server.addr, chanpath );
  101. connecturl = property( getURL, doc="A convenience wrapper for getURL." );
  102. def setDefault( self ):
  103. "Make this the server's default channel."
  104. self.server.defchan = self.chanid;
  105. self.server.save();
  106. is_default = property(
  107. lambda self: self.server.defchan == self.chanid,
  108. doc="True if this channel is the server's default channel."
  109. );
  110. class mmPlayer( object ):
  111. """Represents a Player in Murmur."""
  112. # muted = bool;
  113. # deafened = bool;
  114. # suppressed = bool;
  115. # selfmuted = bool;
  116. # selfdeafened = bool;
  117. # channel = mmChannel();
  118. # dbaseid = int();
  119. # userid = int();
  120. # name = str();
  121. # onlinesince = time();
  122. # bytesPerSec = int();
  123. # mumbleuser = models.MumbleUser();
  124. def __init__( self, srvInstance, playerObj, playerChan ):
  125. ( self.userid, self.muted, self.deafened, self.suppressed, self.selfmuted, self.selfdeafened, chanID, self.dbaseid, self.name, onlinetime, self.bytesPerSec ) = playerObj;
  126. self.onlinesince = datetime.datetime.fromtimestamp( float( time() - onlinetime ) );
  127. self.channel = playerChan;
  128. self.channel.players.append( self );
  129. if self.isAuthed:
  130. from models import Mumble, MumbleUser
  131. try:
  132. self.mumbleuser = MumbleUser.objects.get( mumbleid=self.dbaseid, server=srvInstance );
  133. except MumbleUser.DoesNotExist:
  134. self.mumbleuser = None;
  135. else:
  136. self.mumbleuser = None;
  137. def __str__( self ):
  138. return '<Player "%s" (%d, %d)>' % ( self.name, self.userid, self.dbaseid );
  139. isAuthed = property(
  140. lambda self: self.dbaseid != -1,
  141. doc="True if this player is authenticated (+A)."
  142. );
  143. isAdmin = property(
  144. lambda self: self.mumbleuser and self.mumbleuser.getAdmin(),
  145. doc="True if this player is in the Admin group in the ACL."
  146. );
  147. is_server = False;
  148. is_channel = False;
  149. is_player = True;
  150. # kept for compatibility to mmChannel (useful for traversal funcs)
  151. playerCount = property( lambda self: -1, doc="Exists only for compatibility to mmChannel." );
  152. id = property(
  153. lambda self: "player_%d"%self.userid,
  154. doc="A string ready to be used in an id property of an HTML tag."
  155. );
  156. def visit( self, callback, lvl = 0 ):
  157. """ Call callback on myself. """
  158. callback( self, lvl );
  159. class mmACL:
  160. """Represents an ACL for a certain channel."""
  161. def __init__( self, channelId, aclObj ):
  162. aclsrc, groupsrc, inherit = aclObj;
  163. self.channelId = channelId;
  164. self.acls = [];
  165. for line in aclsrc:
  166. acl = {};
  167. acl['applyHere'], acl['applySubs'], acl['inherited'], acl['playerid'], acl['group'], acl['allow'], acl['deny'] = line;
  168. self.acls.append( acl );
  169. self.groups = [];
  170. for line in groupsrc:
  171. group = {};
  172. group['name'], group['inherited'], group['inherit'], group['inheritable'], group['add'], group['remove'], group['members'] = line;
  173. self.groups.append( group );
  174. if group['name'] == "admin":
  175. self.admingroup = group;
  176. self.inherit = inherit;
  177. def pack( self ):
  178. """ Pack the information in this ACL up in a way that it can be passed to DBus. """
  179. return (
  180. self.channelId,
  181. [( acl['applyHere'], acl['applySubs'], acl['inherited'], acl['playerid'], acl['group'], acl['allow'], acl['deny'] ) for acl in self.acls ],
  182. [( group['name'], group['inherited'], group['inherit'], group['inheritable'], group['add'], group['remove'], group['members'] ) for group in self.groups ],
  183. self.inherit
  184. );