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.

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