From a4b33e07c8c29c1e772c339f3088898b317f00fe Mon Sep 17 00:00:00 2001 From: Sunil Mohan Adapa Date: Mon, 30 Mar 2015 00:04:51 +0530 Subject: [PATCH] xmpp: List all configured vhosts during user registration Try to make the terminology a bit more consistent by renaming server -> vhost and server -> domainname according to the situation. --- actions/xmpp | 51 +++++++++---------- .../modules/xmpp/templates/xmpp_register.html | 7 +-- plinth/modules/xmpp/xmpp.py | 39 ++++++++------ 3 files changed, 48 insertions(+), 49 deletions(-) diff --git a/actions/xmpp b/actions/xmpp index b55961f10..9cf6709c7 100755 --- a/actions/xmpp +++ b/actions/xmpp @@ -42,16 +42,15 @@ def parse_arguments(): # Prepare ejabberd for hostname change pre_hostname_change = subparsers.add_parser( 'pre-change-hostname', - help='Prepare ejabberd for hostname change') + help='Prepare ejabberd for nodename change') pre_hostname_change.add_argument('--old-hostname', help='Previous hostname') pre_hostname_change.add_argument('--new-hostname', help='New hostname') - # Update ejabberd and jwchat with new hostname - hostname_change = subparsers.add_parser( - 'change-hostname', - help='Update ejabberd and jwchat with new hostname') + # Update ejabberd nodename + hostname_change = subparsers.add_parser('change-hostname', + help='Update ejabberd nodename') hostname_change.add_argument('--old-hostname', help='Previous hostname') hostname_change.add_argument('--new-hostname', @@ -63,16 +62,18 @@ def parse_arguments(): help='Update ejabberd and jwchat with new domainname') domainname_change.add_argument('--domainname', help='New domainname') - # Get the current server name used by jwchat - subparsers.add_parser('get-server', help='Get server name') + # Get the list of all virtual hosts configured in ejabberd + subparsers.add_parser( + 'get-vhosts', + help='Get the list of all virtual hosts configured in ejabberd') # Register a new user account register = subparsers.add_parser('register', help='Register a new user account') register.add_argument('--username', help='Username for the new user account') - register.add_argument('--server', - help='Virtual host for the new user account') + register.add_argument('--vhost', + help='Virtual host to register the new user in') register.add_argument('--password', help='Password for the new user account') @@ -123,10 +124,10 @@ def subcommand_change_hostname(arguments): def subcommand_change_domainname(arguments): """Update ejabberd and jwchat with new domainname""" - server = arguments.domainname.strip("'") - if not server: + domainname = arguments.domainname.strip("'") + if not domainname: # If new domainname is blank, use hostname instead. - server = socket.gethostname() + domainname = socket.gethostname() # update jwchat's sitename, if it's installed if os.path.exists(JWCHAT_CONFIG): @@ -135,7 +136,7 @@ def subcommand_change_domainname(arguments): with open(JWCHAT_CONFIG, 'w') as conffile: for line in lines: if re.match(r'\s*var\s+SITENAME', line): - conffile.write('var SITENAME = "' + server + '";\n') + conffile.write('var SITENAME = "' + domainname + '";\n') else: conffile.write(line) else: @@ -144,8 +145,8 @@ def subcommand_change_domainname(arguments): # Check if new domainname is already in ejabberd hosts list. conffile = open(EJABBERD_CONFIG, 'r') - conf = yaml.load(conffile) - if server in conf['hosts']: + conf = yaml.safe_load(conffile) + if domainname in conf['hosts']: return subprocess.call(['service', 'ejabberd', 'stop']) @@ -158,31 +159,25 @@ def subcommand_change_domainname(arguments): for line in lines: conffile.write(line) if re.match(r'\s*hosts:', line): - conffile.write(' - "' + server + '"\n') + conffile.write(' - "' + domainname + '"\n') subprocess.call(['service', 'ejabberd', 'start']) -def subcommand_get_server(_): - """Get the current server name used by jwchat""" - if os.path.exists(JWCHAT_CONFIG): - with open(JWCHAT_CONFIG, 'r') as conffile: - lines = conffile.readlines() - for line in lines: - if re.match(r'\s*var\s+SITENAME', line): - print(line.split('"')[1]) - return +def subcommand_get_vhosts(_): + """Get list of all virtual hosts configured in ejabbered.""" + subprocess.call(['ejabberdctl', 'registered_vhosts']) def subcommand_register(arguments): - """Register a new user account""" + """Register a new user account.""" username = arguments.username - server = arguments.server + vhost = arguments.vhost password = arguments.password try: output = subprocess.check_output(['ejabberdctl', 'register', - username, server, password]) + username, vhost, password]) print(output.decode()) except subprocess.CalledProcessError as e: print('Failed to register XMPP account:', e.output.decode()) diff --git a/plinth/modules/xmpp/templates/xmpp_register.html b/plinth/modules/xmpp/templates/xmpp_register.html index 0052fe3be..7bad2de5e 100644 --- a/plinth/modules/xmpp/templates/xmpp_register.html +++ b/plinth/modules/xmpp/templates/xmpp_register.html @@ -27,12 +27,7 @@
- - @{{ server }} - - {% include 'bootstrapform/field.html' with field=form.username %} - {% include 'bootstrapform/field.html' with field=form.password %} + {{ form|bootstrap }}
diff --git a/plinth/modules/xmpp/xmpp.py b/plinth/modules/xmpp/xmpp.py index d44226fb0..5699e82fc 100644 --- a/plinth/modules/xmpp/xmpp.py +++ b/plinth/modules/xmpp/xmpp.py @@ -134,50 +134,59 @@ def _apply_changes(request, old_status, new_status): class RegisterForm(forms.Form): # pylint: disable-msg=W0232 - """Configuration form""" + """Configuration form.""" username = forms.CharField(label=_('Username')) + vhost = forms.ChoiceField( + label=_('Host'), choices=(), + help_text=_('The new user will be able to login as: username@host')) password = forms.CharField( label=_('Password'), widget=forms.PasswordInput()) + def __init__(self, vhosts, *args, **kwargs): + """Set the list of possible values for hosts.""" + super(RegisterForm, self).__init__(*args, **kwargs) + + self.fields['vhost'].choices = ((vhost, '@' + vhost) + for vhost in vhosts) + @login_required def register(request): - """Serve the registration form""" + """Serve the registration form.""" form = None - server = actions.run('xmpp', ['get-server']).strip() + vhosts = actions.run('xmpp', ['get-vhosts']).split() if request.method == 'POST': - form = RegisterForm(request.POST, prefix='xmpp') + form = RegisterForm(vhosts, request.POST, prefix='xmpp') # pylint: disable-msg=E1101 if form.is_valid(): - _register_user(request, form.cleaned_data, server) - form = RegisterForm(prefix='xmpp') + _register_user(request, form.cleaned_data) + form = RegisterForm(vhosts, prefix='xmpp') else: - form = RegisterForm(prefix='xmpp') + form = RegisterForm(vhosts, prefix='xmpp') return TemplateResponse(request, 'xmpp_register.html', {'title': _('Register XMPP Account'), 'form': form, - 'subsubmenu': subsubmenu, - 'server': server}) + 'subsubmenu': subsubmenu}) -def _register_user(request, data, server): +def _register_user(request, data): """Register a new XMPP user""" output = actions.superuser_run( 'xmpp', ['register', '--username', data['username'], - '--server', server, + '--vhost', data['vhost'], '--password', data['password']]) if 'successfully registered' in output: - messages.success(request, _('Registered account for %s') % - data['username']) + messages.success(request, _('Registered account %s@%s') % + (data['username'], data['vhost'])) else: messages.error(request, - _('Failed to register account for %s: %s') % - (data['username'], output)) + _('Failed to register account %s@%s: %s') % + (data['username'], data['vhost'], output)) def on_pre_hostname_change(sender, old_hostname, new_hostname, **kwargs):