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.

340 lines
11 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
  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 socket
  16. from PIL import Image
  17. from struct import pack, unpack
  18. from zlib import compress, decompress
  19. from django.contrib.auth.models import User
  20. from django.db import models
  21. from django.conf import settings
  22. from mmobjects import *
  23. from mctl import *
  24. class Mumble( models.Model ):
  25. name = models.CharField( 'Server Name', max_length = 200 );
  26. dbus = models.CharField( 'DBus base', max_length = 200, default = 'net.sourceforge.mumble.murmur' );
  27. srvid = models.IntegerField( 'Server ID', editable = False );
  28. addr = models.CharField( 'Server Address', max_length = 200 );
  29. port = models.IntegerField( 'Server Port', blank = True, null = True );
  30. url = models.CharField( 'Website URL', max_length = 200, blank = True );
  31. motd = models.TextField( 'Welcome Message', blank = True );
  32. passwd = models.CharField( 'Server Password', max_length = 200, blank = True );
  33. supw = models.CharField( 'Superuser Password', max_length = 200, blank = True );
  34. users = models.IntegerField( 'Max. Users', blank = True, null = True );
  35. bwidth = models.IntegerField( 'Bandwidth [Bps]', blank = True, null = True );
  36. sslcrt = models.TextField( 'SSL Certificate', blank = True );
  37. sslkey = models.TextField( 'SSL Key', blank = True );
  38. obfsc = models.BooleanField( 'IP Obfuscation', default = False );
  39. player = models.CharField( 'Player name regex', max_length=200, default=r'[-=\w\[\]\{\}\(\)\@\|\.]+' );
  40. channel= models.CharField( 'Channel name regex', max_length=200, default=r'[ \-=\w\#\[\]\{\}\(\)\@\|]+' );
  41. defchan= models.IntegerField( 'Default channel', default=0 );
  42. booted = models.BooleanField( 'Boot Server', default = True );
  43. def __init__( self, *args, **kwargs ):
  44. models.Model.__init__( self, *args, **kwargs );
  45. self._ctl = None;
  46. self._channels = None;
  47. self._rootchan = None;
  48. def __unicode__( self ):
  49. return u'Murmur "%s" (%d)' % ( self.name, self.srvid );
  50. is_server = True;
  51. is_channel = False;
  52. is_player = False;
  53. # Ctl instantiation
  54. def getCtl( self ):
  55. if not self._ctl:
  56. self._ctl = MumbleCtlBase.newInstance( self.dbus );
  57. return self._ctl;
  58. ctl = property( getCtl, None );
  59. def save( self, dontConfigureMurmur=False ):
  60. if dontConfigureMurmur:
  61. # skip murmur configuration, e.g. because we're inserting models for existing servers.
  62. return models.Model.save( self );
  63. # check if this server already exists, if not call newServer and set my srvid first
  64. if self.id is None:
  65. self.srvid = self.ctl.newServer();
  66. self.ctl.setConf( self.srvid, 'host', socket.gethostbyname( self.addr ) );
  67. self.ctl.setConf( self.srvid, 'registername', self.name );
  68. self.ctl.setConf( self.srvid, 'registerurl', self.url );
  69. self.ctl.setConf( self.srvid, 'welcometext', self.motd );
  70. self.ctl.setConf( self.srvid, 'password', self.passwd );
  71. self.ctl.setConf( self.srvid, 'certificate', self.sslcrt );
  72. self.ctl.setConf( self.srvid, 'key', self.sslkey );
  73. self.ctl.setConf( self.srvid, 'obfuscate', str(self.obfsc).lower() );
  74. self.ctl.setConf( self.srvid, 'playername', self.player );
  75. self.ctl.setConf( self.srvid, 'channelname', self.channel );
  76. self.ctl.setConf( self.srvid, 'defaultchannel', str(self.defchan) );
  77. if self.port is not None:
  78. self.ctl.setConf( self.srvid, 'port', str(self.port) );
  79. else:
  80. self.ctl.setConf( self.srvid, 'port', '' );
  81. if self.users is not None:
  82. self.ctl.setConf( self.srvid, 'users', str(self.users) );
  83. else:
  84. self.ctl.setConf( self.srvid, 'users', '' );
  85. if self.bwidth is not None:
  86. self.ctl.setConf( self.srvid, 'bandwidth', str(self.bwidth) );
  87. else:
  88. self.ctl.setConf( self.srvid, 'bandwidth', '' );
  89. # registerHostname needs to take the port no into account
  90. if self.port and self.port != 64738:
  91. self.ctl.setConf( self.srvid, 'registerhostname', "%s:%d" % ( self.addr, self.port ) );
  92. else:
  93. self.ctl.setConf( self.srvid, 'registerhostname', self.addr );
  94. if self.supw:
  95. self.ctl.setSuperUserPassword( self.srvid, self.supw );
  96. self.supw = '';
  97. if self.booted != self.ctl.isBooted( self.srvid ):
  98. if self.booted:
  99. self.ctl.start( self.srvid );
  100. else:
  101. self.ctl.stop( self.srvid );
  102. # Now allow django to save the record set
  103. return models.Model.save( self );
  104. def isUserAdmin( self, user ):
  105. if user.is_authenticated():
  106. try:
  107. return self.mumbleuser_set.get( owner=user ).getAdmin();
  108. except MumbleUser.DoesNotExist:
  109. return False;
  110. return False;
  111. # Deletion handler
  112. def deleteServer( self ):
  113. # Unregister this player in Murmur via ctroller.
  114. self.ctl.deleteServer(self.srvid)
  115. @staticmethod
  116. def pre_delete_listener( **kwargs ):
  117. kwargs['instance'].deleteServer();
  118. # Channel lists: flat list
  119. def getChannels( self ):
  120. if self._channels is None:
  121. self._channels = {};
  122. chanlist = self.ctl.getChannels(self.srvid);
  123. links = {};
  124. # sometimes, ICE seems to return the Channel list in a weird order.
  125. # itercount prevents infinite loops.
  126. itercount = 0;
  127. maxiter = len(chanlist) * 3;
  128. while len(chanlist) and itercount < maxiter:
  129. itercount += 1;
  130. for theChan in chanlist:
  131. # Channels - Fields: 0 = ID, 1 = Name, 2 = Parent-ID, 3 = Links
  132. if( theChan[2] == -1 ):
  133. # No parent
  134. self._channels[theChan[0]] = mmChannel( self, theChan );
  135. elif theChan[2] in self.channels:
  136. # parent already known
  137. self._channels[theChan[0]] = mmChannel( self, theChan, self.channels[theChan[2]] );
  138. else:
  139. continue;
  140. chanlist.remove( theChan );
  141. self._channels[theChan[0]].serverId = self.id;
  142. # process links - if the linked channels are known, link; else save their ids to link later
  143. for linked in theChan[3]:
  144. if linked in self._channels:
  145. self._channels[theChan[0]].linked.append( self._channels[linked] );
  146. else:
  147. if linked not in links:
  148. links[linked] = list();
  149. links[linked].append( self._channels[theChan[0]] );
  150. # check if earlier round trips saved channel ids to be linked to the current channel
  151. if theChan[0] in links:
  152. for targetChan in links[theChan[0]]:
  153. targetChan.linked.append( self._channels[theChan[0]] );
  154. self._channels[0].name = self.name;
  155. self.players = {};
  156. for thePlayer in self.ctl.getPlayers(self.srvid):
  157. # Players - Fields: 0 = UserID, 6 = ChannelID
  158. self.players[ thePlayer[0] ] = mmPlayer( self, thePlayer, self._channels[ thePlayer[6] ] );
  159. return self._channels;
  160. channels = property( getChannels, None );
  161. rootchan = property( lambda self: self.channels[0], None );
  162. def getURL( self, forUser = None ):
  163. # mumble://username@host:port/
  164. userstr = "";
  165. if forUser is not None:
  166. userstr = "%s@" % forUser.name;
  167. return "mumble://%s%s:%d/" % ( userstr, self.addr, self.port );
  168. url = property( getURL, None );
  169. class MumbleUser( models.Model ):
  170. mumbleid = models.IntegerField( 'Mumble player_id', editable = False, default = -1 );
  171. name = models.CharField( 'User name and Login', max_length = 200 );
  172. password = models.CharField( 'Login password', max_length = 200, blank=True );
  173. server = models.ForeignKey( Mumble );
  174. owner = models.ForeignKey( User, null=True, blank=True );
  175. isAdmin = models.BooleanField( 'Admin on root channel', default = False );
  176. is_server = False;
  177. is_channel = False;
  178. is_player = True;
  179. def __unicode__( self ):
  180. return u"Mumble user %s on %s owned by Django user %s" % ( self.name, self.server, self.owner );
  181. def save( self, dontConfigureMurmur=False ):
  182. if dontConfigureMurmur:
  183. # skip murmur configuration, e.g. because we're inserting models for existing players.
  184. return models.Model.save( self );
  185. # Before the record set is saved, update Murmur via controller.
  186. ctl = self.server.ctl;
  187. if self.id is None:
  188. # This is a new user record, so Murmur doesn't know about it yet
  189. self.mumbleid = ctl.registerPlayer(self.server.srvid, self.name);
  190. # Update user's registration
  191. if self.password:
  192. if self.owner:
  193. email = self.owner.email
  194. else:
  195. email = settings.DEFAULT_FROM_EMAIL;
  196. ctl.setRegistration(
  197. self.server.srvid,
  198. self.mumbleid,
  199. self.name,
  200. email,
  201. self.password
  202. );
  203. # Don't save the users' passwords, we don't need them anyway
  204. self.password = '';
  205. self.setAdmin( self.isAdmin );
  206. # Now allow django to save the record set
  207. return models.Model.save( self );
  208. # Admin handlers
  209. def getAdmin( self ):
  210. # Get ACL of root Channel, get the admin group and see if I'm in it
  211. acl = mmACL( 0, self.server.ctl.getACL(self.server.srvid, 0) );
  212. if not hasattr( acl, "admingroup" ):
  213. raise ValueError( "The admin group was not found in the ACL's groups list!" );
  214. return self.mumbleid in acl.admingroup['add'];
  215. def setAdmin( self, value ):
  216. # Get ACL of root Channel, get the admin group and see if I'm in it
  217. ctl = self.server.ctl;
  218. acl = mmACL( 0, ctl.getACL(self.server.srvid, 0) );
  219. if not hasattr( acl, "admingroup" ):
  220. raise ValueError( "The admin group was not found in the ACL's groups list!" );
  221. if value != ( self.mumbleid in acl.admingroup['add'] ):
  222. if value:
  223. acl.admingroup['add'].append( self.mumbleid );
  224. else:
  225. acl.admingroup['add'].remove( self.mumbleid );
  226. ctl.setACL(self.server.srvid, acl);
  227. return value;
  228. # Texture handlers
  229. def getTexture( self ):
  230. return self.server.ctl.getTexture(self.server.srvid, self.mumbleid);
  231. def setTexture( self, infile ):
  232. self.server.ctl.setTexture(self.server.srvid, self.mumbleid, infile)
  233. # Deletion handler
  234. @staticmethod
  235. def pre_delete_listener( **kwargs ):
  236. kwargs['instance'].unregister();
  237. def unregister( self ):
  238. # Unregister this player in Murmur via dbus.
  239. self.server.ctl.unregisterPlayer(self.server.srvid, self.mumbleid)
  240. # "server" field protection
  241. def __setattr__( self, name, value ):
  242. if name == 'server':
  243. if self.id is not None and self.server != value:
  244. raise AttributeError( "This field must not be updated once the Record has been saved." );
  245. models.Model.__setattr__( self, name, value );
  246. from django.db.models import signals
  247. signals.pre_delete.connect( Mumble.pre_delete_listener, sender=Mumble );
  248. signals.pre_delete.connect( MumbleUser.pre_delete_listener, sender=MumbleUser );