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.

298 lines
8.6 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
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
  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. return cmp( a.name, b.name );
  23. class mmChannel( object ):
  24. """Represents a channel in Murmur."""
  25. # channels = list();
  26. # subchans = list();
  27. # chanid = int();
  28. # name = str();
  29. # parent = mmChannel();
  30. # linked = list();
  31. # linkedIDs = list();
  32. def __init__( self, server, channelObj, parentChan = None ):
  33. self.server = server;
  34. self.players = list();
  35. self.subchans = list();
  36. self.linked = list();
  37. self.chanid = channelObj.id;
  38. self.name = channelObj.name;
  39. parent = channelObj.parent;
  40. self.linkedIDs = channelObj.links;
  41. if hasattr( channelObj, "description" ):
  42. self.description = channelObj.description;
  43. else:
  44. self.description = "";
  45. if hasattr( channelObj, "temporary" ):
  46. self.temporary = channelObj.temporary;
  47. else:
  48. self.temporary = False;
  49. if hasattr( channelObj, "position" ):
  50. self.position = channelObj.position;
  51. else:
  52. # None would be better imho, but Murmur reports 0 for unknown too.
  53. self.position = 0;
  54. self.parent = parentChan;
  55. if self.parent is not None:
  56. self.parent.subchans.append( self );
  57. def parentChannels( self ):
  58. """Return the names of this channel's parents in the channel tree."""
  59. if self.parent is None or self.parent.is_server or self.parent.chanid == 0:
  60. return [];
  61. return self.parent.parentChannels() + [self.parent.name];
  62. is_server = False;
  63. is_channel = True;
  64. is_player = False;
  65. playerCount = property(
  66. lambda self: len( self.players ) + sum( [ chan.playerCount for chan in self.subchans ] ),
  67. doc="The number of players in this channel."
  68. );
  69. id = property(
  70. lambda self: "channel_%d"%self.chanid,
  71. doc="A string ready to be used in an id property of an HTML tag."
  72. );
  73. top_or_not_empty = property(
  74. lambda self: self.parent is None or self.parent.chanid == 0 or self.playerCount > 0,
  75. doc="True if this channel needs to be shown because it is root, a child of root, or has players."
  76. );
  77. show = property( lambda self: settings.SHOW_EMPTY_SUBCHANS or self.top_or_not_empty );
  78. def __str__( self ):
  79. return '<Channel "%s" (%d)>' % ( self.name, self.chanid );
  80. def sort( self ):
  81. """Sort my subchannels and players, and then iterate over them and sort them recursively."""
  82. self.subchans.sort( cmp_names );
  83. self.players.sort( cmp_names );
  84. for sc in self.subchans:
  85. sc.sort();
  86. def visit( self, callback, lvl = 0 ):
  87. """Call callback on myself, then visit my subchans, then my players."""
  88. callback( self, lvl );
  89. for sc in self.subchans:
  90. sc.visit( callback, lvl + 1 );
  91. for pl in self.players:
  92. pl.visit( callback, lvl + 1 );
  93. def getURL( self, forUser = None ):
  94. """
  95. Create an URL to connect to this channel. The URL is of the form
  96. mumble://username@host:port/parentchans/self.name
  97. """
  98. userstr = "";
  99. if forUser is not None:
  100. userstr = "%s@" % forUser.name;
  101. versionstr = "version=%d.%d.%d" % tuple(self.server.version[0:3]);
  102. # create list of all my parents and myself
  103. chanlist = self.parentChannels() + [self.name];
  104. # urlencode channel names
  105. chanlist = [ urlquote( chan ) for chan in chanlist ];
  106. # create a path by joining the channel names
  107. chanpath = join( *chanlist );
  108. if self.server.port != settings.MUMBLE_DEFAULT_PORT:
  109. return "mumble://%s%s:%d/%s?%s" % ( userstr, self.server.addr, self.server.port, chanpath, versionstr );
  110. return "mumble://%s%s/%s?%s" % ( userstr, self.server.addr, chanpath, versionstr );
  111. connecturl = property( getURL, doc="A convenience wrapper for getURL." );
  112. def setDefault( self ):
  113. "Make this the server's default channel."
  114. self.server.defchan = self.chanid;
  115. self.server.save();
  116. is_default = property(
  117. lambda self: self.server.defchan == self.chanid,
  118. doc="True if this channel is the server's default channel."
  119. );
  120. def as_dict( self ):
  121. if self.parent:
  122. parentid = self.parent.chanid;
  123. else:
  124. parentid = None;
  125. return { 'chanid': self.chanid,
  126. 'description': self.description,
  127. 'temporary': self.temporary,
  128. 'position': self.position,
  129. 'linked': [],
  130. 'linkedIDs': [],
  131. 'name': self.name,
  132. 'parent': parentid,
  133. 'players': [ pl.as_dict() for pl in self.players ],
  134. 'subchans': [ sc.as_dict() for sc in self.subchans ]
  135. };
  136. class mmPlayer( object ):
  137. """Represents a Player in Murmur."""
  138. # muted = bool;
  139. # deafened = bool;
  140. # suppressed = bool;
  141. # selfmuted = bool;
  142. # selfdeafened = bool;
  143. # channel = mmChannel();
  144. # dbaseid = int();
  145. # userid = int();
  146. # name = str();
  147. # onlinesince = time();
  148. # bytesPerSec = int();
  149. # mumbleuser = models.MumbleUser();
  150. def __init__( self, srvInstance, playerObj, playerChan ):
  151. ( self.userid, self.muted, self.deafened, self.suppressed, self.selfmuted, self.selfdeafened, chanID, self.dbaseid, self.name, onlinetime, self.bytesPerSec ) = playerObj;
  152. self.onlinesince = datetime.datetime.fromtimestamp( float( time() - onlinetime ) );
  153. self.channel = playerChan;
  154. self.channel.players.append( self );
  155. if self.isAuthed:
  156. from models import Mumble, MumbleUser
  157. try:
  158. self.mumbleuser = MumbleUser.objects.get( mumbleid=self.dbaseid, server=srvInstance );
  159. except MumbleUser.DoesNotExist:
  160. self.mumbleuser = None;
  161. else:
  162. self.mumbleuser = None;
  163. def __str__( self ):
  164. return '<Player "%s" (%d, %d)>' % ( self.name, self.userid, self.dbaseid );
  165. isAuthed = property(
  166. lambda self: self.dbaseid != -1,
  167. doc="True if this player is authenticated (+A)."
  168. );
  169. isAdmin = property(
  170. lambda self: self.mumbleuser and self.mumbleuser.getAdmin(),
  171. doc="True if this player is in the Admin group in the ACL."
  172. );
  173. is_server = False;
  174. is_channel = False;
  175. is_player = True;
  176. # kept for compatibility to mmChannel (useful for traversal funcs)
  177. playerCount = property( lambda self: -1, doc="Exists only for compatibility to mmChannel." );
  178. id = property(
  179. lambda self: "player_%d"%self.userid,
  180. doc="A string ready to be used in an id property of an HTML tag."
  181. );
  182. def visit( self, callback, lvl = 0 ):
  183. """ Call callback on myself. """
  184. callback( self, lvl );
  185. def as_dict( self ):
  186. comment = None;
  187. texture = None;
  188. if self.mumbleuser:
  189. comment = self.mumbleuser.comment;
  190. if self.mumbleuser.hasTexture():
  191. texture = self.mumbleuser.textureUrl;
  192. return { 'bytesPerSec': self.bytesPerSec,
  193. 'dbaseid': self.dbaseid,
  194. 'deafened': self.deafened,
  195. 'muted': self.muted,
  196. 'name': self.name,
  197. 'onlinesince': self.onlinesince,
  198. 'selfdeafened': self.selfdeafened,
  199. 'selfmuted': self.selfmuted,
  200. 'suppressed': self.suppressed,
  201. 'userid': self.userid,
  202. 'comment': comment,
  203. 'texture': texture,
  204. };
  205. class mmACL:
  206. """Represents an ACL for a certain channel."""
  207. def __init__( self, channelId, aclObj ):
  208. aclsrc, groupsrc, inherit = aclObj;
  209. self.channelId = channelId;
  210. self.acls = [];
  211. for line in aclsrc:
  212. acl = {};
  213. acl['applyHere'], acl['applySubs'], acl['inherited'], acl['playerid'], acl['group'], acl['allow'], acl['deny'] = line;
  214. self.acls.append( acl );
  215. self.groups = [];
  216. for line in groupsrc:
  217. group = {};
  218. group['name'], group['inherited'], group['inherit'], group['inheritable'], group['add'], group['remove'], group['members'] = line;
  219. self.groups.append( group );
  220. if group['name'] == "admin":
  221. self.admingroup = group;
  222. self.inherit = inherit;
  223. def pack( self ):
  224. """ Pack the information in this ACL up in a way that it can be passed to DBus. """
  225. return (
  226. self.channelId,
  227. [( acl['applyHere'], acl['applySubs'], acl['inherited'], acl['playerid'], acl['group'], acl['allow'], acl['deny'] ) for acl in self.acls ],
  228. [( group['name'], group['inherited'], group['inherit'], group['inheritable'], group['add'], group['remove'], group['members'] ) for group in self.groups ],
  229. self.inherit
  230. );