From 65d41373739dad768b237c83cd6ea101eb0dcbd9 Mon Sep 17 00:00:00 2001 From: JC Brand Date: Tue, 18 Nov 2014 11:23:50 +0100 Subject: [PATCH] Remove reliance on strophe.register plugin. updates #117 --- converse.js | 163 ++++++++++++++++++++++++++++++++++++-------- css/converse.css | 9 ++- less/converse.less | 10 +++ main.js | 1 - src/deps-full.js | 1 - src/deps-website.js | 1 - 6 files changed, 152 insertions(+), 33 deletions(-) diff --git a/converse.js b/converse.js index be8c7e016..8e33f46d7 100644 --- a/converse.js +++ b/converse.js @@ -162,6 +162,20 @@ converse.initialize = function (settings, callback) { var converse = this; + // Add Strophe Namespaces + Strophe.addNamespace('REGISTER', 'jabber:iq:register'); + Strophe.addNamespace('XFORM', 'jabber:x:data'); + + // Add Strophe Statuses + var i = 0; + Object.keys(Strophe.Status).forEach(function (key) { + 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; + // Constants // --------- var UNENCRYPTED = 0; @@ -2395,12 +2409,11 @@ var $form= this.$el.find('form.chatroom-form'), $stanza = $(stanza), $fields = $stanza.find('field'), - title = $stanza.find('title').text(), - instructions = $stanza.find('instructions').text(); + title = $stanza.find('title').text(); $form.find('span.spinner').remove(); $form.append($('').text(title)); if (instructions != title) { - $form.append($('

').text(instructions)); + $form.append($('

').text(this.instructions)); } _.each($fields, function (field) { $form.append(utils.xForm2webForm(field)); @@ -4457,13 +4470,14 @@ id: "register", className: 'controlbox-pane', events: { - 'submit form#converse-register': 'query' + 'submit form#converse-register': 'onProviderChosen' }, initialize: function (cfg) { - this.fields = {}; + this.reset(); this.$parent = cfg.$parent; this.$tabs = cfg.$parent.parent().find('#controlbox-tabs'); + this.registerHooks(); }, render: function () { @@ -4478,17 +4492,108 @@ return this; }, - query: function (ev) { + registerHooks: function () { + /* Hook into Strophe's _connect_cb, so that we can send an IQ + * requesting the registration fields. + */ + var conn = converse.connection; + var connect_cb = conn._connect_cb.bind(conn); + conn._connect_cb = $.proxy(function (req, callback, raw) { + if (!this._registering) { + connect_cb(req, callback, raw); + } else { + if (this.getRegistrationFields(req, callback, raw)) { + delete this._registering; + } + } + }, this); + }, + + getRegistrationFields: function (req, _callback, raw) { + /* Send an IQ stanza to the XMPP server asking for the + * registration fields. + * + * Parameters: + * (Strophe.Request) req - The current request + * (Function) callback + */ + converse.log("sendQueryStanza was called"); + var conn = converse.connection; + conn.connected = true; + + var body = conn._proto._reqToData(req); + if (!body) { return; } + if (conn._proto._connect_cb(body) === Strophe.Status.CONNFAIL) { + return false; + } + var register = body.getElementsByTagName("register"); + var mechanisms = body.getElementsByTagName("mechanism"); + if (register.length === 0 && mechanisms.length === 0) { + conn._proto._no_auth_received(_callback); + return false; + } + if (register.length === 0) { + conn._changeConnectStatus(Strophe.Status.REGIFAIL, null); + return true; + } + // Send an IQ stanza to get all required data fields + conn._addSysHandler(this.onRegistrationFields.bind(this), null, "iq", null, null); + conn.send($iq({type: "get"}).c("query", {xmlns: Strophe.NS.REGISTER}).tree()); + return true; + }, + + onRegistrationFields: function (stanza) { + /* Handler for Registration Fields Request. + * + * Parameters: + * (XMLElement) elem - The query stanza. + */ + if (stanza.getElementsByTagName("query").length !== 1) { + converse.connection._changeConnectStatus(Strophe.Status.REGIFAIL, "unknown"); + return false; + } + this.setFields(stanza); + this.renderRegistrationForm(stanza); + return false; + }, + + reset: function (settings) { + var defaults = { + fields: {}, + title: "", + instructions: "", + registered: false, + _registering: false, + domain: null + }; + _.extend(this, defaults); + if (settings) { + _.extend(this, _.pick(settings, Object.keys(defaults))); + } + }, + + onProviderChosen: function (ev) { + /* Callback method that gets called when the user has chosen an + * XMPP provider. + * + * Parameters: + * (Submit Event) ev - Form submission event. + */ if (ev && ev.preventDefault) { ev.preventDefault(); } var $form = $(ev.target), $domain_input = $form.find('input[name=domain]'), domain = $domain_input.val(), errors = false; - if (!domain) { errors = true; $domain_input.addClass('error'); } - if (errors) { return; } // TODO provide error messages + if (!domain) { + $domain_input.addClass('error'); + return; + } $form.find('input[type=submit]').hide().after('

