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.

280 lines
8.4 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
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 © 2009-2010, 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 datetime
  16. from time import time
  17. from django.utils.http import urlquote
  18. from django.conf import settings
  19. def cmp_names( left, rite ):
  20. """ Compare two objects by their name property. """
  21. return cmp( left.name, rite.name );
  22. class mmChannel( object ):
  23. """ Represents a channel in Murmur. """
  24. def __init__( self, server, channel_obj, parent_chan = None ):
  25. self.server = server;
  26. self.players = list();
  27. self.subchans = list();
  28. self.linked = list();
  29. self.channel_obj = channel_obj;
  30. self.chanid = channel_obj.id;
  31. self.parent = parent_chan;
  32. if self.parent is not None:
  33. self.parent.subchans.append( self );
  34. self._acl = None;
  35. # Lookup unknown attributes in self.channel_obj to automatically include Murmur's fields
  36. def __getattr__( self, key ):
  37. if hasattr( self.channel_obj, key ):
  38. return getattr( self.channel_obj, key );
  39. else:
  40. raise AttributeError( "'%s' object has no attribute '%s'" % ( self.__class__.__name__, key ) );
  41. def parent_channels( self ):
  42. """ Return the names of this channel's parents in the channel tree. """
  43. if self.parent is None or self.parent.is_server or self.parent.chanid == 0:
  44. return [];
  45. return self.parent.parent_channels() + [self.parent.name];
  46. def getACL( self ):
  47. """ Retrieve the ACL for this channel. """
  48. if not self._acl:
  49. self._acl = mmACL( self, self.server.ctl.getACL( self.server.srvid, self.chanid ) );
  50. return self._acl;
  51. acl = property( getACL, doc=getACL.__doc__ );
  52. is_server = False;
  53. is_channel = True;
  54. is_player = False;
  55. playerCount = property(
  56. lambda self: len( self.players ) + sum( [ chan.playerCount for chan in self.subchans ] ),
  57. doc="The number of players in this channel."
  58. );
  59. id = property(
  60. lambda self: "channel_%d"%self.chanid,
  61. doc="A string ready to be used in an id property of an HTML tag."
  62. );
  63. top_or_not_empty = property(
  64. lambda self: self.parent is None or self.parent.chanid == 0 or self.playerCount > 0,
  65. doc="True if this channel needs to be shown because it is root, a child of root, or has players."
  66. );
  67. show = property( lambda self: settings.SHOW_EMPTY_SUBCHANS or self.top_or_not_empty );
  68. def __str__( self ):
  69. return '<Channel "%s" (%d)>' % ( self.name, self.chanid );
  70. def sort( self ):
  71. """ Sort my subchannels and players, and then iterate over them and sort them recursively. """
  72. self.subchans.sort( cmp_names );
  73. self.players.sort( cmp_names );
  74. for subc in self.subchans:
  75. subc.sort();
  76. def visit( self, callback, lvl = 0 ):
  77. """ Call callback on myself, then visit my subchans, then my players. """
  78. callback( self, lvl );
  79. for subc in self.subchans:
  80. subc.visit( callback, lvl + 1 );
  81. for plr in self.players:
  82. plr.visit( callback, lvl + 1 );
  83. def getURL( self, for_user = None ):
  84. """ Create an URL to connect to this channel. The URL is of the form
  85. mumble://username@host:port/parentchans/self.name
  86. """
  87. from urlparse import urlunsplit
  88. versionstr = "version=%d.%d.%d" % tuple(self.server.version[:3]);
  89. if self.parent is not None:
  90. chanlist = self.parent_channels() + [self.name];
  91. chanlist = [ urlquote( chan ) for chan in chanlist ];
  92. urlpath = "/".join( chanlist );
  93. else:
  94. urlpath = "";
  95. if for_user is not None:
  96. netloc = "%s@%s" % ( for_user.name, self.server.netloc );
  97. return urlunsplit(( "mumble", netloc, urlpath, versionstr, "" ))
  98. else:
  99. return urlunsplit(( "mumble", self.server.netloc, urlpath, versionstr, "" ))
  100. connecturl = property( getURL, doc="A convenience wrapper for getURL." );
  101. def setDefault( self ):
  102. """ Make this the server's default channel. """
  103. self.server.defchan = self.chanid;
  104. self.server.save();
  105. is_default = property(
  106. lambda self: self.server.defchan == self.chanid,
  107. doc="True if this channel is the server's default channel."
  108. );
  109. def asDict( self ):
  110. chandata = self.channel_obj.__dict__.copy();
  111. chandata['players'] = [ pl.asDict() for pl in self.players ];
  112. chandata['subchans'] = [ sc.asDict() for sc in self.subchans ];
  113. return chandata;
  114. class mmPlayer( object ):
  115. """ Represents a Player in Murmur. """
  116. def __init__( self, server, player_obj, player_chan ):
  117. self.player_obj = player_obj;
  118. self.onlinesince = datetime.datetime.fromtimestamp( float( time() - player_obj.onlinesecs ) );
  119. self.channel = player_chan;
  120. self.channel.players.append( self );
  121. if self.isAuthed:
  122. from mumble.models import MumbleUser
  123. try:
  124. self.mumbleuser = MumbleUser.objects.get( mumbleid=self.userid, server=server );
  125. except MumbleUser.DoesNotExist:
  126. self.mumbleuser = None;
  127. else:
  128. self.mumbleuser = None;
  129. # Lookup unknown attributes in self.player_obj to automatically include Murmur's fields
  130. def __getattr__( self, key ):
  131. if hasattr( self.player_obj, key ):
  132. return getattr( self.player_obj, key );
  133. else:
  134. raise AttributeError( "'%s' object has no attribute '%s'" % ( self.__class__.__name__, key ) );
  135. def __str__( self ):
  136. return '<Player "%s" (%d, %d)>' % ( self.name, self.session, self.userid );
  137. hasComment = property(
  138. lambda self: hasattr( self.player_obj, "comment" ) and bool(self.player_obj.comment),
  139. doc="True if this player has a comment set."
  140. );
  141. isAuthed = property(
  142. lambda self: self.userid != -1,
  143. doc="True if this player is authenticated (+A)."
  144. );
  145. isAdmin = property(
  146. lambda self: self.mumbleuser and self.mumbleuser.getAdmin(),
  147. doc="True if this player is in the Admin group in the ACL."
  148. );
  149. is_server = False;
  150. is_channel = False;
  151. is_player = True;
  152. # kept for compatibility to mmChannel (useful for traversal funcs)
  153. playerCount = property( lambda self: -1, doc="Exists only for compatibility to mmChannel." );
  154. id = property(
  155. lambda self: "player_%d"%self.session,
  156. doc="A string ready to be used in an id property of an HTML tag."
  157. );
  158. def visit( self, callback, lvl = 0 ):
  159. """ Call callback on myself. """
  160. callback( self, lvl );
  161. def asDict( self ):
  162. pldata = self.player_obj.__dict__.copy();
  163. if self.mumbleuser:
  164. if self.mumbleuser.hasTexture():
  165. pldata['texture'] = self.mumbleuser.textureUrl;
  166. return pldata;
  167. class mmACL( object ):
  168. """ Represents an ACL for a certain channel. """
  169. def __init__( self, channel, acl_obj ):
  170. self.channel = channel;
  171. self.acls, self.groups, self.inherit = acl_obj;
  172. self.groups_dict = {};
  173. for group in self.groups:
  174. self.groups_dict[ group.name ] = group;
  175. def group_has_member( self, name, userid ):
  176. """ Checks if the given userid is a member of the given group in this channel. """
  177. if name not in self.groups_dict:
  178. raise ReferenceError( "No such group '%s'" % name );
  179. return userid in self.groups_dict[name].add or userid in self.groups_dict[name].members;
  180. def group_add_member( self, name, userid ):
  181. """ Make sure this userid is a member of the group in this channel (and subs). """
  182. if name not in self.groups_dict:
  183. raise ReferenceError( "No such group '%s'" % name );
  184. group = self.groups_dict[name];
  185. # if neither inherited nor to be added, add
  186. if userid not in group.members and userid not in group.add:
  187. group.add.append( userid );
  188. # if to be removed, unremove
  189. if userid in group.remove:
  190. group.remove.remove( userid );
  191. def group_remove_member( self, name, userid ):
  192. """ Make sure this userid is NOT a member of the group in this channel (and subs). """
  193. if name not in self.groups_dict:
  194. raise ReferenceError( "No such group '%s'" % name );
  195. group = self.groups_dict[name];
  196. # if added here, unadd
  197. if userid in group.add:
  198. group.add.remove( userid );
  199. # if member and not in remove, add to remove
  200. elif userid in group.members and userid not in group.remove:
  201. group.remove.append( userid );
  202. def save( self ):
  203. """ Send this ACL to Murmur. """
  204. return self.channel.server.ctl.setACL(
  205. self.channel.server.srvid,
  206. self.channel.chanid,
  207. self.acls, self.groups, self.inherit
  208. );