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.

287 lines
8.6 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
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
15 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
15 years ago
15 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. """ 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. hasComment = property(
  141. lambda self: hasattr( self.playerObj, "comment" ) and bool(self.playerObj.comment),
  142. doc="True if this player has a comment set."
  143. );
  144. isAuthed = property(
  145. lambda self: self.userid != -1,
  146. doc="True if this player is authenticated (+A)."
  147. );
  148. isAdmin = property(
  149. lambda self: self.mumbleuser and self.mumbleuser.getAdmin(),
  150. doc="True if this player is in the Admin group in the ACL."
  151. );
  152. is_server = False;
  153. is_channel = False;
  154. is_player = True;
  155. # kept for compatibility to mmChannel (useful for traversal funcs)
  156. playerCount = property( lambda self: -1, doc="Exists only for compatibility to mmChannel." );
  157. id = property(
  158. lambda self: "player_%d"%self.session,
  159. doc="A string ready to be used in an id property of an HTML tag."
  160. );
  161. def visit( self, callback, lvl = 0 ):
  162. """ Call callback on myself. """
  163. callback( self, lvl );
  164. def asDict( self ):
  165. pldata = self.playerObj.__dict__.copy();
  166. if self.mumbleuser:
  167. if self.mumbleuser.hasTexture():
  168. pldata['texture'] = self.mumbleuser.textureUrl;
  169. return pldata;
  170. class mmACL( object ):
  171. """ Represents an ACL for a certain channel. """
  172. def __init__( self, channel, aclObj ):
  173. self.channel = channel;
  174. self.acls, self.groups, self.inherit = aclObj;
  175. self.groups_dict = {};
  176. for group in self.groups:
  177. self.groups_dict[ group.name ] = group;
  178. def groupHasMember( self, name, userid ):
  179. """ Checks if the given userid is a member of the given group in this channel. """
  180. if name not in self.groups_dict:
  181. raise ReferenceError( "No such group '%s'" % name );
  182. return userid in self.groups_dict[name].add or userid in self.groups_dict[name].members;
  183. def groupAddMember( self, name, userid ):
  184. """ Make sure this userid is a member of the group in this channel (and subs). """
  185. if name not in self.groups_dict:
  186. raise ReferenceError( "No such group '%s'" % name );
  187. group = self.groups_dict[name];
  188. # if neither inherited nor to be added, add
  189. if userid not in group.members and userid not in group.add:
  190. group.add.append( userid );
  191. # if to be removed, unremove
  192. if userid in group.remove:
  193. group.remove.remove( userid );
  194. def groupRemoveMember( self, name, userid ):
  195. """ Make sure this userid is NOT a member of the group in this channel (and subs). """
  196. if name not in self.groups_dict:
  197. raise ReferenceError( "No such group '%s'" % name );
  198. group = self.groups_dict[name];
  199. # if added here, unadd
  200. if userid in group.add:
  201. group.add.remove( userid );
  202. # if member and not in remove, add to remove
  203. elif userid in group.members and userid not in group.remove:
  204. group.remove.append( userid );
  205. def save( self ):
  206. """ Send this ACL to Murmur. """
  207. return self.channel.server.ctl.setACL(
  208. self.channel.server.srvid,
  209. self.channel.chanid,
  210. self.acls, self.groups, self.inherit
  211. );