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.

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