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.

247 lines
7.2 KiB

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
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
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
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. """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. versionstr = "version=%d.%d.%d" % tuple(self.server.version[0:3]);
  93. # create list of all my parents and myself
  94. chanlist = self.parentChannels() + [self.name];
  95. # urlencode channel names
  96. chanlist = [ urlquote( chan ) for chan in chanlist ];
  97. # create a path by joining the channel names
  98. chanpath = join( *chanlist );
  99. if self.server.port != settings.MUMBLE_DEFAULT_PORT:
  100. return "mumble://%s%s:%d/%s?%s" % ( userstr, self.server.addr, self.server.port, chanpath, versionstr );
  101. return "mumble://%s%s/%s?%s" % ( userstr, self.server.addr, chanpath, versionstr );
  102. connecturl = property( getURL, doc="A convenience wrapper for getURL." );
  103. def setDefault( self ):
  104. "Make this the server's default channel."
  105. self.server.defchan = self.chanid;
  106. self.server.save();
  107. is_default = property(
  108. lambda self: self.server.defchan == self.chanid,
  109. doc="True if this channel is the server's default channel."
  110. );
  111. class mmPlayer( object ):
  112. """Represents a Player in Murmur."""
  113. # muted = bool;
  114. # deafened = bool;
  115. # suppressed = bool;
  116. # selfmuted = bool;
  117. # selfdeafened = bool;
  118. # channel = mmChannel();
  119. # dbaseid = int();
  120. # userid = int();
  121. # name = str();
  122. # onlinesince = time();
  123. # bytesPerSec = int();
  124. # mumbleuser = models.MumbleUser();
  125. def __init__( self, srvInstance, playerObj, playerChan ):
  126. ( self.userid, self.muted, self.deafened, self.suppressed, self.selfmuted, self.selfdeafened, chanID, self.dbaseid, self.name, onlinetime, self.bytesPerSec ) = playerObj;
  127. self.onlinesince = datetime.datetime.fromtimestamp( float( time() - onlinetime ) );
  128. self.channel = playerChan;
  129. self.channel.players.append( self );
  130. if self.isAuthed:
  131. from models import Mumble, MumbleUser
  132. try:
  133. self.mumbleuser = MumbleUser.objects.get( mumbleid=self.dbaseid, server=srvInstance );
  134. except MumbleUser.DoesNotExist:
  135. self.mumbleuser = None;
  136. else:
  137. self.mumbleuser = None;
  138. def __str__( self ):
  139. return '<Player "%s" (%d, %d)>' % ( self.name, self.userid, self.dbaseid );
  140. isAuthed = property(
  141. lambda self: self.dbaseid != -1,
  142. doc="True if this player is authenticated (+A)."
  143. );
  144. isAdmin = property(
  145. lambda self: self.mumbleuser and self.mumbleuser.getAdmin(),
  146. doc="True if this player is in the Admin group in the ACL."
  147. );
  148. is_server = False;
  149. is_channel = False;
  150. is_player = True;
  151. # kept for compatibility to mmChannel (useful for traversal funcs)
  152. playerCount = property( lambda self: -1, doc="Exists only for compatibility to mmChannel." );
  153. id = property(
  154. lambda self: "player_%d"%self.userid,
  155. doc="A string ready to be used in an id property of an HTML tag."
  156. );
  157. def visit( self, callback, lvl = 0 ):
  158. """ Call callback on myself. """
  159. callback( self, lvl );
  160. class mmACL:
  161. """Represents an ACL for a certain channel."""
  162. def __init__( self, channelId, aclObj ):
  163. aclsrc, groupsrc, inherit = aclObj;
  164. self.channelId = channelId;
  165. self.acls = [];
  166. for line in aclsrc:
  167. acl = {};
  168. acl['applyHere'], acl['applySubs'], acl['inherited'], acl['playerid'], acl['group'], acl['allow'], acl['deny'] = line;
  169. self.acls.append( acl );
  170. self.groups = [];
  171. for line in groupsrc:
  172. group = {};
  173. group['name'], group['inherited'], group['inherit'], group['inheritable'], group['add'], group['remove'], group['members'] = line;
  174. self.groups.append( group );
  175. if group['name'] == "admin":
  176. self.admingroup = group;
  177. self.inherit = inherit;
  178. def pack( self ):
  179. """ Pack the information in this ACL up in a way that it can be passed to DBus. """
  180. return (
  181. self.channelId,
  182. [( acl['applyHere'], acl['applySubs'], acl['inherited'], acl['playerid'], acl['group'], acl['allow'], acl['deny'] ) for acl in self.acls ],
  183. [( group['name'], group['inherited'], group['inherit'], group['inheritable'], group['add'], group['remove'], group['members'] ) for group in self.groups ],
  184. self.inherit
  185. );