2016-02-19 11:43:46 +01:00
|
|
|
// Converse.js (A browser based XMPP chat client)
|
|
|
|
// http://conversejs.org
|
|
|
|
//
|
2017-02-13 15:37:17 +01:00
|
|
|
// Copyright (c) 2012-2017, Jan-Carel Brand <jc@opkode.com>
|
2016-02-19 11:43:46 +01:00
|
|
|
// Licensed under the Mozilla Public License (MPLv2)
|
|
|
|
//
|
2017-04-21 15:52:16 +02:00
|
|
|
/*global define */
|
2016-02-19 11:43:46 +01:00
|
|
|
|
|
|
|
/* This is a Converse.js plugin which add support for in-band registration
|
|
|
|
* as specified in XEP-0077.
|
|
|
|
*/
|
|
|
|
(function (root, factory) {
|
2017-07-10 21:14:48 +02:00
|
|
|
define(["jquery.noconflict",
|
2017-08-16 15:19:41 +02:00
|
|
|
"form-utils",
|
2017-07-10 21:14:48 +02:00
|
|
|
"converse-core",
|
2016-09-23 10:54:55 +02:00
|
|
|
"tpl!form_username",
|
2017-09-16 18:11:31 +02:00
|
|
|
"tpl!register_link",
|
2016-09-23 10:54:55 +02:00
|
|
|
"tpl!register_panel",
|
|
|
|
"tpl!registration_form",
|
|
|
|
"tpl!registration_request",
|
2017-09-17 01:30:52 +02:00
|
|
|
"tpl!form_input",
|
2017-06-14 18:09:22 +02:00
|
|
|
"tpl!spinner",
|
2016-02-28 20:24:06 +01:00
|
|
|
"converse-controlbox"
|
|
|
|
], factory);
|
2016-09-23 10:54:55 +02:00
|
|
|
}(this, function (
|
2017-07-10 21:14:48 +02:00
|
|
|
$,
|
2017-08-16 15:19:41 +02:00
|
|
|
utils,
|
2016-12-20 11:42:20 +01:00
|
|
|
converse,
|
2016-09-23 10:54:55 +02:00
|
|
|
tpl_form_username,
|
2017-09-16 18:11:31 +02:00
|
|
|
tpl_register_link,
|
2016-09-23 10:54:55 +02:00
|
|
|
tpl_register_panel,
|
|
|
|
tpl_registration_form,
|
2017-06-14 18:09:22 +02:00
|
|
|
tpl_registration_request,
|
2017-09-17 01:30:52 +02:00
|
|
|
tpl_form_input,
|
2017-06-14 18:09:22 +02:00
|
|
|
tpl_spinner
|
|
|
|
) {
|
2016-09-23 10:54:55 +02:00
|
|
|
|
2016-02-19 11:43:46 +01:00
|
|
|
"use strict";
|
2016-09-23 10:54:55 +02:00
|
|
|
|
2016-02-19 11:43:46 +01:00
|
|
|
// Strophe methods for building stanzas
|
2017-08-16 15:19:41 +02:00
|
|
|
const { Strophe, Backbone, $iq, _ } = converse.env;
|
2017-03-31 03:54:42 +02:00
|
|
|
|
2016-02-19 11:43:46 +01:00
|
|
|
// Add Strophe Namespaces
|
|
|
|
Strophe.addNamespace('REGISTER', 'jabber:iq:register');
|
|
|
|
|
|
|
|
// Add Strophe Statuses
|
2017-07-10 17:46:22 +02:00
|
|
|
let i = 0;
|
2017-01-26 15:49:02 +01:00
|
|
|
_.each(_.keys(Strophe.Status), function (key) {
|
2016-02-19 11:43:46 +01:00
|
|
|
i = Math.max(i, Strophe.Status[key]);
|
|
|
|
});
|
|
|
|
Strophe.Status.REGIFAIL = i + 1;
|
|
|
|
Strophe.Status.REGISTERED = i + 2;
|
|
|
|
Strophe.Status.CONFLICT = i + 3;
|
|
|
|
Strophe.Status.NOTACCEPTABLE = i + 5;
|
|
|
|
|
2016-12-20 11:42:20 +01:00
|
|
|
converse.plugins.add('converse-register', {
|
2016-02-19 11:43:46 +01:00
|
|
|
|
2017-09-16 18:11:31 +02:00
|
|
|
'overrides': {
|
2016-02-19 11:43:46 +01:00
|
|
|
// Overrides mentioned here will be picked up by converse.js's
|
|
|
|
// plugin architecture they will replace existing methods on the
|
|
|
|
// relevant objects or classes.
|
|
|
|
//
|
|
|
|
// New functions which don't exist yet can also be added.
|
2017-09-16 18:11:31 +02:00
|
|
|
|
|
|
|
LoginPanel: {
|
|
|
|
|
|
|
|
initialize: function (cfg) {
|
2017-09-16 19:29:13 +02:00
|
|
|
const { _converse } = this.__super__;
|
2017-09-16 18:11:31 +02:00
|
|
|
this.__super__.initialize.apply(this, arguments);
|
2017-09-16 19:29:13 +02:00
|
|
|
if (_converse.allow_registration) {
|
|
|
|
const div = document.createElement('div');
|
|
|
|
div.innerHTML = tpl_register_link({'__': _converse.__})
|
|
|
|
this.el.appendChild(div);
|
|
|
|
}
|
2017-09-16 18:11:31 +02:00
|
|
|
}
|
|
|
|
},
|
2016-02-19 11:43:46 +01:00
|
|
|
|
|
|
|
ControlBoxView: {
|
|
|
|
|
2017-09-17 21:50:59 +02:00
|
|
|
events: {
|
|
|
|
'click .toggle-register-login': 'switchToRegisterForm',
|
|
|
|
},
|
|
|
|
|
2017-09-16 19:17:54 +02:00
|
|
|
initialize () {
|
|
|
|
this.__super__.initialize.apply(this, arguments);
|
|
|
|
this.model.on('change:active-form', this.showLoginOrRegisterForm.bind(this))
|
|
|
|
},
|
|
|
|
|
2017-09-17 21:50:59 +02:00
|
|
|
switchToRegisterForm (ev) {
|
|
|
|
ev.preventDefault();
|
|
|
|
if (this.model.get('active-form') == "register") {
|
|
|
|
this.model.set('active-form', 'login');
|
|
|
|
} else {
|
|
|
|
this.model.set('active-form', 'register');
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
showLoginOrRegisterForm () {
|
2017-09-16 19:17:54 +02:00
|
|
|
const { _converse } = this.__super__;
|
2017-09-17 21:50:59 +02:00
|
|
|
if (_.isNil(this.registerpanel)) {
|
|
|
|
return;
|
|
|
|
}
|
2017-09-16 19:17:54 +02:00
|
|
|
if (this.model.get('active-form') == "register") {
|
|
|
|
this.loginpanel.el.classList.add('hidden');
|
|
|
|
this.registerpanel.el.classList.remove('hidden');
|
|
|
|
} else {
|
|
|
|
this.loginpanel.el.classList.remove('hidden');
|
|
|
|
this.registerpanel.el.classList.add('hidden');
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
renderRegistrationPanel () {
|
|
|
|
const { _converse } = this.__super__;
|
2016-12-20 10:30:20 +01:00
|
|
|
if (_converse.allow_registration) {
|
|
|
|
this.registerpanel = new _converse.RegisterPanel({
|
2017-03-31 03:54:42 +02:00
|
|
|
'model': this.model
|
2016-04-08 10:29:30 +02:00
|
|
|
});
|
2017-09-16 18:11:31 +02:00
|
|
|
this.registerpanel.render();
|
|
|
|
this.registerpanel.el.classList.add('hidden');
|
|
|
|
this.el.querySelector('#converse-login-panel').insertAdjacentElement(
|
|
|
|
'afterend',
|
|
|
|
this.registerpanel.el
|
|
|
|
);
|
2017-09-16 19:17:54 +02:00
|
|
|
this.showLoginOrRegisterForm();
|
2016-02-19 11:43:46 +01:00
|
|
|
}
|
|
|
|
return this;
|
2017-06-13 19:11:09 +02:00
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
renderLoginPanel () {
|
2017-06-13 19:11:09 +02:00
|
|
|
/* Also render a registration panel, when rendering the
|
|
|
|
* login panel.
|
|
|
|
*/
|
|
|
|
this.__super__.renderLoginPanel.apply(this, arguments);
|
|
|
|
this.renderRegistrationPanel();
|
|
|
|
return this;
|
2016-02-19 11:43:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
initialize () {
|
2016-02-19 11:43:46 +01:00
|
|
|
/* The initialize function gets called as soon as the plugin is
|
|
|
|
* loaded by converse.js's plugin machinery.
|
|
|
|
*/
|
2017-07-10 17:46:22 +02:00
|
|
|
const { _converse } = this,
|
|
|
|
{ __ } = _converse;
|
2016-12-20 11:42:20 +01:00
|
|
|
|
2017-07-05 11:03:13 +02:00
|
|
|
_converse.api.settings.update({
|
2016-02-19 11:43:46 +01:00
|
|
|
allow_registration: true,
|
|
|
|
domain_placeholder: __(" e.g. conversejs.org"), // Placeholder text shown in the domain input on the registration form
|
|
|
|
providers_link: 'https://xmpp.net/directory.php', // Link to XMPP providers shown on registration page
|
2016-03-13 17:22:42 +01:00
|
|
|
});
|
2016-02-19 11:43:46 +01:00
|
|
|
|
2017-09-16 19:17:54 +02:00
|
|
|
_converse.RegistrationRouter = Backbone.Router.extend({
|
|
|
|
|
|
|
|
initialize () {
|
|
|
|
this.route('converse-login', _.partial(this.setActiveForm, 'login'));
|
|
|
|
this.route('converse-register', _.partial(this.setActiveForm, 'register'));
|
|
|
|
},
|
|
|
|
|
|
|
|
setActiveForm (value) {
|
|
|
|
_converse.api.waitUntil('controlboxInitialized').then(() => {
|
|
|
|
const controlbox = _converse.chatboxes.get('controlbox')
|
2017-09-17 21:50:59 +02:00
|
|
|
controlbox.set({'active-form': value});
|
2017-09-16 19:17:54 +02:00
|
|
|
}).catch(_.partial(_converse.log, _, Strophe.LogLevel.FATAL));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
const router = new _converse.RegistrationRouter();
|
|
|
|
|
|
|
|
|
2016-12-20 10:30:20 +01:00
|
|
|
_converse.RegisterPanel = Backbone.View.extend({
|
2016-02-19 11:43:46 +01:00
|
|
|
tagName: 'div',
|
2017-09-16 18:11:31 +02:00
|
|
|
id: "converse-register-panel",
|
2017-09-17 01:30:52 +02:00
|
|
|
className: 'controlbox-pane fade-in',
|
2016-02-19 11:43:46 +01:00
|
|
|
events: {
|
2017-09-17 21:15:00 +02:00
|
|
|
'submit form#converse-register': 'onProviderChosen',
|
2017-09-17 21:50:59 +02:00
|
|
|
'click .button-cancel': 'cancelRegistration',
|
2016-02-19 11:43:46 +01:00
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
initialize (cfg) {
|
2016-02-19 11:43:46 +01:00
|
|
|
this.reset();
|
|
|
|
this.registerHooks();
|
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
render () {
|
2017-03-31 03:54:42 +02:00
|
|
|
this.model.set('registration_form_rendered', false);
|
2017-09-16 18:11:31 +02:00
|
|
|
this.el.innerHTML = tpl_register_panel({
|
|
|
|
'__': __,
|
|
|
|
'default_domain': _converse.registration_domain,
|
|
|
|
'label_register': __('Fetch registration form'),
|
|
|
|
'help_providers': __('Tip: A list of public XMPP providers is available'),
|
|
|
|
'help_providers_link': __('here'),
|
|
|
|
'href_providers': _converse.providers_link,
|
|
|
|
'domain_placeholder': _converse.domain_placeholder
|
|
|
|
});
|
2017-09-17 21:15:00 +02:00
|
|
|
if (_converse.registration_domain) {
|
|
|
|
this.fetchRegistrationForm(
|
|
|
|
_converse.registration_domain
|
|
|
|
);
|
|
|
|
}
|
2016-02-19 11:43:46 +01:00
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
registerHooks () {
|
2016-02-19 11:43:46 +01:00
|
|
|
/* Hook into Strophe's _connect_cb, so that we can send an IQ
|
2016-03-19 23:16:00 +01:00
|
|
|
* requesting the registration fields.
|
|
|
|
*/
|
2017-07-10 17:46:22 +02:00
|
|
|
const conn = _converse.connection;
|
|
|
|
const connect_cb = conn._connect_cb.bind(conn);
|
|
|
|
conn._connect_cb = (req, callback, raw) => {
|
2016-02-19 11:43:46 +01:00
|
|
|
if (!this._registering) {
|
|
|
|
connect_cb(req, callback, raw);
|
|
|
|
} else {
|
|
|
|
if (this.getRegistrationFields(req, callback, raw)) {
|
|
|
|
this._registering = false;
|
|
|
|
}
|
|
|
|
}
|
2017-07-10 17:46:22 +02:00
|
|
|
};
|
2016-02-19 11:43:46 +01:00
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
getRegistrationFields (req, _callback, raw) {
|
2016-02-19 11:43:46 +01:00
|
|
|
/* Send an IQ stanza to the XMPP server asking for the
|
2016-03-19 23:16:00 +01:00
|
|
|
* registration fields.
|
|
|
|
* Parameters:
|
|
|
|
* (Strophe.Request) req - The current request
|
|
|
|
* (Function) callback
|
|
|
|
*/
|
2017-07-10 17:46:22 +02:00
|
|
|
const conn = _converse.connection;
|
2016-02-19 11:43:46 +01:00
|
|
|
conn.connected = true;
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
const body = conn._proto._reqToData(req);
|
2016-02-19 11:43:46 +01:00
|
|
|
if (!body) { return; }
|
|
|
|
if (conn._proto._connect_cb(body) === Strophe.Status.CONNFAIL) {
|
|
|
|
return false;
|
|
|
|
}
|
2017-07-10 17:46:22 +02:00
|
|
|
const register = body.getElementsByTagName("register");
|
|
|
|
const mechanisms = body.getElementsByTagName("mechanism");
|
2016-02-19 11:43:46 +01:00
|
|
|
if (register.length === 0 && mechanisms.length === 0) {
|
|
|
|
conn._proto._no_auth_received(_callback);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (register.length === 0) {
|
|
|
|
conn._changeConnectStatus(
|
|
|
|
Strophe.Status.REGIFAIL,
|
2017-03-31 03:54:42 +02:00
|
|
|
__("Sorry, the given provider does not support in "+
|
|
|
|
"band account registration. Please try with a "+
|
|
|
|
"different provider.")
|
2016-02-19 11:43:46 +01:00
|
|
|
);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// Send an IQ stanza to get all required data fields
|
|
|
|
conn._addSysHandler(this.onRegistrationFields.bind(this), null, "iq", null, null);
|
2017-09-17 01:30:52 +02:00
|
|
|
const stanza = $iq({type: "get"}).c("query", {xmlns: Strophe.NS.REGISTER}).tree();
|
|
|
|
stanza.setAttribute("id", conn.getUniqueId("sendIQ"));
|
|
|
|
conn.send(stanza);
|
2017-03-31 03:54:42 +02:00
|
|
|
conn.connected = false;
|
2016-02-19 11:43:46 +01:00
|
|
|
return true;
|
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
onRegistrationFields (stanza) {
|
2016-02-19 11:43:46 +01:00
|
|
|
/* Handler for Registration Fields Request.
|
2016-03-19 23:16:00 +01:00
|
|
|
*
|
|
|
|
* Parameters:
|
|
|
|
* (XMLElement) elem - The query stanza.
|
|
|
|
*/
|
2017-09-17 01:30:52 +02:00
|
|
|
if (stanza.getAttribute("type") === "error") {
|
|
|
|
_converse.connection._changeConnectStatus(
|
|
|
|
Strophe.Status.REGIFAIL,
|
|
|
|
__('Something went wrong while establishing a connection with "%1$s". Are you sure it exists?', this.domain)
|
|
|
|
);
|
|
|
|
return false;
|
|
|
|
}
|
2016-02-19 11:43:46 +01:00
|
|
|
if (stanza.getElementsByTagName("query").length !== 1) {
|
2016-12-20 10:30:20 +01:00
|
|
|
_converse.connection._changeConnectStatus(Strophe.Status.REGIFAIL, "unknown");
|
2016-02-19 11:43:46 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
this.setFields(stanza);
|
2017-09-17 21:15:00 +02:00
|
|
|
if (!this.model.get('registration_form_rendered')) {
|
|
|
|
this.renderRegistrationForm(stanza);
|
|
|
|
}
|
2016-02-19 11:43:46 +01:00
|
|
|
return false;
|
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
reset (settings) {
|
|
|
|
const defaults = {
|
2016-02-19 11:43:46 +01:00
|
|
|
fields: {},
|
|
|
|
urls: [],
|
|
|
|
title: "",
|
|
|
|
instructions: "",
|
|
|
|
registered: false,
|
|
|
|
_registering: false,
|
|
|
|
domain: null,
|
|
|
|
form_type: null
|
|
|
|
};
|
|
|
|
_.extend(this, defaults);
|
|
|
|
if (settings) {
|
2017-01-26 15:49:02 +01:00
|
|
|
_.extend(this, _.pick(settings, _.keys(defaults)));
|
2016-02-19 11:43:46 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
onProviderChosen (ev) {
|
2016-02-19 11:43:46 +01:00
|
|
|
/* Callback method that gets called when the user has chosen an
|
2016-03-19 23:16:00 +01:00
|
|
|
* XMPP provider.
|
|
|
|
*
|
|
|
|
* Parameters:
|
|
|
|
* (Submit Event) ev - Form submission event.
|
|
|
|
*/
|
2016-02-19 11:43:46 +01:00
|
|
|
if (ev && ev.preventDefault) { ev.preventDefault(); }
|
2017-07-10 17:46:22 +02:00
|
|
|
const $form = $(ev.target),
|
2016-02-19 11:43:46 +01:00
|
|
|
$domain_input = $form.find('input[name=domain]'),
|
|
|
|
domain = $domain_input.val();
|
|
|
|
if (!domain) {
|
|
|
|
$domain_input.addClass('error');
|
|
|
|
return;
|
|
|
|
}
|
2017-03-20 13:21:03 +01:00
|
|
|
$form.find('input[type=submit]').hide();
|
2017-09-17 21:15:00 +02:00
|
|
|
this.fetchRegistrationForm(domain);
|
2017-03-20 13:21:03 +01:00
|
|
|
},
|
|
|
|
|
2017-09-17 21:15:00 +02:00
|
|
|
fetchRegistrationForm (domain_name) {
|
2017-03-20 13:21:03 +01:00
|
|
|
/* This is called with a domain name based on which, it fetches a
|
|
|
|
* registration form from the requested domain.
|
|
|
|
*
|
|
|
|
* Parameters:
|
2017-09-17 21:15:00 +02:00
|
|
|
* (String) domain_name - XMPP server domain
|
2017-03-31 03:54:42 +02:00
|
|
|
*/
|
2017-09-17 21:15:00 +02:00
|
|
|
if (!this.model.get('registration_form_rendered')) {
|
|
|
|
this.renderRegistrationRequest();
|
|
|
|
}
|
2016-02-19 11:43:46 +01:00
|
|
|
this.reset({
|
2017-03-20 13:21:03 +01:00
|
|
|
domain: Strophe.getDomainFromJid(domain_name),
|
2016-02-19 11:43:46 +01:00
|
|
|
_registering: true
|
|
|
|
});
|
2016-12-20 10:30:20 +01:00
|
|
|
_converse.connection.connect(this.domain, "", this.onRegistering.bind(this));
|
2017-03-31 03:54:42 +02:00
|
|
|
return false;
|
2017-03-20 13:21:03 +01:00
|
|
|
},
|
|
|
|
|
2017-09-17 21:15:00 +02:00
|
|
|
renderRegistrationRequest () {
|
|
|
|
/* Clear the form and inform the user that the registration
|
|
|
|
* form is being fetched.
|
|
|
|
*/
|
|
|
|
this.clearRegistrationForm().insertAdjacentHTML(
|
|
|
|
'beforeend',
|
|
|
|
tpl_registration_request({
|
|
|
|
'__': _converse.__,
|
|
|
|
'cancel': _converse.registration_domain,
|
|
|
|
})
|
|
|
|
);
|
2016-02-19 11:43:46 +01:00
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
giveFeedback (message, klass) {
|
2017-09-17 21:15:00 +02:00
|
|
|
let feedback = this.el.querySelector('.reg-feedback');
|
|
|
|
if (_.isNull(feedback)) {
|
|
|
|
const form = this.el.querySelector('form');
|
|
|
|
form.insertAdjacentHTML(
|
|
|
|
'afterbegin',
|
|
|
|
'<span class="reg-feedback"></span>'
|
|
|
|
);
|
|
|
|
feedback = form.querySelector('.reg-feedback');
|
|
|
|
}
|
|
|
|
feedback.setAttribute('class', 'reg-feedback');
|
|
|
|
feedback.textContent = message;
|
2016-02-19 11:43:46 +01:00
|
|
|
if (klass) {
|
|
|
|
$('.reg-feedback').addClass(klass);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2017-09-17 21:15:00 +02:00
|
|
|
clearRegistrationForm () {
|
|
|
|
const form = this.el.querySelector('form');
|
|
|
|
form.innerHTML = '';
|
|
|
|
return form;
|
|
|
|
},
|
|
|
|
|
|
|
|
showRegistrationForm () {
|
|
|
|
const form = this.el.querySelector('form');
|
|
|
|
form.classList.remove('hidden');
|
|
|
|
return form;
|
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
onRegistering (status, error) {
|
2017-09-17 21:15:00 +02:00
|
|
|
/* Callback function called by Strophe */
|
2016-12-20 10:30:20 +01:00
|
|
|
_converse.log('onRegistering');
|
2017-01-26 15:49:02 +01:00
|
|
|
if (_.includes([
|
2016-02-19 11:43:46 +01:00
|
|
|
Strophe.Status.DISCONNECTED,
|
|
|
|
Strophe.Status.CONNFAIL,
|
|
|
|
Strophe.Status.REGIFAIL,
|
|
|
|
Strophe.Status.NOTACCEPTABLE,
|
|
|
|
Strophe.Status.CONFLICT
|
|
|
|
], status)) {
|
|
|
|
|
2017-07-05 11:33:55 +02:00
|
|
|
_converse.log(
|
2017-07-10 17:46:22 +02:00
|
|
|
`Problem during registration: Strophe.Status is: ${status}`,
|
2017-07-05 11:33:55 +02:00
|
|
|
Strophe.LogLevel.ERROR
|
|
|
|
);
|
2017-09-17 21:15:00 +02:00
|
|
|
this.cancelRegistration(error);
|
2016-02-19 11:43:46 +01:00
|
|
|
if (error) {
|
2017-09-17 21:15:00 +02:00
|
|
|
this.giveFeedback(__(
|
|
|
|
'Something went wrong while establishing a connection with "%1$s". The returned error message is "%2$s"',
|
|
|
|
this.domain, error
|
|
|
|
), 'error');
|
2016-02-19 11:43:46 +01:00
|
|
|
} else {
|
|
|
|
this.giveFeedback(__(
|
2016-12-19 08:46:48 +01:00
|
|
|
'Something went wrong while establishing a connection with "%1$s". Are you sure it exists?',
|
|
|
|
this.domain
|
|
|
|
), 'error');
|
2016-02-19 11:43:46 +01:00
|
|
|
}
|
|
|
|
} else if (status === Strophe.Status.REGISTERED) {
|
2017-09-17 21:15:00 +02:00
|
|
|
router.navigate(); // Strip the URL fragment
|
2016-12-20 10:30:20 +01:00
|
|
|
_converse.log("Registered successfully.");
|
2017-09-17 21:15:00 +02:00
|
|
|
this.model.set('registration_form_rendered', false);
|
2016-12-20 10:30:20 +01:00
|
|
|
_converse.connection.reset();
|
2017-09-17 21:15:00 +02:00
|
|
|
const form = this.el.querySelector('form');
|
|
|
|
form.innerHTML = tpl_spinner();
|
|
|
|
|
|
|
|
if (this.fields.password && this.fields.username) {
|
|
|
|
// automatically log the user in
|
|
|
|
_converse.connection.connect(
|
|
|
|
this.fields.username.toLowerCase()+'@'+this.domain.toLowerCase(),
|
|
|
|
this.fields.password,
|
|
|
|
_converse.onConnectStatusChanged
|
|
|
|
);
|
|
|
|
this.giveFeedback(__('Now logging you in'));
|
|
|
|
} else {
|
|
|
|
_converse.chatboxviews.get('controlbox').renderLoginPanel();
|
|
|
|
_converse.giveFeedback(__('Registered successfully'));
|
|
|
|
}
|
|
|
|
this.reset();
|
2016-02-19 11:43:46 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2017-09-17 01:30:52 +02:00
|
|
|
renderLegacyRegistrationForm (form) {
|
|
|
|
_.each(_.keys(this.fields), (key) => {
|
|
|
|
if (key === "username") {
|
|
|
|
form.insertAdjacentHTML(
|
|
|
|
'beforeend',
|
|
|
|
tpl_form_username({
|
|
|
|
'domain': ` @${this.domain}`,
|
|
|
|
'name': key,
|
|
|
|
'type': "text",
|
|
|
|
'label': key,
|
|
|
|
'value': '',
|
|
|
|
'required': true
|
|
|
|
})
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
form.insertAdjacentHTML(
|
|
|
|
'beforeend',
|
|
|
|
tpl_form_input({
|
|
|
|
'label': key,
|
|
|
|
'name': key,
|
|
|
|
'placeholder': key,
|
|
|
|
'required': true,
|
|
|
|
'type': (key === 'password' || key === 'email') ? key : "text",
|
|
|
|
'value': ''
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
// Show urls
|
|
|
|
_.each(this.urls, (url) => {
|
|
|
|
$(form).append($('<a target="blank"></a>').attr('href', url).text(url));
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
renderRegistrationForm (stanza) {
|
2016-02-19 11:43:46 +01:00
|
|
|
/* Renders the registration form based on the XForm fields
|
2016-03-19 23:16:00 +01:00
|
|
|
* received from the XMPP server.
|
|
|
|
*
|
|
|
|
* Parameters:
|
|
|
|
* (XMLElement) stanza - The IQ stanza received from the XMPP server.
|
|
|
|
*/
|
2017-09-17 21:15:00 +02:00
|
|
|
const form = this.el.querySelector('form');
|
2017-09-17 01:30:52 +02:00
|
|
|
form.innerHTML = tpl_registration_form({
|
|
|
|
'__': _converse.__,
|
2016-02-19 11:43:46 +01:00
|
|
|
'domain': this.domain,
|
|
|
|
'title': this.title,
|
|
|
|
'instructions': this.instructions
|
2017-09-17 01:30:52 +02:00
|
|
|
});
|
2016-02-19 11:43:46 +01:00
|
|
|
if (this.form_type === 'xform') {
|
2017-09-17 01:30:52 +02:00
|
|
|
_.each(stanza.querySelectorAll('field'), (field) => {
|
|
|
|
form.insertAdjacentHTML(
|
|
|
|
'beforeend',
|
|
|
|
utils.xForm2webForm(field, stanza, this.domain)
|
|
|
|
);
|
2017-07-10 17:46:22 +02:00
|
|
|
});
|
2016-02-19 11:43:46 +01:00
|
|
|
} else {
|
2017-09-17 01:30:52 +02:00
|
|
|
this.renderLegacyRegistrationForm(form);
|
2016-02-19 11:43:46 +01:00
|
|
|
}
|
|
|
|
if (this.fields) {
|
2017-09-17 21:15:00 +02:00
|
|
|
form.insertAdjacentHTML(
|
|
|
|
'beforeend',
|
|
|
|
`<input type="submit" class="pure-button button-primary" value="${__('Register')}"/>`
|
|
|
|
);
|
|
|
|
if (!_converse.registration_domain) {
|
|
|
|
form.insertAdjacentHTML(
|
|
|
|
'beforeend',
|
|
|
|
`<input type="button" class="pure-button button-cancel" value="${__('Choose a different provider')}"/>`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
form.addEventListener('submit', this.submitRegistrationForm.bind(this));
|
2016-02-19 11:43:46 +01:00
|
|
|
} else {
|
2017-09-17 21:15:00 +02:00
|
|
|
form.insertAdjacentHTML(
|
|
|
|
'beforeend',
|
|
|
|
`<input type="button" class="submit" value="${__('Return')}"/>`
|
|
|
|
);
|
|
|
|
form.querySelector('input[type=button]').addEventListener(
|
|
|
|
'click', this.cancelRegistration.bind(this));
|
2017-03-20 13:21:03 +01:00
|
|
|
}
|
2017-09-17 21:15:00 +02:00
|
|
|
this.model.set('registration_form_rendered', true);
|
|
|
|
this.showRegistrationForm();
|
2016-02-19 11:43:46 +01:00
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
reportErrors (stanza) {
|
2016-02-19 11:43:46 +01:00
|
|
|
/* Report back to the user any error messages received from the
|
2016-03-19 23:16:00 +01:00
|
|
|
* XMPP server after attempted registration.
|
|
|
|
*
|
|
|
|
* Parameters:
|
|
|
|
* (XMLElement) stanza - The IQ stanza received from the
|
|
|
|
* XMPP server.
|
|
|
|
*/
|
2017-07-10 17:46:22 +02:00
|
|
|
const $form= this.$('form'),
|
|
|
|
$errmsgs = $(stanza).find('error text');
|
|
|
|
|
|
|
|
let $flash = $form.find('.form-errors');
|
2016-02-19 11:43:46 +01:00
|
|
|
if (!$flash.length) {
|
2017-07-10 17:46:22 +02:00
|
|
|
const flash = '<legend class="form-errors"></legend>';
|
2016-02-19 11:43:46 +01:00
|
|
|
if ($form.find('p.instructions').length) {
|
|
|
|
$form.find('p.instructions').append(flash);
|
|
|
|
} else {
|
|
|
|
$form.prepend(flash);
|
|
|
|
}
|
|
|
|
$flash = $form.find('.form-errors');
|
|
|
|
} else {
|
|
|
|
$flash.empty();
|
|
|
|
}
|
|
|
|
$errmsgs.each(function (idx, txt) {
|
2017-09-17 21:15:00 +02:00
|
|
|
$flash.append($('<p class="form-help error">').text($(txt).text()));
|
2016-02-19 11:43:46 +01:00
|
|
|
});
|
|
|
|
if (!$errmsgs.length) {
|
2017-09-17 21:15:00 +02:00
|
|
|
$flash.append($('<p class="form-help error">').text(
|
2016-02-19 11:43:46 +01:00
|
|
|
__('The provider rejected your registration attempt. '+
|
|
|
|
'Please check the values you entered for correctness.')));
|
|
|
|
}
|
|
|
|
$flash.show();
|
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
cancelRegistration (ev) {
|
2016-02-19 11:43:46 +01:00
|
|
|
/* Handler, when the user cancels the registration form.
|
2016-03-19 23:16:00 +01:00
|
|
|
*/
|
2016-02-19 11:43:46 +01:00
|
|
|
if (ev && ev.preventDefault) { ev.preventDefault(); }
|
2017-09-17 21:15:00 +02:00
|
|
|
_converse.connection._proto._abortAllRequests();
|
2016-12-20 10:30:20 +01:00
|
|
|
_converse.connection.reset();
|
2017-09-17 21:15:00 +02:00
|
|
|
if (_converse.registration_domain && this.model.get('registration_form_rendered')) {
|
|
|
|
this.fetchRegistrationForm(
|
|
|
|
_converse.registration_domain
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
this.render();
|
2017-03-20 13:21:03 +01:00
|
|
|
}
|
2016-02-19 11:43:46 +01:00
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
submitRegistrationForm (ev) {
|
2016-02-19 11:43:46 +01:00
|
|
|
/* Handler, when the user submits the registration form.
|
2016-03-19 23:16:00 +01:00
|
|
|
* Provides form error feedback or starts the registration
|
|
|
|
* process.
|
|
|
|
*
|
|
|
|
* Parameters:
|
|
|
|
* (Event) ev - the submit event.
|
|
|
|
*/
|
2016-02-19 11:43:46 +01:00
|
|
|
if (ev && ev.preventDefault) { ev.preventDefault(); }
|
2017-09-17 21:15:00 +02:00
|
|
|
const has_empty_inputs = _.reduce(
|
|
|
|
this.el.querySelectorAll('input.required'),
|
2017-03-30 13:48:19 +02:00
|
|
|
function (result, input) {
|
|
|
|
if (input.value === '') {
|
|
|
|
input.classList.add('error');
|
|
|
|
return result + 1;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}, 0);
|
|
|
|
if (has_empty_inputs) { return; }
|
2017-07-10 17:46:22 +02:00
|
|
|
const $inputs = $(ev.target).find(':input:not([type=button]):not([type=submit])'),
|
2016-02-19 11:43:46 +01:00
|
|
|
iq = $iq({type: "set"}).c("query", {xmlns:Strophe.NS.REGISTER});
|
|
|
|
|
|
|
|
if (this.form_type === 'xform') {
|
|
|
|
iq.c("x", {xmlns: Strophe.NS.XFORM, type: 'submit'});
|
|
|
|
$inputs.each(function () {
|
|
|
|
iq.cnode(utils.webForm2xForm(this)).up();
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
$inputs.each(function () {
|
2017-07-10 17:46:22 +02:00
|
|
|
const $input = $(this);
|
2016-02-19 11:43:46 +01:00
|
|
|
iq.c($input.attr('name'), {}, $input.val());
|
|
|
|
});
|
|
|
|
}
|
2016-12-20 10:30:20 +01:00
|
|
|
_converse.connection._addSysHandler(this._onRegisterIQ.bind(this), null, "iq", null, null);
|
|
|
|
_converse.connection.send(iq);
|
2016-02-19 11:43:46 +01:00
|
|
|
this.setFields(iq.tree());
|
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
setFields (stanza) {
|
2016-02-19 11:43:46 +01:00
|
|
|
/* Stores the values that will be sent to the XMPP server
|
2016-03-19 23:16:00 +01:00
|
|
|
* during attempted registration.
|
|
|
|
*
|
|
|
|
* Parameters:
|
|
|
|
* (XMLElement) stanza - the IQ stanza that will be sent to the XMPP server.
|
|
|
|
*/
|
2017-07-10 17:46:22 +02:00
|
|
|
const $query = $(stanza).find('query');
|
2016-02-19 11:43:46 +01:00
|
|
|
if ($query.length > 0) {
|
2017-07-10 17:46:22 +02:00
|
|
|
const $xform = $query.find(`x[xmlns="${Strophe.NS.XFORM}"]`);
|
2016-02-19 11:43:46 +01:00
|
|
|
if ($xform.length > 0) {
|
|
|
|
this._setFieldsFromXForm($xform);
|
|
|
|
} else {
|
|
|
|
this._setFieldsFromLegacy($query);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
_setFieldsFromLegacy ($query) {
|
|
|
|
$query.children().each((idx, field) => {
|
|
|
|
const $field = $(field);
|
2016-02-19 11:43:46 +01:00
|
|
|
if (field.tagName.toLowerCase() === 'instructions') {
|
|
|
|
this.instructions = Strophe.getText(field);
|
|
|
|
return;
|
|
|
|
} else if (field.tagName.toLowerCase() === 'x') {
|
|
|
|
if ($field.attr('xmlns') === 'jabber:x:oob') {
|
2017-07-10 17:46:22 +02:00
|
|
|
$field.find('url').each((idx, url) => {
|
2016-02-19 11:43:46 +01:00
|
|
|
this.urls.push($(url).text());
|
2017-07-10 17:46:22 +02:00
|
|
|
});
|
2016-02-19 11:43:46 +01:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.fields[field.tagName.toLowerCase()] = Strophe.getText(field);
|
2017-07-10 17:46:22 +02:00
|
|
|
});
|
2016-02-19 11:43:46 +01:00
|
|
|
this.form_type = 'legacy';
|
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
_setFieldsFromXForm ($xform) {
|
2016-02-19 11:43:46 +01:00
|
|
|
this.title = $xform.find('title').text();
|
|
|
|
this.instructions = $xform.find('instructions').text();
|
2017-07-10 17:46:22 +02:00
|
|
|
$xform.find('field').each((idx, field) => {
|
|
|
|
const _var = field.getAttribute('var');
|
2016-02-19 11:43:46 +01:00
|
|
|
if (_var) {
|
|
|
|
this.fields[_var.toLowerCase()] = $(field).children('value').text();
|
|
|
|
} else {
|
|
|
|
// TODO: other option seems to be type="fixed"
|
2017-07-05 11:33:55 +02:00
|
|
|
_converse.log("Found field we couldn't parse", Strophe.LogLevel.WARN);
|
2016-02-19 11:43:46 +01:00
|
|
|
}
|
2017-07-10 17:46:22 +02:00
|
|
|
});
|
2016-02-19 11:43:46 +01:00
|
|
|
this.form_type = 'xform';
|
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
_onRegisterIQ (stanza) {
|
2016-02-19 11:43:46 +01:00
|
|
|
/* Callback method that gets called when a return IQ stanza
|
2016-03-19 23:16:00 +01:00
|
|
|
* is received from the XMPP server, after attempting to
|
|
|
|
* register a new user.
|
|
|
|
*
|
|
|
|
* Parameters:
|
|
|
|
* (XMLElement) stanza - The IQ stanza.
|
|
|
|
*/
|
2017-07-10 17:46:22 +02:00
|
|
|
let error = null,
|
2016-02-19 11:43:46 +01:00
|
|
|
query = stanza.getElementsByTagName("query");
|
|
|
|
if (query.length > 0) {
|
|
|
|
query = query[0];
|
|
|
|
}
|
|
|
|
if (stanza.getAttribute("type") === "error") {
|
2017-07-05 11:33:55 +02:00
|
|
|
_converse.log("Registration failed.", Strophe.LogLevel.ERROR);
|
2016-02-19 11:43:46 +01:00
|
|
|
error = stanza.getElementsByTagName("error");
|
|
|
|
if (error.length !== 1) {
|
2016-12-20 10:30:20 +01:00
|
|
|
_converse.connection._changeConnectStatus(Strophe.Status.REGIFAIL, "unknown");
|
2016-02-19 11:43:46 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
error = error[0].firstChild.tagName.toLowerCase();
|
|
|
|
if (error === 'conflict') {
|
2016-12-20 10:30:20 +01:00
|
|
|
_converse.connection._changeConnectStatus(Strophe.Status.CONFLICT, error);
|
2016-02-19 11:43:46 +01:00
|
|
|
} else if (error === 'not-acceptable') {
|
2016-12-20 10:30:20 +01:00
|
|
|
_converse.connection._changeConnectStatus(Strophe.Status.NOTACCEPTABLE, error);
|
2016-02-19 11:43:46 +01:00
|
|
|
} else {
|
2016-12-20 10:30:20 +01:00
|
|
|
_converse.connection._changeConnectStatus(Strophe.Status.REGIFAIL, error);
|
2016-02-19 11:43:46 +01:00
|
|
|
}
|
|
|
|
this.reportErrors(stanza);
|
|
|
|
} else {
|
2016-12-20 10:30:20 +01:00
|
|
|
_converse.connection._changeConnectStatus(Strophe.Status.REGISTERED, null);
|
2016-02-19 11:43:46 +01:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
remove () {
|
2016-02-19 11:43:46 +01:00
|
|
|
this.$tabs.empty();
|
|
|
|
this.$el.parent().empty();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}));
|