2016-02-16 08:46:47 +01:00
|
|
|
// Converse.js (A browser based XMPP chat client)
|
|
|
|
// http://conversejs.org
|
|
|
|
//
|
2017-02-02 19:29:38 +01:00
|
|
|
// Copyright (c) 2012-2017, Jan-Carel Brand <jc@opkode.com>
|
2016-02-16 08:46:47 +01:00
|
|
|
// Licensed under the Mozilla Public License (MPLv2)
|
|
|
|
//
|
2018-02-09 16:02:56 +01:00
|
|
|
/*global Backbone, define, window, JSON */
|
2016-02-16 08:46:47 +01:00
|
|
|
(function (root, factory) {
|
2017-02-14 15:08:39 +01:00
|
|
|
define(["sizzle",
|
2017-07-10 21:14:48 +02:00
|
|
|
"es6-promise",
|
2017-04-21 15:52:16 +02:00
|
|
|
"lodash.noconflict",
|
2018-02-08 17:06:53 +01:00
|
|
|
"lodash.fp",
|
2017-02-14 15:08:39 +01:00
|
|
|
"polyfill",
|
2017-10-24 12:56:35 +02:00
|
|
|
"i18n",
|
2017-02-14 15:08:39 +01:00
|
|
|
"utils",
|
2017-10-30 10:25:30 +01:00
|
|
|
"moment",
|
2017-02-14 15:08:39 +01:00
|
|
|
"strophe",
|
|
|
|
"pluggable",
|
2017-04-21 15:52:16 +02:00
|
|
|
"backbone.noconflict",
|
2018-01-03 20:02:05 +01:00
|
|
|
"backbone.nativeview",
|
2017-12-20 20:29:57 +01:00
|
|
|
"backbone.browserStorage"
|
2016-02-23 08:13:30 +01:00
|
|
|
], factory);
|
2018-02-19 10:34:52 +01:00
|
|
|
}(this, function (sizzle, Promise, _, f, polyfill, i18n, u, moment, Strophe, pluggable, Backbone) {
|
2017-06-07 09:15:22 +02:00
|
|
|
|
2017-02-18 10:35:16 +01:00
|
|
|
/* Cannot use this due to Safari bug.
|
2016-02-16 08:46:47 +01:00
|
|
|
* See https://github.com/jcbrand/converse.js/issues/196
|
|
|
|
*/
|
2016-02-23 08:13:30 +01:00
|
|
|
// "use strict";
|
|
|
|
|
|
|
|
// Strophe globals
|
2017-07-10 17:46:22 +02:00
|
|
|
const { $build, $iq, $msg, $pres } = Strophe;
|
2017-07-11 15:22:05 +02:00
|
|
|
const b64_sha1 = Strophe.SHA1.b64_sha1;
|
|
|
|
Strophe = Strophe.Strophe;
|
2016-02-16 08:46:47 +01:00
|
|
|
|
2017-09-24 20:36:39 +02:00
|
|
|
// 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');
|
2018-01-04 14:05:35 +01:00
|
|
|
Strophe.addNamespace('FORWARD', 'urn:xmpp:forward:0');
|
2017-09-24 20:36:39 +02:00
|
|
|
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');
|
2018-01-04 14:05:35 +01:00
|
|
|
Strophe.addNamespace('SID', 'urn:xmpp:sid:0');
|
2018-02-05 21:42:29 +01:00
|
|
|
Strophe.addNamespace('SPOILER', 'urn:xmpp:spoiler:0');
|
2017-09-24 20:36:39 +02:00
|
|
|
Strophe.addNamespace('XFORM', 'jabber:x:data');
|
|
|
|
|
2016-02-16 08:46:47 +01:00
|
|
|
// Use Mustache style syntax for variable interpolation
|
2017-01-26 10:08:43 +01:00
|
|
|
/* Configuration of Lodash templates (this config is distinct to the
|
2016-02-16 08:46:47 +01:00
|
|
|
* config of requirejs-tpl in main.js). This one is for normal inline templates.
|
|
|
|
*/
|
|
|
|
_.templateSettings = {
|
2017-05-09 09:45:12 +02:00
|
|
|
'escape': /\{\{\{([\s\S]+?)\}\}\}/g,
|
|
|
|
'evaluate': /\{\[([\s\S]+?)\]\}/g,
|
2017-09-26 18:27:41 +02:00
|
|
|
'interpolate': /\{\{([\s\S]+?)\}\}/g,
|
|
|
|
'imports': { '_': _ }
|
2016-02-16 08:46:47 +01:00
|
|
|
};
|
|
|
|
|
2017-07-10 21:14:48 +02:00
|
|
|
const _converse = {
|
|
|
|
'templates': {},
|
|
|
|
'promises': {}
|
|
|
|
}
|
|
|
|
|
2017-07-11 15:22:05 +02:00
|
|
|
_.extend(_converse, Backbone.Events);
|
2016-02-16 08:46:47 +01:00
|
|
|
|
2017-02-03 19:12:19 +01:00
|
|
|
_converse.core_plugins = [
|
|
|
|
'converse-bookmarks',
|
2017-08-16 11:16:22 +02:00
|
|
|
'converse-chatboxes',
|
2017-02-03 19:12:19 +01:00
|
|
|
'converse-chatview',
|
|
|
|
'converse-controlbox',
|
|
|
|
'converse-core',
|
2017-07-21 12:41:16 +02:00
|
|
|
'converse-disco',
|
2017-02-03 19:12:19 +01:00
|
|
|
'converse-dragresize',
|
2017-11-02 15:30:24 +01:00
|
|
|
'converse-fullscreen',
|
2017-02-03 19:12:19 +01:00
|
|
|
'converse-headline',
|
|
|
|
'converse-mam',
|
|
|
|
'converse-minimize',
|
2018-02-22 18:41:01 +01:00
|
|
|
'converse-modal',
|
2017-02-03 19:12:19 +01:00
|
|
|
'converse-muc',
|
2018-02-09 16:33:48 +01:00
|
|
|
'converse-muc-embedded',
|
2018-02-21 22:29:21 +01:00
|
|
|
'converse-muc-views',
|
2017-02-03 19:12:19 +01:00
|
|
|
'converse-notification',
|
|
|
|
'converse-otr',
|
|
|
|
'converse-ping',
|
2017-11-17 13:32:03 +01:00
|
|
|
'converse-profile',
|
2017-02-03 19:12:19 +01:00
|
|
|
'converse-register',
|
2017-06-14 10:47:57 +02:00
|
|
|
'converse-roomslist',
|
2017-02-03 19:12:19 +01:00
|
|
|
'converse-rosterview',
|
2017-11-01 10:37:24 +01:00
|
|
|
'converse-singleton',
|
2018-02-09 16:33:48 +01:00
|
|
|
'converse-spoilers',
|
|
|
|
'converse-vcard'
|
2017-02-03 19:12:19 +01:00
|
|
|
];
|
|
|
|
|
2016-06-09 11:01:54 +02:00
|
|
|
// Make converse pluggable
|
2016-12-20 11:28:30 +01:00
|
|
|
pluggable.enable(_converse, '_converse', 'pluggable');
|
2016-06-09 11:01:54 +02:00
|
|
|
|
2016-02-20 16:06:12 +01:00
|
|
|
// Module-level constants
|
2016-12-20 10:30:20 +01:00
|
|
|
_converse.STATUS_WEIGHTS = {
|
2016-02-16 08:46:47 +01:00
|
|
|
'offline': 6,
|
|
|
|
'unavailable': 5,
|
|
|
|
'xa': 4,
|
|
|
|
'away': 3,
|
|
|
|
'dnd': 2,
|
|
|
|
'chat': 1, // We currently don't differentiate between "chat" and "online"
|
|
|
|
'online': 1
|
|
|
|
};
|
2017-02-28 06:46:21 +01:00
|
|
|
_converse.PRETTY_CHAT_STATUS = {
|
|
|
|
'offline': 'Offline',
|
|
|
|
'unavailable': 'Unavailable',
|
|
|
|
'xa': 'Extended Away',
|
|
|
|
'away': 'Away',
|
|
|
|
'dnd': 'Do not disturb',
|
|
|
|
'chat': 'Chattty',
|
|
|
|
'online': 'Online'
|
|
|
|
};
|
2016-12-20 10:30:20 +01:00
|
|
|
_converse.ANONYMOUS = "anonymous";
|
|
|
|
_converse.CLOSED = 'closed';
|
|
|
|
_converse.EXTERNAL = "external";
|
|
|
|
_converse.LOGIN = "login";
|
|
|
|
_converse.LOGOUT = "logout";
|
|
|
|
_converse.OPENED = 'opened';
|
|
|
|
_converse.PREBIND = "prebind";
|
2016-02-16 08:46:47 +01:00
|
|
|
|
2017-09-22 18:55:02 +02:00
|
|
|
_converse.CONNECTION_STATUS = {
|
2016-02-29 21:54:34 +01:00
|
|
|
0: 'ERROR',
|
|
|
|
1: 'CONNECTING',
|
|
|
|
2: 'CONNFAIL',
|
|
|
|
3: 'AUTHENTICATING',
|
|
|
|
4: 'AUTHFAIL',
|
|
|
|
5: 'CONNECTED',
|
|
|
|
6: 'DISCONNECTED',
|
|
|
|
7: 'DISCONNECTING',
|
|
|
|
8: 'ATTACHED',
|
2017-09-22 18:55:02 +02:00
|
|
|
9: 'REDIRECT',
|
2017-12-13 23:04:49 +01:00
|
|
|
10: 'RECONNECTING',
|
2016-02-29 21:54:34 +01:00
|
|
|
};
|
|
|
|
|
2017-08-16 11:16:22 +02:00
|
|
|
_converse.DEFAULT_IMAGE_TYPE = 'image/png';
|
|
|
|
_converse.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==";
|
2017-01-23 11:15:07 +01:00
|
|
|
|
2017-10-26 15:47:49 +02:00
|
|
|
_converse.log = function (message, level, style='') {
|
2017-09-26 18:27:41 +02:00
|
|
|
/* Logs messages to the browser's developer console.
|
|
|
|
*
|
|
|
|
* Parameters:
|
|
|
|
* (String) message - The message to be logged.
|
|
|
|
* (Integer) level - The loglevel which allows for filtering of log
|
|
|
|
* messages.
|
2017-10-24 12:56:35 +02:00
|
|
|
*
|
2017-09-26 18:27:41 +02:00
|
|
|
* Available loglevels are 0 for 'debug', 1 for 'info', 2 for 'warn',
|
|
|
|
* 3 for 'error' and 4 for 'fatal'.
|
|
|
|
*
|
2017-10-24 12:56:35 +02:00
|
|
|
* When using the 'error' or 'warn' loglevels, a full stacktrace will be
|
2017-09-26 18:27:41 +02:00
|
|
|
* logged as well.
|
|
|
|
*/
|
2017-10-26 15:47:49 +02:00
|
|
|
if (level === Strophe.LogLevel.ERROR || level === Strophe.LogLevel.FATAL) {
|
|
|
|
style = style || 'color: maroon';
|
|
|
|
}
|
2017-07-21 12:38:16 +02:00
|
|
|
if (message instanceof Error) {
|
|
|
|
message = message.stack;
|
|
|
|
}
|
2017-10-26 15:47:49 +02:00
|
|
|
const prefix = style ? '%c' : '';
|
2017-08-21 12:52:18 +02:00
|
|
|
const logger = _.assign({
|
|
|
|
'debug': _.get(console, 'log') ? console.log.bind(console) : _.noop,
|
|
|
|
'error': _.get(console, 'log') ? console.log.bind(console) : _.noop,
|
|
|
|
'info': _.get(console, 'log') ? console.log.bind(console) : _.noop,
|
|
|
|
'warn': _.get(console, 'log') ? console.log.bind(console) : _.noop
|
2017-07-10 10:43:13 +02:00
|
|
|
}, console);
|
2017-07-05 11:33:55 +02:00
|
|
|
if (level === Strophe.LogLevel.ERROR) {
|
2018-01-29 11:16:18 +01:00
|
|
|
logger.error(`${prefix} ERROR: ${message}`, style);
|
2017-07-05 11:33:55 +02:00
|
|
|
} else if (level === Strophe.LogLevel.WARN) {
|
2017-10-25 17:09:35 +02:00
|
|
|
if (_converse.debug) {
|
2017-10-26 15:47:49 +02:00
|
|
|
logger.warn(`${prefix} ${moment().format()} WARNING: ${message}`, style);
|
2017-10-25 17:09:35 +02:00
|
|
|
}
|
2017-07-05 11:33:55 +02:00
|
|
|
} else if (level === Strophe.LogLevel.FATAL) {
|
2018-01-29 11:16:18 +01:00
|
|
|
logger.error(`${prefix} FATAL: ${message}`, style);
|
2017-07-05 11:33:55 +02:00
|
|
|
} else if (_converse.debug) {
|
|
|
|
if (level === Strophe.LogLevel.DEBUG) {
|
2017-10-26 15:47:49 +02:00
|
|
|
logger.debug(`${prefix} ${moment().format()} DEBUG: ${message}`, style);
|
2017-07-10 10:43:13 +02:00
|
|
|
} else {
|
2017-10-26 15:47:49 +02:00
|
|
|
logger.info(`${prefix} ${moment().format()} INFO: ${message}`, style);
|
2016-02-29 21:54:34 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-10-25 17:09:35 +02:00
|
|
|
Strophe.log = function (level, msg) { _converse.log(level+' '+msg, level); };
|
|
|
|
Strophe.error = function (msg) { _converse.log(msg, Strophe.LogLevel.ERROR); };
|
|
|
|
|
|
|
|
|
2017-09-26 18:27:41 +02:00
|
|
|
_converse.__ = function (str) {
|
|
|
|
/* Translate the given string based on the current locale.
|
|
|
|
*
|
|
|
|
* Parameters:
|
|
|
|
* (String) str - The string to translate.
|
|
|
|
*/
|
2017-10-24 12:56:35 +02:00
|
|
|
if (_.isUndefined(i18n)) {
|
2017-09-26 18:27:41 +02:00
|
|
|
return str;
|
|
|
|
}
|
2017-10-24 12:56:35 +02:00
|
|
|
return i18n.translate.apply(i18n, arguments);
|
2017-09-27 00:33:58 +02:00
|
|
|
}
|
2017-09-26 18:27:41 +02:00
|
|
|
|
2017-10-24 12:56:35 +02:00
|
|
|
const __ = _converse.__;
|
2017-09-26 18:27:41 +02:00
|
|
|
|
2017-07-11 15:22:05 +02:00
|
|
|
const PROMISES = [
|
2017-08-16 12:29:31 +02:00
|
|
|
'initialized',
|
2017-07-11 15:22:05 +02:00
|
|
|
'cachedRoster',
|
2017-08-16 12:31:17 +02:00
|
|
|
'connectionInitialized',
|
2017-07-11 15:22:05 +02:00
|
|
|
'pluginsInitialized',
|
|
|
|
'roster',
|
|
|
|
'rosterContactsFetched',
|
|
|
|
'rosterGroupsFetched',
|
|
|
|
'rosterInitialized',
|
|
|
|
'statusInitialized'
|
|
|
|
];
|
|
|
|
|
|
|
|
function addPromise (promise) {
|
|
|
|
/* Private function, used to add a new promise to the ones already
|
|
|
|
* available via the `waitUntil` api method.
|
|
|
|
*/
|
2018-02-19 10:34:52 +01:00
|
|
|
_converse.promises[promise] = u.getResolveablePromise();
|
2017-07-11 15:22:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
_converse.emit = function (name) {
|
|
|
|
/* Event emitter and promise resolver */
|
|
|
|
_converse.trigger.apply(this, arguments);
|
|
|
|
const promise = _converse.promises[name];
|
|
|
|
if (!_.isUndefined(promise)) {
|
|
|
|
promise.resolve();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-10-31 21:47:27 +01:00
|
|
|
_converse.router = new Backbone.Router();
|
|
|
|
|
|
|
|
|
2016-12-20 10:30:20 +01:00
|
|
|
_converse.initialize = function (settings, callback) {
|
2016-02-16 08:46:47 +01:00
|
|
|
"use strict";
|
2017-01-26 15:49:02 +01:00
|
|
|
settings = !_.isUndefined(settings) ? settings : {};
|
2018-02-19 10:34:52 +01:00
|
|
|
const init_promise = u.getResolveablePromise();
|
2016-12-06 12:07:52 +01:00
|
|
|
|
2017-07-11 15:22:05 +02:00
|
|
|
_.each(PROMISES, addPromise);
|
|
|
|
|
2017-08-16 11:16:22 +02:00
|
|
|
if (!_.isUndefined(_converse.connection)) {
|
2016-12-20 10:30:20 +01:00
|
|
|
// Looks like _converse.initialized was called again without logging
|
2016-12-06 12:07:52 +01:00
|
|
|
// out or disconnecting in the previous session.
|
2016-12-19 08:33:46 +01:00
|
|
|
// This happens in tests. We therefore first clean up.
|
2017-09-17 21:50:59 +02:00
|
|
|
Backbone.history.stop();
|
2017-12-19 20:17:38 +01:00
|
|
|
_converse.chatboxviews.closeAllChatBoxes();
|
2017-08-29 12:29:16 +02:00
|
|
|
delete _converse.controlboxtoggle;
|
2017-12-19 20:17:38 +01:00
|
|
|
delete _converse.chatboxviews;
|
2016-12-20 10:30:20 +01:00
|
|
|
_converse.connection.reset();
|
|
|
|
_converse.off();
|
|
|
|
_converse.stopListening();
|
|
|
|
_converse._tearDown();
|
2016-12-06 12:07:52 +01:00
|
|
|
}
|
|
|
|
|
2017-06-12 20:30:58 +02:00
|
|
|
let unloadevent;
|
2016-02-16 08:46:47 +01:00
|
|
|
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';
|
|
|
|
}
|
|
|
|
|
2016-03-01 09:58:36 +01:00
|
|
|
// Instance level constants
|
2016-02-16 08:46:47 +01:00
|
|
|
this.TIMEOUTS = { // Set as module attr so that we can override in tests.
|
2016-08-18 11:28:48 +02:00
|
|
|
'PAUSED': 10000,
|
2016-02-16 08:46:47 +01:00
|
|
|
'INACTIVE': 90000
|
|
|
|
};
|
|
|
|
|
2016-03-01 09:58:36 +01:00
|
|
|
// 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';
|
|
|
|
|
2016-02-16 08:46:47 +01:00
|
|
|
// Default configuration values
|
|
|
|
// ----------------------------
|
|
|
|
this.default_settings = {
|
|
|
|
allow_contact_requests: true,
|
2017-02-01 10:14:36 +01:00
|
|
|
allow_non_roster_messaging: false,
|
2016-02-16 08:46:47 +01:00
|
|
|
animate: true,
|
2016-09-16 13:06:52 +02:00
|
|
|
authentication: 'login', // Available values are "login", "prebind", "anonymous" and "external".
|
2016-02-16 08:46:47 +01:00
|
|
|
auto_away: 0, // Seconds after which user status is set to 'away'
|
|
|
|
auto_login: false, // Currently only used in connection with anonymous login
|
2018-02-09 16:20:05 +01:00
|
|
|
auto_reconnect: true,
|
2016-02-16 08:46:47 +01:00
|
|
|
auto_subscribe: false,
|
|
|
|
auto_xa: 0, // Seconds after which user status is set to 'xa'
|
2017-02-03 19:12:19 +01:00
|
|
|
blacklisted_plugins: [],
|
|
|
|
bosh_service_url: undefined,
|
2016-10-12 13:55:47 +02:00
|
|
|
connection_options: {},
|
2016-03-31 10:37:28 +02:00
|
|
|
credentials_url: null, // URL from where login credentials can be fetched
|
2016-02-16 08:46:47 +01:00
|
|
|
csi_waiting_time: 0, // Support for XEP-0352. Seconds before client is considered idle and CSI is sent out.
|
|
|
|
debug: false,
|
2016-05-31 10:03:06 +02:00
|
|
|
default_state: 'online',
|
2016-02-16 08:46:47 +01:00
|
|
|
expose_rid_and_sid: false,
|
2016-05-28 08:35:16 +02:00
|
|
|
filter_by_resource: false,
|
2016-02-16 08:46:47 +01:00
|
|
|
forward_messages: false,
|
|
|
|
hide_offline_users: false,
|
|
|
|
include_offline_state: false,
|
|
|
|
jid: undefined,
|
2017-02-17 08:54:58 +01:00
|
|
|
keepalive: true,
|
2018-02-04 09:19:08 +01:00
|
|
|
locales_url: 'locale/{{{locale}}}/LC_MESSAGES/converse.json',
|
2017-09-26 18:27:41 +02:00
|
|
|
locales: [
|
2018-03-04 06:10:32 +01:00
|
|
|
'af', 'bg', 'ca', 'de', 'es', 'en', 'fr', 'he',
|
2017-09-26 18:27:41 +02:00
|
|
|
'hu', 'id', 'it', 'ja', 'nb', 'nl',
|
2018-03-04 06:10:32 +01:00
|
|
|
'pl', 'pt_BR', 'ru', 'tr', 'uk', 'zh_CN', 'zh_TW'
|
2017-09-26 18:27:41 +02:00
|
|
|
],
|
2017-02-27 11:09:15 +01:00
|
|
|
message_carbons: true,
|
2017-02-01 10:14:36 +01:00
|
|
|
message_storage: 'session',
|
2016-02-16 08:46:47 +01:00
|
|
|
password: undefined,
|
|
|
|
prebind_url: null,
|
2017-02-22 22:13:23 +01:00
|
|
|
priority: 0,
|
2017-03-20 13:21:03 +01:00
|
|
|
registration_domain: '',
|
2016-02-16 08:46:47 +01:00
|
|
|
rid: undefined,
|
2018-02-09 16:02:56 +01:00
|
|
|
root: window.document,
|
2017-02-17 08:54:58 +01:00
|
|
|
roster_groups: true,
|
2016-02-16 08:46:47 +01:00
|
|
|
show_only_online_users: false,
|
2017-04-19 14:18:30 +02:00
|
|
|
show_send_button: false,
|
2016-02-16 08:46:47 +01:00
|
|
|
sid: undefined,
|
|
|
|
storage: 'session',
|
2016-03-14 18:03:48 +01:00
|
|
|
strict_plugin_dependencies: false,
|
2017-02-03 19:12:19 +01:00
|
|
|
synchronize_availability: true,
|
2017-11-01 10:37:24 +01:00
|
|
|
view_mode: 'overlayed', // Choices are 'overlayed', 'fullscreen', 'mobile'
|
2016-02-16 08:46:47 +01:00
|
|
|
websocket_url: undefined,
|
2018-02-21 14:40:41 +01:00
|
|
|
whitelisted_plugins: []
|
2016-02-16 08:46:47 +01:00
|
|
|
};
|
2017-01-26 10:08:43 +01:00
|
|
|
_.assignIn(this, this.default_settings);
|
2016-02-16 08:46:47 +01:00
|
|
|
// Allow only whitelisted configuration attributes to be overwritten
|
2017-01-26 15:49:02 +01:00
|
|
|
_.assignIn(this, _.pick(settings, _.keys(this.default_settings)));
|
2016-02-16 08:46:47 +01:00
|
|
|
|
2016-12-20 10:30:20 +01:00
|
|
|
if (this.authentication === _converse.ANONYMOUS) {
|
2016-07-24 10:54:57 +02:00
|
|
|
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.");
|
2016-02-16 08:46:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-24 12:56:35 +02:00
|
|
|
/* Localisation */
|
|
|
|
if (!_.isUndefined(i18n)) {
|
|
|
|
i18n.setLocales(settings.i18n, _converse);
|
|
|
|
} else {
|
|
|
|
_converse.locale = 'en';
|
|
|
|
}
|
2017-09-27 00:33:58 +02:00
|
|
|
|
2016-02-16 08:46:47 +01:00
|
|
|
// Module-level variables
|
|
|
|
// ----------------------
|
2017-01-26 15:49:02 +01:00
|
|
|
this.callback = callback || _.noop;
|
2016-02-16 08:46:47 +01:00
|
|
|
/* 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;
|
2016-10-26 12:50:00 +02:00
|
|
|
this.user_settings = settings; // Save the user settings so that they can be used by plugins
|
2016-02-16 08:46:47 +01:00
|
|
|
|
|
|
|
// Module-level functions
|
|
|
|
// ----------------------
|
|
|
|
|
2018-01-22 09:53:35 +01:00
|
|
|
this.generateResource = () => `/converse.js-${Math.floor(Math.random()*139749528).toString()}`;
|
2016-02-16 14:31:46 +01:00
|
|
|
|
2016-02-16 08:46:47 +01:00
|
|
|
this.sendCSI = function (stat) {
|
2017-01-26 15:49:02 +01:00
|
|
|
/* Send out a Chat Status Notification (XEP-0352)
|
|
|
|
*
|
|
|
|
* Parameters:
|
|
|
|
* (String) stat: The user's chat status
|
|
|
|
*/
|
2016-12-20 10:30:20 +01:00
|
|
|
/* Send out a Chat Status Notification (XEP-0352) */
|
2017-01-26 15:49:02 +01:00
|
|
|
// XXX if (converse.features[Strophe.NS.CSI] || true) {
|
2016-12-20 10:30:20 +01:00
|
|
|
_converse.connection.send($build(stat, {xmlns: Strophe.NS.CSI}));
|
|
|
|
_converse.inactive = (stat === _converse.INACTIVE) ? true : false;
|
2016-02-16 08:46:47 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
this.onUserActivity = function () {
|
|
|
|
/* Resets counters and flags relating to CSI and auto_away/auto_xa */
|
2016-12-20 10:30:20 +01:00
|
|
|
if (_converse.idle_seconds > 0) {
|
|
|
|
_converse.idle_seconds = 0;
|
2016-02-16 08:46:47 +01:00
|
|
|
}
|
2016-12-20 10:30:20 +01:00
|
|
|
if (!_converse.connection.authenticated) {
|
2016-02-16 08:46:47 +01:00
|
|
|
// We can't send out any stanzas when there's no authenticated connection.
|
2016-04-08 10:29:30 +02:00
|
|
|
// converse can happen when the connection reconnects.
|
2016-02-16 08:46:47 +01:00
|
|
|
return;
|
|
|
|
}
|
2016-12-20 10:30:20 +01:00
|
|
|
if (_converse.inactive) {
|
|
|
|
_converse.sendCSI(_converse.ACTIVE);
|
2016-02-16 08:46:47 +01:00
|
|
|
}
|
2016-12-20 10:30:20 +01:00
|
|
|
if (_converse.auto_changed_status === true) {
|
|
|
|
_converse.auto_changed_status = false;
|
2016-06-01 18:01:09 +02:00
|
|
|
// XXX: we should really remember the original state here, and
|
|
|
|
// then set it back to that...
|
2018-02-21 14:40:41 +01:00
|
|
|
_converse.xmppstatus.set('status', _converse.default_state);
|
2016-02-16 08:46:47 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
this.onEverySecond = function () {
|
|
|
|
/* An interval handler running every second.
|
2016-04-08 10:29:30 +02:00
|
|
|
* Used for CSI and the auto_away and auto_xa features.
|
2016-02-16 08:46:47 +01:00
|
|
|
*/
|
2016-12-20 10:30:20 +01:00
|
|
|
if (!_converse.connection.authenticated) {
|
2016-02-16 08:46:47 +01:00
|
|
|
// We can't send out any stanzas when there's no authenticated connection.
|
|
|
|
// This can happen when the connection reconnects.
|
|
|
|
return;
|
|
|
|
}
|
2018-02-21 14:40:41 +01:00
|
|
|
const stat = _converse.xmppstatus.get('status');
|
2016-12-20 10:30:20 +01:00
|
|
|
_converse.idle_seconds++;
|
|
|
|
if (_converse.csi_waiting_time > 0 &&
|
|
|
|
_converse.idle_seconds > _converse.csi_waiting_time &&
|
|
|
|
!_converse.inactive) {
|
|
|
|
_converse.sendCSI(_converse.INACTIVE);
|
2016-02-16 08:46:47 +01:00
|
|
|
}
|
2016-12-20 10:30:20 +01:00
|
|
|
if (_converse.auto_away > 0 &&
|
|
|
|
_converse.idle_seconds > _converse.auto_away &&
|
2017-02-25 10:52:31 +01:00
|
|
|
stat !== 'away' && stat !== 'xa' && stat !== 'dnd') {
|
2016-12-20 10:30:20 +01:00
|
|
|
_converse.auto_changed_status = true;
|
2018-02-21 14:40:41 +01:00
|
|
|
_converse.xmppstatus.set('status', 'away');
|
2016-12-20 10:30:20 +01:00
|
|
|
} else if (_converse.auto_xa > 0 &&
|
2017-02-25 10:52:31 +01:00
|
|
|
_converse.idle_seconds > _converse.auto_xa &&
|
|
|
|
stat !== 'xa' && stat !== 'dnd') {
|
2016-12-20 10:30:20 +01:00
|
|
|
_converse.auto_changed_status = true;
|
2018-02-21 14:40:41 +01:00
|
|
|
_converse.xmppstatus.set('status', 'xa');
|
2016-02-16 08:46:47 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
2016-12-20 10:30:20 +01:00
|
|
|
if (_converse.auto_away < 1 && _converse.auto_xa < 1 && _converse.csi_waiting_time < 1) {
|
2016-02-16 08:46:47 +01:00
|
|
|
// Waiting time of less then one second means features aren't used.
|
|
|
|
return;
|
|
|
|
}
|
2016-12-20 10:30:20 +01:00
|
|
|
_converse.idle_seconds = 0;
|
|
|
|
_converse.auto_changed_status = false; // Was the user's status changed by _converse.js?
|
2017-04-11 11:14:26 +02:00
|
|
|
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);
|
2016-12-20 10:30:20 +01:00
|
|
|
_converse.everySecondTrigger = window.setInterval(_converse.onEverySecond, 1000);
|
2016-02-16 08:46:47 +01:00
|
|
|
};
|
2016-02-29 21:54:34 +01:00
|
|
|
|
2017-09-22 18:55:02 +02:00
|
|
|
this.setConnectionStatus = function (connection_status, message) {
|
2017-09-08 14:24:38 +02:00
|
|
|
_converse.connfeedback.set({
|
2017-09-22 18:55:02 +02:00
|
|
|
'connection_status': connection_status,
|
2017-09-08 14:24:38 +02:00
|
|
|
'message': message
|
2016-08-23 10:00:41 +02:00
|
|
|
});
|
2016-02-16 08:46:47 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
this.rejectPresenceSubscription = function (jid, message) {
|
|
|
|
/* Reject or cancel another user's subscription to our presence updates.
|
2017-02-01 17:01:43 +01:00
|
|
|
*
|
2016-02-16 08:46:47 +01:00
|
|
|
* Parameters:
|
|
|
|
* (String) jid - The Jabber ID of the user whose subscription
|
|
|
|
* is being canceled.
|
|
|
|
* (String) message - An optional message to the user
|
|
|
|
*/
|
2017-06-12 20:30:58 +02:00
|
|
|
const pres = $pres({to: jid, type: "unsubscribed"});
|
2016-02-16 08:46:47 +01:00
|
|
|
if (message && message !== "") { pres.c("status").t(message); }
|
2016-12-20 10:30:20 +01:00
|
|
|
_converse.connection.send(pres);
|
2016-02-16 08:46:47 +01:00
|
|
|
};
|
|
|
|
|
2017-01-26 15:49:02 +01:00
|
|
|
this.reconnect = _.debounce(function () {
|
2016-12-20 10:30:20 +01:00
|
|
|
_converse.log('RECONNECTING');
|
|
|
|
_converse.log('The connection has dropped, attempting to reconnect.');
|
2017-09-22 18:55:02 +02:00
|
|
|
_converse.setConnectionStatus(
|
|
|
|
Strophe.Status.RECONNECTING,
|
2016-11-02 13:44:56 +01:00
|
|
|
__('The connection has dropped, attempting to reconnect.')
|
2016-08-23 10:00:41 +02:00
|
|
|
);
|
2016-12-20 10:30:20 +01:00
|
|
|
_converse.connection.reconnecting = true;
|
|
|
|
_converse._tearDown();
|
|
|
|
_converse.logIn(null, true);
|
2017-02-01 17:01:43 +01:00
|
|
|
}, 3000, {'leading': true});
|
2016-02-16 08:46:47 +01:00
|
|
|
|
2016-12-04 16:00:46 +01:00
|
|
|
this.disconnect = function () {
|
2016-12-20 10:30:20 +01:00
|
|
|
_converse.log('DISCONNECTED');
|
|
|
|
delete _converse.connection.reconnecting;
|
2017-02-18 10:46:33 +01:00
|
|
|
_converse.connection.reset();
|
2016-12-20 10:30:20 +01:00
|
|
|
_converse._tearDown();
|
|
|
|
_converse.emit('disconnected');
|
2016-12-04 16:00:46 +01:00
|
|
|
};
|
|
|
|
|
2017-02-01 17:01:43 +01:00
|
|
|
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.
|
|
|
|
*/
|
2017-09-08 14:24:38 +02:00
|
|
|
const reason = _converse.disconnection_reason;
|
|
|
|
|
2016-12-20 10:30:20 +01:00
|
|
|
if (_converse.disconnection_cause === Strophe.Status.AUTHFAIL) {
|
|
|
|
if (_converse.credentials_url && _converse.auto_reconnect) {
|
2017-02-01 17:34:38 +01:00
|
|
|
/* In this case, we reconnect, because we might be receiving
|
|
|
|
* expirable tokens from the credentials_url.
|
|
|
|
*/
|
2016-12-20 10:30:20 +01:00
|
|
|
_converse.emit('will-reconnect');
|
|
|
|
return _converse.reconnect();
|
2017-02-01 17:34:38 +01:00
|
|
|
} else {
|
2016-12-20 10:30:20 +01:00
|
|
|
return _converse.disconnect();
|
2017-02-01 17:34:38 +01:00
|
|
|
}
|
2016-12-20 10:30:20 +01:00
|
|
|
} else if (_converse.disconnection_cause === _converse.LOGOUT ||
|
2017-09-08 14:24:38 +02:00
|
|
|
(!_.isUndefined(reason) && reason === _.get(Strophe, 'ErrorCondition.NO_AUTH_MECH')) ||
|
|
|
|
reason === "host-unknown" ||
|
|
|
|
reason === "remote-connection-failed" ||
|
2016-12-20 10:30:20 +01:00
|
|
|
!_converse.auto_reconnect) {
|
|
|
|
return _converse.disconnect();
|
2016-08-18 21:43:18 +02:00
|
|
|
}
|
2016-12-20 10:30:20 +01:00
|
|
|
_converse.emit('will-reconnect');
|
|
|
|
_converse.reconnect();
|
2016-08-18 21:43:18 +02:00
|
|
|
};
|
|
|
|
|
2017-01-31 20:57:19 +01:00
|
|
|
this.setDisconnectionCause = function (cause, reason, override) {
|
2017-02-01 17:01:43 +01:00
|
|
|
/* 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)) {
|
2016-12-20 10:30:20 +01:00
|
|
|
delete _converse.disconnection_cause;
|
|
|
|
delete _converse.disconnection_reason;
|
|
|
|
} else if (_.isUndefined(_converse.disconnection_cause) || override) {
|
|
|
|
_converse.disconnection_cause = cause;
|
|
|
|
_converse.disconnection_reason = reason;
|
2016-03-31 14:23:45 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-09-08 14:24:38 +02:00
|
|
|
this.onConnectStatusChanged = function (status, message) {
|
2017-02-01 17:01:43 +01:00
|
|
|
/* Callback method called by Strophe as the Strophe.Connection goes
|
|
|
|
* through various states while establishing or tearing down a
|
|
|
|
* connection.
|
|
|
|
*/
|
2017-09-22 18:55:02 +02:00
|
|
|
_converse.log(`Status changed to: ${_converse.CONNECTION_STATUS[status]}`);
|
2016-02-16 08:46:47 +01:00
|
|
|
if (status === Strophe.Status.CONNECTED || status === Strophe.Status.ATTACHED) {
|
2017-09-22 18:55:02 +02:00
|
|
|
_converse.setConnectionStatus(status);
|
2016-02-16 08:46:47 +01:00
|
|
|
// By default we always want to send out an initial presence stanza.
|
2016-12-20 10:30:20 +01:00
|
|
|
_converse.send_initial_presence = true;
|
|
|
|
_converse.setDisconnectionCause();
|
|
|
|
if (_converse.connection.reconnecting) {
|
|
|
|
_converse.log(status === Strophe.Status.CONNECTED ? 'Reconnected' : 'Reattached');
|
|
|
|
_converse.onConnected(true);
|
2016-02-16 08:46:47 +01:00
|
|
|
} else {
|
2016-12-20 10:30:20 +01:00
|
|
|
_converse.log(status === Strophe.Status.CONNECTED ? 'Connected' : 'Attached');
|
|
|
|
if (_converse.connection.restored) {
|
2016-08-18 21:43:18 +02:00
|
|
|
// No need to send an initial presence stanza when
|
|
|
|
// we're restoring an existing session.
|
2016-12-20 10:30:20 +01:00
|
|
|
_converse.send_initial_presence = false;
|
2016-02-16 08:46:47 +01:00
|
|
|
}
|
2016-12-20 10:30:20 +01:00
|
|
|
_converse.onConnected();
|
2016-02-16 08:46:47 +01:00
|
|
|
}
|
|
|
|
} else if (status === Strophe.Status.DISCONNECTED) {
|
2017-09-08 14:24:38 +02:00
|
|
|
_converse.setDisconnectionCause(status, message);
|
2016-12-20 10:30:20 +01:00
|
|
|
_converse.onDisconnected();
|
2016-02-16 08:46:47 +01:00
|
|
|
} else if (status === Strophe.Status.ERROR) {
|
2017-09-22 18:55:02 +02:00
|
|
|
_converse.setConnectionStatus(
|
|
|
|
status,
|
2016-08-23 10:00:41 +02:00
|
|
|
__('An error occurred while connecting to the chat server.')
|
|
|
|
);
|
2016-02-16 08:46:47 +01:00
|
|
|
} else if (status === Strophe.Status.CONNECTING) {
|
2017-09-22 18:55:02 +02:00
|
|
|
_converse.setConnectionStatus(status);
|
2016-02-16 08:46:47 +01:00
|
|
|
} else if (status === Strophe.Status.AUTHENTICATING) {
|
2017-09-22 18:55:02 +02:00
|
|
|
_converse.setConnectionStatus(status);
|
2016-02-16 08:46:47 +01:00
|
|
|
} else if (status === Strophe.Status.AUTHFAIL) {
|
2017-09-22 10:45:03 +02:00
|
|
|
if (!message) {
|
|
|
|
message = __('Your Jabber ID and/or password is incorrect. Please try again.');
|
|
|
|
}
|
2017-09-22 18:55:02 +02:00
|
|
|
_converse.setConnectionStatus(status, message);
|
2017-09-08 14:24:38 +02:00
|
|
|
_converse.setDisconnectionCause(status, message, true);
|
2017-02-13 16:22:56 +01:00
|
|
|
_converse.onDisconnected();
|
2016-12-04 15:14:40 +01:00
|
|
|
} else if (status === Strophe.Status.CONNFAIL) {
|
2017-09-08 14:24:38 +02:00
|
|
|
let feedback = message;
|
|
|
|
if (message === "host-unknown" || message == "remote-connection-failed") {
|
2018-01-17 17:02:51 +01:00
|
|
|
feedback = __("Sorry, we could not connect to the XMPP host with domain: %1$s",
|
|
|
|
`\"${Strophe.getDomainFromJid(_converse.connection.jid)}\"`);
|
2017-09-08 14:24:38 +02:00
|
|
|
} else if (!_.isUndefined(message) && message === _.get(Strophe, 'ErrorCondition.NO_AUTH_MECH')) {
|
|
|
|
feedback = __("The XMPP server did not offer a supported authentication mechanism");
|
|
|
|
}
|
2017-09-22 18:55:02 +02:00
|
|
|
_converse.setConnectionStatus(status, feedback);
|
2017-09-08 14:24:38 +02:00
|
|
|
_converse.setDisconnectionCause(status, message);
|
2016-12-04 15:14:40 +01:00
|
|
|
} else if (status === Strophe.Status.DISCONNECTING) {
|
2017-09-08 14:24:38 +02:00
|
|
|
_converse.setDisconnectionCause(status, message);
|
2016-02-16 08:46:47 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
this.incrementMsgCounter = function () {
|
|
|
|
this.msg_counter += 1;
|
2017-06-12 20:30:58 +02:00
|
|
|
const unreadMsgCount = this.msg_counter;
|
2018-02-09 16:02:56 +01:00
|
|
|
let title = document.title;
|
|
|
|
if (_.isNil(title)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (title.search(/^Messages \(\d+\) /) === -1) {
|
|
|
|
title = `Messages (${unreadMsgCount}) ${title}`;
|
2017-05-03 09:06:28 +02:00
|
|
|
} else {
|
2018-02-09 16:02:56 +01:00
|
|
|
title = title.replace(/^Messages \(\d+\) /, `Messages (${unreadMsgCount})`);
|
2017-05-03 09:06:28 +02:00
|
|
|
}
|
2016-02-16 08:46:47 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
this.clearMsgCounter = function () {
|
|
|
|
this.msg_counter = 0;
|
2018-02-09 16:02:56 +01:00
|
|
|
let title = document.title;
|
|
|
|
if (_.isNil(title)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (title.search(/^Messages \(\d+\) /) !== -1) {
|
|
|
|
title = title.replace(/^Messages \(\d+\) /, "");
|
2017-05-03 09:06:28 +02:00
|
|
|
}
|
2016-02-16 08:46:47 +01:00
|
|
|
};
|
|
|
|
|
2018-02-22 11:06:30 +01:00
|
|
|
this.initStatus = (reconnecting) => {
|
|
|
|
|
|
|
|
// 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(reconnecting);
|
|
|
|
} else {
|
2017-07-10 21:14:48 +02:00
|
|
|
this.xmppstatus = new this.XMPPStatus();
|
|
|
|
const 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({
|
2018-02-22 11:06:30 +01:00
|
|
|
success: _.partial(_converse.onStatusInitialized, reconnecting),
|
|
|
|
error: _.partial(_converse.onStatusInitialized, reconnecting)
|
2017-07-10 21:14:48 +02:00
|
|
|
});
|
2018-02-22 11:06:30 +01:00
|
|
|
}
|
|
|
|
}
|
2016-02-16 08:46:47 +01:00
|
|
|
|
|
|
|
this.initSession = function () {
|
2017-08-16 12:31:17 +02:00
|
|
|
_converse.session = new Backbone.Model();
|
2017-06-12 20:30:58 +02:00
|
|
|
const id = b64_sha1('converse.bosh-session');
|
2017-08-16 12:31:17 +02:00
|
|
|
_converse.session.id = id; // Appears to be necessary for backbone.browserStorage
|
|
|
|
_converse.session.browserStorage = new Backbone.BrowserStorage[_converse.storage](id);
|
|
|
|
_converse.session.fetch();
|
2016-02-16 08:46:47 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
this.clearSession = function () {
|
2016-10-17 10:34:09 +02:00
|
|
|
if (!_.isUndefined(this.roster)) {
|
2016-02-16 08:46:47 +01:00
|
|
|
this.roster.browserStorage._clear();
|
|
|
|
}
|
2017-06-16 14:55:40 +02:00
|
|
|
if (!_.isUndefined(this.session) && this.session.browserStorage) {
|
|
|
|
this.session.browserStorage._clear();
|
|
|
|
}
|
2016-02-16 08:46:47 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
this.logOut = function () {
|
2017-06-23 18:21:00 +02:00
|
|
|
_converse.clearSession();
|
2016-12-20 10:30:20 +01:00
|
|
|
_converse.setDisconnectionCause(_converse.LOGOUT, undefined, true);
|
|
|
|
if (!_.isUndefined(_converse.connection)) {
|
|
|
|
_converse.connection.disconnect();
|
2017-06-23 18:21:00 +02:00
|
|
|
} else {
|
|
|
|
_converse._tearDown();
|
2016-04-13 11:15:15 +02:00
|
|
|
}
|
2018-02-22 13:26:06 +01:00
|
|
|
// Recreate all the promises
|
|
|
|
_.each(_.keys(_converse.promises), addPromise);
|
|
|
|
|
2016-12-20 10:30:20 +01:00
|
|
|
_converse.emit('logout');
|
2016-02-16 08:46:47 +01:00
|
|
|
};
|
|
|
|
|
2016-06-17 11:30:47 +02:00
|
|
|
this.saveWindowState = function (ev, hidden) {
|
|
|
|
// XXX: eventually we should be able to just use
|
|
|
|
// document.visibilityState (when we drop support for older
|
|
|
|
// browsers).
|
2017-06-12 20:30:58 +02:00
|
|
|
let state;
|
|
|
|
const event_map = {
|
|
|
|
'focus': "visible",
|
|
|
|
'focusin': "visible",
|
|
|
|
'pageshow': "visible",
|
|
|
|
'blur': "hidden",
|
|
|
|
'focusout': "hidden",
|
|
|
|
'pagehide': "hidden"
|
|
|
|
};
|
2016-06-17 11:30:47 +02:00
|
|
|
ev = ev || document.createEvent('Events');
|
2016-06-16 18:14:22 +02:00
|
|
|
if (ev.type in event_map) {
|
|
|
|
state = event_map[ev.type];
|
|
|
|
} else {
|
|
|
|
state = document[hidden] ? "hidden" : "visible";
|
|
|
|
}
|
|
|
|
if (state === 'visible') {
|
2016-12-20 10:30:20 +01:00
|
|
|
_converse.clearMsgCounter();
|
2016-06-16 18:14:22 +02:00
|
|
|
}
|
2016-12-20 10:30:20 +01:00
|
|
|
_converse.windowState = state;
|
2017-07-10 17:46:22 +02:00
|
|
|
_converse.emit('windowStateChanged', {state});
|
2016-06-16 18:14:22 +02:00
|
|
|
};
|
|
|
|
|
2016-02-16 08:46:47 +01:00
|
|
|
this.registerGlobalEventHandlers = function () {
|
2016-06-16 18:14:22 +02:00
|
|
|
// Taken from:
|
|
|
|
// http://stackoverflow.com/questions/1060008/is-there-a-way-to-detect-if-a-browser-window-is-not-currently-active
|
2017-06-12 20:30:58 +02:00
|
|
|
let hidden = "hidden";
|
2016-06-16 18:14:22 +02:00
|
|
|
// Standards:
|
|
|
|
if (hidden in document) {
|
2016-12-20 10:30:20 +01:00
|
|
|
document.addEventListener("visibilitychange", _.partial(_converse.saveWindowState, _, hidden));
|
2016-06-16 18:14:22 +02:00
|
|
|
} else if ((hidden = "mozHidden") in document) {
|
2016-12-20 10:30:20 +01:00
|
|
|
document.addEventListener("mozvisibilitychange", _.partial(_converse.saveWindowState, _, hidden));
|
2016-06-16 18:14:22 +02:00
|
|
|
} else if ((hidden = "webkitHidden") in document) {
|
2016-12-20 10:30:20 +01:00
|
|
|
document.addEventListener("webkitvisibilitychange", _.partial(_converse.saveWindowState, _, hidden));
|
2016-06-16 18:14:22 +02:00
|
|
|
} else if ((hidden = "msHidden") in document) {
|
2016-12-20 10:30:20 +01:00
|
|
|
document.addEventListener("msvisibilitychange", _.partial(_converse.saveWindowState, _, hidden));
|
2016-06-16 18:14:22 +02:00
|
|
|
} else if ("onfocusin" in document) {
|
|
|
|
// IE 9 and lower:
|
2016-12-20 10:30:20 +01:00
|
|
|
document.onfocusin = document.onfocusout = _.partial(_converse.saveWindowState, _, hidden);
|
2016-06-16 18:14:22 +02:00
|
|
|
} else {
|
|
|
|
// All others:
|
2016-12-20 10:30:20 +01:00
|
|
|
window.onpageshow = window.onpagehide = window.onfocus = window.onblur = _.partial(_converse.saveWindowState, _, hidden);
|
2016-06-16 18:14:22 +02:00
|
|
|
}
|
|
|
|
// set the initial state (but only if browser supports the Page Visibility API)
|
|
|
|
if( document[hidden] !== undefined ) {
|
2016-12-20 10:30:20 +01:00
|
|
|
_.partial(_converse.saveWindowState, _, hidden)({type: document[hidden] ? "blur" : "focus"});
|
2016-06-16 18:14:22 +02:00
|
|
|
}
|
2016-02-16 08:46:47 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2017-06-12 20:30:58 +02:00
|
|
|
const carbons_iq = new Strophe.Builder('iq', {
|
2016-02-16 08:46:47 +01:00
|
|
|
from: this.connection.jid,
|
|
|
|
id: 'enablecarbons',
|
|
|
|
type: 'set'
|
|
|
|
})
|
|
|
|
.c('enable', {xmlns: Strophe.NS.CARBONS});
|
2017-07-10 17:46:22 +02:00
|
|
|
this.connection.addHandler((iq) => {
|
2016-11-24 02:07:32 +01:00
|
|
|
if (iq.querySelectorAll('error').length > 0) {
|
2017-07-05 11:33:55 +02:00
|
|
|
_converse.log(
|
|
|
|
'An error occured while trying to enable message carbons.',
|
|
|
|
Strophe.LogLevel.ERROR);
|
2016-02-16 08:46:47 +01:00
|
|
|
} else {
|
|
|
|
this.session.save({carbons_enabled: true});
|
2016-12-20 10:30:20 +01:00
|
|
|
_converse.log('Message carbons have been enabled.');
|
2016-02-16 08:46:47 +01:00
|
|
|
}
|
2017-07-10 17:46:22 +02:00
|
|
|
}, null, "iq", null, "enablecarbons");
|
2016-02-16 08:46:47 +01:00
|
|
|
this.connection.send(carbons_iq);
|
|
|
|
};
|
|
|
|
|
2016-06-22 12:21:56 +02:00
|
|
|
this.initRoster = function () {
|
2016-11-02 14:13:49 +01:00
|
|
|
/* Initialize the Bakcbone collections that represent the contats
|
|
|
|
* roster and the roster groups.
|
|
|
|
*/
|
2016-12-20 10:30:20 +01:00
|
|
|
_converse.roster = new _converse.RosterContacts();
|
|
|
|
_converse.roster.browserStorage = new Backbone.BrowserStorage.session(
|
2017-07-10 17:46:22 +02:00
|
|
|
b64_sha1(`converse.contacts-${_converse.bare_jid}`));
|
2016-12-20 10:30:20 +01:00
|
|
|
_converse.rostergroups = new _converse.RosterGroups();
|
|
|
|
_converse.rostergroups.browserStorage = new Backbone.BrowserStorage.session(
|
2017-07-10 17:46:22 +02:00
|
|
|
b64_sha1(`converse.roster.groups${_converse.bare_jid}`));
|
2016-12-20 10:30:20 +01:00
|
|
|
_converse.emit('rosterInitialized');
|
2016-06-22 12:21:56 +02:00
|
|
|
};
|
|
|
|
|
2017-10-27 10:32:02 +02:00
|
|
|
this.populateRoster = function (ignore_cache=false) {
|
2016-09-21 15:00:09 +02:00
|
|
|
/* Fetch all the roster groups, and then the roster contacts.
|
|
|
|
* Emit an event after fetching is done in each case.
|
2017-10-27 10:32:02 +02:00
|
|
|
*
|
|
|
|
* Parameters:
|
|
|
|
* (Bool) ignore_cache - If set to to true, the local cache
|
|
|
|
* will be ignored it's guaranteed that the XMPP server
|
|
|
|
* will be queried for the roster.
|
2016-09-21 15:00:09 +02:00
|
|
|
*/
|
2017-10-27 10:32:02 +02:00
|
|
|
if (ignore_cache) {
|
|
|
|
_converse.send_initial_presence = true;
|
|
|
|
_converse.roster.fetchFromServer()
|
2017-10-31 18:06:28 +01:00
|
|
|
.then(() => {
|
|
|
|
_converse.emit('rosterContactsFetched');
|
|
|
|
_converse.sendInitialPresence();
|
2017-12-18 13:19:14 +01:00
|
|
|
}).catch((reason) => {
|
|
|
|
_converse.log(reason, Strophe.LogLevel.ERROR);
|
|
|
|
_converse.sendInitialPresence();
|
|
|
|
});
|
2017-10-27 10:32:02 +02:00
|
|
|
} else {
|
|
|
|
_converse.rostergroups.fetchRosterGroups().then(() => {
|
|
|
|
_converse.emit('rosterGroupsFetched');
|
|
|
|
return _converse.roster.fetchRosterContacts();
|
|
|
|
}).then(() => {
|
2016-12-20 10:30:20 +01:00
|
|
|
_converse.emit('rosterContactsFetched');
|
|
|
|
_converse.sendInitialPresence();
|
2017-12-18 13:19:14 +01:00
|
|
|
}).catch((reason) => {
|
|
|
|
_converse.log(reason, Strophe.LogLevel.ERROR);
|
2017-10-27 10:32:02 +02:00
|
|
|
_converse.sendInitialPresence();
|
2016-09-21 15:00:09 +02:00
|
|
|
});
|
2017-10-27 10:32:02 +02:00
|
|
|
}
|
2016-09-21 15:00:09 +02:00
|
|
|
};
|
|
|
|
|
2016-06-22 12:21:56 +02:00
|
|
|
this.unregisterPresenceHandler = function () {
|
2016-12-20 10:30:20 +01:00
|
|
|
if (!_.isUndefined(_converse.presence_ref)) {
|
|
|
|
_converse.connection.deleteHandler(_converse.presence_ref);
|
|
|
|
delete _converse.presence_ref;
|
2016-06-22 12:21:56 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
this.registerPresenceHandler = function () {
|
2016-12-20 10:30:20 +01:00
|
|
|
_converse.unregisterPresenceHandler();
|
|
|
|
_converse.presence_ref = _converse.connection.addHandler(
|
2016-06-22 12:21:56 +02:00
|
|
|
function (presence) {
|
2016-12-20 10:30:20 +01:00
|
|
|
_converse.roster.presenceHandler(presence);
|
2016-06-22 12:21:56 +02:00
|
|
|
return true;
|
|
|
|
}, null, 'presence', null);
|
|
|
|
};
|
|
|
|
|
2016-09-20 12:10:28 +02:00
|
|
|
|
|
|
|
this.sendInitialPresence = function () {
|
2016-12-20 10:30:20 +01:00
|
|
|
if (_converse.send_initial_presence) {
|
|
|
|
_converse.xmppstatus.sendPresence();
|
2016-09-20 12:10:28 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-11-23 12:53:06 +01:00
|
|
|
this.onStatusInitialized = function (reconnecting) {
|
|
|
|
/* Continue with session establishment (e.g. fetching chat boxes,
|
|
|
|
* populating the roster etc.) necessary once the connection has
|
|
|
|
* been established.
|
|
|
|
*/
|
2018-02-22 11:06:30 +01:00
|
|
|
_converse.emit('statusInitialized');
|
2016-11-23 12:53:06 +01:00
|
|
|
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.
|
2016-12-20 10:30:20 +01:00
|
|
|
_converse.emit('rosterReadyAfterReconnection');
|
2016-11-23 12:53:06 +01:00
|
|
|
} else {
|
2016-12-20 10:30:20 +01:00
|
|
|
_converse.registerIntervalHandler();
|
|
|
|
_converse.initRoster();
|
2016-11-23 12:53:06 +01:00
|
|
|
}
|
2017-06-16 11:42:46 +02:00
|
|
|
_converse.roster.onConnected();
|
2017-10-27 10:32:02 +02:00
|
|
|
_converse.populateRoster(reconnecting);
|
2016-12-20 10:30:20 +01:00
|
|
|
_converse.registerPresenceHandler();
|
2018-02-22 11:06:30 +01:00
|
|
|
if (reconnecting) {
|
|
|
|
_converse.emit('reconnected');
|
|
|
|
} else {
|
2017-07-10 21:14:48 +02:00
|
|
|
init_promise.resolve();
|
2016-12-20 10:30:20 +01:00
|
|
|
_converse.emit('initialized');
|
2018-02-22 11:06:30 +01:00
|
|
|
_converse.emit('connected');
|
2016-03-16 12:16:32 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-11-02 13:44:56 +01:00
|
|
|
this.setUserJid = function () {
|
2016-12-20 10:30:20 +01:00
|
|
|
_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);
|
2016-11-02 13:44:56 +01:00
|
|
|
};
|
|
|
|
|
2016-11-23 12:53:06 +01:00
|
|
|
this.onConnected = function (reconnecting) {
|
2016-11-02 13:44:56 +01:00
|
|
|
/* Called as soon as a new connection has been established, either
|
|
|
|
* by logging in or by attaching to an existing BOSH session.
|
|
|
|
*/
|
2018-02-22 11:06:30 +01:00
|
|
|
_converse.connection.flush(); // Solves problem of returned PubSub BOSH response not received by browser
|
2016-12-20 10:30:20 +01:00
|
|
|
_converse.setUserJid();
|
2017-06-27 09:29:46 +02:00
|
|
|
_converse.initSession();
|
2016-12-20 10:30:20 +01:00
|
|
|
_converse.enableCarbons();
|
2018-02-22 11:06:30 +01:00
|
|
|
_converse.initStatus(reconnecting)
|
2016-11-02 14:13:49 +01:00
|
|
|
};
|
|
|
|
|
2016-02-29 22:18:28 +01:00
|
|
|
this.RosterContact = Backbone.Model.extend({
|
2016-02-29 22:59:56 +01:00
|
|
|
|
2017-05-15 18:04:03 +02:00
|
|
|
defaults: {
|
|
|
|
'chat_state': undefined,
|
|
|
|
'chat_status': 'offline',
|
2017-08-16 11:16:22 +02:00
|
|
|
'image': _converse.DEFAULT_IMAGE,
|
|
|
|
'image_type': _converse.DEFAULT_IMAGE_TYPE,
|
2017-05-15 18:04:03 +02:00
|
|
|
'num_unread': 0,
|
|
|
|
'status': '',
|
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
initialize (attributes) {
|
2018-03-04 07:09:49 +01:00
|
|
|
const { jid } = attributes,
|
|
|
|
bare_jid = Strophe.getBareJidFromJid(jid).toLowerCase(),
|
|
|
|
resource = Strophe.getResourceFromJid(jid);
|
|
|
|
|
2016-02-29 22:18:28 +01:00
|
|
|
attributes.jid = bare_jid;
|
2017-01-26 10:08:43 +01:00
|
|
|
this.set(_.assignIn({
|
2018-02-08 17:05:22 +01:00
|
|
|
'fullname': bare_jid,
|
|
|
|
'groups': [],
|
2016-02-29 22:18:28 +01:00
|
|
|
'id': bare_jid,
|
|
|
|
'jid': bare_jid,
|
2018-02-08 17:05:22 +01:00
|
|
|
'resources': {},
|
|
|
|
'user_id': Strophe.getNodeFromJid(jid)
|
2016-02-29 22:18:28 +01:00
|
|
|
}, attributes));
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
this.on('destroy', () => { this.removeFromRoster(); });
|
2016-03-08 11:42:52 +01:00
|
|
|
this.on('change:chat_status', function (item) {
|
2016-12-20 10:30:20 +01:00
|
|
|
_converse.emit('contactStatusChanged', item.attributes);
|
2016-03-08 11:42:52 +01:00
|
|
|
});
|
2016-02-29 22:18:28 +01:00
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
subscribe (message) {
|
2016-02-29 22:18:28 +01:00
|
|
|
/* Send a presence subscription request to this roster contact
|
2016-03-18 09:49:32 +01:00
|
|
|
*
|
|
|
|
* Parameters:
|
|
|
|
* (String) message - An optional message to explain the
|
|
|
|
* reason for the subscription request.
|
|
|
|
*/
|
2016-02-29 22:18:28 +01:00
|
|
|
this.save('ask', "subscribe"); // ask === 'subscribe' Means we have ask to subscribe to them.
|
2017-06-12 20:30:58 +02:00
|
|
|
const pres = $pres({to: this.get('jid'), type: "subscribe"});
|
2016-02-29 22:18:28 +01:00
|
|
|
if (message && message !== "") {
|
|
|
|
pres.c("status").t(message).up();
|
|
|
|
}
|
2017-06-12 20:30:58 +02:00
|
|
|
const nick = _converse.xmppstatus.get('fullname');
|
2016-02-29 22:18:28 +01:00
|
|
|
if (nick && nick !== "") {
|
|
|
|
pres.c('nick', {'xmlns': Strophe.NS.NICK}).t(nick).up();
|
|
|
|
}
|
2016-12-20 10:30:20 +01:00
|
|
|
_converse.connection.send(pres);
|
2016-02-29 22:18:28 +01:00
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
ackSubscribe () {
|
2016-02-29 22:18:28 +01:00
|
|
|
/* Upon receiving the presence stanza of type "subscribed",
|
2016-03-18 09:49:32 +01:00
|
|
|
* the user SHOULD acknowledge receipt of that subscription
|
|
|
|
* state notification by sending a presence stanza of type
|
|
|
|
* "subscribe" to the contact
|
|
|
|
*/
|
2016-12-20 10:30:20 +01:00
|
|
|
_converse.connection.send($pres({
|
2016-02-29 22:18:28 +01:00
|
|
|
'type': 'subscribe',
|
|
|
|
'to': this.get('jid')
|
|
|
|
}));
|
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
ackUnsubscribe () {
|
2016-02-29 22:18:28 +01:00
|
|
|
/* Upon receiving the presence stanza of type "unsubscribed",
|
2016-03-18 09:49:32 +01:00
|
|
|
* 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
|
|
|
|
*/
|
2016-12-20 10:30:20 +01:00
|
|
|
_converse.connection.send($pres({'type': 'unsubscribe', 'to': this.get('jid')}));
|
2016-02-29 22:18:28 +01:00
|
|
|
this.destroy(); // Will cause removeFromRoster to be called.
|
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
unauthorize (message) {
|
2016-02-29 22:18:28 +01:00
|
|
|
/* Unauthorize this contact's presence subscription
|
2016-03-18 09:49:32 +01:00
|
|
|
* Parameters:
|
|
|
|
* (String) message - Optional message to send to the person being unauthorized
|
|
|
|
*/
|
2016-12-20 10:30:20 +01:00
|
|
|
_converse.rejectPresenceSubscription(this.get('jid'), message);
|
2016-02-29 22:18:28 +01:00
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
authorize (message) {
|
2016-02-29 22:18:28 +01:00
|
|
|
/* Authorize presence subscription
|
2016-03-18 09:49:32 +01:00
|
|
|
* Parameters:
|
|
|
|
* (String) message - Optional message to send to the person being authorized
|
|
|
|
*/
|
2017-06-12 20:30:58 +02:00
|
|
|
const pres = $pres({to: this.get('jid'), type: "subscribed"});
|
2016-02-29 22:18:28 +01:00
|
|
|
if (message && message !== "") {
|
|
|
|
pres.c("status").t(message);
|
|
|
|
}
|
2016-12-20 10:30:20 +01:00
|
|
|
_converse.connection.send(pres);
|
2016-02-29 22:18:28 +01:00
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
addResource (presence) {
|
2017-02-21 23:25:19 +01:00
|
|
|
/* 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).
|
|
|
|
*/
|
2017-06-12 20:30:58 +02:00
|
|
|
const jid = presence.getAttribute('from'),
|
2017-02-21 23:25:19 +01:00
|
|
|
chat_status = _.propertyOf(presence.querySelector('show'))('textContent') || 'online',
|
|
|
|
resource = Strophe.getResourceFromJid(jid),
|
2017-07-10 17:46:22 +02:00
|
|
|
delay = presence.querySelector(
|
|
|
|
`delay[xmlns="${Strophe.NS.DELAY}"]`
|
|
|
|
),
|
2017-02-21 23:25:19 +01:00
|
|
|
timestamp = _.isNull(delay) ? moment().format() : moment(delay.getAttribute('stamp')).format();
|
|
|
|
|
2017-06-12 20:30:58 +02:00
|
|
|
let priority = _.propertyOf(presence.querySelector('priority'))('textContent') || 0;
|
2017-02-21 23:25:19 +01:00
|
|
|
priority = _.isNaN(parseInt(priority, 10)) ? 0 : parseInt(priority, 10);
|
|
|
|
|
2017-06-12 20:30:58 +02:00
|
|
|
const resources = _.isObject(this.get('resources')) ? this.get('resources') : {};
|
2017-02-20 22:27:34 +01:00
|
|
|
resources[resource] = {
|
2018-02-08 17:06:53 +01:00
|
|
|
'name': resource,
|
2017-02-21 23:25:19 +01:00
|
|
|
'priority': priority,
|
2017-02-21 22:13:49 +01:00
|
|
|
'status': chat_status,
|
2017-03-08 15:33:00 +01:00
|
|
|
'timestamp': timestamp
|
2017-02-20 22:27:34 +01:00
|
|
|
};
|
2017-06-12 20:30:58 +02:00
|
|
|
const changed = {'resources': resources};
|
|
|
|
const hpr = this.getHighestPriorityResource();
|
2017-02-21 23:25:19 +01:00
|
|
|
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);
|
2017-02-20 22:27:34 +01:00
|
|
|
return resources;
|
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
removeResource (resource) {
|
2017-02-21 23:25:19 +01:00
|
|
|
/* Remove the passed in resource from the contact's resources map.
|
|
|
|
*
|
|
|
|
* Also recomputes the chat_status given that there's one less
|
|
|
|
* resource.
|
2017-02-20 22:27:34 +01:00
|
|
|
*/
|
2017-06-12 20:30:58 +02:00
|
|
|
let resources = this.get('resources');
|
2017-02-20 22:27:34 +01:00
|
|
|
if (!_.isObject(resources)) {
|
|
|
|
resources = {};
|
|
|
|
} else {
|
|
|
|
delete resources[resource];
|
2016-02-29 22:18:28 +01:00
|
|
|
}
|
2017-02-21 23:25:19 +01:00
|
|
|
this.save({
|
|
|
|
'resources': resources,
|
|
|
|
'chat_status': _.propertyOf(
|
|
|
|
this.getHighestPriorityResource())('status') || 'offline'
|
|
|
|
});
|
2017-02-20 22:27:34 +01:00
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
getHighestPriorityResource () {
|
2017-02-21 23:25:19 +01:00
|
|
|
/* Return the resource with the highest priority.
|
2017-02-21 22:13:49 +01:00
|
|
|
*
|
|
|
|
* If multiple resources have the same priority, take the
|
|
|
|
* newest one.
|
2017-02-20 22:27:34 +01:00
|
|
|
*/
|
2017-06-12 20:30:58 +02:00
|
|
|
const resources = this.get('resources');
|
2017-02-20 22:27:34 +01:00
|
|
|
if (_.isObject(resources) && _.size(resources)) {
|
2017-06-12 20:30:58 +02:00
|
|
|
const val = _.flow(
|
2017-02-21 22:13:49 +01:00
|
|
|
_.values,
|
|
|
|
_.partial(_.sortBy, _, ['priority', 'timestamp']),
|
|
|
|
_.reverse
|
|
|
|
)(resources)[0];
|
2017-02-20 22:27:34 +01:00
|
|
|
if (!_.isUndefined(val)) {
|
2017-02-21 23:25:19 +01:00
|
|
|
return val;
|
2017-02-20 22:27:34 +01:00
|
|
|
}
|
2016-02-29 22:18:28 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
removeFromRoster (callback) {
|
2016-02-29 22:18:28 +01:00
|
|
|
/* Instruct the XMPP server to remove this contact from our roster
|
2016-03-18 09:49:32 +01:00
|
|
|
* Parameters:
|
|
|
|
* (Function) callback
|
|
|
|
*/
|
2017-06-12 20:30:58 +02:00
|
|
|
const iq = $iq({type: 'set'})
|
2016-02-29 22:18:28 +01:00
|
|
|
.c('query', {xmlns: Strophe.NS.ROSTER})
|
|
|
|
.c('item', {jid: this.get('jid'), subscription: "remove"});
|
2016-12-20 10:30:20 +01:00
|
|
|
_converse.connection.sendIQ(iq, callback, callback);
|
2016-02-29 22:18:28 +01:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
this.RosterContacts = Backbone.Collection.extend({
|
2016-12-20 10:30:20 +01:00
|
|
|
model: _converse.RosterContact,
|
2016-02-29 22:59:56 +01:00
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
comparator (contact1, contact2) {
|
2017-06-12 20:30:58 +02:00
|
|
|
const status1 = contact1.get('chat_status') || 'offline';
|
|
|
|
const status2 = contact2.get('chat_status') || 'offline';
|
2016-12-20 10:30:20 +01:00
|
|
|
if (_converse.STATUS_WEIGHTS[status1] === _converse.STATUS_WEIGHTS[status2]) {
|
2017-06-12 20:30:58 +02:00
|
|
|
const name1 = contact1.get('fullname').toLowerCase();
|
|
|
|
const name2 = contact2.get('fullname').toLowerCase();
|
2016-02-29 22:18:28 +01:00
|
|
|
return name1 < name2 ? -1 : (name1 > name2? 1 : 0);
|
|
|
|
} else {
|
2016-12-20 10:30:20 +01:00
|
|
|
return _converse.STATUS_WEIGHTS[status1] < _converse.STATUS_WEIGHTS[status2] ? -1 : 1;
|
2016-02-29 22:18:28 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
onConnected () {
|
2017-06-16 11:42:46 +02:00
|
|
|
/* 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();
|
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
registerRosterHandler () {
|
2017-06-16 11:42:46 +02:00
|
|
|
/* 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"
|
|
|
|
);
|
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
registerRosterXHandler () {
|
2017-06-16 11:42:46 +02:00
|
|
|
/* Register a handler for RosterX message stanzas, which are
|
|
|
|
* used to suggest roster contacts to a user.
|
|
|
|
*/
|
2017-07-10 17:46:22 +02:00
|
|
|
let t = 0;
|
2017-06-16 11:42:46 +02:00
|
|
|
_converse.connection.addHandler(
|
|
|
|
function (msg) {
|
|
|
|
window.setTimeout(
|
|
|
|
function () {
|
|
|
|
_converse.connection.flush();
|
|
|
|
_converse.roster.subscribeToSuggestedItems.bind(_converse.roster)(msg);
|
|
|
|
}, t);
|
2017-07-10 21:14:48 +02:00
|
|
|
t += msg.querySelectorAll('item').length*250;
|
2017-06-16 11:42:46 +02:00
|
|
|
return true;
|
|
|
|
},
|
|
|
|
Strophe.NS.ROSTERX, 'message', null
|
|
|
|
);
|
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
fetchRosterContacts () {
|
2016-09-20 12:10:28 +02:00
|
|
|
/* 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.
|
|
|
|
*/
|
2017-07-10 21:14:48 +02:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
this.fetch({
|
2017-12-17 16:03:28 +01:00
|
|
|
'add': true,
|
|
|
|
'silent': true,
|
2017-07-10 21:14:48 +02:00
|
|
|
success (collection) {
|
|
|
|
if (collection.length === 0) {
|
|
|
|
_converse.send_initial_presence = true;
|
2017-10-27 10:32:02 +02:00
|
|
|
_converse.roster.fetchFromServer().then(resolve).catch(reject);
|
2017-07-10 21:14:48 +02:00
|
|
|
} else {
|
|
|
|
_converse.emit('cachedRoster', collection);
|
|
|
|
resolve();
|
|
|
|
}
|
2016-09-20 12:10:28 +02:00
|
|
|
}
|
2017-07-10 21:14:48 +02:00
|
|
|
});
|
2016-09-20 12:10:28 +02:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
subscribeToSuggestedItems (msg) {
|
2016-11-24 02:07:32 +01:00
|
|
|
_.each(msg.querySelectorAll('item'), function (item) {
|
|
|
|
if (item.getAttribute('action') === 'add') {
|
2016-12-20 10:30:20 +01:00
|
|
|
_converse.roster.addAndSubscribe(
|
2016-11-24 02:07:32 +01:00
|
|
|
item.getAttribute('jid'),
|
|
|
|
null,
|
2016-12-20 10:30:20 +01:00
|
|
|
_converse.xmppstatus.get('fullname')
|
2016-11-24 02:07:32 +01:00
|
|
|
);
|
2016-02-29 22:18:28 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
isSelf (jid) {
|
2018-02-19 10:34:52 +01:00
|
|
|
return u.isSameBareJID(jid, _converse.connection.jid);
|
2016-02-29 22:18:28 +01:00
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
addAndSubscribe (jid, name, groups, message, attributes) {
|
2016-02-29 22:18:28 +01:00
|
|
|
/* Add a roster contact and then once we have confirmation from
|
2016-03-18 09:49:32 +01:00
|
|
|
* 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.
|
|
|
|
*/
|
2017-07-10 21:14:48 +02:00
|
|
|
const handler = (contact) => {
|
2016-12-20 10:30:20 +01:00
|
|
|
if (contact instanceof _converse.RosterContact) {
|
2016-02-29 22:18:28 +01:00
|
|
|
contact.subscribe(message);
|
|
|
|
}
|
2017-07-10 21:14:48 +02:00
|
|
|
}
|
|
|
|
this.addContact(jid, name, groups, attributes).then(handler, handler);
|
2016-02-29 22:18:28 +01:00
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
sendContactAddIQ (jid, name, groups, callback, errback) {
|
2016-02-29 22:18:28 +01:00
|
|
|
/* Send an IQ stanza to the XMPP server to add a new roster contact.
|
2016-03-01 22:57:27 +01:00
|
|
|
*
|
|
|
|
* 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
|
2016-03-16 12:16:32 +01:00
|
|
|
* (Function) callback - A function to call once the IQ is returned
|
2016-03-01 22:57:27 +01:00
|
|
|
* (Function) errback - A function to call if an error occured
|
|
|
|
*/
|
2016-02-29 22:18:28 +01:00
|
|
|
name = _.isEmpty(name)? jid: name;
|
2017-06-12 20:30:58 +02:00
|
|
|
const iq = $iq({type: 'set'})
|
2016-02-29 22:18:28 +01:00
|
|
|
.c('query', {xmlns: Strophe.NS.ROSTER})
|
2017-07-10 17:46:22 +02:00
|
|
|
.c('item', { jid, name });
|
2017-01-26 15:49:02 +01:00
|
|
|
_.each(groups, function (group) { iq.c('group').t(group).up(); });
|
2016-12-20 10:30:20 +01:00
|
|
|
_converse.connection.sendIQ(iq, callback, errback);
|
2016-02-29 22:18:28 +01:00
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
addContact (jid, name, groups, attributes) {
|
2016-12-20 10:30:20 +01:00
|
|
|
/* Adds a RosterContact instance to _converse.roster and
|
2016-03-01 22:57:27 +01:00
|
|
|
* 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.
|
|
|
|
*/
|
2017-07-10 21:14:48 +02:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
groups = groups || [];
|
|
|
|
name = _.isEmpty(name)? jid: name;
|
|
|
|
this.sendContactAddIQ(jid, name, groups,
|
|
|
|
() => {
|
|
|
|
const contact = this.create(_.assignIn({
|
|
|
|
ask: undefined,
|
|
|
|
fullname: name,
|
|
|
|
groups,
|
|
|
|
jid,
|
|
|
|
requesting: false,
|
|
|
|
subscription: 'none'
|
|
|
|
}, attributes), {sort: false});
|
|
|
|
resolve(contact);
|
|
|
|
},
|
|
|
|
function (err) {
|
2017-08-23 11:57:17 +02:00
|
|
|
alert(__('Sorry, there was an error while trying to add %1$s as a contact.', name));
|
2017-07-10 21:14:48 +02:00
|
|
|
_converse.log(err, Strophe.LogLevel.ERROR);
|
|
|
|
resolve(err);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
2016-02-29 22:18:28 +01:00
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
subscribeBack (bare_jid) {
|
2017-06-12 20:30:58 +02:00
|
|
|
const contact = this.get(bare_jid);
|
2016-12-20 10:30:20 +01:00
|
|
|
if (contact instanceof _converse.RosterContact) {
|
2016-02-29 22:18:28 +01:00
|
|
|
contact.authorize().subscribe();
|
|
|
|
} else {
|
|
|
|
// Can happen when a subscription is retried or roster was deleted
|
2017-07-10 21:14:48 +02:00
|
|
|
const handler = (contact) => {
|
2016-12-20 10:30:20 +01:00
|
|
|
if (contact instanceof _converse.RosterContact) {
|
2016-02-29 22:18:28 +01:00
|
|
|
contact.authorize().subscribe();
|
|
|
|
}
|
2017-07-10 21:14:48 +02:00
|
|
|
}
|
|
|
|
this.addContact(bare_jid, '', [], { 'subscription': 'from' }).then(handler, handler);
|
2016-02-29 22:18:28 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
getNumOnlineContacts () {
|
2017-06-12 20:30:58 +02:00
|
|
|
let ignored = ['offline', 'unavailable'];
|
2016-12-20 10:30:20 +01:00
|
|
|
if (_converse.show_only_online_users) {
|
2016-02-29 22:18:28 +01:00
|
|
|
ignored = _.union(ignored, ['dnd', 'xa', 'away']);
|
|
|
|
}
|
2017-07-10 17:46:22 +02:00
|
|
|
return _.sum(this.models.filter((model) => !_.includes(ignored, model.get('chat_status'))));
|
2016-02-29 22:18:28 +01:00
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
onRosterPush (iq) {
|
2016-02-29 22:18:28 +01:00
|
|
|
/* Handle roster updates from the XMPP server.
|
2016-03-18 09:49:32 +01:00
|
|
|
* See: https://xmpp.org/rfcs/rfc6121.html#roster-syntax-actions-push
|
|
|
|
*
|
|
|
|
* Parameters:
|
|
|
|
* (XMLElement) IQ - The IQ stanza received from the XMPP server.
|
|
|
|
*/
|
2017-06-12 20:30:58 +02:00
|
|
|
const id = iq.getAttribute('id');
|
|
|
|
const from = iq.getAttribute('from');
|
2016-12-20 10:30:20 +01:00
|
|
|
if (from && from !== "" && Strophe.getBareJidFromJid(from) !== _converse.bare_jid) {
|
2016-02-29 22:18:28 +01:00
|
|
|
// Receiving client MUST ignore stanza unless it has no from or from = user's bare JID.
|
|
|
|
// XXX: Some naughty servers apparently send from a full
|
|
|
|
// JID so we need to explicitly compare bare jids here.
|
|
|
|
// https://github.com/jcbrand/converse.js/issues/493
|
2016-12-20 10:30:20 +01:00
|
|
|
_converse.connection.send(
|
2017-07-10 17:46:22 +02:00
|
|
|
$iq({type: 'error', id, from: _converse.connection.jid})
|
2016-02-29 22:18:28 +01:00
|
|
|
.c('error', {'type': 'cancel'})
|
|
|
|
.c('service-unavailable', {'xmlns': Strophe.NS.ROSTER })
|
|
|
|
);
|
|
|
|
return true;
|
|
|
|
}
|
2017-07-10 17:46:22 +02:00
|
|
|
_converse.connection.send($iq({type: 'result', id, from: _converse.connection.jid}));
|
|
|
|
const items = sizzle(`query[xmlns="${Strophe.NS.ROSTER}"] item`, iq);
|
2016-11-24 02:07:32 +01:00
|
|
|
_.each(items, this.updateContact.bind(this));
|
2016-12-20 10:30:20 +01:00
|
|
|
_converse.emit('rosterPush', iq);
|
2016-02-29 22:18:28 +01:00
|
|
|
return true;
|
|
|
|
},
|
|
|
|
|
2017-10-27 10:32:02 +02:00
|
|
|
fetchFromServer () {
|
|
|
|
/* Fetch the roster from the XMPP server */
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const iq = $iq({
|
|
|
|
'type': 'get',
|
|
|
|
'id': _converse.connection.getUniqueId('roster')
|
|
|
|
}).c('query', {xmlns: Strophe.NS.ROSTER});
|
|
|
|
|
|
|
|
const callback = _.flow(this.onReceivedFromServer.bind(this), resolve);
|
|
|
|
const errback = function (iq) {
|
|
|
|
const errmsg = "Error while trying to fetch roster from the server";
|
|
|
|
_converse.log(errmsg, Strophe.LogLevel.ERROR);
|
|
|
|
reject(new Error(errmsg));
|
|
|
|
}
|
|
|
|
return _converse.connection.sendIQ(iq, callback, errback);
|
2017-04-21 17:52:54 +02:00
|
|
|
});
|
2016-02-29 22:18:28 +01:00
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
onReceivedFromServer (iq) {
|
2016-02-29 22:18:28 +01:00
|
|
|
/* An IQ stanza containing the roster has been received from
|
2016-03-18 09:49:32 +01:00
|
|
|
* the XMPP server.
|
|
|
|
*/
|
2017-07-10 17:46:22 +02:00
|
|
|
const items = sizzle(`query[xmlns="${Strophe.NS.ROSTER}"] item`, iq);
|
2016-11-24 02:07:32 +01:00
|
|
|
_.each(items, this.updateContact.bind(this));
|
2016-12-20 10:30:20 +01:00
|
|
|
_converse.emit('roster', iq);
|
2016-02-29 22:18:28 +01:00
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
updateContact (item) {
|
2016-02-29 22:18:28 +01:00
|
|
|
/* Update or create RosterContact models based on items
|
2016-03-18 09:49:32 +01:00
|
|
|
* received in the IQ from the server.
|
|
|
|
*/
|
2017-06-12 20:30:58 +02:00
|
|
|
const jid = item.getAttribute('jid');
|
2016-02-29 22:18:28 +01:00
|
|
|
if (this.isSelf(jid)) { return; }
|
2017-06-12 20:30:58 +02:00
|
|
|
|
|
|
|
const contact = this.get(jid),
|
|
|
|
subscription = item.getAttribute("subscription"),
|
|
|
|
ask = item.getAttribute("ask"),
|
|
|
|
groups = _.map(item.getElementsByTagName('group'), Strophe.getText);
|
|
|
|
|
2016-02-29 22:18:28 +01:00
|
|
|
if (!contact) {
|
|
|
|
if ((subscription === "none" && ask === null) || (subscription === "remove")) {
|
|
|
|
return; // We're lazy when adding contacts.
|
|
|
|
}
|
|
|
|
this.create({
|
2017-07-10 17:46:22 +02:00
|
|
|
ask,
|
2016-02-29 22:18:28 +01:00
|
|
|
fullname: item.getAttribute("name") || jid,
|
2017-07-10 17:46:22 +02:00
|
|
|
groups,
|
|
|
|
jid,
|
|
|
|
subscription
|
2016-02-29 22:18:28 +01:00
|
|
|
}, {sort: false});
|
|
|
|
} else {
|
|
|
|
if (subscription === "remove") {
|
|
|
|
return contact.destroy(); // will trigger removeFromRoster
|
|
|
|
}
|
|
|
|
// We only find out about requesting contacts via the
|
|
|
|
// presence handler, so if we receive a contact
|
|
|
|
// here, we know they aren't requesting anymore.
|
|
|
|
// see docs/DEVELOPER.rst
|
|
|
|
contact.save({
|
2017-07-10 17:46:22 +02:00
|
|
|
subscription,
|
|
|
|
ask,
|
2016-02-29 22:18:28 +01:00
|
|
|
requesting: null,
|
2017-07-10 17:46:22 +02:00
|
|
|
groups
|
2016-02-29 22:18:28 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
createRequestingContact (presence) {
|
2016-03-16 12:16:32 +01:00
|
|
|
/* Creates a Requesting Contact.
|
|
|
|
*
|
|
|
|
* Note: this method gets completely overridden by converse-vcard.js
|
2016-03-01 22:57:27 +01:00
|
|
|
*/
|
2017-06-12 20:30:58 +02:00
|
|
|
const bare_jid = Strophe.getBareJidFromJid(presence.getAttribute('from')),
|
2017-07-10 17:46:22 +02:00
|
|
|
nick_el = presence.querySelector(`nick[xmlns="${Strophe.NS.NICK}"]`);
|
2017-06-12 20:30:58 +02:00
|
|
|
const user_data = {
|
2016-02-29 22:18:28 +01:00
|
|
|
jid: bare_jid,
|
|
|
|
subscription: 'none',
|
|
|
|
ask: null,
|
|
|
|
requesting: true,
|
2016-11-24 02:07:32 +01:00
|
|
|
fullname: nick_el && nick_el.textContent || bare_jid,
|
2016-03-01 22:57:27 +01:00
|
|
|
};
|
|
|
|
this.create(user_data);
|
2016-12-20 10:30:20 +01:00
|
|
|
_converse.emit('contactRequest', user_data);
|
2016-02-29 22:18:28 +01:00
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
handleIncomingSubscription (presence) {
|
2017-06-12 20:30:58 +02:00
|
|
|
const jid = presence.getAttribute('from'),
|
|
|
|
bare_jid = Strophe.getBareJidFromJid(jid),
|
|
|
|
contact = this.get(bare_jid);
|
|
|
|
|
2016-12-20 10:30:20 +01:00
|
|
|
if (!_converse.allow_contact_requests) {
|
|
|
|
_converse.rejectPresenceSubscription(
|
2016-03-16 12:16:32 +01:00
|
|
|
jid,
|
|
|
|
__("This client does not allow presence subscriptions")
|
|
|
|
);
|
2016-02-29 22:18:28 +01:00
|
|
|
}
|
2016-12-20 10:30:20 +01:00
|
|
|
if (_converse.auto_subscribe) {
|
2016-02-29 22:18:28 +01:00
|
|
|
if ((!contact) || (contact.get('subscription') !== 'to')) {
|
|
|
|
this.subscribeBack(bare_jid);
|
|
|
|
} else {
|
|
|
|
contact.authorize();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (contact) {
|
|
|
|
if (contact.get('subscription') !== 'none') {
|
|
|
|
contact.authorize();
|
|
|
|
} else if (contact.get('ask') === "subscribe") {
|
|
|
|
contact.authorize();
|
|
|
|
}
|
2017-06-12 20:30:58 +02:00
|
|
|
} else {
|
2016-03-16 12:16:32 +01:00
|
|
|
this.createRequestingContact(presence);
|
2016-02-29 22:18:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
presenceHandler (presence) {
|
2017-06-12 20:30:58 +02:00
|
|
|
const presence_type = presence.getAttribute('type');
|
2016-02-29 22:18:28 +01:00
|
|
|
if (presence_type === 'error') { return true; }
|
2017-06-12 20:30:58 +02:00
|
|
|
|
|
|
|
const jid = presence.getAttribute('from'),
|
2016-02-29 22:18:28 +01:00
|
|
|
bare_jid = Strophe.getBareJidFromJid(jid),
|
|
|
|
resource = Strophe.getResourceFromJid(jid),
|
2016-11-24 02:07:32 +01:00
|
|
|
chat_status = _.propertyOf(presence.querySelector('show'))('textContent') || 'online',
|
|
|
|
status_message = _.propertyOf(presence.querySelector('status'))('textContent'),
|
2016-02-29 22:18:28 +01:00
|
|
|
contact = this.get(bare_jid);
|
2017-02-20 22:27:34 +01:00
|
|
|
|
2016-02-29 22:18:28 +01:00
|
|
|
if (this.isSelf(bare_jid)) {
|
2016-12-20 10:30:20 +01:00
|
|
|
if ((_converse.connection.jid !== jid) &&
|
2016-03-16 16:49:44 +01:00
|
|
|
(presence_type !== 'unavailable') &&
|
2016-12-20 10:30:20 +01:00
|
|
|
(_converse.synchronize_availability === true ||
|
|
|
|
_converse.synchronize_availability === resource)) {
|
2016-03-16 16:49:44 +01:00
|
|
|
// Another resource has changed its status and
|
|
|
|
// synchronize_availability option set to update,
|
|
|
|
// we'll update ours as well.
|
2016-12-20 10:30:20 +01:00
|
|
|
_converse.xmppstatus.save({'status': chat_status});
|
2016-11-24 02:07:32 +01:00
|
|
|
if (status_message) {
|
2016-12-20 10:30:20 +01:00
|
|
|
_converse.xmppstatus.save({'status_message': status_message});
|
2016-03-16 16:49:44 +01:00
|
|
|
}
|
2016-02-29 22:18:28 +01:00
|
|
|
}
|
|
|
|
return;
|
2017-07-10 17:46:22 +02:00
|
|
|
} else if (sizzle(`query[xmlns="${Strophe.NS.MUC}"]`, presence).length) {
|
2016-02-29 22:18:28 +01:00
|
|
|
return; // Ignore MUC
|
|
|
|
}
|
2016-11-24 02:07:32 +01:00
|
|
|
if (contact && (status_message !== contact.get('status'))) {
|
|
|
|
contact.save({'status': status_message});
|
2016-02-29 22:18:28 +01:00
|
|
|
}
|
|
|
|
if (presence_type === 'subscribed' && contact) {
|
|
|
|
contact.ackSubscribe();
|
|
|
|
} else if (presence_type === 'unsubscribed' && contact) {
|
|
|
|
contact.ackUnsubscribe();
|
|
|
|
} else if (presence_type === 'unsubscribe') {
|
|
|
|
return;
|
|
|
|
} else if (presence_type === 'subscribe') {
|
2016-03-16 12:16:32 +01:00
|
|
|
this.handleIncomingSubscription(presence);
|
2016-02-29 22:18:28 +01:00
|
|
|
} else if (presence_type === 'unavailable' && contact) {
|
2017-02-20 22:27:34 +01:00
|
|
|
contact.removeResource(resource);
|
2017-02-21 23:25:19 +01:00
|
|
|
} else if (contact) {
|
|
|
|
// presence_type is undefined
|
|
|
|
contact.addResource(presence);
|
2016-02-29 22:18:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2016-06-21 17:06:51 +02:00
|
|
|
this.RosterGroup = Backbone.Model.extend({
|
2017-12-17 16:03:28 +01:00
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
initialize (attributes) {
|
2017-01-26 10:08:43 +01:00
|
|
|
this.set(_.assignIn({
|
2017-04-19 14:18:30 +02:00
|
|
|
description: __('Click to hide these contacts'),
|
2016-12-20 10:30:20 +01:00
|
|
|
state: _converse.OPENED
|
2016-06-21 17:06:51 +02:00
|
|
|
}, attributes));
|
|
|
|
// Collection of contacts belonging to this group.
|
2016-12-20 10:30:20 +01:00
|
|
|
this.contacts = new _converse.RosterContacts();
|
2016-06-21 17:06:51 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
this.RosterGroups = Backbone.Collection.extend({
|
2016-12-20 10:30:20 +01:00
|
|
|
model: _converse.RosterGroup,
|
2016-09-20 12:10:28 +02:00
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
fetchRosterGroups () {
|
2016-09-20 12:10:28 +02:00
|
|
|
/* Fetches all the roster groups from sessionStorage.
|
|
|
|
*
|
|
|
|
* Returns a promise which resolves once the groups have been
|
|
|
|
* returned.
|
|
|
|
*/
|
2017-07-10 21:14:48 +02:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
this.fetch({
|
|
|
|
silent: true, // We need to first have all groups before
|
|
|
|
// we can start positioning them, so we set
|
|
|
|
// 'silent' to true.
|
|
|
|
success: resolve
|
|
|
|
});
|
2016-09-20 12:10:28 +02:00
|
|
|
});
|
|
|
|
}
|
2016-06-21 17:06:51 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
|
2017-09-08 14:24:38 +02:00
|
|
|
this.ConnectionFeedback = Backbone.Model.extend({
|
2017-09-22 18:55:02 +02:00
|
|
|
defaults: {
|
2017-12-13 23:04:49 +01:00
|
|
|
'connection_status': Strophe.Status.DISCONNECTED,
|
2017-09-22 18:55:02 +02:00
|
|
|
'message': ''
|
2017-09-08 14:24:38 +02:00
|
|
|
},
|
|
|
|
|
2017-09-22 18:55:02 +02:00
|
|
|
initialize () {
|
|
|
|
this.on('change', () => {
|
|
|
|
_converse.emit('connfeedback', _converse.connfeedback);
|
2017-09-08 14:24:38 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
this.connfeedback = new this.ConnectionFeedback();
|
|
|
|
|
2016-02-28 20:24:06 +01:00
|
|
|
this.XMPPStatus = Backbone.Model.extend({
|
2018-02-23 22:15:04 +01:00
|
|
|
|
|
|
|
defaults () {
|
|
|
|
return {
|
|
|
|
"status": _converse.default_state,
|
|
|
|
"jid": _converse.bare_jid,
|
|
|
|
"vcard_updated": null
|
|
|
|
}
|
2018-02-21 14:40:41 +01:00
|
|
|
},
|
2018-02-07 17:30:44 +01:00
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
initialize () {
|
2018-02-21 14:40:41 +01:00
|
|
|
this.on('change:status', (item) => {
|
|
|
|
const status = this.get('status');
|
|
|
|
this.sendPresence(status);
|
|
|
|
_converse.emit('statusChanged', status);
|
2016-02-28 20:24:06 +01:00
|
|
|
});
|
2018-02-21 14:40:41 +01:00
|
|
|
|
|
|
|
this.on('change:status_message', () => {
|
|
|
|
const status_message = this.get('status_message');
|
|
|
|
this.sendPresence(this.get('status'), status_message);
|
|
|
|
_converse.emit('statusMessageChanged', status_message);
|
2017-07-10 17:46:22 +02:00
|
|
|
});
|
2016-02-16 08:46:47 +01:00
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
constructPresence (type, status_message) {
|
2017-06-12 20:30:58 +02:00
|
|
|
let presence;
|
2016-12-20 10:30:20 +01:00
|
|
|
type = _.isString(type) ? type : (this.get('status') || _converse.default_state);
|
2018-03-02 19:35:06 +01:00
|
|
|
status_message = _.isString(status_message) ? status_message : this.get('status_message');
|
2016-02-28 20:24:06 +01:00
|
|
|
// Most of these presence types are actually not explicitly sent,
|
2016-05-31 11:39:14 +02:00
|
|
|
// but I add all of them here for reference and future proofing.
|
2016-02-28 20:24:06 +01:00
|
|
|
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'});
|
2016-05-31 11:39:14 +02:00
|
|
|
} else if (type === 'online') {
|
|
|
|
presence = $pres();
|
2016-02-28 20:24:06 +01:00
|
|
|
} else {
|
2016-05-31 11:39:14 +02:00
|
|
|
presence = $pres().c('show').t(type).up();
|
|
|
|
}
|
|
|
|
if (status_message) {
|
2017-02-22 22:13:23 +01:00
|
|
|
presence.c('status').t(status_message).up();
|
2016-02-28 20:24:06 +01:00
|
|
|
}
|
2017-02-22 22:13:23 +01:00
|
|
|
presence.c('priority').t(
|
|
|
|
_.isNaN(Number(_converse.priority)) ? 0 : _converse.priority
|
|
|
|
);
|
2016-02-28 20:24:06 +01:00
|
|
|
return presence;
|
2016-02-16 08:46:47 +01:00
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
sendPresence (type, status_message) {
|
2016-12-20 10:30:20 +01:00
|
|
|
_converse.connection.send(this.constructPresence(type, status_message));
|
2016-02-16 08:46:47 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
this.setUpXMLLogging = function () {
|
2016-05-28 10:55:03 +02:00
|
|
|
Strophe.log = function (level, msg) {
|
2016-12-20 10:30:20 +01:00
|
|
|
_converse.log(msg, level);
|
2016-05-28 10:55:03 +02:00
|
|
|
};
|
2016-02-16 08:46:47 +01:00
|
|
|
if (this.debug) {
|
2017-07-10 10:43:13 +02:00
|
|
|
this.connection.xmlInput = function (body) {
|
2017-10-26 15:47:49 +02:00
|
|
|
_converse.log(body.outerHTML, Strophe.LogLevel.DEBUG, 'color: darkgoldenrod');
|
2017-07-10 10:43:13 +02:00
|
|
|
};
|
|
|
|
this.connection.xmlOutput = function (body) {
|
2017-10-26 15:47:49 +02:00
|
|
|
_converse.log(body.outerHTML, Strophe.LogLevel.DEBUG, 'color: darkcyan');
|
2017-07-10 10:43:13 +02:00
|
|
|
};
|
2016-02-16 08:46:47 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-07-10 21:14:48 +02:00
|
|
|
this.fetchLoginCredentials = () =>
|
|
|
|
new Promise((resolve, reject) => {
|
|
|
|
const 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) {
|
|
|
|
const data = JSON.parse(xhr.responseText);
|
|
|
|
resolve({
|
|
|
|
'jid': data.jid,
|
|
|
|
'password': data.password
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
xhr.onerror();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
xhr.onerror = function () {
|
|
|
|
delete _converse.connection;
|
2017-08-16 11:16:22 +02:00
|
|
|
_converse.emit('noResumeableSession', this);
|
2017-07-10 21:14:48 +02:00
|
|
|
reject(xhr.responseText);
|
|
|
|
};
|
|
|
|
xhr.send();
|
|
|
|
});
|
2016-03-31 10:37:28 +02:00
|
|
|
|
2016-02-16 08:46:47 +01:00
|
|
|
this.startNewBOSHSession = function () {
|
2017-06-12 20:30:58 +02:00
|
|
|
const xhr = new XMLHttpRequest();
|
2017-02-25 23:26:14 +01:00
|
|
|
xhr.open('GET', _converse.prebind_url, true);
|
|
|
|
xhr.setRequestHeader('Accept', "application/json, text/javascript");
|
|
|
|
xhr.onload = function() {
|
|
|
|
if (xhr.status >= 200 && xhr.status < 400) {
|
2017-06-12 20:30:58 +02:00
|
|
|
const data = JSON.parse(xhr.responseText);
|
2017-02-25 23:26:14 +01:00
|
|
|
_converse.connection.attach(
|
|
|
|
data.jid, data.sid, data.rid,
|
|
|
|
_converse.onConnectStatusChanged);
|
|
|
|
} else {
|
|
|
|
xhr.onerror();
|
2016-11-07 18:30:10 +01:00
|
|
|
}
|
2017-02-25 23:26:14 +01:00
|
|
|
};
|
|
|
|
xhr.onerror = function () {
|
|
|
|
delete _converse.connection;
|
2017-08-16 11:16:22 +02:00
|
|
|
_converse.emit('noResumeableSession', this);
|
2017-02-25 23:26:14 +01:00
|
|
|
};
|
|
|
|
xhr.send();
|
2016-02-16 08:46:47 +01:00
|
|
|
};
|
|
|
|
|
2017-05-15 16:47:10 +02:00
|
|
|
this.restoreBOSHSession = function (jid_is_required) {
|
2017-05-11 10:23:29 +02:00
|
|
|
/* Tries to restore a cached BOSH session. */
|
|
|
|
if (!this.jid) {
|
2017-06-12 20:30:58 +02:00
|
|
|
const msg = "restoreBOSHSession: tried to restore a \"keepalive\" session "+
|
|
|
|
"but we don't have the JID for the user!";
|
2017-05-15 16:47:10 +02:00
|
|
|
if (jid_is_required) {
|
|
|
|
throw new Error(msg);
|
|
|
|
} else {
|
|
|
|
_converse.log(msg);
|
|
|
|
}
|
2017-05-11 10:23:29 +02:00
|
|
|
}
|
|
|
|
try {
|
|
|
|
this.connection.restore(this.jid, this.onConnectStatusChanged);
|
|
|
|
return true;
|
|
|
|
} catch (e) {
|
2017-08-21 12:52:18 +02:00
|
|
|
_converse.log(
|
2017-05-11 10:23:29 +02:00
|
|
|
"Could not restore session for jid: "+
|
2017-12-18 13:19:14 +01:00
|
|
|
this.jid+" Error message: "+e.message, Strophe.LogLevel.WARN);
|
2017-05-11 10:23:29 +02:00
|
|
|
this.clearSession(); // If there's a roster, we want to clear it (see #555)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-11-23 12:53:06 +01:00
|
|
|
this.attemptPreboundSession = function (reconnecting) {
|
2017-05-11 10:23:29 +02:00
|
|
|
/* Handle session resumption or initialization when prebind is
|
|
|
|
* being used.
|
2016-02-16 08:46:47 +01:00
|
|
|
*/
|
2017-05-11 10:23:29 +02:00
|
|
|
if (!reconnecting) {
|
2017-05-15 16:47:10 +02:00
|
|
|
if (this.keepalive && this.restoreBOSHSession(true)) {
|
2017-05-11 10:23:29 +02:00
|
|
|
return;
|
2016-02-16 08:46:47 +01:00
|
|
|
}
|
2017-05-11 10:23:29 +02:00
|
|
|
// 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
|
|
|
|
);
|
2016-02-16 08:46:47 +01:00
|
|
|
}
|
|
|
|
}
|
2017-05-11 10:23:29 +02:00
|
|
|
if (this.prebind_url) {
|
2016-11-07 18:30:10 +01:00
|
|
|
return this.startNewBOSHSession();
|
2016-02-16 08:46:47 +01:00
|
|
|
} else {
|
2017-05-11 10:23:29 +02:00
|
|
|
throw new Error(
|
|
|
|
"attemptPreboundSession: If you use prebind and not keepalive, "+
|
2016-11-07 18:30:10 +01:00
|
|
|
"then you MUST supply JID, RID and SID values or a prebind_url.");
|
2016-02-16 08:46:47 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-05-11 10:35:45 +02:00
|
|
|
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;
|
|
|
|
}
|
2017-09-16 20:02:47 +02:00
|
|
|
|
|
|
|
if (credentials) {
|
|
|
|
// When credentials are passed in, they override prebinding
|
|
|
|
// or credentials fetching via HTTP
|
|
|
|
this.autoLogin(credentials);
|
|
|
|
} else if (this.auto_login) {
|
|
|
|
if (this.credentials_url) {
|
2017-07-10 21:14:48 +02:00
|
|
|
this.fetchLoginCredentials().then(
|
|
|
|
this.autoLogin.bind(this),
|
|
|
|
this.autoLogin.bind(this)
|
|
|
|
);
|
2017-05-11 10:35:45 +02:00
|
|
|
} 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();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-03-31 10:37:28 +02:00
|
|
|
this.autoLogin = function (credentials) {
|
|
|
|
if (credentials) {
|
2017-05-11 14:33:41 +02:00
|
|
|
// If passed in, the credentials come from credentials_url,
|
|
|
|
// so we set them on the converse object.
|
2016-03-31 10:37:28 +02:00
|
|
|
this.jid = credentials.jid;
|
|
|
|
}
|
2016-12-20 10:30:20 +01:00
|
|
|
if (this.authentication === _converse.ANONYMOUS) {
|
2016-07-24 10:54:57 +02:00
|
|
|
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 " +
|
2016-12-20 10:30:20 +01:00
|
|
|
"_converse.api.user.login.");
|
2016-07-24 10:54:57 +02:00
|
|
|
}
|
2017-06-23 20:19:38 +02:00
|
|
|
if (!this.connection.reconnecting) {
|
|
|
|
this.connection.reset();
|
|
|
|
}
|
2016-03-31 10:37:28 +02:00
|
|
|
this.connection.connect(this.jid.toLowerCase(), null, this.onConnectStatusChanged);
|
2016-12-20 10:30:20 +01:00
|
|
|
} else if (this.authentication === _converse.LOGIN) {
|
2017-06-12 20:30:58 +02:00
|
|
|
const password = _.isNil(credentials) ? (_converse.connection.pass || this.password) : credentials.password;
|
2016-08-18 21:43:18 +02:00
|
|
|
if (!password) {
|
2017-05-11 14:33:41 +02:00
|
|
|
if (this.auto_login) {
|
2016-08-23 20:10:08 +02:00
|
|
|
throw new Error("initConnection: If you use auto_login and "+
|
|
|
|
"authentication='login' then you also need to provide a password.");
|
|
|
|
}
|
2016-12-20 10:30:20 +01:00
|
|
|
_converse.setDisconnectionCause(Strophe.Status.AUTHFAIL, undefined, true);
|
|
|
|
_converse.disconnect();
|
2016-10-27 12:58:51 +02:00
|
|
|
return;
|
2016-03-31 10:37:28 +02:00
|
|
|
}
|
2017-06-12 20:30:58 +02:00
|
|
|
const resource = Strophe.getResourceFromJid(this.jid);
|
2016-03-31 10:37:28 +02:00
|
|
|
if (!resource) {
|
2016-12-20 10:30:20 +01:00
|
|
|
this.jid = this.jid.toLowerCase() + _converse.generateResource();
|
2016-03-31 10:37:28 +02:00
|
|
|
} else {
|
|
|
|
this.jid = Strophe.getBareJidFromJid(this.jid).toLowerCase()+'/'+resource;
|
|
|
|
}
|
2017-06-23 20:19:38 +02:00
|
|
|
if (!this.connection.reconnecting) {
|
|
|
|
this.connection.reset();
|
|
|
|
}
|
2016-08-18 21:43:18 +02:00
|
|
|
this.connection.connect(this.jid, password, this.onConnectStatusChanged);
|
2016-03-31 10:37:28 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-11-23 12:53:06 +01:00
|
|
|
this.logIn = function (credentials, reconnecting) {
|
2016-11-07 18:30:10 +01:00
|
|
|
// We now try to resume or automatically set up a new session.
|
|
|
|
// Otherwise the user will be shown a login form.
|
2016-12-20 10:30:20 +01:00
|
|
|
if (this.authentication === _converse.PREBIND) {
|
2016-11-23 12:53:06 +01:00
|
|
|
this.attemptPreboundSession(reconnecting);
|
2016-04-13 13:52:28 +02:00
|
|
|
} else {
|
2016-11-23 12:53:06 +01:00
|
|
|
this.attemptNonPreboundSession(credentials, reconnecting);
|
2016-04-13 13:52:28 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-02-16 08:46:47 +01:00
|
|
|
this.initConnection = function () {
|
2017-08-16 12:31:17 +02:00
|
|
|
if (!this.connection) {
|
|
|
|
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.");
|
|
|
|
}
|
2016-02-16 08:46:47 +01:00
|
|
|
}
|
2017-08-16 12:31:17 +02:00
|
|
|
_converse.emit('connectionInitialized');
|
2016-02-16 08:46:47 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
this._tearDown = function () {
|
|
|
|
/* Remove those views which are only allowed with a valid
|
|
|
|
* connection.
|
|
|
|
*/
|
2017-07-21 12:41:16 +02:00
|
|
|
_converse.emit('beforeTearDown');
|
2017-08-16 12:31:17 +02:00
|
|
|
_converse.unregisterPresenceHandler();
|
|
|
|
if (_converse.roster) {
|
|
|
|
_converse.roster.off().reset(); // Removes roster contacts
|
|
|
|
}
|
|
|
|
if (!_.isUndefined(_converse.session)) {
|
|
|
|
_converse.session.destroy();
|
2016-02-16 08:46:47 +01:00
|
|
|
}
|
2017-04-11 11:14:26 +02:00
|
|
|
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);
|
2016-12-20 10:30:20 +01:00
|
|
|
window.clearInterval(_converse.everySecondTrigger);
|
2017-07-21 12:41:16 +02:00
|
|
|
_converse.emit('afterTearDown');
|
2017-08-16 12:31:17 +02:00
|
|
|
return _converse;
|
2016-02-16 08:46:47 +01:00
|
|
|
};
|
|
|
|
|
2017-03-07 12:02:40 +01:00
|
|
|
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 = [];
|
2017-06-12 20:30:58 +02:00
|
|
|
const whitelist = _converse.core_plugins.concat(
|
2017-03-07 12:02:40 +01:00
|
|
|
_converse.whitelisted_plugins);
|
|
|
|
|
2018-02-09 16:33:48 +01:00
|
|
|
if (_converse.view_mode === 'embedded') {
|
|
|
|
_.forEach([ // eslint-disable-line lodash/prefer-map
|
|
|
|
"converse-bookmarks",
|
|
|
|
"converse-controlbox",
|
|
|
|
"converse-dragresize",
|
|
|
|
"converse-headline",
|
|
|
|
"converse-minimize",
|
|
|
|
"converse-otr",
|
|
|
|
"converse-register",
|
|
|
|
"converse-vcard",
|
|
|
|
], (name) => {
|
|
|
|
_converse.blacklisted_plugins.push(name)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-03-07 12:02:40 +01:00
|
|
|
_converse.pluggable.initializePlugins({
|
2017-07-10 17:46:22 +02:00
|
|
|
'updateSettings' () {
|
2017-07-05 11:33:55 +02:00
|
|
|
_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);
|
|
|
|
},
|
2017-03-07 12:02:40 +01:00
|
|
|
'_converse': _converse
|
|
|
|
}, whitelist, _converse.blacklisted_plugins);
|
|
|
|
_converse.emit('pluginsInitialized');
|
|
|
|
};
|
2017-02-03 19:12:19 +01:00
|
|
|
|
2017-03-07 12:02:40 +01:00
|
|
|
// Initialization
|
|
|
|
// --------------
|
|
|
|
// This is the end of the initialize method.
|
|
|
|
if (settings.connection) {
|
|
|
|
this.connection = settings.connection;
|
|
|
|
}
|
2016-11-02 23:08:20 +01:00
|
|
|
|
2017-09-24 20:36:39 +02:00
|
|
|
function finishInitialization () {
|
|
|
|
_converse.initPlugins();
|
|
|
|
_converse.initConnection();
|
|
|
|
_converse.setUpXMLLogging();
|
|
|
|
_converse.logIn();
|
|
|
|
_converse.registerGlobalEventHandlers();
|
2017-09-28 23:29:37 +02:00
|
|
|
|
|
|
|
if (!Backbone.history.started) {
|
|
|
|
Backbone.history.start();
|
|
|
|
}
|
2017-09-24 20:36:39 +02:00
|
|
|
}
|
|
|
|
|
2016-12-20 10:30:20 +01:00
|
|
|
if (!_.isUndefined(_converse.connection) &&
|
2017-10-24 12:56:35 +02:00
|
|
|
_converse.connection.service === 'jasmine tests') {
|
2017-09-24 20:36:39 +02:00
|
|
|
finishInitialization();
|
2016-12-20 10:30:20 +01:00
|
|
|
return _converse;
|
2017-10-24 12:56:35 +02:00
|
|
|
} else if (_.isUndefined(i18n)) {
|
|
|
|
finishInitialization();
|
2016-11-02 23:08:20 +01:00
|
|
|
} else {
|
2017-10-24 12:56:35 +02:00
|
|
|
i18n.fetchTranslations(
|
2017-09-24 20:36:39 +02:00
|
|
|
_converse.locale,
|
2017-10-24 12:56:35 +02:00
|
|
|
_converse.locales,
|
2018-02-19 10:35:58 +01:00
|
|
|
u.interpolate(_converse.locales_url, {'locale': _converse.locale}))
|
2018-02-09 16:33:48 +01:00
|
|
|
.catch(_.partial(_converse.log, _, Strophe.LogLevel.FATAL))
|
|
|
|
.then(finishInitialization)
|
|
|
|
.catch(_.partial(_converse.log, _, Strophe.LogLevel.FATAL));
|
2016-11-02 23:08:20 +01:00
|
|
|
}
|
2017-11-10 15:52:50 +01:00
|
|
|
return init_promise;
|
2016-02-16 08:46:47 +01:00
|
|
|
};
|
2017-02-14 15:08:39 +01:00
|
|
|
|
|
|
|
// API methods only available to plugins
|
|
|
|
_converse.api = {
|
|
|
|
'connection': {
|
2017-07-10 17:46:22 +02:00
|
|
|
'connected' () {
|
2017-02-14 15:08:39 +01:00
|
|
|
return _converse.connection && _converse.connection.connected || false;
|
|
|
|
},
|
2017-07-10 17:46:22 +02:00
|
|
|
'disconnect' () {
|
2017-02-14 15:08:39 +01:00
|
|
|
_converse.connection.disconnect();
|
|
|
|
},
|
|
|
|
},
|
2017-07-10 17:46:22 +02:00
|
|
|
'emit' () {
|
2017-07-05 11:03:13 +02:00
|
|
|
_converse.emit.apply(_converse, arguments);
|
|
|
|
},
|
2017-02-14 15:08:39 +01:00
|
|
|
'user': {
|
2017-07-10 17:46:22 +02:00
|
|
|
'jid' () {
|
2017-02-14 15:08:39 +01:00
|
|
|
return _converse.connection.jid;
|
|
|
|
},
|
2017-07-10 17:46:22 +02:00
|
|
|
'login' (credentials) {
|
2017-02-14 15:08:39 +01:00
|
|
|
_converse.initConnection();
|
|
|
|
_converse.logIn(credentials);
|
|
|
|
},
|
2017-07-10 17:46:22 +02:00
|
|
|
'logout' () {
|
2017-02-14 15:08:39 +01:00
|
|
|
_converse.logOut();
|
|
|
|
},
|
|
|
|
'status': {
|
2017-07-10 17:46:22 +02:00
|
|
|
'get' () {
|
2017-02-14 15:08:39 +01:00
|
|
|
return _converse.xmppstatus.get('status');
|
|
|
|
},
|
2017-07-10 17:46:22 +02:00
|
|
|
'set' (value, message) {
|
2017-06-12 20:30:58 +02:00
|
|
|
const data = {'status': value};
|
2017-02-14 15:08:39 +01:00
|
|
|
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': {
|
2017-07-10 17:46:22 +02:00
|
|
|
'get' () {
|
2017-02-14 15:08:39 +01:00
|
|
|
return _converse.xmppstatus.get('status_message');
|
|
|
|
},
|
2017-07-10 17:46:22 +02:00
|
|
|
'set' (stat) {
|
2017-02-14 15:08:39 +01:00
|
|
|
_converse.xmppstatus.save({'status_message': stat});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
'settings': {
|
2017-07-10 17:46:22 +02:00
|
|
|
'update' (settings) {
|
2018-02-19 10:34:52 +01:00
|
|
|
u.merge(_converse.default_settings, settings);
|
|
|
|
u.merge(_converse, settings);
|
|
|
|
u.applyUserSettings(_converse, settings, _converse.user_settings);
|
2017-07-05 11:03:13 +02:00
|
|
|
},
|
2017-07-10 17:46:22 +02:00
|
|
|
'get' (key) {
|
2017-02-14 15:08:39 +01:00
|
|
|
if (_.includes(_.keys(_converse.default_settings), key)) {
|
|
|
|
return _converse[key];
|
|
|
|
}
|
|
|
|
},
|
2017-07-10 17:46:22 +02:00
|
|
|
'set' (key, val) {
|
2017-06-12 20:30:58 +02:00
|
|
|
const o = {};
|
2017-02-14 15:08:39 +01:00
|
|
|
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)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2017-07-05 11:03:13 +02:00
|
|
|
'promises': {
|
2017-07-10 17:46:22 +02:00
|
|
|
'add' (promises) {
|
2017-07-05 11:03:13 +02:00
|
|
|
promises = _.isArray(promises) ? promises : [promises]
|
2017-07-10 21:14:48 +02:00
|
|
|
_.each(promises, addPromise);
|
2017-07-05 11:03:13 +02:00
|
|
|
}
|
|
|
|
},
|
2017-02-14 15:08:39 +01:00
|
|
|
'contacts': {
|
2017-07-10 17:46:22 +02:00
|
|
|
'get' (jids) {
|
2017-06-12 20:30:58 +02:00
|
|
|
const _transform = function (jid) {
|
|
|
|
const contact = _converse.roster.get(Strophe.getBareJidFromJid(jid));
|
2017-02-14 15:08:39 +01:00
|
|
|
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);
|
|
|
|
},
|
2017-07-10 17:46:22 +02:00
|
|
|
'add' (jid, name) {
|
2017-02-14 15:08:39 +01:00
|
|
|
if (!_.isString(jid) || !_.includes(jid, '@')) {
|
|
|
|
throw new TypeError('contacts.add: invalid jid');
|
|
|
|
}
|
|
|
|
_converse.roster.addAndSubscribe(jid, _.isEmpty(name)? jid: name);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
'tokens': {
|
2017-07-10 17:46:22 +02:00
|
|
|
'get' (id) {
|
2017-02-14 15:08:39 +01:00
|
|
|
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': {
|
2017-03-08 12:52:41 +01:00
|
|
|
'once': _converse.once.bind(_converse),
|
|
|
|
'on': _converse.on.bind(_converse),
|
|
|
|
'not': _converse.off.bind(_converse),
|
2017-07-10 17:46:22 +02:00
|
|
|
'stanza' (name, options, handler) {
|
2017-02-14 15:08:39 +01:00
|
|
|
if (_.isFunction(options)) {
|
|
|
|
handler = options;
|
|
|
|
options = {};
|
|
|
|
} else {
|
|
|
|
options = options || {};
|
|
|
|
}
|
|
|
|
_converse.connection.addHandler(
|
|
|
|
handler,
|
|
|
|
options.ns,
|
|
|
|
name,
|
|
|
|
options.type,
|
|
|
|
options.id,
|
|
|
|
options.from,
|
|
|
|
options
|
|
|
|
);
|
|
|
|
},
|
|
|
|
},
|
2017-07-10 17:46:22 +02:00
|
|
|
'waitUntil' (name) {
|
2017-06-12 20:30:58 +02:00
|
|
|
const promise = _converse.promises[name];
|
2017-02-14 15:08:39 +01:00
|
|
|
if (_.isUndefined(promise)) {
|
|
|
|
return null;
|
|
|
|
}
|
2017-11-10 15:52:50 +01:00
|
|
|
return promise;
|
2017-02-14 15:08:39 +01:00
|
|
|
},
|
2017-07-10 17:46:22 +02:00
|
|
|
'send' (stanza) {
|
2017-02-14 15:08:39 +01:00
|
|
|
_converse.connection.send(stanza);
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
// The public API
|
2018-02-09 16:16:18 +01:00
|
|
|
window.converse = {
|
2017-07-10 17:46:22 +02:00
|
|
|
'initialize' (settings, callback) {
|
2017-02-14 15:08:39 +01:00
|
|
|
return _converse.initialize(settings, callback);
|
|
|
|
},
|
|
|
|
'plugins': {
|
2017-07-10 17:46:22 +02:00
|
|
|
'add' (name, plugin) {
|
2017-02-14 15:08:39 +01:00
|
|
|
plugin.__name__ = name;
|
|
|
|
if (!_.isUndefined(_converse.pluggable.plugins[name])) {
|
|
|
|
throw new TypeError(
|
2017-07-10 17:46:22 +02:00
|
|
|
`Error: plugin with name "${name}" has already been `+
|
2017-02-14 15:08:39 +01:00
|
|
|
'registered!');
|
|
|
|
} else {
|
|
|
|
_converse.pluggable.plugins[name] = plugin;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
'env': {
|
|
|
|
'$build': $build,
|
|
|
|
'$iq': $iq,
|
|
|
|
'$msg': $msg,
|
|
|
|
'$pres': $pres,
|
2017-04-21 15:52:16 +02:00
|
|
|
'Backbone': Backbone,
|
2017-07-10 21:14:48 +02:00
|
|
|
'Promise': Promise,
|
2017-02-14 15:08:39 +01:00
|
|
|
'Strophe': Strophe,
|
|
|
|
'_': _,
|
2018-02-08 17:06:53 +01:00
|
|
|
'f': f,
|
2017-04-21 15:52:16 +02:00
|
|
|
'b64_sha1': b64_sha1,
|
2017-02-14 15:08:39 +01:00
|
|
|
'moment': moment,
|
2017-04-21 15:52:16 +02:00
|
|
|
'sizzle': sizzle,
|
2018-02-19 10:34:52 +01:00
|
|
|
'utils': u
|
2017-02-14 15:08:39 +01:00
|
|
|
}
|
|
|
|
};
|
2018-03-02 15:51:04 +01:00
|
|
|
window.dispatchEvent(new CustomEvent('converse-loaded'));
|
2018-02-09 16:16:18 +01:00
|
|
|
return window.converse;
|
2016-02-16 08:46:47 +01:00
|
|
|
}));
|