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.

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