').text(register.instructions)); + $form.empty().append($('

').text(this.title)); + $form.append($('

').text(this.instructions)); _.each($fields, function (field) { $form.append(utils.xForm2webForm(field)); }); @@ -4624,7 +4734,7 @@ var $inputs = $(ev.target).find(':input:not([type=button]):not([type=submit])'), iq = $iq({type: "set"}) .c("query", {xmlns:Strophe.NS.REGISTER}) - .c("x", {xmlns: 'jabber:x:data', type: 'submit'}); // TODO: Add Strophe namespace + .c("x", {xmlns: Strophe.NS.XFORM, type: 'submit'}); // TODO: Add Strophe namespace $inputs.each(function () { iq.cnode(utils.webForm2xForm(this)).up(); @@ -4641,14 +4751,14 @@ * Parameters: * (XMLElement) stanza - the IQ stanza that will be sent to the XMPP server. */ - var query = stanza.getElementsByTagName("query"), field, i; - if (query.length > 0) { - query = query[0]; - $(query).find('field').each($.proxy(function (idx, field) { + var $query = $(stanza).find('x[xmlns="'+Strophe.NS.XFORM+'"]'); + if ($query.length > 0) { + this.title = $query.find('title').text(); + this.instructions = $query.find('instructions').text(); + $query.find('field').each($.proxy(function (idx, field) { var name = field.getAttribute('var').toLowerCase(); var value = $(field).children('value').text(); this.fields[name] = value; - converse.connection.register.fields[name] = value; }, this)); } }, @@ -4688,11 +4798,6 @@ return false; }, - reset: function () { - this.fields = {}; - delete this.domain; - }, - remove: function () { // XXX ? this.$tabs.empty(); diff --git a/css/converse.css b/css/converse.css index 31c817c6b..e2dc4ee31 100644 --- a/css/converse.css +++ b/css/converse.css @@ -1114,7 +1114,7 @@ dl.add-converse-contact { font-size: 14px; height: 289px; height: calc(100% - 35px); - overflow-y: hidden; + overflow-y: scroll; padding: 0; position: absolute; width: 100%; @@ -1127,6 +1127,13 @@ dl.add-converse-contact { #conversejs .controlbox-pane dd.odd { background-color: #DCEAC5; } +#conversejs form#converse-register .title { + font-weight: bold; +} +#conversejs form#converse-register .instructions { + font-style: italic; + color: gray; +} #conversejs form#converse-register .form-errors { color: red; display: none; diff --git a/less/converse.less b/less/converse.less index 1375a8ae7..0ac82bd0a 100644 --- a/less/converse.less +++ b/less/converse.less @@ -1270,6 +1270,16 @@ dl.add-converse-contact { background-color: #DCEAC5; } +#conversejs form#converse-register .title { + font-weight: bold; +} + +#conversejs form#converse-register .instructions { + font-style: italic; + color: gray; + font-size: 85%; +} + #conversejs form#converse-register .form-errors { color: red; display: none; diff --git a/main.js b/main.js index 183bd16b3..2d2c203f8 100644 --- a/main.js +++ b/main.js @@ -17,7 +17,6 @@ require.config({ "strophe": "components/strophe/strophe", "strophe.disco": "components/strophejs-plugins/disco/strophe.disco", "strophe.muc": "components/strophe.muc/index", - "strophe.register": "components/strophejs-plugins/register/strophe.register", "strophe.roster": "src/strophe.roster", "strophe.vcard": "components/strophejs-plugins/vcard/strophe.vcard", "text": 'components/requirejs-text/text', diff --git a/src/deps-full.js b/src/deps-full.js index 8b5cb87cb..1e2a71b70 100644 --- a/src/deps-full.js +++ b/src/deps-full.js @@ -10,7 +10,6 @@ define("converse-dependencies", [ "typeahead", "strophe", "strophe.muc", - "strophe.register", "strophe.roster", "strophe.vcard", "strophe.disco" diff --git a/src/deps-website.js b/src/deps-website.js index 44e8be0be..def8fe87c 100644 --- a/src/deps-website.js +++ b/src/deps-website.js @@ -12,7 +12,6 @@ define("converse-dependencies", [ "typeahead", "strophe", "strophe.muc", - "strophe.register", "strophe.roster", "strophe.vcard", "strophe.disco"