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.

200 lines
6.3 KiB

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