\n';
}
return __p
};});
define('tpl!form_input', ['lodash'], function(_) {return function(obj) {
obj || (obj = {});
var __t, __p = '', __e = _.escape, __j = Array.prototype.join;
function print() { __p += __j.call(arguments, '') }
with (obj) {
if (label) { ;
__p += '\n\n';
} ;
__p += '\n\n';
}
return __p
};});
define('tpl!form_captcha', ['lodash'], function(_) {return function(obj) {
obj || (obj = {});
var __t, __p = '', __e = _.escape, __j = Array.prototype.join;
function print() { __p += __j.call(arguments, '') }
with (obj) {
if (label) { ;
__p += '\n\n';
} ;
__p += '\n\n\n\n\n';
}
return __p
};});
/*global define, escape, locales, Jed */
(function (root, factory) {
define('utils',[
"jquery.noconflict",
"sizzle",
"jquery.browser",
"lodash.noconflict",
"locales",
"moment_with_locales",
"strophe",
"tpl!field",
"tpl!select_option",
"tpl!form_select",
"tpl!form_textarea",
"tpl!form_checkbox",
"tpl!form_username",
"tpl!form_input",
"tpl!form_captcha"
], factory);
}(this, function (
$, sizzle, dummy, _,
locales,
moment,
Strophe,
tpl_field,
tpl_select_option,
tpl_form_select,
tpl_form_textarea,
tpl_form_checkbox,
tpl_form_username,
tpl_form_input,
tpl_form_captcha
) {
"use strict";
locales = locales || {};
Strophe = Strophe.Strophe;
var XFORM_TYPE_MAP = {
'text-private': 'password',
'text-single': 'text',
'fixed': 'label',
'boolean': 'checkbox',
'hidden': 'hidden',
'jid-multi': 'textarea',
'list-single': 'dropdown',
'list-multi': 'dropdown'
};
var afterAnimationEnd = function (el, callback) {
el.classList.remove('visible');
if (_.isFunction(callback)) {
callback();
}
};
var unescapeHTML = function (htmlEscapedText) {
/* Helper method that replace HTML-escaped symbols with equivalent characters
* (e.g. transform occurrences of '&' to '&')
*
* Parameters:
* (String) htmlEscapedText: a String containing the HTML-escaped symbols.
*/
var div = document.createElement('div');
div.innerHTML = htmlEscapedText;
return div.innerText;
}
var isImage = function (url) {
var deferred = new $.Deferred();
var img = new Image();
var timer = window.setTimeout(function () {
deferred.reject();
img = null;
}, 3000);
img.onerror = img.onabort = function () {
clearTimeout(timer);
deferred.reject();
};
img.onload = function () {
clearTimeout(timer);
deferred.resolve(img);
};
img.src = url;
return deferred.promise();
};
$.fn.hasScrollBar = function() {
if (!$.contains(document, this.get(0))) {
return false;
}
if(this.parent().height() < this.get(0).scrollHeight) {
return true;
}
return false;
};
var throttledHTML = _.throttle(function (el, html) {
el.innerHTML = html;
}, 500);
$.fn.addHyperlinks = function () {
if (this.length > 0) {
this.each(function (i, obj) {
var prot, escaped_url;
var x = obj.innerHTML;
var list = x.match(/\b(https?:\/\/|www\.|https?:\/\/www\.)[^\s<]{2,200}\b/g );
if (list) {
for (i=0; i'+ list[i] + '' );
}
}
obj.innerHTML = x;
_.forEach(list, function (url) {
isImage(unescapeHTML(url)).then(function (img) {
img.className = 'chat-image';
throttledHTML(obj.querySelector('a'), img.outerHTML);
});
});
});
}
return this;
};
$.fn.addEmoticons = function (allowed) {
if (allowed) {
if (this.length > 0) {
this.each(function (i, obj) {
var text = $(obj).html();
text = text.replace(/>:\)/g, '');
text = text.replace(/:\)/g, '');
text = text.replace(/:\-\)/g, '');
text = text.replace(/;\)/g, '');
text = text.replace(/;\-\)/g, '');
text = text.replace(/:D/g, '');
text = text.replace(/:\-D/g, '');
text = text.replace(/:P/g, '');
text = text.replace(/:\-P/g, '');
text = text.replace(/:p/g, '');
text = text.replace(/:\-p/g, '');
text = text.replace(/8\)/g, '');
text = text.replace(/:S/g, '');
text = text.replace(/:\\/g, '');
text = text.replace(/:\/ /g, '');
text = text.replace(/>:\(/g, '');
text = text.replace(/:\(/g, '');
text = text.replace(/:\-\(/g, '');
text = text.replace(/:O/g, '');
text = text.replace(/:\-O/g, '');
text = text.replace(/\=\-O/g, '');
text = text.replace(/\(\^.\^\)b/g, '');
text = text.replace(/<3/g, '');
$(obj).html(text);
});
}
}
return this;
};
var utils = {
// Translation machinery
// ---------------------
__: function (str) {
if (!utils.isConverseLocale(this.locale) || this.locale === 'en') {
return Jed.sprintf.apply(Jed, arguments);
}
if (typeof this.jed === "undefined") {
this.jed = new Jed(window.JSON.parse(locales[this.locale]));
}
var t = this.jed.translate(str);
if (arguments.length>1) {
return t.fetch.apply(t, [].slice.call(arguments,1));
} else {
return t.fetch();
}
},
___: function (str) {
/* XXX: This is part of a hack to get gettext to scan strings to be
* translated. Strings we cannot send to the function above because
* they require variable interpolation and we don't yet have the
* variables at scan time.
*
* See actionInfoMessages in src/converse-muc.js
*/
return str;
},
isLocaleAvailable: function (locale, available) {
/* Check whether the locale or sub locale (e.g. en-US, en) is supported.
*
* Parameters:
* (Function) available - returns a boolean indicating whether the locale is supported
*/
if (available(locale)) {
return locale;
} else {
var sublocale = locale.split("-")[0];
if (sublocale !== locale && available(sublocale)) {
return sublocale;
}
}
},
fadeIn: function (el, callback) {
if ($.fx.off) {
el.classList.remove('hidden');
if (_.isFunction(callback)) {
callback();
}
return;
}
if (_.includes(el.classList, 'hidden')) {
/* XXX: This doesn't appear to be working...
el.addEventListener("webkitAnimationEnd", _.partial(afterAnimationEnd, el, callback), false);
el.addEventListener("animationend", _.partial(afterAnimationEnd, el, callback), false);
*/
setTimeout(_.partial(afterAnimationEnd, el, callback), 351);
el.classList.add('visible');
el.classList.remove('hidden');
} else {
afterAnimationEnd(el, callback);
}
},
isSameBareJID: function (jid1, jid2) {
return Strophe.getBareJidFromJid(jid1).toLowerCase() ===
Strophe.getBareJidFromJid(jid2).toLowerCase();
},
isNewMessage: function (message) {
/* Given a stanza, determine whether it's a new
* message, i.e. not a MAM archived one.
*/
if (message instanceof Element) {
return !(sizzle('result[xmlns="'+Strophe.NS.MAM+'"]', message).length);
} else {
return !message.get('archive_id');
}
},
isOTRMessage: function (message) {
var body = message.querySelector('body'),
text = (!_.isNull(body) ? body.textContent: undefined);
return text && !!text.match(/^\?OTR/);
},
isHeadlineMessage: function (message) {
var from_jid = message.getAttribute('from');
if (message.getAttribute('type') === 'headline') {
return true;
}
if (message.getAttribute('type') !== 'error' &&
!_.isNil(from_jid) &&
!_.includes(from_jid, '@')) {
// Some servers (I'm looking at you Prosody) don't set the message
// type to "headline" when sending server messages. For now we
// check if an @ signal is included, and if not, we assume it's
// a headline message.
return true;
}
return false;
},
merge: function merge (first, second) {
/* Merge the second object into the first one.
*/
for (var k in second) {
if (_.isObject(first[k])) {
merge(first[k], second[k]);
} else {
first[k] = second[k];
}
}
},
applyUserSettings: function applyUserSettings (context, settings, user_settings) {
/* Configuration settings might be nested objects. We only want to
* add settings which are whitelisted.
*/
for (var k in settings) {
if (_.isUndefined(user_settings[k])) {
continue;
}
if (_.isObject(settings[k]) && !_.isArray(settings[k])) {
applyUserSettings(context[k], settings[k], user_settings[k]);
} else {
context[k] = user_settings[k];
}
}
},
refreshWebkit: function () {
/* This works around a webkit bug. Refreshes the browser's viewport,
* otherwise chatboxes are not moved along when one is closed.
*/
if ($.browser.webkit && window.requestAnimationFrame) {
window.requestAnimationFrame(function () {
var conversejs = document.getElementById('conversejs');
conversejs.style.display = 'none';
var tmp = conversejs.offsetHeight; // jshint ignore:line
conversejs.style.display = 'block';
});
}
},
webForm2xForm: function (field) {
/* Takes an HTML DOM and turns it into an XForm field.
*
* Parameters:
* (DOMElement) field - the field to convert
*/
var $input = $(field), value;
if ($input.is('[type=checkbox]')) {
value = $input.is(':checked') && 1 || 0;
} else if ($input.is('textarea')) {
value = [];
var lines = $input.val().split('\n');
for( var vk=0; vk into consideration
var options = [], j, $options, $values, value, values;
if ($field.attr('type') === 'list-single' || $field.attr('type') === 'list-multi') {
values = [];
$values = $field.children('value');
for (j=0; j<$values.length; j++) {
values.push($($values[j]).text());
}
$options = $field.children('option');
for (j=0; j<$options.length; j++) {
value = $($options[j]).find('value').text();
options.push(tpl_select_option({
value: value,
label: $($options[j]).attr('label'),
selected: _.startsWith(values, value),
required: $field.find('required').length
}));
}
return tpl_form_select({
name: $field.attr('var'),
label: $field.attr('label'),
options: options.join(''),
multiple: ($field.attr('type') === 'list-multi'),
required: $field.find('required').length
});
} else if ($field.attr('type') === 'fixed') {
return $('
').text($field.find('value').text());
} else if ($field.attr('type') === 'jid-multi') {
return tpl_form_textarea({
name: $field.attr('var'),
label: $field.attr('label') || '',
value: $field.find('value').text(),
required: $field.find('required').length
});
} else if ($field.attr('type') === 'boolean') {
return tpl_form_checkbox({
name: $field.attr('var'),
type: XFORM_TYPE_MAP[$field.attr('type')],
label: $field.attr('label') || '',
checked: $field.find('value').text() === "1" && 'checked="1"' || '',
required: $field.find('required').length
});
} else if ($field.attr('type') && $field.attr('var') === 'username') {
return tpl_form_username({
domain: ' @'+this.domain,
name: $field.attr('var'),
type: XFORM_TYPE_MAP[$field.attr('type')],
label: $field.attr('label') || '',
value: $field.find('value').text(),
required: $field.find('required').length
});
} else if ($field.attr('type')) {
return tpl_form_input({
name: $field.attr('var'),
type: XFORM_TYPE_MAP[$field.attr('type')],
label: $field.attr('label') || '',
value: $field.find('value').text(),
required: $field.find('required').length
});
} else {
if ($field.attr('var') === 'ocr') { // Captcha
return _.reduce(_.map($field.find('uri'),
$.proxy(function (uri) {
return tpl_form_captcha({
label: this.$field.attr('label'),
name: this.$field.attr('var'),
data: this.$stanza.find('data[cid="'+uri.textContent.replace(/^cid:/, '')+'"]').text(),
type: uri.getAttribute('type'),
required: this.$field.find('required').length
});
}, {'$stanza': $stanza, '$field': $field})
),
function (memo, num) { return memo + num; }, ''
);
}
}
}
};
utils.detectLocale = function (library_check) {
/* Determine which locale is supported by the user's system as well
* as by the relevant library (e.g. converse.js or moment.js).
*
* Parameters:
* (Function) library_check - returns a boolean indicating whether
* the locale is supported.
*/
var locale, i;
if (window.navigator.userLanguage) {
locale = utils.isLocaleAvailable(window.navigator.userLanguage, library_check);
}
if (window.navigator.languages && !locale) {
for (i=0; i plugin1.MyFunc.__super__.myFunc => original.myFunc`
_overrideAttribute: function _overrideAttribute(key, plugin) {
var value = plugin.overrides[key];
if (typeof value === "function") {
var default_super = {};
default_super[this.name] = this.plugged;
var wrapped_function = _.partial(this.wrappedOverride, key, value, this.plugged[key], default_super);
this.plugged[key] = wrapped_function;
} else {
this.plugged[key] = value;
}
},
_extendObject: function _extendObject(obj, attributes) {
if (!obj.prototype.__super__) {
obj.prototype.__super__ = {};
obj.prototype.__super__[this.name] = this.plugged;
}
var that = this;
_.each(attributes, function (value, key) {
if (key === 'events') {
obj.prototype[key] = _.extend(value, obj.prototype[key]);
} else if (typeof value === 'function') {
// We create a partially applied wrapper function, that
// makes sure to set the proper super method when the
// overriding method is called. This is done to enable
// chaining of plugin methods, all the way up to the
// original method.
var default_super = {};
default_super[that.name] = that.plugged;
var wrapped_function = _.partial(that.wrappedOverride, key, value, obj.prototype[key], default_super);
obj.prototype[key] = wrapped_function;
} else {
obj.prototype[key] = value;
}
});
},
// Plugins can specify optional dependencies (by means of the
// `optional_dependencies` list attribute) which refers to dependencies
// which will be initialized first, before the plugin itself gets initialized.
// They are optional in the sense that if they aren't available, an
// error won't be thrown.
// However, if you want to make these dependencies strict (i.e.
// non-optional), you can set the `strict_plugin_dependencies` attribute to `true`
// on the object being made pluggable (i.e. the object passed to
// `pluggable.enable`).
loadOptionalDependencies: function loadOptionalDependencies(plugin) {
var _this = this;
_.each(plugin.optional_dependencies, function (name) {
var dep = _this.plugins[name];
if (dep) {
if (_.includes(dep.optional_dependencies, plugin.__name__)) {
/* FIXME: circular dependency checking is only one level deep. */
throw "Found a circular dependency between the plugins \"" + plugin.__name__ + "\" and \"" + name + "\"";
}
_this.initializePlugin(dep);
} else {
_this.throwUndefinedDependencyError("Could not find optional dependency \"" + name + "\" " + "for the plugin \"" + plugin.__name__ + "\". " + "If it's needed, make sure it's loaded by require.js");
}
});
},
throwUndefinedDependencyError: function throwUndefinedDependencyError(msg) {
if (this.plugged.strict_plugin_dependencies) {
throw msg;
} else {
console.log(msg);
return;
}
},
// `applyOverrides` is called by initializePlugin. It applies any
// and all overrides of methods or Backbone views and models that
// are defined on any of the plugins.
applyOverrides: function applyOverrides(plugin) {
var _this2 = this;
_.each(Object.keys(plugin.overrides || {}), function (key) {
var override = plugin.overrides[key];
if ((typeof override === 'undefined' ? 'undefined' : _typeof(override)) === "object") {
if (typeof _this2.plugged[key] === 'undefined') {
_this2.throwUndefinedDependencyError("Error: Plugin \"" + plugin.__name__ + "\" tried to override " + key + " but it's not found.");
} else {
_this2._extendObject(_this2.plugged[key], override);
}
} else {
_this2._overrideAttribute(key, plugin);
}
});
},
// `initializePlugin` applies the overrides (if any) defined on all
// the registered plugins and then calls the initialize method for each plugin.
initializePlugin: function initializePlugin(plugin) {
if (!_.includes(_.keys(this.allowed_plugins), plugin.__name__)) {
/* Don't initialize disallowed plugins. */
return;
}
if (_.includes(this.initialized_plugins, plugin.__name__)) {
/* Don't initialize plugins twice, otherwise we get
* infinite recursion in overridden methods.
*/
return;
}
_.extend(plugin, this.properties);
if (plugin.optional_dependencies) {
this.loadOptionalDependencies(plugin);
}
this.applyOverrides(plugin);
if (typeof plugin.initialize === "function") {
plugin.initialize.bind(plugin)(this);
}
this.initialized_plugins.push(plugin.__name__);
},
// `registerPlugin` registers (or inserts, if you'd like) a plugin,
// by adding it to the `plugins` map on the PluginSocket instance.
registerPlugin: function registerPlugin(name, plugin) {
if (name in this.plugins) {
throw new Error('Error: Plugin name ' + name + ' is already taken');
}
plugin.__name__ = name;
this.plugins[name] = plugin;
},
// `initializePlugins` should get called once all plugins have been
// registered. It will then iterate through all the plugins, calling
// `initializePlugin` for each.
// The passed in properties variable is an object with attributes and methods
// which will be attached to the plugins.
initializePlugins: function initializePlugins() {
var properties = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
var whitelist = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
var blacklist = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : [];
if (!_.size(this.plugins)) {
return;
}
this.properties = properties;
this.allowed_plugins = _.pickBy(this.plugins, function (plugin, key) {
return (!whitelist.length || whitelist.length && _.includes(whitelist, key)) && !_.includes(blacklist, key);
});
_.each(_.values(this.allowed_plugins), this.initializePlugin.bind(this));
}
});
function enable(object, name, attrname) {
// Call the `enable` method to make an object pluggable
//
// It takes three parameters:
// - `object`: The object that gets made pluggable.
// - `name`: The string name by which the now pluggable object
// may be referenced on the __super__ obj (in overrides).
// The default value is "plugged".
// - `attrname`: The string name of the attribute on the now
// pluggable object, which refers to the PluginSocket instance
// that gets created.
if (typeof attrname === "undefined") {
attrname = "pluginSocket";
}
if (typeof name === 'undefined') {
name = 'plugged';
}
var ref = {};
ref[attrname] = new PluginSocket(object, name);
return _.extend(object, ref);
}
exports.enable = enable;
exports.default = {
enable: enable
};
});
//# sourceMappingURL=pluggable.js.map;
// Converse.js (A browser based XMPP chat client)
// http://conversejs.org
//
// Copyright (c) 2012-2017, Jan-Carel Brand
// Licensed under the Mozilla Public License (MPLv2)
//
/*global Backbone, define, window, document, JSON */
(function (root, factory) {
define('converse-core',["sizzle",
"jquery.noconflict",
"lodash.noconflict",
"lodash.converter",
"polyfill",
"utils",
"moment_with_locales",
"strophe",
"pluggable",
"backbone.noconflict",
"strophe.disco",
"backbone.browserStorage",
"backbone.overview",
], factory);
}(this, function (
sizzle, $, _, lodashConverter, polyfill,
utils, moment, Strophe, pluggable, Backbone) {
/* Cannot use this due to Safari bug.
* See https://github.com/jcbrand/converse.js/issues/196
*/
// "use strict";
// Create the FP (functional programming) version of lodash
var fp = lodashConverter(_.runInContext());
// Strophe globals
var $build = Strophe.$build;
var $iq = Strophe.$iq;
var $msg = Strophe.$msg;
var $pres = Strophe.$pres;
var b64_sha1 = Strophe.SHA1.b64_sha1;
Strophe = Strophe.Strophe;
// Use Mustache style syntax for variable interpolation
/* Configuration of Lodash templates (this config is distinct to the
* config of requirejs-tpl in main.js). This one is for normal inline templates.
*/
_.templateSettings = {
'escape': /\{\{\{([\s\S]+?)\}\}\}/g,
'evaluate': /\{\[([\s\S]+?)\]\}/g,
'interpolate': /\{\{([\s\S]+?)\}\}/g
};
var _converse = {};
_converse.templates = {};
_.extend(_converse, Backbone.Events);
_converse.promises = {
'cachedRoster': new $.Deferred(),
'chatBoxesFetched': new $.Deferred(),
'connected': new $.Deferred(),
'pluginsInitialized': new $.Deferred(),
'roster': new $.Deferred(),
'rosterContactsFetched': new $.Deferred(),
'rosterGroupsFetched': new $.Deferred(),
'rosterInitialized': new $.Deferred(),
'statusInitialized': new $.Deferred()
};
_converse.emit = function (name) {
_converse.trigger.apply(this, arguments);
var promise = _converse.promises[name];
if (!_.isUndefined(promise)) {
promise.resolve();
}
};
_converse.core_plugins = [
'converse-bookmarks',
'converse-chatview',
'converse-controlbox',
'converse-core',
'converse-dragresize',
'converse-headline',
'converse-mam',
'converse-minimize',
'converse-muc',
'converse-notification',
'converse-otr',
'converse-ping',
'converse-register',
'converse-roomslist',
'converse-rosterview',
'converse-vcard'
];
// Make converse pluggable
pluggable.enable(_converse, '_converse', 'pluggable');
// Module-level constants
_converse.STATUS_WEIGHTS = {
'offline': 6,
'unavailable': 5,
'xa': 4,
'away': 3,
'dnd': 2,
'chat': 1, // We currently don't differentiate between "chat" and "online"
'online': 1
};
_converse.PRETTY_CHAT_STATUS = {
'offline': 'Offline',
'unavailable': 'Unavailable',
'xa': 'Extended Away',
'away': 'Away',
'dnd': 'Do not disturb',
'chat': 'Chattty',
'online': 'Online'
};
_converse.ANONYMOUS = "anonymous";
_converse.CLOSED = 'closed';
_converse.EXTERNAL = "external";
_converse.LOGIN = "login";
_converse.LOGOUT = "logout";
_converse.OPENED = 'opened';
_converse.PREBIND = "prebind";
var PRETTY_CONNECTION_STATUS = {
0: 'ERROR',
1: 'CONNECTING',
2: 'CONNFAIL',
3: 'AUTHENTICATING',
4: 'AUTHFAIL',
5: 'CONNECTED',
6: 'DISCONNECTED',
7: 'DISCONNECTING',
8: 'ATTACHED',
9: 'REDIRECT'
};
var DEFAULT_IMAGE_TYPE = 'image/png';
var DEFAULT_IMAGE = "iVBORw0KGgoAAAANSUhEUgAAAGAAAABgCAIAAABt+uBvAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAB3RJTUUH3gwHCy455JBsggAABkJJREFUeNrtnM1PE1sUwHvvTD8otWLHST/Gimi1CEgr6M6FEWuIBo2pujDVsNDEP8GN/4MbN7oxrlipG2OCgZgYlxAbkRYw1KqkIDRCSkM7nXvvW8x7vjyNeQ9m7p1p3z1LQk/v/Dhz7vkEXL161cHl9wI5Ag6IA+KAOCAOiAPigDggLhwQB2S+iNZ+PcYY/SWEEP2HAAAIoSAIoihCCP+ngDDGtVotGAz29/cfOXJEUZSOjg6n06lp2sbGRqlUWlhYyGazS0tLbrdbEASrzgksyeYJId3d3el0uqenRxRFAAAA4KdfIIRgjD9+/Pj8+fOpqSndslofEIQwHA6Pjo4mEon//qmFhYXHjx8vLi4ihBgDEnp7e9l8E0Jo165dQ0NDd+/eDYVC2/qsJElDQ0OEkKWlpa2tLZamxAhQo9EIBoOjo6MXL17csZLe3l5FUT59+lQul5l5JRaAVFWNRqN37tw5ceKEQVWRSOTw4cOFQuHbt2+iKLYCIISQLMu3b99OJpOmKAwEAgcPHszn8+vr6wzsiG6UQQhxuVyXLl0aGBgwUW0sFstkMl6v90fo1KyAMMYDAwPnzp0zXfPg4GAqlWo0Gk0MiBAiy/L58+edTqf5Aa4onj59OhaLYYybFRCEMBaL0fNxBw4cSCQStN0QRUBut3t4eJjq6U+dOiVJElVPRBFQIBDo6+ujCqirqyscDlONGykC2lYyYSR6pBoQQapHZwAoHo/TuARYAOrs7GQASFEUqn6aIiBJkhgA6ujooFpUo6iaTa7koFwnaoWadLNe81tbWwzoaJrWrICWl5cZAFpbW6OabVAEtLi4yABQsVjUNK0pAWWzWQaAcrlcswKanZ1VVZUqHYRQEwOq1Wpv3ryhCmh6erpcLjdrNl+v1ycnJ+l5UELI27dvv3//3qxxEADgy5cvExMT9Mznw4cPtFtAdAPFarU6Pj5eKpVM17yxsfHy5cvV1VXazXu62gVBKBQKT58+rdVqJqrFGL948eLdu3dU8/g/H4FBUaJYLAqC0NPTY9brMD4+PjY25mDSracOCABACJmZmXE6nUePHjWu8NWrV48ePSKEsGlAs7Agfd5nenq6Wq0mk0kjDzY2NvbkyRMIIbP2PLvhBUEQ8vl8NpuNx+M+n29bzhVjvLKycv/+/YmJCcazQuwA6YzW1tYmJyf1SY+2trZ/rRk1Go1SqfT69esHDx4UCgVmNaa/zZ/9ABUhRFXVYDB48uTJeDweiUQkSfL7/T9MA2NcqVTK5fLy8vL8/PzU1FSxWHS5XJaM4wGr9sUwxqqqer3eUCgkSZJuUBBCfTRvc3OzXC6vrKxUKhWn02nhCJ5lM4oQQo/HgxD6+vXr58+fHf8sDOp+HQDg8XgclorFU676dKLlo6yWRdItIBwQB8QBcUCtfosRQjRNQwhhjPUC4w46WXryBSHU1zgEQWBz99EFhDGu1+t+v//48ePxeFxRlD179ng8nh0Efgiher2+vr6ur3HMzMysrq7uTJVdACGEurq6Ll++nEgkPB7Pj9jPoDHqOxyqqubz+WfPnuVyuV9XPeyeagAAAoHArVu3BgcHab8CuVzu4cOHpVKJUnfA5GweY+xyuc6cOXPv3r1IJMLAR8iyPDw8XK/Xi8Wiqqqmm5KZgBBC7e3tN27cuHbtGuPVpf7+/lAoNDs7W61WzfVKpgHSSzw3b95MpVKW3MfRaDQSiczNzVUqFRMZmQOIEOL1eq9fv3727FlL1t50URRFluX5+flqtWpWEGAOIFEUU6nUlStXLKSjy759+xwOx9zcnKZpphzGHMzhcDiTydgk9r1w4YIp7RPTAAmCkMlk2FeLf/tIEKbTab/fbwtAhJBoNGrutpNx6e7uPnTokC1eMU3T0um0DZPMkZER6wERQnw+n/FFSxpy7Nix3bt3WwwIIcRgIWnHkkwmjecfRgGx7DtuV/r6+iwGhDHev3+/bQF1dnYaH6E2CkiWZdsC2rt3r8WAHA5HW1ubbQGZcjajgOwTH/4qNko1Wlg4IA6IA+KAOKBWBUQIsfNojyliKIoRRfH9+/dut9umf3wzpoUNNQ4BAJubmwz+ic+OxefzWWlBhJD29nbug7iT5sIBcUAcEAfEAXFAHBAHxOVn+QMrmWpuPZx12gAAAABJRU5ErkJggg==";
_converse.log = function (txt, level) {
var logger;
if (_.isUndefined(console) || _.isUndefined(console.log)) {
logger = { log: _.noop, error: _.noop };
} else {
logger = console;
}
if (level === Strophe.LogLevel.ERROR) {
logger.log('ERROR: '+txt);
} else if (level === Strophe.LogLevel.WARN) {
logger.log('WARNING: '+txt);
} else if (level === Strophe.LogLevel.FATAL) {
logger.log('FATAL: '+txt);
} else if (_converse.debug) {
if (level === Strophe.LogLevel.DEBUG) {
logger.log('DEBUG: '+txt);
} else if (level === Strophe.LogLevel.INFO) {
logger.log('INFO: '+txt);
}
}
};
_converse.initialize = function (settings, callback) {
"use strict";
settings = !_.isUndefined(settings) ? settings : {};
var init_deferred = new $.Deferred();
if (!_.isUndefined(_converse.chatboxes)) {
// Looks like _converse.initialized was called again without logging
// out or disconnecting in the previous session.
// This happens in tests. We therefore first clean up.
_converse.connection.reset();
_converse.off();
_converse.stopListening();
_converse._tearDown();
}
var unloadevent;
if ('onpagehide' in window) {
// Pagehide gets thrown in more cases than unload. Specifically it
// gets thrown when the page is cached and not just
// closed/destroyed. It's the only viable event on mobile Safari.
// https://www.webkit.org/blog/516/webkit-page-cache-ii-the-unload-event/
unloadevent = 'pagehide';
} else if ('onbeforeunload' in window) {
unloadevent = 'beforeunload';
} else if ('onunload' in window) {
unloadevent = 'unload';
}
// Logging
Strophe.log = function (level, msg) { _converse.log(level+' '+msg, level); };
Strophe.error = function (msg) { _converse.log(msg, Strophe.LogLevel.ERROR); };
// Add Strophe Namespaces
Strophe.addNamespace('CARBONS', 'urn:xmpp:carbons:2');
Strophe.addNamespace('CHATSTATES', 'http://jabber.org/protocol/chatstates');
Strophe.addNamespace('CSI', 'urn:xmpp:csi:0');
Strophe.addNamespace('DELAY', 'urn:xmpp:delay');
Strophe.addNamespace('HINTS', 'urn:xmpp:hints');
Strophe.addNamespace('MAM', 'urn:xmpp:mam:2');
Strophe.addNamespace('NICK', 'http://jabber.org/protocol/nick');
Strophe.addNamespace('PUBSUB', 'http://jabber.org/protocol/pubsub');
Strophe.addNamespace('ROSTERX', 'http://jabber.org/protocol/rosterx');
Strophe.addNamespace('RSM', 'http://jabber.org/protocol/rsm');
Strophe.addNamespace('XFORM', 'jabber:x:data');
// Instance level constants
this.TIMEOUTS = { // Set as module attr so that we can override in tests.
'PAUSED': 10000,
'INACTIVE': 90000
};
// Internationalization
this.locale = utils.getLocale(settings.i18n, utils.isConverseLocale);
if (!moment.locale) {
//moment.lang is deprecated after 2.8.1, use moment.locale instead
moment.locale = moment.lang;
}
moment.locale(utils.getLocale(settings.i18n, utils.isMomentLocale));
var __ = _converse.__ = utils.__.bind(_converse);
_converse.___ = utils.___;
// XEP-0085 Chat states
// http://xmpp.org/extensions/xep-0085.html
this.INACTIVE = 'inactive';
this.ACTIVE = 'active';
this.COMPOSING = 'composing';
this.PAUSED = 'paused';
this.GONE = 'gone';
// Default configuration values
// ----------------------------
this.default_settings = {
allow_contact_requests: true,
allow_non_roster_messaging: false,
animate: true,
authentication: 'login', // Available values are "login", "prebind", "anonymous" and "external".
auto_away: 0, // Seconds after which user status is set to 'away'
auto_login: false, // Currently only used in connection with anonymous login
auto_reconnect: false,
auto_subscribe: false,
auto_xa: 0, // Seconds after which user status is set to 'xa'
blacklisted_plugins: [],
bosh_service_url: undefined,
connection_options: {},
credentials_url: null, // URL from where login credentials can be fetched
csi_waiting_time: 0, // Support for XEP-0352. Seconds before client is considered idle and CSI is sent out.
debug: false,
default_state: 'online',
expose_rid_and_sid: false,
filter_by_resource: false,
forward_messages: false,
hide_offline_users: false,
include_offline_state: false,
jid: undefined,
keepalive: true,
locked_domain: undefined,
message_carbons: true,
message_storage: 'session',
password: undefined,
prebind_url: null,
priority: 0,
registration_domain: '',
rid: undefined,
roster_groups: true,
show_only_online_users: false,
show_send_button: false,
sid: undefined,
storage: 'session',
strict_plugin_dependencies: false,
synchronize_availability: true,
websocket_url: undefined,
whitelisted_plugins: [],
xhr_custom_status: false,
xhr_custom_status_url: '',
};
_.assignIn(this, this.default_settings);
// Allow only whitelisted configuration attributes to be overwritten
_.assignIn(this, _.pick(settings, _.keys(this.default_settings)));
if (this.authentication === _converse.ANONYMOUS) {
if (this.auto_login && !this.jid) {
throw new Error("Config Error: you need to provide the server's " +
"domain via the 'jid' option when using anonymous " +
"authentication with auto_login.");
}
}
$.fx.off = !this.animate;
// Module-level variables
// ----------------------
this.callback = callback || _.noop;
/* When reloading the page:
* For new sessions, we need to send out a presence stanza to notify
* the server/network that we're online.
* When re-attaching to an existing session (e.g. via the keepalive
* option), we don't need to again send out a presence stanza, because
* it's as if "we never left" (see onConnectStatusChanged).
* https://github.com/jcbrand/converse.js/issues/521
*/
this.send_initial_presence = true;
this.msg_counter = 0;
this.user_settings = settings; // Save the user settings so that they can be used by plugins
// Module-level functions
// ----------------------
this.getViewForChatBox = function (chatbox) {
if (!chatbox) { return; }
return _converse.chatboxviews.get(chatbox.get('id'));
};
this.generateResource = function () {
return '/converse.js-' + Math.floor(Math.random()*139749825).toString();
};
this.sendCSI = function (stat) {
/* Send out a Chat Status Notification (XEP-0352)
*
* Parameters:
* (String) stat: The user's chat status
*/
/* Send out a Chat Status Notification (XEP-0352) */
// XXX if (converse.features[Strophe.NS.CSI] || true) {
_converse.connection.send($build(stat, {xmlns: Strophe.NS.CSI}));
_converse.inactive = (stat === _converse.INACTIVE) ? true : false;
};
this.onUserActivity = function () {
/* Resets counters and flags relating to CSI and auto_away/auto_xa */
if (_converse.idle_seconds > 0) {
_converse.idle_seconds = 0;
}
if (!_converse.connection.authenticated) {
// We can't send out any stanzas when there's no authenticated connection.
// converse can happen when the connection reconnects.
return;
}
if (_converse.inactive) {
_converse.sendCSI(_converse.ACTIVE);
}
if (_converse.auto_changed_status === true) {
_converse.auto_changed_status = false;
// XXX: we should really remember the original state here, and
// then set it back to that...
_converse.xmppstatus.setStatus(_converse.default_state);
}
};
this.onEverySecond = function () {
/* An interval handler running every second.
* Used for CSI and the auto_away and auto_xa features.
*/
if (!_converse.connection.authenticated) {
// We can't send out any stanzas when there's no authenticated connection.
// This can happen when the connection reconnects.
return;
}
var stat = _converse.xmppstatus.getStatus();
_converse.idle_seconds++;
if (_converse.csi_waiting_time > 0 &&
_converse.idle_seconds > _converse.csi_waiting_time &&
!_converse.inactive) {
_converse.sendCSI(_converse.INACTIVE);
}
if (_converse.auto_away > 0 &&
_converse.idle_seconds > _converse.auto_away &&
stat !== 'away' && stat !== 'xa' && stat !== 'dnd') {
_converse.auto_changed_status = true;
_converse.xmppstatus.setStatus('away');
} else if (_converse.auto_xa > 0 &&
_converse.idle_seconds > _converse.auto_xa &&
stat !== 'xa' && stat !== 'dnd') {
_converse.auto_changed_status = true;
_converse.xmppstatus.setStatus('xa');
}
};
this.registerIntervalHandler = function () {
/* Set an interval of one second and register a handler for it.
* Required for the auto_away, auto_xa and csi_waiting_time features.
*/
if (_converse.auto_away < 1 && _converse.auto_xa < 1 && _converse.csi_waiting_time < 1) {
// Waiting time of less then one second means features aren't used.
return;
}
_converse.idle_seconds = 0;
_converse.auto_changed_status = false; // Was the user's status changed by _converse.js?
window.addEventListener('click', _converse.onUserActivity);
window.addEventListener('focus', _converse.onUserActivity);
window.addEventListener('keypress', _converse.onUserActivity);
window.addEventListener('mousemove', _converse.onUserActivity);
window.addEventListener(unloadevent, _converse.onUserActivity);
_converse.everySecondTrigger = window.setInterval(_converse.onEverySecond, 1000);
};
this.giveFeedback = function (subject, klass, message) {
var els = document.querySelectorAll('.conn-feedback');
_.forEach(els, function (el) {
el.classList.add('conn-feedback');
el.textContent = subject;
if (klass) {
el.classList.add(klass);
} else {
el.classList.remove('error');
}
});
_converse.emit('feedback', {
'klass': klass,
'message': message,
'subject': subject
});
};
this.rejectPresenceSubscription = function (jid, message) {
/* Reject or cancel another user's subscription to our presence updates.
*
* Parameters:
* (String) jid - The Jabber ID of the user whose subscription
* is being canceled.
* (String) message - An optional message to the user
*/
var pres = $pres({to: jid, type: "unsubscribed"});
if (message && message !== "") { pres.c("status").t(message); }
_converse.connection.send(pres);
};
this.reconnect = _.debounce(function () {
_converse.log('RECONNECTING');
_converse.log('The connection has dropped, attempting to reconnect.');
_converse.giveFeedback(
__("Reconnecting"),
'warn',
__('The connection has dropped, attempting to reconnect.')
);
_converse.connection.reconnecting = true;
_converse._tearDown();
_converse.logIn(null, true);
}, 3000, {'leading': true});
this.disconnect = function () {
_converse.log('DISCONNECTED');
delete _converse.connection.reconnecting;
_converse.connection.reset();
_converse._tearDown();
_converse.chatboxviews.closeAllChatBoxes();
_converse.emit('disconnected');
};
this.onDisconnected = function () {
/* Gets called once strophe's status reaches Strophe.Status.DISCONNECTED.
* Will either start a teardown process for converse.js or attempt
* to reconnect.
*/
if (_converse.disconnection_cause === Strophe.Status.AUTHFAIL) {
if (_converse.credentials_url && _converse.auto_reconnect) {
/* In this case, we reconnect, because we might be receiving
* expirable tokens from the credentials_url.
*/
_converse.emit('will-reconnect');
return _converse.reconnect();
} else {
return _converse.disconnect();
}
} else if (_converse.disconnection_cause === _converse.LOGOUT ||
_converse.disconnection_reason === "host-unknown" ||
!_converse.auto_reconnect) {
return _converse.disconnect();
}
_converse.emit('will-reconnect');
_converse.reconnect();
};
this.setDisconnectionCause = function (cause, reason, override) {
/* Used to keep track of why we got disconnected, so that we can
* decide on what the next appropriate action is (in onDisconnected)
*/
if (_.isUndefined(cause)) {
delete _converse.disconnection_cause;
delete _converse.disconnection_reason;
} else if (_.isUndefined(_converse.disconnection_cause) || override) {
_converse.disconnection_cause = cause;
_converse.disconnection_reason = reason;
}
};
this.onConnectStatusChanged = function (status, condition) {
/* Callback method called by Strophe as the Strophe.Connection goes
* through various states while establishing or tearing down a
* connection.
*/
_converse.log("Status changed to: "+PRETTY_CONNECTION_STATUS[status]);
if (status === Strophe.Status.CONNECTED || status === Strophe.Status.ATTACHED) {
// By default we always want to send out an initial presence stanza.
_converse.send_initial_presence = true;
_converse.setDisconnectionCause();
if (_converse.connection.reconnecting) {
_converse.log(status === Strophe.Status.CONNECTED ? 'Reconnected' : 'Reattached');
_converse.onConnected(true);
} else {
_converse.log(status === Strophe.Status.CONNECTED ? 'Connected' : 'Attached');
if (_converse.connection.restored) {
// No need to send an initial presence stanza when
// we're restoring an existing session.
_converse.send_initial_presence = false;
}
_converse.onConnected();
}
} else if (status === Strophe.Status.DISCONNECTED) {
_converse.setDisconnectionCause(status, condition);
_converse.onDisconnected();
} else if (status === Strophe.Status.ERROR) {
_converse.giveFeedback(
__('Connection error'), 'error',
__('An error occurred while connecting to the chat server.')
);
} else if (status === Strophe.Status.CONNECTING) {
_converse.giveFeedback(__('Connecting'));
} else if (status === Strophe.Status.AUTHENTICATING) {
_converse.giveFeedback(__('Authenticating'));
} else if (status === Strophe.Status.AUTHFAIL) {
_converse.giveFeedback(__('Authentication Failed'), 'error');
_converse.setDisconnectionCause(status, condition, true);
_converse.onDisconnected();
} else if (status === Strophe.Status.CONNFAIL) {
_converse.giveFeedback(
__('Connection failed'), 'error',
__('An error occurred while connecting to the chat server: '+condition)
);
_converse.setDisconnectionCause(status, condition);
} else if (status === Strophe.Status.DISCONNECTING) {
_converse.setDisconnectionCause(status, condition);
}
};
this.incrementMsgCounter = function () {
this.msg_counter += 1;
var unreadMsgCount = this.msg_counter;
if (document.title.search(/^Messages \(\d+\) /) === -1) {
document.title = "Messages (" + unreadMsgCount + ") " + document.title;
} else {
document.title = document.title.replace(/^Messages \(\d+\) /, "Messages (" + unreadMsgCount + ") ");
}
};
this.clearMsgCounter = function () {
this.msg_counter = 0;
if (document.title.search(/^Messages \(\d+\) /) !== -1) {
document.title = document.title.replace(/^Messages \(\d+\) /, "");
}
};
this.initStatus = function () {
var deferred = new $.Deferred();
this.xmppstatus = new this.XMPPStatus();
var id = b64_sha1('converse.xmppstatus-'+_converse.bare_jid);
this.xmppstatus.id = id; // Appears to be necessary for backbone.browserStorage
this.xmppstatus.browserStorage = new Backbone.BrowserStorage[_converse.storage](id);
this.xmppstatus.fetch({
success: deferred.resolve,
error: deferred.resolve
});
_converse.emit('statusInitialized');
return deferred.promise();
};
this.initSession = function () {
this.session = new this.Session();
var id = b64_sha1('converse.bosh-session');
this.session.id = id; // Appears to be necessary for backbone.browserStorage
this.session.browserStorage = new Backbone.BrowserStorage[_converse.storage](id);
this.session.fetch();
};
this.clearSession = function () {
if (!_.isUndefined(this.roster)) {
this.roster.browserStorage._clear();
}
if (!_.isUndefined(this.session) && this.session.browserStorage) {
this.session.browserStorage._clear();
}
};
this.logOut = function () {
_converse.chatboxviews.closeAllChatBoxes();
_converse.clearSession();
_converse.setDisconnectionCause(_converse.LOGOUT, undefined, true);
if (!_.isUndefined(_converse.connection)) {
_converse.connection.disconnect();
} else {
_converse._tearDown();
}
_converse.emit('logout');
};
this.saveWindowState = function (ev, hidden) {
// XXX: eventually we should be able to just use
// document.visibilityState (when we drop support for older
// browsers).
var state;
var v = "visible", h = "hidden",
event_map = {
'focus': v,
'focusin': v,
'pageshow': v,
'blur': h,
'focusout': h,
'pagehide': h
};
ev = ev || document.createEvent('Events');
if (ev.type in event_map) {
state = event_map[ev.type];
} else {
state = document[hidden] ? "hidden" : "visible";
}
if (state === 'visible') {
_converse.clearMsgCounter();
}
_converse.windowState = state;
_converse.emit('windowStateChanged', {state: state})
};
this.registerGlobalEventHandlers = function () {
// Taken from:
// http://stackoverflow.com/questions/1060008/is-there-a-way-to-detect-if-a-browser-window-is-not-currently-active
var hidden = "hidden";
// Standards:
if (hidden in document) {
document.addEventListener("visibilitychange", _.partial(_converse.saveWindowState, _, hidden));
} else if ((hidden = "mozHidden") in document) {
document.addEventListener("mozvisibilitychange", _.partial(_converse.saveWindowState, _, hidden));
} else if ((hidden = "webkitHidden") in document) {
document.addEventListener("webkitvisibilitychange", _.partial(_converse.saveWindowState, _, hidden));
} else if ((hidden = "msHidden") in document) {
document.addEventListener("msvisibilitychange", _.partial(_converse.saveWindowState, _, hidden));
} else if ("onfocusin" in document) {
// IE 9 and lower:
document.onfocusin = document.onfocusout = _.partial(_converse.saveWindowState, _, hidden);
} else {
// All others:
window.onpageshow = window.onpagehide = window.onfocus = window.onblur = _.partial(_converse.saveWindowState, _, hidden);
}
// set the initial state (but only if browser supports the Page Visibility API)
if( document[hidden] !== undefined ) {
_.partial(_converse.saveWindowState, _, hidden)({type: document[hidden] ? "blur" : "focus"});
}
};
this.enableCarbons = function () {
/* Ask the XMPP server to enable Message Carbons
* See XEP-0280 https://xmpp.org/extensions/xep-0280.html#enabling
*/
if (!this.message_carbons || this.session.get('carbons_enabled')) {
return;
}
var carbons_iq = new Strophe.Builder('iq', {
from: this.connection.jid,
id: 'enablecarbons',
type: 'set'
})
.c('enable', {xmlns: Strophe.NS.CARBONS});
this.connection.addHandler(function (iq) {
if (iq.querySelectorAll('error').length > 0) {
_converse.log(
'An error occured while trying to enable message carbons.',
Strophe.LogLevel.ERROR);
} else {
this.session.save({carbons_enabled: true});
_converse.log('Message carbons have been enabled.');
}
}.bind(this), null, "iq", null, "enablecarbons");
this.connection.send(carbons_iq);
};
this.initRoster = function () {
/* Initialize the Bakcbone collections that represent the contats
* roster and the roster groups.
*/
_converse.roster = new _converse.RosterContacts();
_converse.roster.browserStorage = new Backbone.BrowserStorage.session(
b64_sha1('converse.contacts-'+_converse.bare_jid));
_converse.rostergroups = new _converse.RosterGroups();
_converse.rostergroups.browserStorage = new Backbone.BrowserStorage.session(
b64_sha1('converse.roster.groups'+_converse.bare_jid));
_converse.emit('rosterInitialized');
};
this.populateRoster = function () {
/* Fetch all the roster groups, and then the roster contacts.
* Emit an event after fetching is done in each case.
*/
_converse.rostergroups.fetchRosterGroups().then(function () {
_converse.emit('rosterGroupsFetched');
_converse.roster.fetchRosterContacts().then(function () {
_converse.emit('rosterContactsFetched');
_converse.sendInitialPresence();
});
});
};
this.unregisterPresenceHandler = function () {
if (!_.isUndefined(_converse.presence_ref)) {
_converse.connection.deleteHandler(_converse.presence_ref);
delete _converse.presence_ref;
}
};
this.registerPresenceHandler = function () {
_converse.unregisterPresenceHandler();
_converse.presence_ref = _converse.connection.addHandler(
function (presence) {
_converse.roster.presenceHandler(presence);
return true;
}, null, 'presence', null);
};
this.sendInitialPresence = function () {
if (_converse.send_initial_presence) {
_converse.xmppstatus.sendPresence();
}
};
this.onStatusInitialized = function (reconnecting) {
/* Continue with session establishment (e.g. fetching chat boxes,
* populating the roster etc.) necessary once the connection has
* been established.
*/
if (reconnecting) {
// No need to recreate the roster, otherwise we lose our
// cached data. However we still emit an event, to give
// event handlers a chance to register views for the
// roster and its groups, before we start populating.
_converse.emit('rosterReadyAfterReconnection');
} else {
_converse.registerIntervalHandler();
_converse.initRoster();
}
// First set up chat boxes, before populating the roster, so that
// the controlbox is properly set up and ready for the rosterview.
_converse.roster.onConnected();
_converse.chatboxes.onConnected();
_converse.populateRoster();
_converse.registerPresenceHandler();
_converse.giveFeedback(__('Contacts'));
if (reconnecting) {
_converse.xmppstatus.sendPresence();
} else {
init_deferred.resolve();
_converse.emit('initialized');
}
};
this.setUserJid = function () {
_converse.jid = _converse.connection.jid;
_converse.bare_jid = Strophe.getBareJidFromJid(_converse.connection.jid);
_converse.resource = Strophe.getResourceFromJid(_converse.connection.jid);
_converse.domain = Strophe.getDomainFromJid(_converse.connection.jid);
};
this.onConnected = function (reconnecting) {
/* Called as soon as a new connection has been established, either
* by logging in or by attaching to an existing BOSH session.
*/
// Solves problem of returned PubSub BOSH response not received
// by browser.
_converse.connection.flush();
_converse.setUserJid();
_converse.initSession();
_converse.enableCarbons();
// If there's no xmppstatus obj, then we were never connected to
// begin with, so we set reconnecting to false.
reconnecting = _.isUndefined(_converse.xmppstatus) ? false : reconnecting;
if (reconnecting) {
_converse.onStatusInitialized(true);
_converse.emit('reconnected');
} else {
// There might be some open chat boxes. We don't
// know whether these boxes are of the same account or not, so we
// close them now.
_converse.chatboxviews.closeAllChatBoxes();
_converse.features = new _converse.Features();
_converse.initStatus().done(_.partial(_converse.onStatusInitialized, false));
_converse.emit('connected');
}
};
this.RosterContact = Backbone.Model.extend({
defaults: {
'bookmarked': false,
'chat_state': undefined,
'chat_status': 'offline',
'groups': [],
'image': DEFAULT_IMAGE,
'image_type': DEFAULT_IMAGE_TYPE,
'num_unread': 0,
'status': '',
},
initialize: function (attributes) {
var jid = attributes.jid;
var bare_jid = Strophe.getBareJidFromJid(jid).toLowerCase();
var resource = Strophe.getResourceFromJid(jid);
attributes.jid = bare_jid;
this.set(_.assignIn({
'id': bare_jid,
'jid': bare_jid,
'fullname': bare_jid,
'user_id': Strophe.getNodeFromJid(jid),
'resources': resource ? {resource :0} : {},
}, attributes));
this.on('destroy', function () { this.removeFromRoster(); }.bind(this));
this.on('change:chat_status', function (item) {
_converse.emit('contactStatusChanged', item.attributes);
});
},
subscribe: function (message) {
/* Send a presence subscription request to this roster contact
*
* Parameters:
* (String) message - An optional message to explain the
* reason for the subscription request.
*/
this.save('ask', "subscribe"); // ask === 'subscribe' Means we have ask to subscribe to them.
var pres = $pres({to: this.get('jid'), type: "subscribe"});
if (message && message !== "") {
pres.c("status").t(message).up();
}
var nick = _converse.xmppstatus.get('fullname');
if (nick && nick !== "") {
pres.c('nick', {'xmlns': Strophe.NS.NICK}).t(nick).up();
}
_converse.connection.send(pres);
return this;
},
ackSubscribe: function () {
/* Upon receiving the presence stanza of type "subscribed",
* the user SHOULD acknowledge receipt of that subscription
* state notification by sending a presence stanza of type
* "subscribe" to the contact
*/
_converse.connection.send($pres({
'type': 'subscribe',
'to': this.get('jid')
}));
},
ackUnsubscribe: function () {
/* Upon receiving the presence stanza of type "unsubscribed",
* the user SHOULD acknowledge receipt of that subscription state
* notification by sending a presence stanza of type "unsubscribe"
* this step lets the user's server know that it MUST no longer
* send notification of the subscription state change to the user.
* Parameters:
* (String) jid - The Jabber ID of the user who is unsubscribing
*/
_converse.connection.send($pres({'type': 'unsubscribe', 'to': this.get('jid')}));
this.destroy(); // Will cause removeFromRoster to be called.
},
unauthorize: function (message) {
/* Unauthorize this contact's presence subscription
* Parameters:
* (String) message - Optional message to send to the person being unauthorized
*/
_converse.rejectPresenceSubscription(this.get('jid'), message);
return this;
},
authorize: function (message) {
/* Authorize presence subscription
* Parameters:
* (String) message - Optional message to send to the person being authorized
*/
var pres = $pres({to: this.get('jid'), type: "subscribed"});
if (message && message !== "") {
pres.c("status").t(message);
}
_converse.connection.send(pres);
return this;
},
addResource: function (presence) {
/* Adds a new resource and it's associated attributes as taken
* from the passed in presence stanza.
*
* Also updates the contact's chat_status if the presence has
* higher priority (and is newer).
*/
var jid = presence.getAttribute('from'),
chat_status = _.propertyOf(presence.querySelector('show'))('textContent') || 'online',
resource = Strophe.getResourceFromJid(jid),
priority = _.propertyOf(presence.querySelector('priority'))('textContent') || 0,
delay = presence.querySelector('delay[xmlns="'+Strophe.NS.DELAY+'"]'),
timestamp = _.isNull(delay) ? moment().format() : moment(delay.getAttribute('stamp')).format();
priority = _.isNaN(parseInt(priority, 10)) ? 0 : parseInt(priority, 10);
var resources = _.isObject(this.get('resources')) ? this.get('resources') : {};
resources[resource] = {
'priority': priority,
'status': chat_status,
'timestamp': timestamp
};
var changed = {'resources': resources};
var hpr = this.getHighestPriorityResource();
if (priority == hpr.priority && timestamp == hpr.timestamp) {
// Only set the chat status if this is the newest resource
// with the highest priority
changed.chat_status = chat_status;
}
this.save(changed);
return resources;
},
removeResource: function (resource) {
/* Remove the passed in resource from the contact's resources map.
*
* Also recomputes the chat_status given that there's one less
* resource.
*/
var resources = this.get('resources');
if (!_.isObject(resources)) {
resources = {};
} else {
delete resources[resource];
}
this.save({
'resources': resources,
'chat_status': _.propertyOf(
this.getHighestPriorityResource())('status') || 'offline'
});
},
getHighestPriorityResource: function () {
/* Return the resource with the highest priority.
*
* If multiple resources have the same priority, take the
* newest one.
*/
var resources = this.get('resources');
if (_.isObject(resources) && _.size(resources)) {
var val = _.flow(
_.values,
_.partial(_.sortBy, _, ['priority', 'timestamp']),
_.reverse
)(resources)[0];
if (!_.isUndefined(val)) {
return val;
}
}
},
removeFromRoster: function (callback) {
/* Instruct the XMPP server to remove this contact from our roster
* Parameters:
* (Function) callback
*/
var iq = $iq({type: 'set'})
.c('query', {xmlns: Strophe.NS.ROSTER})
.c('item', {jid: this.get('jid'), subscription: "remove"});
_converse.connection.sendIQ(iq, callback, callback);
return this;
}
});
this.RosterContacts = Backbone.Collection.extend({
model: _converse.RosterContact,
comparator: function (contact1, contact2) {
var name1, name2;
var status1 = contact1.get('chat_status') || 'offline';
var status2 = contact2.get('chat_status') || 'offline';
if (_converse.STATUS_WEIGHTS[status1] === _converse.STATUS_WEIGHTS[status2]) {
name1 = contact1.get('fullname').toLowerCase();
name2 = contact2.get('fullname').toLowerCase();
return name1 < name2 ? -1 : (name1 > name2? 1 : 0);
} else {
return _converse.STATUS_WEIGHTS[status1] < _converse.STATUS_WEIGHTS[status2] ? -1 : 1;
}
},
onConnected: function () {
/* Called as soon as the connection has been established
* (either after initial login, or after reconnection).
*
* Use the opportunity to register stanza handlers.
*/
this.registerRosterHandler();
this.registerRosterXHandler();
},
registerRosterHandler: function () {
/* Register a handler for roster IQ "set" stanzas, which update
* roster contacts.
*/
_converse.connection.addHandler(
_converse.roster.onRosterPush.bind(_converse.roster),
Strophe.NS.ROSTER, 'iq', "set"
);
},
registerRosterXHandler: function () {
/* Register a handler for RosterX message stanzas, which are
* used to suggest roster contacts to a user.
*/
var t = 0;
_converse.connection.addHandler(
function (msg) {
window.setTimeout(
function () {
_converse.connection.flush();
_converse.roster.subscribeToSuggestedItems.bind(_converse.roster)(msg);
}, t);
t += $(msg).find('item').length*250;
return true;
},
Strophe.NS.ROSTERX, 'message', null
);
},
fetchRosterContacts: function () {
/* Fetches the roster contacts, first by trying the
* sessionStorage cache, and if that's empty, then by querying
* the XMPP server.
*
* Returns a promise which resolves once the contacts have been
* fetched.
*/
var deferred = new $.Deferred();
this.fetch({
add: true,
success: function (collection) {
if (collection.length === 0) {
/* We don't have any roster contacts stored in sessionStorage,
* so lets fetch the roster from the XMPP server. We pass in
* 'sendPresence' as callback method, because after initially
* fetching the roster we are ready to receive presence
* updates from our contacts.
*/
_converse.send_initial_presence = true;
_converse.roster.fetchFromServer(deferred.resolve);
} else {
_converse.emit('cachedRoster', collection);
deferred.resolve();
}
}
});
return deferred.promise();
},
subscribeToSuggestedItems: function (msg) {
_.each(msg.querySelectorAll('item'), function (item) {
if (item.getAttribute('action') === 'add') {
_converse.roster.addAndSubscribe(
item.getAttribute('jid'),
null,
_converse.xmppstatus.get('fullname')
);
}
});
return true;
},
isSelf: function (jid) {
return utils.isSameBareJID(jid, _converse.connection.jid);
},
addAndSubscribe: function (jid, name, groups, message, attributes) {
/* Add a roster contact and then once we have confirmation from
* the XMPP server we subscribe to that contact's presence updates.
* Parameters:
* (String) jid - The Jabber ID of the user being added and subscribed to.
* (String) name - The name of that user
* (Array of Strings) groups - Any roster groups the user might belong to
* (String) message - An optional message to explain the
* reason for the subscription request.
* (Object) attributes - Any additional attributes to be stored on the user's model.
*/
this.addContact(jid, name, groups, attributes).done(function (contact) {
if (contact instanceof _converse.RosterContact) {
contact.subscribe(message);
}
});
},
sendContactAddIQ: function (jid, name, groups, callback, errback) {
/* Send an IQ stanza to the XMPP server to add a new roster contact.
*
* Parameters:
* (String) jid - The Jabber ID of the user being added
* (String) name - The name of that user
* (Array of Strings) groups - Any roster groups the user might belong to
* (Function) callback - A function to call once the IQ is returned
* (Function) errback - A function to call if an error occured
*/
name = _.isEmpty(name)? jid: name;
var iq = $iq({type: 'set'})
.c('query', {xmlns: Strophe.NS.ROSTER})
.c('item', { jid: jid, name: name });
_.each(groups, function (group) { iq.c('group').t(group).up(); });
_converse.connection.sendIQ(iq, callback, errback);
},
addContact: function (jid, name, groups, attributes) {
/* Adds a RosterContact instance to _converse.roster and
* registers the contact on the XMPP server.
* Returns a promise which is resolved once the XMPP server has
* responded.
*
* Parameters:
* (String) jid - The Jabber ID of the user being added and subscribed to.
* (String) name - The name of that user
* (Array of Strings) groups - Any roster groups the user might belong to
* (Object) attributes - Any additional attributes to be stored on the user's model.
*/
var deferred = new $.Deferred();
groups = groups || [];
name = _.isEmpty(name)? jid: name;
this.sendContactAddIQ(jid, name, groups,
function () {
var contact = this.create(_.assignIn({
ask: undefined,
fullname: name,
groups: groups,
jid: jid,
requesting: false,
subscription: 'none'
}, attributes), {sort: false});
deferred.resolve(contact);
}.bind(this),
function (err) {
alert(__("Sorry, there was an error while trying to add "+name+" as a contact."));
_converse.log(err, Strophe.LogLevel.ERROR);
deferred.resolve(err);
}
);
return deferred.promise();
},
subscribeBack: function (bare_jid) {
var contact = this.get(bare_jid);
if (contact instanceof _converse.RosterContact) {
contact.authorize().subscribe();
} else {
// Can happen when a subscription is retried or roster was deleted
this.addContact(bare_jid, '', [], { 'subscription': 'from' }).done(function (contact) {
if (contact instanceof _converse.RosterContact) {
contact.authorize().subscribe();
}
});
}
},
getNumOnlineContacts: function () {
var count = 0,
ignored = ['offline', 'unavailable'],
models = this.models,
models_length = models.length,
i;
if (_converse.show_only_online_users) {
ignored = _.union(ignored, ['dnd', 'xa', 'away']);
}
for (i=0; i tag to be present.
document.querySelector('body').appendChild(el);
}
el.innerHTML = '';
this.setElement(el, false);
} else {
this.setElement(_.result(this, 'el'), false);
}
},
onChatBoxAdded: function (item) {
// Views aren't created here, since the core code doesn't
// contain any views. Instead, they're created in overrides in
// plugins, such as in converse-chatview.js and converse-muc.js
return this.get(item.get('id'));
},
removeChat: function (item) {
this.remove(item.get('id'));
},
closeAllChatBoxes: function () {
/* This method gets overridden in src/converse-controlbox.js if
* the controlbox plugin is active.
*/
this.each(function (view) { view.close(); });
return this;
},
chatBoxMayBeShown: function (chatbox) {
return this.model.chatBoxMayBeShown(chatbox);
},
getChatBox: function (attrs, create) {
var chatbox = this.model.get(attrs.jid);
if (!chatbox && create) {
chatbox = this.model.create(attrs, {
'error': function (model, response) {
_converse.log(response.responseText);
}
});
}
return chatbox;
},
showChat: function (attrs) {
/* Find the chat box and show it (if it may be shown).
* If it doesn't exist, create it.
*/
var chatbox = this.getChatBox(attrs, true);
if (this.chatBoxMayBeShown(chatbox)) {
chatbox.trigger('show', true);
}
return chatbox;
}
});
this.XMPPStatus = Backbone.Model.extend({
initialize: function () {
this.set({
'status' : this.getStatus()
});
this.on('change', function (item) {
if (_.has(item.changed, 'status')) {
_converse.emit('statusChanged', this.get('status'));
}
if (_.has(item.changed, 'status_message')) {
_converse.emit('statusMessageChanged', this.get('status_message'));
}
}.bind(this));
},
constructPresence: function (type, status_message) {
var presence;
type = _.isString(type) ? type : (this.get('status') || _converse.default_state);
status_message = _.isString(status_message) ? status_message : undefined;
// Most of these presence types are actually not explicitly sent,
// but I add all of them here for reference and future proofing.
if ((type === 'unavailable') ||
(type === 'probe') ||
(type === 'error') ||
(type === 'unsubscribe') ||
(type === 'unsubscribed') ||
(type === 'subscribe') ||
(type === 'subscribed')) {
presence = $pres({'type': type});
} else if (type === 'offline') {
presence = $pres({'type': 'unavailable'});
} else if (type === 'online') {
presence = $pres();
} else {
presence = $pres().c('show').t(type).up();
}
if (status_message) {
presence.c('status').t(status_message).up();
}
presence.c('priority').t(
_.isNaN(Number(_converse.priority)) ? 0 : _converse.priority
);
return presence;
},
sendPresence: function (type, status_message) {
_converse.connection.send(this.constructPresence(type, status_message));
},
setStatus: function (value) {
this.sendPresence(value);
this.save({'status': value});
},
getStatus: function () {
return this.get('status') || _converse.default_state;
},
setStatusMessage: function (status_message) {
this.sendPresence(this.getStatus(), status_message);
var prev_status = this.get('status_message');
this.save({'status_message': status_message});
if (this.xhr_custom_status) {
var xhr = new XMLHttpRequest();
xhr.open('POST', this.xhr_custom_status_url, true);
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
xhr.send({'msg': status_message});
}
if (prev_status === status_message) {
this.trigger("update-status-ui", this);
}
}
});
this.Session = Backbone.Model; // General session settings to be saved to sessionStorage.
this.Feature = Backbone.Model;
this.Features = Backbone.Collection.extend({
/* Service Discovery
* -----------------
* This collection stores Feature Models, representing features
* provided by available XMPP entities (e.g. servers)
* See XEP-0030 for more details: http://xmpp.org/extensions/xep-0030.html
* All features are shown here: http://xmpp.org/registrar/disco-features.html
*/
model: _converse.Feature,
initialize: function () {
this.addClientIdentities().addClientFeatures();
this.browserStorage = new Backbone.BrowserStorage[_converse.storage](
b64_sha1('converse.features'+_converse.bare_jid)
);
this.on('add', this.onFeatureAdded, this);
if (this.browserStorage.records.length === 0) {
// browserStorage is empty, so we've likely never queried this
// domain for features yet
_converse.connection.disco.info(_converse.domain, null, this.onInfo.bind(this));
_converse.connection.disco.items(_converse.domain, null, this.onItems.bind(this));
} else {
this.fetch({add:true});
}
},
onFeatureAdded: function (feature) {
_converse.emit('serviceDiscovered', feature);
},
addClientIdentities: function () {
/* See http://xmpp.org/registrar/disco-categories.html
*/
_converse.connection.disco.addIdentity('client', 'web', 'Converse.js');
return this;
},
addClientFeatures: function () {
/* The strophe.disco.js plugin keeps a list of features which
* it will advertise to any #info queries made to it.
*
* See: http://xmpp.org/extensions/xep-0030.html#info
*/
_converse.connection.disco.addFeature(Strophe.NS.BOSH);
_converse.connection.disco.addFeature(Strophe.NS.CHATSTATES);
_converse.connection.disco.addFeature(Strophe.NS.DISCO_INFO);
_converse.connection.disco.addFeature(Strophe.NS.ROSTERX); // Limited support
if (_converse.message_carbons) {
_converse.connection.disco.addFeature(Strophe.NS.CARBONS);
}
return this;
},
onItems: function (stanza) {
var that = this;
_.each(stanza.querySelectorAll('query item'), function (item) {
_converse.connection.disco.info(
item.getAttribute('jid'),
null,
that.onInfo.bind(that));
});
},
onInfo: function (stanza) {
if ((sizzle('identity[category=server][type=im]', stanza).length === 0) &&
(sizzle('identity[category=conference][type=text]', stanza).length === 0)) {
// This isn't an IM server component
return;
}
var that = this;
_.forEach(stanza.querySelectorAll('feature'), function (feature) {
var namespace = feature.getAttribute('var');
that[namespace] = true;
that.create({
'var': namespace,
'from': stanza.getAttribute('from')
});
});
}
});
this.setUpXMLLogging = function () {
Strophe.log = function (level, msg) {
_converse.log(msg, level);
};
if (this.debug) {
this.connection.xmlInput = function (body) { _converse.log(body.outerHTML); };
this.connection.xmlOutput = function (body) { _converse.log(body.outerHTML); };
}
};
this.fetchLoginCredentials = function () {
var deferred = new $.Deferred();
var xhr = new XMLHttpRequest();
xhr.open('GET', _converse.credentials_url, true);
xhr.setRequestHeader('Accept', "application/json, text/javascript");
xhr.onload = function() {
if (xhr.status >= 200 && xhr.status < 400) {
var data = JSON.parse(xhr.responseText);
deferred.resolve({
'jid': data.jid,
'password': data.password
});
} else {
xhr.onerror();
}
};
xhr.onerror = function () {
delete _converse.connection;
_converse.emit('noResumeableSession');
deferred.reject(xhr.responseText);
};
xhr.send();
return deferred.promise();
};
this.startNewBOSHSession = function () {
var xhr = new XMLHttpRequest();
xhr.open('GET', _converse.prebind_url, true);
xhr.setRequestHeader('Accept', "application/json, text/javascript");
xhr.onload = function() {
if (xhr.status >= 200 && xhr.status < 400) {
var data = JSON.parse(xhr.responseText);
_converse.connection.attach(
data.jid, data.sid, data.rid,
_converse.onConnectStatusChanged);
} else {
xhr.onerror();
}
};
xhr.onerror = function () {
delete _converse.connection;
_converse.emit('noResumeableSession');
};
xhr.send();
};
this.restoreBOSHSession = function (jid_is_required) {
/* Tries to restore a cached BOSH session. */
if (!this.jid) {
var msg = "restoreBOSHSession: tried to restore a \"keepalive\" session "+
"but we don't have the JID for the user!";
if (jid_is_required) {
throw new Error(msg);
} else {
_converse.log(msg);
}
}
try {
this.connection.restore(this.jid, this.onConnectStatusChanged);
return true;
} catch (e) {
this.log(
"Could not restore session for jid: "+
this.jid+" Error message: "+e.message);
this.clearSession(); // If there's a roster, we want to clear it (see #555)
return false;
}
};
this.attemptPreboundSession = function (reconnecting) {
/* Handle session resumption or initialization when prebind is
* being used.
*/
if (!reconnecting) {
if (this.keepalive && this.restoreBOSHSession(true)) {
return;
}
// No keepalive, or session resumption has failed.
if (this.jid && this.sid && this.rid) {
return this.connection.attach(
this.jid, this.sid, this.rid,
this.onConnectStatusChanged
);
}
}
if (this.prebind_url) {
return this.startNewBOSHSession();
} else {
throw new Error(
"attemptPreboundSession: If you use prebind and not keepalive, "+
"then you MUST supply JID, RID and SID values or a prebind_url.");
}
};
this.attemptNonPreboundSession = function (credentials, reconnecting) {
/* Handle session resumption or initialization when prebind is not being used.
*
* Two potential options exist and are handled in this method:
* 1. keepalive
* 2. auto_login
*/
if (!reconnecting && this.keepalive && this.restoreBOSHSession()) {
return;
}
if (this.auto_login) {
if (credentials) {
// When credentials are passed in, they override prebinding
// or credentials fetching via HTTP
this.autoLogin(credentials);
} else if (this.credentials_url) {
this.fetchLoginCredentials().done(this.autoLogin.bind(this));
} else if (!this.jid) {
throw new Error(
"attemptNonPreboundSession: If you use auto_login, "+
"you also need to give either a jid value (and if "+
"applicable a password) or you need to pass in a URL "+
"from where the username and password can be fetched "+
"(via credentials_url)."
);
} else {
this.autoLogin(); // Probably ANONYMOUS login
}
} else if (reconnecting) {
this.autoLogin();
}
};
this.autoLogin = function (credentials) {
if (credentials) {
// If passed in, the credentials come from credentials_url,
// so we set them on the converse object.
this.jid = credentials.jid;
}
if (this.authentication === _converse.ANONYMOUS) {
if (!this.jid) {
throw new Error("Config Error: when using anonymous login " +
"you need to provide the server's domain via the 'jid' option. " +
"Either when calling converse.initialize, or when calling " +
"_converse.api.user.login.");
}
if (!this.connection.reconnecting) {
this.connection.reset();
}
this.connection.connect(this.jid.toLowerCase(), null, this.onConnectStatusChanged);
} else if (this.authentication === _converse.LOGIN) {
var password = _.isNil(credentials) ? (_converse.connection.pass || this.password) : credentials.password;
if (!password) {
if (this.auto_login) {
throw new Error("initConnection: If you use auto_login and "+
"authentication='login' then you also need to provide a password.");
}
_converse.setDisconnectionCause(Strophe.Status.AUTHFAIL, undefined, true);
_converse.disconnect();
return;
}
var resource = Strophe.getResourceFromJid(this.jid);
if (!resource) {
this.jid = this.jid.toLowerCase() + _converse.generateResource();
} else {
this.jid = Strophe.getBareJidFromJid(this.jid).toLowerCase()+'/'+resource;
}
if (!this.connection.reconnecting) {
this.connection.reset();
}
this.connection.connect(this.jid, password, this.onConnectStatusChanged);
}
};
this.logIn = function (credentials, reconnecting) {
// We now try to resume or automatically set up a new session.
// Otherwise the user will be shown a login form.
if (this.authentication === _converse.PREBIND) {
this.attemptPreboundSession(reconnecting);
} else {
this.attemptNonPreboundSession(credentials, reconnecting);
}
};
this.initConnection = function () {
if (this.connection) {
return;
}
if (!this.bosh_service_url && ! this.websocket_url) {
throw new Error("initConnection: you must supply a value for either the bosh_service_url or websocket_url or both.");
}
if (('WebSocket' in window || 'MozWebSocket' in window) && this.websocket_url) {
this.connection = new Strophe.Connection(this.websocket_url, this.connection_options);
} else if (this.bosh_service_url) {
this.connection = new Strophe.Connection(
this.bosh_service_url,
_.assignIn(this.connection_options, {'keepalive': this.keepalive})
);
} else {
throw new Error("initConnection: this browser does not support websockets and bosh_service_url wasn't specified.");
}
};
this._tearDown = function () {
/* Remove those views which are only allowed with a valid
* connection.
*/
this.unregisterPresenceHandler();
if (this.roster) {
this.roster.off().reset(); // Removes roster contacts
}
this.chatboxes.remove(); // Don't call off(), events won't get re-registered upon reconnect.
delete this.chatboxes.browserStorage;
if (this.features) {
this.features.reset();
}
this.session.destroy();
window.removeEventListener('click', _converse.onUserActivity);
window.removeEventListener('focus', _converse.onUserActivity);
window.removeEventListener('keypress', _converse.onUserActivity);
window.removeEventListener('mousemove', _converse.onUserActivity);
window.removeEventListener(unloadevent, _converse.onUserActivity);
window.clearInterval(_converse.everySecondTrigger);
return this;
};
this.initChatBoxes = function () {
this.chatboxes = new this.ChatBoxes();
this.chatboxviews = new this.ChatBoxViews({model: this.chatboxes});
};
this.initPlugins = function () {
// If initialize gets called a second time (e.g. during tests), then we
// need to re-apply all plugins (for a new converse instance), and we
// therefore need to clear this array that prevents plugins from being
// initialized twice.
// If initialize is called for the first time, then this array is empty
// in any case.
_converse.pluggable.initialized_plugins = [];
var whitelist = _converse.core_plugins.concat(
_converse.whitelisted_plugins);
_converse.pluggable.initializePlugins({
'updateSettings': function () {
_converse.log(
"(DEPRECATION) "+
"The `updateSettings` method has been deprecated. "+
"Please use `_converse.api.settings.update` instead.",
Strophe.LogLevel.WARN
)
_converse.api.settings.update.apply(_converse, arguments);
},
'_converse': _converse
}, whitelist, _converse.blacklisted_plugins);
_converse.emit('pluginsInitialized');
};
// Initialization
// --------------
// This is the end of the initialize method.
if (settings.connection) {
this.connection = settings.connection;
}
_converse.initPlugins();
_converse.initChatBoxes();
_converse.initConnection();
_converse.setUpXMLLogging();
_converse.logIn();
_converse.registerGlobalEventHandlers();
if (!_.isUndefined(_converse.connection) &&
_converse.connection.service === 'jasmine tests') {
return _converse;
} else {
return init_deferred.promise();
}
};
// API methods only available to plugins
_converse.api = {
'connection': {
'connected': function () {
return _converse.connection && _converse.connection.connected || false;
},
'disconnect': function () {
_converse.connection.disconnect();
},
},
'emit': function () {
_converse.emit.apply(_converse, arguments);
},
'user': {
'jid': function () {
return _converse.connection.jid;
},
'login': function (credentials) {
_converse.initConnection();
_converse.logIn(credentials);
},
'logout': function () {
_converse.logOut();
},
'status': {
'get': function () {
return _converse.xmppstatus.get('status');
},
'set': function (value, message) {
var data = {'status': value};
if (!_.includes(_.keys(_converse.STATUS_WEIGHTS), value)) {
throw new Error('Invalid availability value. See https://xmpp.org/rfcs/rfc3921.html#rfc.section.2.2.2.1');
}
if (_.isString(message)) {
data.status_message = message;
}
_converse.xmppstatus.sendPresence(value);
_converse.xmppstatus.save(data);
},
'message': {
'get': function () {
return _converse.xmppstatus.get('status_message');
},
'set': function (stat) {
_converse.xmppstatus.save({'status_message': stat});
}
}
},
},
'settings': {
'update': function (settings) {
utils.merge(_converse.default_settings, settings);
utils.merge(_converse, settings);
utils.applyUserSettings(_converse, settings, _converse.user_settings);
},
'get': function (key) {
if (_.includes(_.keys(_converse.default_settings), key)) {
return _converse[key];
}
},
'set': function (key, val) {
var o = {};
if (_.isObject(key)) {
_.assignIn(_converse, _.pick(key, _.keys(_converse.default_settings)));
} else if (_.isString("string")) {
o[key] = val;
_.assignIn(_converse, _.pick(o, _.keys(_converse.default_settings)));
}
}
},
'promises': {
'add': function (promises) {
promises = _.isArray(promises) ? promises : [promises]
_.each(promises, function (promise) {
_converse.promises[promise] = new $.Deferred();
});
}
},
'contacts': {
'get': function (jids) {
var _transform = function (jid) {
var contact = _converse.roster.get(Strophe.getBareJidFromJid(jid));
if (contact) {
return contact.attributes;
}
return null;
};
if (_.isUndefined(jids)) {
jids = _converse.roster.pluck('jid');
} else if (_.isString(jids)) {
return _transform(jids);
}
return _.map(jids, _transform);
},
'add': function (jid, name) {
if (!_.isString(jid) || !_.includes(jid, '@')) {
throw new TypeError('contacts.add: invalid jid');
}
_converse.roster.addAndSubscribe(jid, _.isEmpty(name)? jid: name);
}
},
'chats': {
'open': function (jids, attrs) {
var chatbox;
if (_.isUndefined(jids)) {
_converse.log("chats.open: You need to provide at least one JID", Strophe.LogLevel.ERROR);
return null;
} else if (_.isString(jids)) {
chatbox = _converse.getViewForChatBox(
_converse.chatboxes.getChatBox(jids, true, attrs).trigger('show')
);
return chatbox;
}
return _.map(jids, function (jid) {
chatbox = _converse.getViewForChatBox(
_converse.chatboxes.getChatBox(jid, true, attrs).trigger('show')
);
return chatbox;
});
},
'get': function (jids) {
if (_.isUndefined(jids)) {
var result = [];
_converse.chatboxes.each(function (chatbox) {
// FIXME: Leaky abstraction from MUC. We need to add a
// base type for chat boxes, and check for that.
if (chatbox.get('type') !== 'chatroom') {
result.push(_converse.getViewForChatBox(chatbox));
}
});
return result;
} else if (_.isString(jids)) {
return _converse.getViewForChatBox(_converse.chatboxes.getChatBox(jids));
}
return _.map(jids,
_.partial(
_.flow(
_converse.chatboxes.getChatBox.bind(_converse.chatboxes),
_converse.getViewForChatBox.bind(_converse)
), _, true
)
);
}
},
'tokens': {
'get': function (id) {
if (!_converse.expose_rid_and_sid || _.isUndefined(_converse.connection)) {
return null;
}
if (id.toLowerCase() === 'rid') {
return _converse.connection.rid || _converse.connection._proto.rid;
} else if (id.toLowerCase() === 'sid') {
return _converse.connection.sid || _converse.connection._proto.sid;
}
}
},
'listen': {
'once': _converse.once.bind(_converse),
'on': _converse.on.bind(_converse),
'not': _converse.off.bind(_converse),
'stanza': function (name, options, handler) {
if (_.isFunction(options)) {
handler = options;
options = {};
} else {
options = options || {};
}
_converse.connection.addHandler(
handler,
options.ns,
name,
options.type,
options.id,
options.from,
options
);
},
},
'waitUntil': function (name) {
var promise = _converse.promises[name];
if (_.isUndefined(promise)) {
return null;
}
return _converse.promises[name].promise();
},
'send': function (stanza) {
_converse.connection.send(stanza);
},
};
// The public API
return {
'initialize': function (settings, callback) {
return _converse.initialize(settings, callback);
},
'plugins': {
'add': function (name, plugin) {
plugin.__name__ = name;
if (!_.isUndefined(_converse.pluggable.plugins[name])) {
throw new TypeError(
'Error: plugin with name "'+name+'" has already been '+
'registered!');
} else {
_converse.pluggable.plugins[name] = plugin;
}
}
},
'env': {
'$build': $build,
'$iq': $iq,
'$msg': $msg,
'$pres': $pres,
'Backbone': Backbone,
'Strophe': Strophe,
'_': _,
'fp': fp,
'b64_sha1': b64_sha1,
'jQuery': $,
'moment': moment,
'sizzle': sizzle,
'utils': utils
}
};
}));
define('tpl!chatbox', ['lodash'], function(_) {return function(obj) {
obj || (obj = {});
var __t, __p = '', __e = _.escape, __j = Array.prototype.join;
function print() { __p += __j.call(arguments, '') }
with (obj) {
__p += '
\n';
} ;
__p += '\n';
}
return __p
};});
define('tpl!avatar', ['lodash'], function(_) {return function(obj) {
obj || (obj = {});
var __t, __p = '';
with (obj) {
__p += '\n';
}
return __p
};});
define('tpl!spinner', ['lodash'], function(_) {return function(obj) {
obj || (obj = {});
var __t, __p = '';
with (obj) {
__p += '\n';
}
return __p
};});
// Converse.js (A browser based XMPP chat client)
// http://conversejs.org
//
// Copyright (c) 2012-2017, Jan-Carel Brand
// Licensed under the Mozilla Public License (MPLv2)
//
/*global define */
(function (root, factory) {
define('converse-chatview',[
"converse-core",
"tpl!chatbox",
"tpl!new_day",
"tpl!action",
"tpl!message",
"tpl!help_message",
"tpl!toolbar",
"tpl!avatar",
"tpl!spinner"
], factory);
}(this, function (
converse,
tpl_chatbox,
tpl_new_day,
tpl_action,
tpl_message,
tpl_help_message,
tpl_toolbar,
tpl_avatar,
tpl_spinner
) {
"use strict";
var $ = converse.env.jQuery,
$msg = converse.env.$msg,
Backbone = converse.env.Backbone,
Strophe = converse.env.Strophe,
_ = converse.env._,
moment = converse.env.moment,
utils = converse.env.utils;
var KEY = {
ENTER: 13,
FORWARD_SLASH: 47
};
converse.plugins.add('converse-chatview', {
overrides: {
// 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.
ChatBoxViews: {
onChatBoxAdded: function (item) {
var _converse = this.__super__._converse;
var view = this.get(item.get('id'));
if (!view) {
view = new _converse.ChatBoxView({model: item});
this.add(item.get('id'), view);
return view;
} else {
return this.__super__.onChatBoxAdded.apply(this, arguments);
}
}
}
},
initialize: function () {
/* The initialize function gets called as soon as the plugin is
* loaded by converse.js's plugin machinery.
*/
var _converse = this._converse,
__ = _converse.__;
_converse.api.settings.update({
chatview_avatar_height: 32,
chatview_avatar_width: 32,
show_toolbar: true,
time_format: 'HH:mm',
visible_toolbar_buttons: {
'emoticons': true,
'call': false,
'clear': true
},
});
var onWindowStateChanged = function (data) {
var state = data.state;
_converse.chatboxviews.each(function (chatboxview) {
chatboxview.onWindowStateChanged(state);
})
};
_converse.api.listen.on('windowStateChanged', onWindowStateChanged);
_converse.ChatBoxView = Backbone.View.extend({
length: 200,
tagName: 'div',
className: 'chatbox hidden',
is_chatroom: false, // Leaky abstraction from MUC
events: {
'click .close-chatbox-button': 'close',
'keypress .chat-textarea': 'keyPressed',
'click .send-button': 'onSendButtonClicked',
'click .toggle-smiley': 'toggleEmoticonMenu',
'click .toggle-smiley ul li': 'insertEmoticon',
'click .toggle-clear': 'clearMessages',
'click .toggle-call': 'toggleCall',
'click .new-msgs-indicator': 'viewUnreadMessages'
},
initialize: function () {
this.model.messages.on('add', this.onMessageAdded, this);
this.model.on('show', this.show, this);
this.model.on('destroy', this.hide, this);
// TODO check for changed fullname as well
this.model.on('change:chat_state', this.sendChatState, this);
this.model.on('change:chat_status', this.onChatStatusChanged, this);
this.model.on('change:image', this.renderAvatar, this);
this.model.on('change:status', this.onStatusChanged, this);
this.model.on('showHelpMessages', this.showHelpMessages, this);
this.model.on('sendMessage', this.sendMessage, this);
this.render().fetchMessages();
_converse.emit('chatBoxInitialized', this);
},
render: function () {
this.$el.attr('id', this.model.get('box_id'))
.html(tpl_chatbox(
_.extend(this.model.toJSON(), {
show_toolbar: _converse.show_toolbar,
show_textarea: true,
show_send_button: _converse.show_send_button,
title: this.model.get('fullname'),
unread_msgs: __('You have unread messages'),
info_close: __('Close this chat box'),
label_personal_message: __('Personal message'),
label_send: __('Send')
}
)
)
);
this.$content = this.$el.find('.chat-content');
this.renderToolbar().renderAvatar();
_converse.emit('chatBoxOpened', this);
utils.refreshWebkit();
return this.showStatusMessage();
},
afterMessagesFetched: function () {
this.insertIntoDOM();
this.scrollDown();
// We only start listening for the scroll event after
// cached messages have been fetched
this.$content.on('scroll', this.markScrolled.bind(this));
},
fetchMessages: function () {
this.model.messages.fetch({
'add': true,
'success': this.afterMessagesFetched.bind(this),
'error': this.afterMessagesFetched.bind(this),
});
return this;
},
insertIntoDOM: function () {
/* This method gets overridden in src/converse-controlbox.js if
* the controlbox plugin is active.
*/
var container = document.querySelector('#conversejs');
if (this.el.parentNode !== container) {
container.insertBefore(this.el, container.firstChild);
}
return this;
},
clearStatusNotification: function () {
this.$content.find('div.chat-event').remove();
},
showStatusNotification: function (message, keep_old, permanent) {
if (!keep_old) {
this.clearStatusNotification();
}
var $el = $('').text(message);
if (!permanent) {
$el.addClass('chat-event');
}
this.$content.append($el);
this.scrollDown();
},
addSpinner: function () {
if (_.isNull(this.el.querySelector('.spinner'))) {
this.$content.prepend(tpl_spinner);
}
},
clearSpinner: function () {
if (this.$content.children(':first').is('span.spinner')) {
this.$content.children(':first').remove();
}
},
insertDayIndicator: function (date, prepend) {
/* Appends (or prepends if "prepend" is truthy) an indicator
* into the chat area, showing the day as given by the
* passed in date.
*
* Parameters:
* (String) date - An ISO8601 date string.
*/
var day_date = moment(date).startOf('day');
var insert = prepend ? this.$content.prepend: this.$content.append;
insert.call(this.$content, tpl_new_day({
isodate: day_date.format(),
datestring: day_date.format("dddd MMM Do YYYY")
}));
},
insertMessage: function (attrs, prepend) {
/* Helper method which appends a message (or prepends if the
* 2nd parameter is set to true) to the end of the chat box's
* content area.
*
* Parameters:
* (Object) attrs: An object containing the message attributes.
*/
var that = this;
var insert = prepend ? this.$content.prepend : this.$content.append;
_.flow(
function ($el) {
insert.call(that.$content, $el);
return $el;
},
this.scrollDown.bind(this)
)(this.renderMessage(attrs));
},
showMessage: function (attrs) {
/* Inserts a chat message into the content area of the chat box.
* Will also insert a new day indicator if the message is on a
* different day.
*
* The message to show may either be newer than the newest
* message, or older than the oldest message.
*
* Parameters:
* (Object) attrs: An object containing the message
* attributes.
*/
var msg_dates,
$first_msg = this.$content.find('.chat-message:first'),
first_msg_date = $first_msg.data('isodate'),
current_msg_date = moment(attrs.time) || moment,
last_msg_date = this.$content.find('.chat-message:last').data('isodate');
if (!first_msg_date) {
// This is the first received message, so we insert a
// date indicator before it.
this.insertDayIndicator(current_msg_date);
this.insertMessage(attrs);
return;
}
if (current_msg_date.isAfter(last_msg_date) ||
current_msg_date.isSame(last_msg_date)) {
// The new message is after the last message
if (current_msg_date.isAfter(last_msg_date, 'day')) {
// Append a new day indicator
this.insertDayIndicator(current_msg_date);
}
this.insertMessage(attrs);
return;
}
if (current_msg_date.isBefore(first_msg_date) ||
current_msg_date.isSame(first_msg_date)) {
// The message is before the first, but on the same day.
// We need to prepend the message immediately before the
// first message (so that it'll still be after the day
// indicator).
this.insertMessage(attrs, 'prepend');
if (current_msg_date.isBefore(first_msg_date, 'day')) {
// This message is also on a different day, so
// we prepend a day indicator.
this.insertDayIndicator(current_msg_date, 'prepend');
}
return;
}
// Find the correct place to position the message
current_msg_date = current_msg_date.format();
msg_dates = _.map(this.$content.find('.chat-message'), function (el) {
return $(el).data('isodate');
});
msg_dates.push(current_msg_date);
msg_dates.sort();
var idx = msg_dates.indexOf(current_msg_date)-1;
var $latest_message = this.$content.find('.chat-message[data-isodate="'+msg_dates[idx]+'"]:last');
_.flow(
function ($el) {
$el.insertAfter($latest_message);
},
this.scrollDown.bind(this)
)(this.renderMessage(attrs));
},
getExtraMessageTemplateAttributes: function () {
/* Provides a hook for sending more attributes to the
* message template.
*
* Parameters:
* (Object) attrs: An object containing message attributes.
*/
return {};
},
getExtraMessageClasses: function (attrs) {
return attrs.delayed && 'delayed' || '';
},
renderMessage: function (attrs) {
/* Renders a chat message based on the passed in attributes.
*
* Parameters:
* (Object) attrs: An object containing the message attributes.
*
* Returns:
* The DOM element representing the message.
*/
var msg_time = moment(attrs.time) || moment,
text = attrs.message,
match = text.match(/^\/(.*?)(?: (.*))?$/),
fullname = this.model.get('fullname') || attrs.fullname,
template, username;
if ((match) && (match[1] === 'me')) {
text = text.replace(/^\/me/, '');
template = tpl_action;
if (attrs.sender === 'me') {
fullname = _converse.xmppstatus.get('fullname') || attrs.fullname;
username = _.isNil(fullname)? _converse.bare_jid: fullname;
} else {
username = attrs.fullname;
}
} else {
template = tpl_message;
username = attrs.sender === 'me' && __('me') || fullname;
}
this.$content.find('div.chat-event').remove();
if (text.length > 8000) {
text = text.substring(0, 10) + '...';
this.showStatusNotification(
__("A very large message has been received."+
"This might be due to an attack meant to degrade the chat performance."+
"Output has been shortened."),
true, true);
}
var $msg = $(template(
_.extend(this.getExtraMessageTemplateAttributes(attrs), {
'msgid': attrs.msgid,
'sender': attrs.sender,
'time': msg_time.format(_converse.time_format),
'isodate': msg_time.format(),
'username': username,
'extra_classes': this.getExtraMessageClasses(attrs)
})
));
$msg.find('.chat-msg-content').first()
.text(text)
.addHyperlinks()
.addEmoticons(_converse.visible_toolbar_buttons.emoticons);
return $msg;
},
showHelpMessages: function (msgs, type, spinner) {
var i, msgs_length = msgs.length;
for (i=0; i').text(message.get('message')));
this.scrollDown();
}
},
onMessageAdded: function (message) {
/* Handler that gets called when a new message object is created.
*
* Parameters:
* (Object) message - The message Backbone object that was added.
*/
if (!_.isUndefined(this.clear_status_timeout)) {
window.clearTimeout(this.clear_status_timeout);
delete this.clear_status_timeout;
}
if (message.get('type') === 'error') {
this.handleErrorMessage(message);
} else if (!message.get('message')) {
this.handleChatStateMessage(message);
} else {
this.handleTextMessage(message);
}
_converse.emit('messageAdded', {
'message': message,
'chatbox': this.model
});
},
createMessageStanza: function (message) {
return $msg({
from: _converse.connection.jid,
to: this.model.get('jid'),
type: 'chat',
id: message.get('msgid')
}).c('body').t(message.get('message')).up()
.c(_converse.ACTIVE, {'xmlns': Strophe.NS.CHATSTATES}).up();
},
sendMessage: function (message) {
/* Responsible for sending off a text message.
*
* Parameters:
* (Message) message - The chat message
*/
// TODO: We might want to send to specfic resources.
// Especially in the OTR case.
var messageStanza = this.createMessageStanza(message);
_converse.connection.send(messageStanza);
if (_converse.forward_messages) {
// Forward the message, so that other connected resources are also aware of it.
_converse.connection.send(
$msg({ to: _converse.bare_jid, type: 'chat', id: message.get('msgid') })
.c('forwarded', {xmlns:'urn:xmpp:forward:0'})
.c('delay', {xmns:'urn:xmpp:delay',stamp:(new Date()).getTime()}).up()
.cnode(messageStanza.tree())
);
}
},
onMessageSubmitted: function (text) {
/* This method gets called once the user has typed a message
* and then pressed enter in a chat box.
*
* Parameters:
* (string) text - The chat message text.
*/
if (!_converse.connection.authenticated) {
return this.showHelpMessages(
['Sorry, the connection has been lost, '+
'and your message could not be sent'],
'error'
);
}
var match = text.replace(/^\s*/, "").match(/^\/(.*)\s*$/), msgs;
if (match) {
if (match[1] === "clear") {
return this.clearMessages();
}
else if (match[1] === "help") {
msgs = [
'/help:'+__('Show this menu')+'',
'/me:'+__('Write in the third person')+'',
'/clear:'+__('Remove messages')+''
];
this.showHelpMessages(msgs);
return;
}
}
var fullname = _converse.xmppstatus.get('fullname');
fullname = _.isEmpty(fullname)? _converse.bare_jid: fullname;
var message = this.model.messages.create({
fullname: fullname,
sender: 'me',
time: moment().format(),
message: text
});
this.sendMessage(message);
},
sendChatState: function () {
/* Sends a message with the status of the user in this chat session
* as taken from the 'chat_state' attribute of the chat box.
* See XEP-0085 Chat State Notifications.
*/
_converse.connection.send(
$msg({'to':this.model.get('jid'), 'type': 'chat'})
.c(this.model.get('chat_state'), {'xmlns': Strophe.NS.CHATSTATES}).up()
.c('no-store', {'xmlns': Strophe.NS.HINTS}).up()
.c('no-permanent-store', {'xmlns': Strophe.NS.HINTS})
);
},
setChatState: function (state, no_save) {
/* Mutator for setting the chat state of this chat session.
* Handles clearing of any chat state notification timeouts and
* setting new ones if necessary.
* Timeouts are set when the state being set is COMPOSING or PAUSED.
* After the timeout, COMPOSING will become PAUSED and PAUSED will become INACTIVE.
* See XEP-0085 Chat State Notifications.
*
* Parameters:
* (string) state - The chat state (consts ACTIVE, COMPOSING, PAUSED, INACTIVE, GONE)
* (Boolean) no_save - Just do the cleanup or setup but don't actually save the state.
*/
if (!_.isUndefined(this.chat_state_timeout)) {
window.clearTimeout(this.chat_state_timeout);
delete this.chat_state_timeout;
}
if (state === _converse.COMPOSING) {
this.chat_state_timeout = window.setTimeout(
this.setChatState.bind(this), _converse.TIMEOUTS.PAUSED, _converse.PAUSED);
} else if (state === _converse.PAUSED) {
this.chat_state_timeout = window.setTimeout(
this.setChatState.bind(this), _converse.TIMEOUTS.INACTIVE, _converse.INACTIVE);
}
if (!no_save && this.model.get('chat_state') !== state) {
this.model.set('chat_state', state);
}
return this;
},
keyPressed: function (ev) {
/* Event handler for when a key is pressed in a chat box textarea.
*/
var textarea = ev.target, message;
if (ev.keyCode === KEY.ENTER) {
ev.preventDefault();
message = textarea.value;
textarea.value = '';
textarea.focus();
if (message !== '') {
this.onMessageSubmitted(message);
_converse.emit('messageSend', message);
}
this.setChatState(_converse.ACTIVE);
} else {
// Set chat state to composing if keyCode is not a forward-slash
// (which would imply an internal command and not a message).
this.setChatState(_converse.COMPOSING, ev.keyCode === KEY.FORWARD_SLASH);
}
},
onSendButtonClicked: function(ev) {
/* Event handler for when a send button is clicked in a chat box textarea.
*/
ev.preventDefault();
var textarea = this.el.querySelector('.chat-textarea'),
message = textarea.value;
textarea.value = '';
textarea.focus();
if (message !== '') {
this.onMessageSubmitted(message);
_converse.emit('messageSend', message);
}
this.setChatState(_converse.ACTIVE);
},
clearMessages: function (ev) {
if (ev && ev.preventDefault) { ev.preventDefault(); }
var result = confirm(__("Are you sure you want to clear the messages from this chat box?"));
if (result === true) {
this.$content.empty();
this.model.messages.reset();
this.model.messages.browserStorage._clear();
}
return this;
},
insertIntoTextArea: function (value) {
var $textbox = this.$el.find('textarea.chat-textarea');
var existing = $textbox.val();
if (existing && (existing[existing.length-1] !== ' ')) {
existing = existing + ' ';
}
$textbox.focus().val(existing+value+' ');
},
insertEmoticon: function (ev) {
ev.stopPropagation();
this.$el.find('.toggle-smiley ul').slideToggle(200);
var $target = $(ev.target);
$target = $target.is('a') ? $target : $target.children('a');
this.insertIntoTextArea($target.data('emoticon'));
},
toggleEmoticonMenu: function (ev) {
ev.stopPropagation();
this.$el.find('.toggle-smiley ul').slideToggle(200);
},
toggleCall: function (ev) {
ev.stopPropagation();
_converse.emit('callButtonClicked', {
connection: _converse.connection,
model: this.model
});
},
onChatStatusChanged: function (item) {
var chat_status = item.get('chat_status'),
fullname = item.get('fullname');
fullname = _.isEmpty(fullname)? item.get('jid'): fullname;
if (this.$el.is(':visible')) {
if (chat_status === 'offline') {
this.showStatusNotification(fullname+' '+__('has gone offline'));
} else if (chat_status === 'away') {
this.showStatusNotification(fullname+' '+__('has gone away'));
} else if ((chat_status === 'dnd')) {
this.showStatusNotification(fullname+' '+__('is busy'));
} else if (chat_status === 'online') {
this.$el.find('div.chat-event').remove();
}
}
},
onStatusChanged: function (item) {
this.showStatusMessage();
_converse.emit('contactStatusMessageChanged', {
'contact': item.attributes,
'message': item.get('status')
});
},
showStatusMessage: function (msg) {
msg = msg || this.model.get('status');
if (_.isString(msg)) {
this.$el.find('p.user-custom-message').text(msg).attr('title', msg);
}
return this;
},
close: function (ev) {
if (ev && ev.preventDefault) { ev.preventDefault(); }
if (_converse.connection.connected) {
// Immediately sending the chat state, because the
// model is going to be destroyed afterwards.
this.model.set('chat_state', _converse.INACTIVE);
this.sendChatState();
}
try {
this.model.destroy();
} catch (e) {
_converse.log(e, Strophe.LogLevel.ERROR);
}
this.remove();
_converse.emit('chatBoxClosed', this);
return this;
},
getToolbarOptions: function (options) {
return _.extend(options || {}, {
'label_clear': __('Clear all messages'),
'label_insert_smiley': __('Insert a smiley'),
'label_start_call': __('Start a call'),
'show_call_button': _converse.visible_toolbar_buttons.call,
'show_clear_button': _converse.visible_toolbar_buttons.clear,
'show_emoticons': _converse.visible_toolbar_buttons.emoticons,
});
},
renderToolbar: function (toolbar, options) {
if (!_converse.show_toolbar) { return; }
toolbar = toolbar || tpl_toolbar;
options = _.extend(
this.model.toJSON(),
this.getToolbarOptions(options || {})
);
this.$el.find('.chat-toolbar').html(toolbar(options));
return this;
},
renderAvatar: function () {
if (!this.model.get('image')) {
return;
}
var width = _converse.chatview_avatar_width;
var height = _converse.chatview_avatar_height;
var img_src = 'data:'+this.model.get('image_type')+';base64,'+this.model.get('image'),
canvas = $(tpl_avatar({
'width': width,
'height': height
})).get(0);
if (!(canvas.getContext && canvas.getContext('2d'))) {
return this;
}
var ctx = canvas.getContext('2d');
var img = new Image(); // Create new Image object
img.onload = function () {
var ratio = img.width/img.height;
if (ratio < 1) {
ctx.drawImage(img, 0,0, width, height*(1/ratio));
} else {
ctx.drawImage(img, 0,0, width, height*ratio);
}
};
img.src = img_src;
this.$el.find('.chat-title').before(canvas);
return this;
},
focus: function () {
this.$el.find('.chat-textarea').focus();
_converse.emit('chatBoxFocused', this);
return this;
},
hide: function () {
this.el.classList.add('hidden');
utils.refreshWebkit();
return this;
},
afterShown: function (focus) {
if (utils.isPersistableModel(this.model)) {
this.model.save();
}
this.setChatState(_converse.ACTIVE);
this.scrollDown();
if (focus) {
this.focus();
}
},
_show: function (focus) {
/* Inner show method that gets debounced */
if (this.$el.is(':visible') && this.$el.css('opacity') === "1") {
if (focus) { this.focus(); }
return;
}
utils.fadeIn(this.el, _.bind(this.afterShown, this, focus));
},
show: function (focus) {
if (_.isUndefined(this.debouncedShow)) {
/* We wrap the method in a debouncer and set it on the
* instance, so that we have it debounced per instance.
* Debouncing it on the class-level is too broad.
*/
this.debouncedShow = _.debounce(this._show, 250, {'leading': true});
}
this.debouncedShow.apply(this, arguments);
return this;
},
hideNewMessagesIndicator: function () {
var new_msgs_indicator = this.el.querySelector('.new-msgs-indicator');
if (!_.isNull(new_msgs_indicator)) {
new_msgs_indicator.classList.add('hidden');
}
},
markScrolled: _.debounce(function (ev) {
/* Called when the chat content is scrolled up or down.
* We want to record when the user has scrolled away from
* the bottom, so that we don't automatically scroll away
* from what the user is reading when new messages are
* received.
*/
if (ev && ev.preventDefault) { ev.preventDefault(); }
if (this.model.get('auto_scrolled')) {
this.model.set({
'scrolled': false,
'auto_scrolled': false
});
return;
}
var scrolled = true;
var is_at_bottom =
(this.$content.scrollTop() + this.$content.innerHeight()) >=
this.$content[0].scrollHeight-10;
if (is_at_bottom) {
scrolled = false;
this.onScrolledDown();
}
utils.safeSave(this.model, {'scrolled': scrolled});
}, 150),
viewUnreadMessages: function () {
this.model.save('scrolled', false);
this.scrollDown();
},
_scrollDown: function () {
/* Inner method that gets debounced */
if (this.$content.is(':visible') && !this.model.get('scrolled')) {
this.$content.scrollTop(this.$content[0].scrollHeight);
this.onScrolledDown();
this.model.save({'auto_scrolled': true});
}
},
onScrolledDown: function() {
this.hideNewMessagesIndicator();
if (_converse.windowState !== 'hidden') {
this.model.clearUnreadMsgCounter();
}
_converse.emit('chatBoxScrolledDown', {'chatbox': this.model});
},
scrollDown: function () {
if (_.isUndefined(this.debouncedScrollDown)) {
/* We wrap the method in a debouncer and set it on the
* instance, so that we have it debounced per instance.
* Debouncing it on the class-level is too broad.
*/
this.debouncedScrollDown = _.debounce(this._scrollDown, 250);
}
this.debouncedScrollDown.apply(this, arguments);
return this;
},
onWindowStateChanged: function (state) {
if (this.model.get('num_unread', 0) && !this.model.newMessageWillBeHidden()) {
this.model.clearUnreadMsgCounter();
}
}
});
}
});
return converse;
}));
define('tpl!add_contact_dropdown', ['lodash'], function(_) {return function(obj) {
obj || (obj = {});
var __t, __p = '', __e = _.escape;
with (obj) {
__p += '
\n ';
if (num_unread) { ;
__p += '\n ' +
__e( num_unread ) +
'\n ';
} ;
__p += '\n ' +
__e(fullname) +
'\n\n';
if (allow_contact_removal) { ;
__p += '\n\n';
} ;
__p += '\n\n\n';
}
return __p
};});
// Converse.js (A browser based XMPP chat client)
// http://conversejs.org
//
// Copyright (c) 2012-2017, Jan-Carel Brand
// Licensed under the Mozilla Public License (MPLv2)
//
/*global define */
(function (root, factory) {
define('converse-rosterview',["converse-core",
"tpl!group_header",
"tpl!pending_contact",
"tpl!requesting_contact",
"tpl!roster",
"tpl!roster_filter",
"tpl!roster_item"
], factory);
}(this, function (
converse,
tpl_group_header,
tpl_pending_contact,
tpl_requesting_contact,
tpl_roster,
tpl_roster_filter,
tpl_roster_item) {
"use strict";
var $ = converse.env.jQuery,
Backbone = converse.env.Backbone,
utils = converse.env.utils,
Strophe = converse.env.Strophe,
$iq = converse.env.$iq,
b64_sha1 = converse.env.b64_sha1,
sizzle = converse.env.sizzle,
_ = converse.env._;
converse.plugins.add('converse-rosterview', {
overrides: {
// 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.
afterReconnected: function () {
this.__super__.afterReconnected.apply(this, arguments);
},
_tearDown: function () {
/* Remove the rosterview when tearing down. It gets created
* anew when reconnecting or logging in.
*/
this.__super__._tearDown.apply(this, arguments);
if (!_.isUndefined(this.rosterview)) {
this.rosterview.remove();
}
},
RosterGroups: {
comparator: function () {
// RosterGroupsComparator only gets set later (once i18n is
// set up), so we need to wrap it in this nameless function.
var _converse = this.__super__._converse;
return _converse.RosterGroupsComparator.apply(this, arguments);
}
}
},
initialize: function () {
/* The initialize function gets called as soon as the plugin is
* loaded by converse.js's plugin machinery.
*/
var _converse = this._converse,
__ = _converse.__,
___ = _converse.___;
_converse.api.settings.update({
allow_chat_pending_contacts: true,
allow_contact_removal: true,
show_toolbar: true,
});
var STATUSES = {
'dnd': __('This contact is busy'),
'online': __('This contact is online'),
'offline': __('This contact is offline'),
'unavailable': __('This contact is unavailable'),
'xa': __('This contact is away for an extended period'),
'away': __('This contact is away')
};
var LABEL_CONTACTS = __('Contacts');
var LABEL_GROUPS = __('Groups');
var HEADER_CURRENT_CONTACTS = __('My contacts');
var HEADER_PENDING_CONTACTS = __('Pending contacts');
var HEADER_REQUESTING_CONTACTS = __('Contact requests');
var HEADER_UNGROUPED = __('Ungrouped');
var HEADER_WEIGHTS = {};
HEADER_WEIGHTS[HEADER_REQUESTING_CONTACTS] = 0;
HEADER_WEIGHTS[HEADER_CURRENT_CONTACTS] = 1;
HEADER_WEIGHTS[HEADER_UNGROUPED] = 2;
HEADER_WEIGHTS[HEADER_PENDING_CONTACTS] = 3;
_converse.RosterGroupsComparator = function (a, b) {
/* Groups are sorted alphabetically, ignoring case.
* However, Ungrouped, Requesting Contacts and Pending Contacts
* appear last and in that order.
*/
a = a.get('name');
b = b.get('name');
var special_groups = _.keys(HEADER_WEIGHTS);
var a_is_special = _.includes(special_groups, a);
var b_is_special = _.includes(special_groups, b);
if (!a_is_special && !b_is_special ) {
return a.toLowerCase() < b.toLowerCase() ? -1 : (a.toLowerCase() > b.toLowerCase() ? 1 : 0);
} else if (a_is_special && b_is_special) {
return HEADER_WEIGHTS[a] < HEADER_WEIGHTS[b] ? -1 : (HEADER_WEIGHTS[a] > HEADER_WEIGHTS[b] ? 1 : 0);
} else if (!a_is_special && b_is_special) {
return (b === HEADER_REQUESTING_CONTACTS) ? 1 : -1;
} else if (a_is_special && !b_is_special) {
return (a === HEADER_REQUESTING_CONTACTS) ? -1 : 1;
}
};
_converse.RosterFilter = Backbone.Model.extend({
initialize: function () {
this.set({
'filter_text': '',
'filter_type': 'contacts',
'chat_state': ''
});
},
});
_converse.RosterFilterView = Backbone.View.extend({
tagName: 'span',
events: {
"keydown .roster-filter": "liveFilter",
"submit form.roster-filter-form": "submitFilter",
"click .onX": "clearFilter",
"mousemove .x": "toggleX",
"change .filter-type": "changeTypeFilter",
"change .state-type": "changeChatStateFilter"
},
initialize: function () {
this.model.on('change:filter_type', this.render, this);
this.model.on('change:filter_text', this.renderClearButton, this);
},
render: function () {
this.el.innerHTML = tpl_roster_filter(
_.extend(this.model.toJSON(), {
placeholder: __('Filter'),
label_contacts: LABEL_CONTACTS,
label_groups: LABEL_GROUPS,
label_state: __('State'),
label_any: __('Any'),
label_unread_messages: __('Unread'),
label_online: __('Online'),
label_chatty: __('Chatty'),
label_busy: __('Busy'),
label_away: __('Away'),
label_xa: __('Extended Away'),
label_offline: __('Offline')
}));
this.renderClearButton();
return this.$el;
},
renderClearButton: function () {
var roster_filter = this.el.querySelector('.roster-filter');
if (_.isNull(roster_filter)) {
return;
}
roster_filter.classList[this.tog(roster_filter.value)]('x');
},
tog: function (v) {
return v?'add':'remove';
},
toggleX: function (ev) {
if (ev && ev.preventDefault) { ev.preventDefault(); }
var el = ev.target;
el.classList[this.tog(el.offsetWidth-18 < ev.clientX-el.getBoundingClientRect().left)]('onX');
},
changeChatStateFilter: function (ev) {
if (ev && ev.preventDefault) { ev.preventDefault(); }
this.model.save({
'chat_state': this.el.querySelector('.state-type').value
});
},
changeTypeFilter: function (ev) {
if (ev && ev.preventDefault) { ev.preventDefault(); }
var type = ev.target.value;
if (type === 'state') {
this.model.save({
'filter_type': type,
'chat_state': this.el.querySelector('.state-type').value
});
} else {
this.model.save({
'filter_type': type,
'filter_text': this.el.querySelector('.roster-filter').value
});
}
},
liveFilter: _.debounce(function (ev) {
this.model.save({
'filter_type': this.el.querySelector('.filter-type').value,
'filter_text': this.el.querySelector('.roster-filter').value
});
}, 250),
submitFilter: function (ev) {
if (ev && ev.preventDefault) { ev.preventDefault(); }
this.liveFilter();
this.render();
},
isActive: function () {
/* Returns true if the filter is enabled (i.e. if the user
* has added values to the filter).
*/
if (this.model.get('filter_type') === 'state' ||
this.model.get('filter_text')) {
return true;
}
return false;
},
show: function () {
if (this.$el.is(':visible')) { return this; }
this.$el.show();
return this;
},
hide: function () {
if (!this.$el.is(':visible')) { return this; }
if (this.el.querySelector('.roster-filter').value.length > 0) {
// Don't hide if user is currently filtering.
return;
}
this.model.save({
'filter_text': '',
'chat_state': ''
});
this.$el.hide();
return this;
},
clearFilter: function (ev) {
if (ev && ev.preventDefault) {
ev.preventDefault();
$(ev.target).removeClass('x onX').val('');
}
this.model.save({
'filter_text': ''
});
}
});
_converse.RosterView = Backbone.Overview.extend({
tagName: 'div',
id: 'converse-roster',
initialize: function () {
_converse.roster.on("add", this.onContactAdd, this);
_converse.roster.on('change', this.onContactChange, this);
_converse.roster.on("destroy", this.update, this);
_converse.roster.on("remove", this.update, this);
this.model.on("add", this.onGroupAdd, this);
this.model.on("reset", this.reset, this);
_converse.on('rosterGroupsFetched', this.positionFetchedGroups, this);
_converse.on('rosterContactsFetched', this.update, this);
this.createRosterFilter();
},
render: function () {
this.renderRoster();
this.$el.html(this.filter_view.render());
if (!_converse.allow_contact_requests) {
// XXX: if we ever support live editing of config then
// we'll need to be able to remove this class on the fly.
this.el.classList.add('no-contact-requests');
}
return this;
},
renderRoster: function () {
this.$roster = $(tpl_roster());
this.roster = this.$roster[0];
},
createRosterFilter: function () {
// Create a model on which we can store filter properties
var model = new _converse.RosterFilter();
model.id = b64_sha1('_converse.rosterfilter'+_converse.bare_jid);
model.browserStorage = new Backbone.BrowserStorage.local(this.filter.id);
this.filter_view = new _converse.RosterFilterView({'model': model});
this.filter_view.model.on('change', this.updateFilter, this);
this.filter_view.model.fetch();
},
updateFilter: _.debounce(function () {
/* Filter the roster again.
* Called whenever the filter settings have been changed or
* when contacts have been added, removed or changed.
*
* Debounced so that it doesn't get called for every
* contact fetched from browser storage.
*/
var type = this.filter_view.model.get('filter_type');
if (type === 'state') {
this.filter(this.filter_view.model.get('chat_state'), type);
} else {
this.filter(this.filter_view.model.get('filter_text'), type);
}
}, 100),
update: _.debounce(function () {
if (_.isNull(this.roster.parentElement)) {
this.$el.append(this.$roster.show());
}
return this.showHideFilter();
}, _converse.animate ? 100 : 0),
showHideFilter: function () {
if (!this.$el.is(':visible')) {
return;
}
if (this.$roster.hasScrollBar()) {
this.filter_view.show();
} else if (!this.filter_view.isActive()) {
this.filter_view.hide();
}
return this;
},
filter: function (query, type) {
// First we make sure the filter is restored to its
// original state
_.each(this.getAll(), function (view) {
if (view.model.contacts.length > 0) {
view.show().filter('');
}
});
// Now we can filter
query = query.toLowerCase();
if (type === 'groups') {
_.each(this.getAll(), function (view, idx) {
if (!_.includes(view.model.get('name').toLowerCase(), query.toLowerCase())) {
view.hide();
} else if (view.model.contacts.length > 0) {
view.show();
}
});
} else {
_.each(this.getAll(), function (view) {
view.filter(query, type);
});
}
},
reset: function () {
_converse.roster.reset();
this.removeAll();
this.renderRoster();
this.render().update();
return this;
},
onGroupAdd: function (group) {
var view = new _converse.RosterGroupView({model: group});
this.add(group.get('name'), view.render());
this.positionGroup(view);
},
onContactAdd: function (contact) {
this.addRosterContact(contact).update();
this.updateFilter();
},
onContactChange: function (contact) {
this.updateChatBox(contact).update();
if (_.has(contact.changed, 'subscription')) {
if (contact.changed.subscription === 'from') {
this.addContactToGroup(contact, HEADER_PENDING_CONTACTS);
} else if (_.includes(['both', 'to'], contact.get('subscription'))) {
this.addExistingContact(contact);
}
}
if (_.has(contact.changed, 'ask') && contact.changed.ask === 'subscribe') {
this.addContactToGroup(contact, HEADER_PENDING_CONTACTS);
}
if (_.has(contact.changed, 'subscription') && contact.changed.requesting === 'true') {
this.addContactToGroup(contact, HEADER_REQUESTING_CONTACTS);
}
this.updateFilter();
},
updateChatBox: function (contact) {
var chatbox = _converse.chatboxes.get(contact.get('jid')),
changes = {};
if (!chatbox) {
return this;
}
if (_.has(contact.changed, 'chat_status')) {
changes.chat_status = contact.get('chat_status');
}
if (_.has(contact.changed, 'status')) {
changes.status = contact.get('status');
}
chatbox.save(changes);
return this;
},
positionFetchedGroups: function () {
/* Instead of throwing an add event for each group
* fetched, we wait until they're all fetched and then
* we position them.
* Works around the problem of positionGroup not
* working when all groups besides the one being
* positioned aren't already in inserted into the
* roster DOM element.
*/
var that = this;
this.model.sort();
this.model.each(function (group, idx) {
var view = that.get(group.get('name'));
if (!view) {
view = new _converse.RosterGroupView({model: group});
that.add(group.get('name'), view.render());
}
if (idx === 0) {
that.$roster.append(view.$el);
} else {
that.appendGroup(view);
}
});
},
positionGroup: function (view) {
/* Place the group's DOM element in the correct alphabetical
* position amongst the other groups in the roster.
*/
var $groups = this.$roster.find('.roster-group'),
index = $groups.length ? this.model.indexOf(view.model) : 0;
if (index === 0) {
this.$roster.prepend(view.$el);
} else if (index === (this.model.length-1)) {
this.appendGroup(view);
} else {
$($groups.eq(index)).before(view.$el);
}
return this;
},
appendGroup: function (view) {
/* Add the group at the bottom of the roster
*/
var $last = this.$roster.find('.roster-group').last();
var $siblings = $last.siblings('dd');
if ($siblings.length > 0) {
$siblings.last().after(view.$el);
} else {
$last.after(view.$el);
}
return this;
},
getGroup: function (name) {
/* Returns the group as specified by name.
* Creates the group if it doesn't exist.
*/
var view = this.get(name);
if (view) {
return view.model;
}
return this.model.create({name: name, id: b64_sha1(name)});
},
addContactToGroup: function (contact, name) {
this.getGroup(name).contacts.add(contact);
},
addExistingContact: function (contact) {
var groups;
if (_converse.roster_groups) {
groups = contact.get('groups');
if (groups.length === 0) {
groups = [HEADER_UNGROUPED];
}
} else {
groups = [HEADER_CURRENT_CONTACTS];
}
_.each(groups, _.bind(this.addContactToGroup, this, contact));
},
addRosterContact: function (contact) {
if (contact.get('subscription') === 'both' || contact.get('subscription') === 'to') {
this.addExistingContact(contact);
} else {
if ((contact.get('ask') === 'subscribe') || (contact.get('subscription') === 'from')) {
this.addContactToGroup(contact, HEADER_PENDING_CONTACTS);
} else if (contact.get('requesting') === true) {
this.addContactToGroup(contact, HEADER_REQUESTING_CONTACTS);
}
}
return this;
}
});
_converse.RosterContactView = Backbone.View.extend({
tagName: 'dd',
events: {
"click .accept-xmpp-request": "acceptRequest",
"click .decline-xmpp-request": "declineRequest",
"click .open-chat": "openChat",
"click .remove-xmpp-contact": "removeContact"
},
initialize: function () {
this.model.on("change", this.render, this);
this.model.on("remove", this.remove, this);
this.model.on("destroy", this.remove, this);
this.model.on("open", this.openChat, this);
},
render: function () {
var that = this;
if (!this.mayBeShown()) {
this.$el.hide();
return this;
}
var item = this.model,
ask = item.get('ask'),
chat_status = item.get('chat_status'),
requesting = item.get('requesting'),
subscription = item.get('subscription');
var classes_to_remove = [
'current-xmpp-contact',
'pending-xmpp-contact',
'requesting-xmpp-contact'
].concat(_.keys(STATUSES));
_.each(classes_to_remove,
function (cls) {
if (_.includes(that.el.className, cls)) {
that.el.classList.remove(cls);
}
});
this.$el.addClass(chat_status).data('status', chat_status);
if ((ask === 'subscribe') || (subscription === 'from')) {
/* ask === 'subscribe'
* Means we have asked to subscribe to them.
*
* subscription === 'from'
* They are subscribed to use, but not vice versa.
* We assume that there is a pending subscription
* from us to them (otherwise we're in a state not
* supported by converse.js).
*
* So in both cases the user is a "pending" contact.
*/
this.el.classList.add('pending-xmpp-contact');
this.$el.html(tpl_pending_contact(
_.extend(item.toJSON(), {
'desc_remove': __(___('Click to remove %1$s as a contact'), item.get('fullname')),
'allow_chat_pending_contacts': _converse.allow_chat_pending_contacts
})
));
} else if (requesting === true) {
this.el.classList.add('requesting-xmpp-contact');
this.$el.html(tpl_requesting_contact(
_.extend(item.toJSON(), {
'desc_accept': __(___("Click to accept the contact request from %1$s"), item.get('fullname')),
'desc_decline': __(___("Click to decline the contact request from %1$s"), item.get('fullname')),
'allow_chat_pending_contacts': _converse.allow_chat_pending_contacts
})
));
} else if (subscription === 'both' || subscription === 'to') {
this.el.classList.add('current-xmpp-contact');
this.el.classList.remove(_.without(['both', 'to'], subscription)[0])
this.el.classList.add(subscription);
this.renderRosterItem(item);
}
return this;
},
renderRosterItem: function (item) {
var chat_status = item.get('chat_status');
this.$el.html(tpl_roster_item(
_.extend(item.toJSON(), {
'desc_status': STATUSES[chat_status||'offline'],
'desc_chat': __('Click to chat with this contact'),
'desc_remove': __(___('Click to remove %1$s as a contact'), item.get('fullname')),
'title_fullname': __('Name'),
'allow_contact_removal': _converse.allow_contact_removal,
'num_unread': item.get('num_unread') || 0
})
));
return this;
},
isGroupCollapsed: function () {
/* Check whether the group in which this contact appears is
* collapsed.
*/
// XXX: this sucks and is fragile.
// It's because I tried to do the "right thing"
// and use definition lists to represent roster groups.
// If roster group items were inside the group elements, we
// would simplify things by not having to check whether the
// group is collapsed or not.
var name = this.$el.prevAll('dt:first').data('group');
var group = _.head(_converse.rosterview.model.where({'name': name.toString()}));
if (group.get('state') === _converse.CLOSED) {
return true;
}
return false;
},
mayBeShown: function () {
/* Return a boolean indicating whether this contact should
* generally be visible in the roster.
*
* It doesn't check for the more specific case of whether
* the group it's in is collapsed (see isGroupCollapsed).
*/
var chatStatus = this.model.get('chat_status');
if ((_converse.show_only_online_users && chatStatus !== 'online') ||
(_converse.hide_offline_users && chatStatus === 'offline')) {
// If pending or requesting, show
if ((this.model.get('ask') === 'subscribe') ||
(this.model.get('subscription') === 'from') ||
(this.model.get('requesting') === true)) {
return true;
}
return false;
}
return true;
},
openChat: function (ev) {
if (ev && ev.preventDefault) { ev.preventDefault(); }
return _converse.chatboxviews.showChat(this.model.attributes, true);
},
removeContact: function (ev) {
if (ev && ev.preventDefault) { ev.preventDefault(); }
if (!_converse.allow_contact_removal) { return; }
var result = confirm(__("Are you sure you want to remove this contact?"));
if (result === true) {
var iq = $iq({type: 'set'})
.c('query', {xmlns: Strophe.NS.ROSTER})
.c('item', {jid: this.model.get('jid'), subscription: "remove"});
_converse.connection.sendIQ(iq,
function (iq) {
this.model.destroy();
this.remove();
}.bind(this),
function (err) {
alert(__("Sorry, there was an error while trying to remove "+name+" as a contact."));
_converse.log(err, Strophe.LogLevel.ERROR);
}
);
}
},
acceptRequest: function (ev) {
if (ev && ev.preventDefault) { ev.preventDefault(); }
_converse.roster.sendContactAddIQ(
this.model.get('jid'),
this.model.get('fullname'),
[],
function () { this.model.authorize().subscribe(); }.bind(this)
);
},
declineRequest: function (ev) {
if (ev && ev.preventDefault) { ev.preventDefault(); }
var result = confirm(__("Are you sure you want to decline this contact request?"));
if (result === true) {
this.model.unauthorize().destroy();
}
return this;
}
});
_converse.RosterGroupView = Backbone.Overview.extend({
tagName: 'dt',
className: 'roster-group',
events: {
"click a.group-toggle": "toggle"
},
initialize: function () {
this.model.contacts.on("add", this.addContact, this);
this.model.contacts.on("change:subscription", this.onContactSubscriptionChange, this);
this.model.contacts.on("change:requesting", this.onContactRequestChange, this);
this.model.contacts.on("change:chat_status", function (contact) {
// This might be optimized by instead of first sorting,
// finding the correct position in positionContact
this.model.contacts.sort();
this.positionContact(contact).render();
}, this);
this.model.contacts.on("destroy", this.onRemove, this);
this.model.contacts.on("remove", this.onRemove, this);
_converse.roster.on('change:groups', this.onContactGroupChange, this);
},
render: function () {
this.el.setAttribute('data-group', this.model.get('name'));
var html = tpl_group_header({
label_group: this.model.get('name'),
desc_group_toggle: this.model.get('description'),
toggle_state: this.model.get('state')
});
this.el.innerHTML = html;
return this;
},
addContact: function (contact) {
var view = new _converse.RosterContactView({model: contact});
this.add(contact.get('id'), view);
view = this.positionContact(contact).render();
if (view.mayBeShown()) {
if (this.model.get('state') === _converse.CLOSED) {
if (view.$el[0].style.display !== "none") { view.$el.hide(); }
if (!this.$el.is(':visible')) { this.$el.show(); }
} else {
if (this.$el[0].style.display !== "block") { this.show(); }
}
}
},
positionContact: function (contact) {
/* Place the contact's DOM element in the correct alphabetical
* position amongst the other contacts in this group.
*/
var view = this.get(contact.get('id'));
var index = this.model.contacts.indexOf(contact);
view.$el.detach();
if (index === 0) {
this.$el.after(view.$el);
} else if (index === (this.model.contacts.length-1)) {
this.$el.nextUntil('dt').last().after(view.$el);
} else {
this.$el.nextUntil('dt').eq(index).before(view.$el);
}
return view;
},
show: function () {
this.$el.show();
_.each(this.getAll(), function (view) {
if (view.mayBeShown() && !view.isGroupCollapsed()) {
view.$el.show();
}
});
return this;
},
hide: function () {
this.$el.nextUntil('dt').addBack().hide();
},
filter: function (q, type) {
/* Filter the group's contacts based on the query "q".
* The query is matched against the contact's full name.
* If all contacts are filtered out (i.e. hidden), then the
* group must be filtered out as well.
*/
var matches;
if (q.length === 0) {
if (this.model.get('state') === _converse.OPENED) {
this.model.contacts.each(function (item) {
var view = this.get(item.get('id'));
if (view.mayBeShown() && !view.isGroupCollapsed()) {
view.$el.show();
}
}.bind(this));
}
this.showIfNecessary();
} else {
q = q.toLowerCase();
if (type === 'state') {
if (this.model.get('name') === HEADER_REQUESTING_CONTACTS) {
// When filtering by chat state, we still want to
// show requesting contacts, even though they don't
// have the state in question.
matches = this.model.contacts.filter(
function (contact) {
return utils.contains.not('chat_status', q)(contact) && !contact.get('requesting');
}
);
} else if (q === 'unread_messages') {
matches = this.model.contacts.filter({'num_unread': 0});
} else {
matches = this.model.contacts.filter(
utils.contains.not('chat_status', q)
);
}
} else {
matches = this.model.contacts.filter(
utils.contains.not('fullname', q)
);
}
if (matches.length === this.model.contacts.length) {
// hide the whole group
this.hide();
} else {
_.each(matches, function (item) {
this.get(item.get('id')).$el.hide();
}.bind(this));
_.each(this.model.contacts.reject(utils.contains.not('fullname', q)), function (item) {
this.get(item.get('id')).$el.show();
}.bind(this));
this.showIfNecessary();
}
}
},
showIfNecessary: function () {
if (!this.$el.is(':visible') && this.model.contacts.length > 0) {
this.$el.show();
}
},
toggle: function (ev) {
if (ev && ev.preventDefault) { ev.preventDefault(); }
var $el = $(ev.target);
if ($el.hasClass("icon-opened")) {
this.$el.nextUntil('dt').slideUp();
this.model.save({state: _converse.CLOSED});
$el.removeClass("icon-opened").addClass("icon-closed");
} else {
$el.removeClass("icon-closed").addClass("icon-opened");
this.model.save({state: _converse.OPENED});
this.filter(
_converse.rosterview.$('.roster-filter').val() || '',
_converse.rosterview.$('.filter-type').val()
);
}
},
onContactGroupChange: function (contact) {
var in_this_group = _.includes(contact.get('groups'), this.model.get('name'));
var cid = contact.get('id');
var in_this_overview = !this.get(cid);
if (in_this_group && !in_this_overview) {
this.model.contacts.remove(cid);
} else if (!in_this_group && in_this_overview) {
this.addContact(contact);
}
},
onContactSubscriptionChange: function (contact) {
if ((this.model.get('name') === HEADER_PENDING_CONTACTS) && contact.get('subscription') !== 'from') {
this.model.contacts.remove(contact.get('id'));
}
},
onContactRequestChange: function (contact) {
if ((this.model.get('name') === HEADER_REQUESTING_CONTACTS) && !contact.get('requesting')) {
/* We suppress events, otherwise the remove event will
* also cause the contact's view to be removed from the
* "Pending Contacts" group.
*/
this.model.contacts.remove(contact.get('id'), {'silent': true});
// Since we suppress events, we make sure the view and
// contact are removed from this group.
this.get(contact.get('id')).remove();
this.onRemove(contact);
}
},
onRemove: function (contact) {
this.remove(contact.get('id'));
if (this.model.contacts.length === 0) {
this.$el.hide();
}
}
});
/* -------- Event Handlers ----------- */
var onChatBoxMaximized = function (chatboxview) {
/* When a chat box gets maximized, the num_unread counter needs
* to be cleared, but if chatbox is scrolled up, then num_unread should not be cleared.
*/
var chatbox = chatboxview.model;
if (chatbox.get('type') !== 'chatroom') {
var contact = _.head(_converse.roster.where({'jid': chatbox.get('jid')}));
if (!_.isUndefined(contact) && !chatbox.isScrolledUp()) {
contact.save({'num_unread': 0});
}
}
};
var onMessageReceived = function (data) {
/* Given a newly received message, update the unread counter on
* the relevant roster contact.
*/
var chatbox = data.chatbox;
if (_.isUndefined(chatbox)) {
return;
}
if (_.isNull(data.stanza.querySelector('body'))) {
return; // The message has no text
}
if (chatbox.get('type') !== 'chatroom' &&
utils.isNewMessage(data.stanza) &&
chatbox.newMessageWillBeHidden()) {
var contact = _.head(_converse.roster.where({'jid': chatbox.get('jid')}));
if (!_.isUndefined(contact)) {
contact.save({'num_unread': contact.get('num_unread') + 1});
}
}
};
var onChatBoxScrolledDown = function (data) {
var chatbox = data.chatbox;
if (_.isUndefined(chatbox)) {
return;
}
var contact = _.head(_converse.roster.where({'jid': chatbox.get('jid')}));
if (!_.isUndefined(contact)) {
contact.save({'num_unread': 0});
}
};
var initRoster = function () {
/* Create an instance of RosterView once the RosterGroups
* collection has been created (in converse-core.js)
*/
_converse.rosterview = new _converse.RosterView({
'model': _converse.rostergroups
});
_converse.rosterview.render();
};
_converse.api.listen.on('rosterInitialized', initRoster);
_converse.api.listen.on('rosterReadyAfterReconnection', initRoster);
_converse.api.listen.on('message', onMessageReceived);
_converse.api.listen.on('chatBoxMaximized', onChatBoxMaximized);
_converse.api.listen.on('chatBoxScrolledDown', onChatBoxScrolledDown);
}
});
}));
// Converse.js (A browser based XMPP chat client)
// http://conversejs.org
//
// Copyright (c) 2012-2017, Jan-Carel Brand
// Licensed under the Mozilla Public License (MPLv2)
//
/*global define */
(function (root, factory) {
define('converse-controlbox',["converse-core",
"tpl!add_contact_dropdown",
"tpl!add_contact_form",
"tpl!change_status_message",
"tpl!chat_status",
"tpl!choose_status",
"tpl!contacts_panel",
"tpl!contacts_tab",
"tpl!controlbox",
"tpl!controlbox_toggle",
"tpl!login_panel",
"tpl!login_tab",
"tpl!search_contact",
"tpl!status_option",
"converse-chatview",
"converse-rosterview"
], factory);
}(this, function (
converse,
tpl_add_contact_dropdown,
tpl_add_contact_form,
tpl_change_status_message,
tpl_chat_status,
tpl_choose_status,
tpl_contacts_panel,
tpl_contacts_tab,
tpl_controlbox,
tpl_controlbox_toggle,
tpl_login_panel,
tpl_login_tab,
tpl_search_contact,
tpl_status_option
) {
"use strict";
var USERS_PANEL_ID = 'users';
var CHATBOX_TYPE = 'chatbox';
// Strophe methods for building stanzas
var Strophe = converse.env.Strophe,
Backbone = converse.env.Backbone,
utils = converse.env.utils;
// Other necessary globals
var $ = converse.env.jQuery,
_ = converse.env._,
fp = converse.env.fp,
moment = converse.env.moment;
converse.plugins.add('converse-controlbox', {
overrides: {
// 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.
initChatBoxes: function () {
this.__super__.initChatBoxes.apply(this, arguments);
this.controlboxtoggle = new this.ControlBoxToggle();
},
initConnection: function () {
this.__super__.initConnection.apply(this, arguments);
if (this.connection) {
this.addControlBox();
}
},
_tearDown: function () {
this.__super__._tearDown.apply(this, arguments);
if (this.rosterview) {
// Removes roster groups
this.rosterview.model.off().reset();
this.rosterview.each(function (groupview) {
groupview.removeAll();
groupview.remove();
});
this.rosterview.removeAll().remove();
}
},
clearSession: function () {
this.__super__.clearSession.apply(this, arguments);
var controlbox = this.chatboxes.get('controlbox');
if (controlbox &&
controlbox.collection &&
controlbox.collection.browserStorage) {
controlbox.save({'connected': false});
}
},
ChatBoxes: {
chatBoxMayBeShown: function (chatbox) {
return this.__super__.chatBoxMayBeShown.apply(this, arguments) &&
chatbox.get('id') !== 'controlbox';
},
onChatBoxesFetched: function (collection, resp) {
var _converse = this.__super__._converse;
this.__super__.onChatBoxesFetched.apply(this, arguments);
if (!_.includes(_.map(collection, 'id'), 'controlbox')) {
_converse.addControlBox();
}
this.get('controlbox').save({connected:true});
},
},
ChatBoxViews: {
onChatBoxAdded: function (item) {
var _converse = this.__super__._converse;
if (item.get('box_id') === 'controlbox') {
var view = this.get(item.get('id'));
if (view) {
view.model = item;
view.initialize();
return view;
} else {
view = new _converse.ControlBoxView({model: item});
return this.add(item.get('id'), view);
}
} else {
return this.__super__.onChatBoxAdded.apply(this, arguments);
}
},
closeAllChatBoxes: function () {
var _converse = this.__super__._converse;
this.each(function (view) {
if (view.model.get('id') === 'controlbox' &&
(_converse.disconnection_cause !== _converse.LOGOUT || _converse.show_controlbox_by_default)) {
return;
}
view.close();
});
return this;
},
getChatBoxWidth: function (view) {
var _converse = this.__super__._converse;
var controlbox = this.get('controlbox');
if (view.model.get('id') === 'controlbox') {
/* We return the width of the controlbox or its toggle,
* depending on which is visible.
*/
if (!controlbox || !controlbox.$el.is(':visible')) {
return _converse.controlboxtoggle.$el.outerWidth(true);
} else {
return controlbox.$el.outerWidth(true);
}
} else {
return this.__super__.getChatBoxWidth.apply(this, arguments);
}
}
},
ChatBox: {
initialize: function () {
if (this.get('id') === 'controlbox') {
this.set({'time_opened': moment(0).valueOf()});
} else {
this.__super__.initialize.apply(this, arguments);
}
},
},
ChatBoxView: {
insertIntoDOM: function () {
var _converse = this.__super__._converse;
this.$el.insertAfter(_converse.chatboxviews.get("controlbox").$el);
return this;
}
}
},
initialize: function () {
/* The initialize function gets called as soon as the plugin is
* loaded by converse.js's plugin machinery.
*/
var _converse = this._converse,
__ = _converse.__;
_converse.api.settings.update({
allow_logout: true,
default_domain: undefined,
show_controlbox_by_default: false,
sticky_controlbox: false,
xhr_user_search: false,
xhr_user_search_url: ''
});
var LABEL_CONTACTS = __('Contacts');
_converse.addControlBox = function () {
return _converse.chatboxes.add({
id: 'controlbox',
box_id: 'controlbox',
closed: !_converse.show_controlbox_by_default
});
};
_converse.ControlBoxView = _converse.ChatBoxView.extend({
tagName: 'div',
className: 'chatbox',
id: 'controlbox',
events: {
'click a.close-chatbox-button': 'close',
'click ul#controlbox-tabs li a': 'switchTab',
},
initialize: function () {
this.$el.insertAfter(_converse.controlboxtoggle.$el);
this.model.on('change:connected', this.onConnected, this);
this.model.on('destroy', this.hide, this);
this.model.on('hide', this.hide, this);
this.model.on('show', this.show, this);
this.model.on('change:closed', this.ensureClosedState, this);
this.render();
if (this.model.get('connected')) {
this.insertRoster();
}
},
render: function () {
if (this.model.get('connected')) {
if (_.isUndefined(this.model.get('closed'))) {
this.model.set('closed', !_converse.show_controlbox_by_default);
}
}
if (!this.model.get('closed')) {
this.show();
} else {
this.hide();
}
this.el.innerHTML = tpl_controlbox(
_.extend(this.model.toJSON(), {
'sticky_controlbox': _converse.sticky_controlbox
}));
if (!_converse.connection.connected ||
!_converse.connection.authenticated ||
_converse.connection.disconnecting) {
this.renderLoginPanel();
} else if (this.model.get('connected') &&
(!this.contactspanel || !this.contactspanel.$el.is(':visible'))) {
this.renderContactsPanel();
}
return this;
},
onConnected: function () {
if (this.model.get('connected')) {
this.render().insertRoster();
this.model.save();
}
},
insertRoster: function () {
/* Place the rosterview inside the "Contacts" panel.
*/
this.contactspanel.$el.append(_converse.rosterview.$el);
return this;
},
renderLoginPanel: function () {
this.loginpanel = new _converse.LoginPanel({
'$parent': this.$el.find('.controlbox-panes'),
'model': this
});
this.loginpanel.render();
return this;
},
renderContactsPanel: function () {
if (_.isUndefined(this.model.get('active-panel'))) {
this.model.save({'active-panel': USERS_PANEL_ID});
}
this.contactspanel = new _converse.ContactsPanel({
'$parent': this.$el.find('.controlbox-panes')
});
this.contactspanel.insertIntoDOM();
_converse.xmppstatusview = new _converse.XMPPStatusView({
'model': _converse.xmppstatus
});
_converse.xmppstatusview.render();
},
close: function (ev) {
if (ev && ev.preventDefault) { ev.preventDefault(); }
if (_converse.sticky_controlbox) {
return;
}
if (_converse.connection.connected && !_converse.connection.disconnecting) {
this.model.save({'closed': true});
} else {
this.model.trigger('hide');
}
_converse.emit('controlBoxClosed', this);
return this;
},
ensureClosedState: function () {
if (this.model.get('closed')) {
this.hide();
} else {
this.show();
}
},
hide: function (callback) {
if (_converse.sticky_controlbox) {
return;
}
this.$el.addClass('hidden');
utils.refreshWebkit();
_converse.emit('chatBoxClosed', this);
if (!_converse.connection.connected) {
_converse.controlboxtoggle.render();
}
_converse.controlboxtoggle.show(callback);
return this;
},
onControlBoxToggleHidden: function () {
var that = this;
utils.fadeIn(this.el, function () {
_converse.controlboxtoggle.updateOnlineCount();
utils.refreshWebkit();
that.model.set('closed', false);
_converse.emit('controlBoxOpened', that);
});
},
show: function () {
_converse.controlboxtoggle.hide(
this.onControlBoxToggleHidden.bind(this)
);
return this;
},
switchTab: function (ev) {
// TODO: automatically focus the relevant input
if (ev && ev.preventDefault) { ev.preventDefault(); }
var $tab = $(ev.target),
$sibling = $tab.parent().siblings('li').children('a'),
$tab_panel = $($tab.attr('href'));
$($sibling.attr('href')).addClass('hidden');
$sibling.removeClass('current');
$tab.addClass('current');
$tab_panel.removeClass('hidden');
if (!_.isUndefined(_converse.chatboxes.browserStorage)) {
this.model.save({'active-panel': $tab.data('id')});
}
return this;
},
showHelpMessages: function () {
/* Override showHelpMessages in ChatBoxView, for now do nothing.
*
* Parameters:
* (Array) msgs: Array of messages
*/
return;
}
});
_converse.LoginPanel = Backbone.View.extend({
tagName: 'div',
id: "login-dialog",
className: 'controlbox-pane',
events: {
'submit form#converse-login': 'authenticate'
},
initialize: function (cfg) {
cfg.$parent.html(this.$el.html(
tpl_login_panel({
'ANONYMOUS': _converse.ANONYMOUS,
'EXTERNAL': _converse.EXTERNAL,
'LOGIN': _converse.LOGIN,
'PREBIND': _converse.PREBIND,
'auto_login': _converse.auto_login,
'authentication': _converse.authentication,
'label_username': __('XMPP Username:'),
'label_password': __('Password:'),
'label_anon_login': __('Click here to log in anonymously'),
'label_login': __('Log In'),
'placeholder_username': (_converse.locked_domain || _converse.default_domain) && __('Username') || __('user@server'),
'placeholder_password': __('password')
})
));
this.$tabs = cfg.$parent.parent().find('#controlbox-tabs');
},
render: function () {
this.$tabs.append(tpl_login_tab({label_sign_in: __('Sign in')}));
this.$el.find('input#jid').focus();
if (!this.$el.is(':visible')) {
this.$el.show();
}
return this;
},
authenticate: function (ev) {
if (ev && ev.preventDefault) { ev.preventDefault(); }
var $form = $(ev.target);
if (_converse.authentication === _converse.ANONYMOUS) {
this.connect($form, _converse.jid, null);
return;
}
var $jid_input = $form.find('input[name=jid]'),
jid = $jid_input.val(),
$pw_input = $form.find('input[name=password]'),
password = $pw_input.val(),
errors = false;
if (!jid) {
errors = true;
$jid_input.addClass('error');
}
if (!password && _converse.authentication !== _converse.EXTERNAL) {
errors = true;
$pw_input.addClass('error');
}
if (errors) { return; }
if (_converse.locked_domain) {
jid = Strophe.escapeNode(jid) + '@' + _converse.locked_domain;
} else if (_converse.default_domain && !_.includes(jid, '@')) {
jid = jid + '@' + _converse.default_domain;
}
this.connect($form, jid, password);
return false;
},
connect: function ($form, jid, password) {
var resource;
if ($form) {
$form.find('input[type=submit]').hide().after('');
}
if (jid) {
resource = Strophe.getResourceFromJid(jid);
if (!resource) {
jid = jid.toLowerCase() + _converse.generateResource();
} else {
jid = Strophe.getBareJidFromJid(jid).toLowerCase()+'/'+resource;
}
}
_converse.connection.reset();
_converse.connection.connect(jid, password, _converse.onConnectStatusChanged);
},
remove: function () {
this.$tabs.empty();
this.$el.parent().empty();
}
});
_converse.XMPPStatusView = Backbone.View.extend({
el: "form#set-xmpp-status",
events: {
"click a.choose-xmpp-status": "toggleOptions",
"click #fancy-xmpp-status-select a.change-xmpp-status-message": "renderStatusChangeForm",
"submit": "setStatusMessage",
"click .dropdown dd ul li a": "setStatus"
},
initialize: function () {
this.model.on("change:status", this.updateStatusUI, this);
this.model.on("change:status_message", this.updateStatusUI, this);
this.model.on("update-status-ui", this.updateStatusUI, this);
},
render: function () {
// Replace the default dropdown with something nicer
var $select = this.$el.find('select#select-xmpp-status'),
chat_status = this.model.get('status') || 'offline',
options = $('option', $select),
$options_target,
options_list = [];
this.$el.html(tpl_choose_status());
this.$el.find('#fancy-xmpp-status-select')
.html(tpl_chat_status({
'status_message': this.model.get('status_message') || __("I am %1$s", this.getPrettyStatus(chat_status)),
'chat_status': chat_status,
'desc_custom_status': __('Click here to write a custom status message'),
'desc_change_status': __('Click to change your chat status')
}));
// iterate through all the