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.

220 lines
8.0 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
  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. from django.contrib.auth.models import User
  16. from django.db import models
  17. from mmobjects import mmServer, mmACL
  18. from django.conf import settings
  19. from mctl import *
  20. import socket
  21. class Mumble( models.Model ):
  22. name = models.CharField( 'Server Name', max_length = 200 );
  23. dbus = models.CharField( 'DBus base', max_length = 200, default = 'net.sourceforge.mumble.murmur' );
  24. srvid = models.IntegerField( 'Server ID', editable = False );
  25. addr = models.CharField( 'Server Address', max_length = 200 );
  26. port = models.IntegerField( 'Server Port', blank = True, null = True );
  27. url = models.CharField( 'Website URL', max_length = 200, blank = True );
  28. motd = models.TextField( 'Welcome Message', blank = True );
  29. passwd = models.CharField( 'Server Password', max_length = 200, blank = True );
  30. supw = models.CharField( 'Superuser Password', max_length = 200, blank = True );
  31. users = models.IntegerField( 'Max. Users', blank = True, null = True );
  32. bwidth = models.IntegerField( 'Bandwidth [Bps]', blank = True, null = True );
  33. sslcrt = models.TextField( 'SSL Certificate', blank = True );
  34. sslkey = models.TextField( 'SSL Key', blank = True );
  35. obfsc = models.BooleanField( 'IP Obfuscation', default = False );
  36. player = models.CharField( 'Player name regex', max_length=200, default=r'[-=\w\[\]\{\}\(\)\@\|\.]+' );
  37. channel= models.CharField( 'Channel name regex', max_length=200, default=r'[ \-=\w\#\[\]\{\}\(\)\@\|]+' );
  38. defchan= models.IntegerField( 'Default channel', default=0 );
  39. booted = models.BooleanField( 'Boot Server', default = True );
  40. def getServerObject( self ):
  41. return mmServer( self.srvid, MumbleCtlBase.newInstance(), self.name );
  42. def __unicode__( self ):
  43. return u'Murmur "%s" (%d)' % ( self.name, self.srvid );
  44. def save( self, dontConfigureMurmur=False ):
  45. if dontConfigureMurmur:
  46. # skip murmur configuration, e.g. because we're inserting models for existing servers.
  47. return models.Model.save( self );
  48. # check if this server already exists, if not call newServer and set my srvid first
  49. ctl = MumbleCtlBase.newInstance();
  50. if self.id is None:
  51. self.srvid = ctl.newServer();
  52. ctl.setConf( self.srvid, 'host', socket.gethostbyname( self.addr ) );
  53. ctl.setConf( self.srvid, 'registername', self.name );
  54. ctl.setConf( self.srvid, 'registerurl', self.url );
  55. ctl.setConf( self.srvid, 'welcometext', self.motd );
  56. ctl.setConf( self.srvid, 'password', self.passwd );
  57. ctl.setConf( self.srvid, 'certificate', self.sslcrt );
  58. ctl.setConf( self.srvid, 'key', self.sslkey );
  59. ctl.setConf( self.srvid, 'obfuscate', str(self.obfsc).lower() );
  60. ctl.setConf( self.srvid, 'playername', self.player );
  61. ctl.setConf( self.srvid, 'channelname', self.channel );
  62. ctl.setConf( self.srvid, 'defaultchannel', str(self.defchan) );
  63. if self.port is not None:
  64. ctl.setConf( self.srvid, 'port', str(self.port) );
  65. else:
  66. ctl.setConf( self.srvid, 'port', '' );
  67. if self.users is not None:
  68. ctl.setConf( self.srvid, 'users', str(self.users) );
  69. else:
  70. ctl.setConf( self.srvid, 'users', '' );
  71. if self.bwidth is not None:
  72. ctl.setConf( self.srvid, 'bandwidth', str(self.bwidth) );
  73. else:
  74. ctl.setConf( self.srvid, 'bandwidth', '' );
  75. # registerHostname needs to take the port no into account
  76. if self.port and self.port != 64738:
  77. ctl.setConf( self.srvid, 'registerhostname', "%s:%d" % ( self.addr, self.port ) );
  78. else:
  79. ctl.setConf( self.srvid, 'registerhostname', self.addr );
  80. if self.supw:
  81. ctl.setSuperUserPassword( self.srvid, self.supw );
  82. self.supw = '';
  83. if self.booted != ctl.isBooted( self.srvid ):
  84. if self.booted:
  85. ctl.start( self.srvid );
  86. else:
  87. ctl.stop( self.srvid );
  88. # Now allow django to save the record set
  89. return models.Model.save( self );
  90. def isUserAdmin( self, user ):
  91. if user.is_authenticated():
  92. try:
  93. return self.mumbleuser_set.get( owner=user ).getAdmin();
  94. except MumbleUser.DoesNotExist:
  95. return False;
  96. return False;
  97. def deleteServer( self ):
  98. # Unregister this player in Murmur via ctroller.
  99. print MumbleCtlBase.newInstance()
  100. MumbleCtlBase.newInstance().deleteServer(self.srvid)
  101. @staticmethod
  102. def pre_delete_listener( **kwargs ):
  103. kwargs['instance'].deleteServer();
  104. class MumbleUser( models.Model ):
  105. mumbleid = models.IntegerField( 'Mumble player_id', editable = False, default = -1 );
  106. name = models.CharField( 'User name and Login', max_length = 200 );
  107. password = models.CharField( 'Login password', max_length = 200, blank=True );
  108. server = models.ForeignKey( Mumble );
  109. owner = models.ForeignKey( User, null=True, blank=True );
  110. isAdmin = models.BooleanField( 'Admin on root channel', default = False );
  111. def __unicode__( self ):
  112. return u"Mumble user %s on %s owned by Django user %s" % ( self.name, self.server, self.owner );
  113. def save( self, dontConfigureMurmur=False ):
  114. if dontConfigureMurmur:
  115. # skip murmur configuration, e.g. because we're inserting models for existing players.
  116. return models.Model.save( self );
  117. # Before the record set is saved, update Murmur via ctroller.
  118. ctl = MumbleCtlBase.newInstance();
  119. if self.id is None:
  120. # This is a new user record, so Murmur doesn't know about it yet
  121. self.mumbleid = ctl.registerPlayer(self.server.srvid, self.name);
  122. # Update user's registration
  123. if self.password:
  124. if self.owner:
  125. email = self.owner.email
  126. else:
  127. email = settings.DEFAULT_FROM_EMAIL;
  128. ctl.setRegistration(
  129. self.server.srvid,
  130. self.mumbleid,
  131. self.name,
  132. email,
  133. self.password
  134. );
  135. # Don't save the users' passwords, we don't need them anyway
  136. self.password = '';
  137. self.setAdmin( self.isAdmin );
  138. # Now allow django to save the record set
  139. return models.Model.save( self );
  140. def getAdmin( self ):
  141. # Get ACL of root Channel, get the admin group and see if I'm in it
  142. acl = mmACL( 0, MumbleCtlBase.newInstance().getACL(self.server.srvid, 0) );
  143. if not hasattr( acl, "admingroup" ):
  144. raise ValueError( "The admin group was not found in the ACL's groups list!" );
  145. return self.mumbleid in acl.admingroup['add'];
  146. def setAdmin( self, value ):
  147. # Get ACL of root Channel, get the admin group and see if I'm in it
  148. ctl = MumbleCtlBase.newInstance();
  149. acl = mmACL( 0, ctl.getACL(self.server.srvid, 0) );
  150. if not hasattr( acl, "admingroup" ):
  151. raise ValueError( "The admin group was not found in the ACL's groups list!" );
  152. if value != ( self.mumbleid in acl.admingroup['add'] ):
  153. if value:
  154. acl.admingroup['add'].append( self.mumbleid );
  155. else:
  156. acl.admingroup['add'].remove( self.mumbleid );
  157. ctl.setACL(self.server.srvid, acl);
  158. return value;
  159. @staticmethod
  160. def pre_delete_listener( **kwargs ):
  161. kwargs['instance'].unregister();
  162. def unregister( self ):
  163. # Unregister this player in Murmur via ctroller.
  164. MumbleCtlBase.newInstance().unregisterPlayer(self.server.srvid, self.mumbleid)
  165. from django.db.models import signals
  166. signals.pre_delete.connect( Mumble.pre_delete_listener, sender=Mumble );
  167. signals.pre_delete.connect( MumbleUser.pre_delete_listener, sender=MumbleUser );