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.

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