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.

222 lines
7.1 KiB

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 re
  17. from django import forms
  18. from django.conf import settings
  19. from django.forms import Form, ModelForm
  20. from django.utils.translation import ugettext_lazy as _
  21. from mumble.models import Mumble, MumbleUser
  22. class MumbleAdminForm( ModelForm ):
  23. """ A Mumble Server admin form intended to be used by the server hoster. """
  24. class Meta:
  25. model = Mumble;
  26. def clean_port( self ):
  27. """ If portno == -1 autoassign, and check if the port number is valid. """
  28. port = self.cleaned_data['port'];
  29. if port == -1:
  30. port = max( [ rec['port'] for rec in Mumble.objects.values('port') ] ) + 1;
  31. if port < 1 or port >= 2**16:
  32. raise forms.ValidationError(
  33. _("Port number %(portno)d is not within the allowed range %(minrange)d - %(maxrange)d") % {
  34. 'portno': port,
  35. 'minrange': 1,
  36. 'maxrange': 2**16,
  37. });
  38. return port;
  39. def clean( self ):
  40. """ Try to bind to the addr and port to verify that they are available. """
  41. if self.instance.id is not None or 'addr' not in self.cleaned_data or 'port' not in self.cleaned_data:
  42. # Editing old instance or previous validation failed already, don't try to bind
  43. return self.cleaned_data;
  44. addr = socket.gethostbyname( self.cleaned_data['addr'] );
  45. port = self.cleaned_data['port'];
  46. try:
  47. socktcp = socket.socket( socket.AF_INET, socket.SOCK_STREAM );
  48. socktcp.bind( ( addr, port ) );
  49. except socket.error, err:
  50. raise forms.ValidationError( err.args[1] );
  51. finally:
  52. socktcp.close();
  53. try:
  54. sockudp = socket.socket( socket.AF_INET, socket.SOCK_DGRAM );
  55. sockudp.bind( ( addr, port ) );
  56. except socket.error, err:
  57. raise forms.ValidationError( err.args[1] );
  58. finally:
  59. sockudp.close();
  60. return self.cleaned_data;
  61. class MumbleForm( ModelForm ):
  62. """
  63. The Mumble Server admin form that allows to configure settings which do not necessarily
  64. have to be reserved to the server hoster.
  65. Server hosters are expected to use the Django admin application instead, where everything
  66. can be configured freely.
  67. """
  68. class Meta:
  69. model = Mumble;
  70. exclude = ( 'dbus', 'booted', 'addr', 'port', 'users', 'bwidth', 'sslcrt', 'sslkey', );
  71. class MumbleUserForm( ModelForm ):
  72. """ The user registration form used to register an account. """
  73. def __init__( self, *args, **kwargs ):
  74. ModelForm.__init__( self, *args, **kwargs );
  75. self.server = None;
  76. def clean_name( self ):
  77. """ Check if the desired name is forbidden or taken. """
  78. name = self.cleaned_data['name'];
  79. if self.server is None:
  80. raise AttributeError( "You need to set the form's server attribute to the server instance "
  81. "for validation to work." );
  82. if self.server.player and re.compile( self.server.player ).match( name ) is None:
  83. raise forms.ValidationError( _( "That name is forbidden by the server." ) );
  84. if not self.instance.id and len( self.server.ctl.getRegisteredPlayers( self.server.srvid, name ) ) > 0:
  85. raise forms.ValidationError( _( "Another player already registered that name." ) );
  86. return name;
  87. def clean_password( self ):
  88. """ Verify a password has been given. """
  89. passwd = self.cleaned_data['password'];
  90. if not passwd:
  91. raise forms.ValidationError( _( "Cannot register player without a password!" ) );
  92. return passwd;
  93. class Meta:
  94. model = MumbleUser;
  95. fields = ( 'name', 'password' );
  96. class MumbleUserPasswordForm( MumbleUserForm ):
  97. """ The user registration form used to register an account on a private server in protected mode. """
  98. serverpw = forms.CharField(
  99. label=_('Server Password'),
  100. help_text=_('This server is private and protected mode is active. Please enter the server password.'),
  101. widget=forms.PasswordInput(render_value=False)
  102. );
  103. def clean_serverpw( self ):
  104. """ Validate the password """
  105. serverpw = self.cleaned_data['serverpw'];
  106. if self.server.passwd != serverpw:
  107. raise forms.ValidationError( _( "The password you entered is incorrect." ) );
  108. return serverpw;
  109. def clean( self ):
  110. """ prevent save() from trying to store the password in the Model instance. """
  111. # clean() will be called after clean_serverpw(), so it has already been validated here.
  112. if 'serverpw' in self.cleaned_data:
  113. del( self.cleaned_data['serverpw'] );
  114. return self.cleaned_data;
  115. class MumbleUserLinkForm( MumbleUserForm ):
  116. """ Special registration form to either register or link an account. """
  117. linkacc = forms.BooleanField(
  118. label=_('Link account'),
  119. help_text=_('The account already exists and belongs to me, just link it instead of creating.'),
  120. required=False,
  121. );
  122. def __init__( self, *args, **kwargs ):
  123. MumbleUserForm.__init__( self, *args, **kwargs );
  124. self.mumbleid = None;
  125. def clean_name( self ):
  126. """ Check if the target account exists in Murmur. """
  127. if 'linkacc' not in self.data:
  128. return MumbleUserForm.clean_name( self );
  129. # Check if user exists
  130. name = self.cleaned_data['name'];
  131. if len( self.server.ctl.getRegisteredPlayers( self.server.srvid, name ) ) != 1:
  132. raise forms.ValidationError( _( "No such user found." ) );
  133. return name;
  134. def clean_password( self ):
  135. """ Verify that the password is correct. """
  136. if 'linkacc' not in self.data:
  137. return MumbleUserForm.clean_password( self );
  138. if 'name' not in self.cleaned_data:
  139. # keep clean() from trying to find a user that CAN'T exist
  140. self.mumbleid = -10;
  141. return '';
  142. # Validate password with Murmur
  143. passwd = self.cleaned_data['password'];
  144. self.mumbleid = self.server.ctl.verifyPassword( self.server.srvid, self.cleaned_data['name'], passwd )
  145. if self.mumbleid <= 0:
  146. raise forms.ValidationError( _( "The password you entered is incorrect." ) );
  147. return passwd;
  148. def clean( self ):
  149. """ Create the MumbleUser instance to save in. """
  150. if 'linkacc' not in self.data or self.mumbleid <= 0:
  151. return self.cleaned_data;
  152. try:
  153. m_user = MumbleUser.objects.get( server=self.server, mumbleid=self.mumbleid );
  154. except MumbleUser.DoesNotExist:
  155. m_user = MumbleUser( server=self.server, name=self.cleaned_data['name'], mumbleid=self.mumbleid );
  156. m_user.isAdmin = m_user.getAdmin();
  157. m_user.save( dontConfigureMurmur=True );
  158. else:
  159. if m_user.owner is not None:
  160. raise forms.ValidationError( _( "That account belongs to someone else." ) );
  161. if m_user.getAdmin() and not settings.ALLOW_ACCOUNT_LINKING_ADMINS:
  162. raise forms.ValidationError( _( "Linking Admin accounts is not allowed." ) );
  163. self.instance = m_user;
  164. return self.cleaned_data;
  165. class MumbleTextureForm( Form ):
  166. """ The form used to upload a new image to be set as texture. """
  167. texturefile = forms.ImageField();