xmpp.chapril.org-conversejs/src/headless/converse-core.js

1939 lines
73 KiB
JavaScript
Raw Normal View History

// Converse.js
// https://conversejs.org
//
2019-02-18 19:17:06 +01:00
// Copyright (c) 2013-2019, the Converse.js developers
// Licensed under the Mozilla Public License (MPLv2)
import * as bosh from 'strophe.js/src/bosh';
import * as strophe from 'strophe.js/src/core';
import * as websocket from 'strophe.js/src/websocket';
import Backbone from 'backbone';
import BrowserStorage from 'backbone.browserStorage';
import Promise from 'es6-promise/dist/es6-promise.auto';
import _ from './lodash.noconflict';
import advancedFormat from 'dayjs/plugin/advancedFormat';
import dayjs from 'dayjs';
import i18n from './i18n';
import pluggable from 'pluggable.js/src/pluggable';
import polyfill from './polyfill';
import sizzle from 'sizzle';
import u from '@converse/headless/utils/core';
2018-10-23 03:41:38 +02:00
const Strophe = strophe.default.Strophe;
const $build = strophe.default.$build;
const $iq = strophe.default.$iq;
const $msg = strophe.default.$msg;
const $pres = strophe.default.$pres;
2018-11-05 17:58:27 +01:00
Backbone = Backbone.noConflict();
2018-10-23 03:41:38 +02:00
dayjs.extend(advancedFormat);
2019-05-06 11:16:56 +02:00
2018-10-23 03:41:38 +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');
Strophe.addNamespace('FORWARD', 'urn:xmpp:forward:0');
Strophe.addNamespace('HINTS', 'urn:xmpp:hints');
Strophe.addNamespace('HTTPUPLOAD', 'urn:xmpp:http:upload:0');
Strophe.addNamespace('IDLE', 'urn:xmpp:idle:1');
2018-10-23 03:41:38 +02:00
Strophe.addNamespace('MAM', 'urn:xmpp:mam:2');
Strophe.addNamespace('NICK', 'http://jabber.org/protocol/nick');
Strophe.addNamespace('OMEMO', 'eu.siacs.conversations.axolotl');
2018-10-23 03:41:38 +02:00
Strophe.addNamespace('OUTOFBAND', 'jabber:x:oob');
Strophe.addNamespace('PUBSUB', 'http://jabber.org/protocol/pubsub');
Strophe.addNamespace('REGISTER', 'jabber:iq:register');
Strophe.addNamespace('ROSTERX', 'http://jabber.org/protocol/rosterx');
Strophe.addNamespace('RSM', 'http://jabber.org/protocol/rsm');
Strophe.addNamespace('SID', 'urn:xmpp:sid:0');
Strophe.addNamespace('SPOILER', 'urn:xmpp:spoiler:0');
Strophe.addNamespace('STANZAS', 'urn:ietf:params:xml:ns:xmpp-stanzas');
2018-10-23 03:41:38 +02:00
Strophe.addNamespace('VCARD', 'vcard-temp');
Strophe.addNamespace('VCARDUPDATE', 'vcard-temp:x:update');
Strophe.addNamespace('XFORM', 'jabber:x:data');
// Use Mustache style syntax for variable interpolation
/* Configuration of Lodash templates (this config is distinct to the
* config of requirejs-tpl in main.js). This one is for normal inline templates.
*/
_.templateSettings = {
'escape': /\{\{\{([\s\S]+?)\}\}\}/g,
'evaluate': /\{\[([\s\S]+?)\]\}/g,
'interpolate': /\{\{([\s\S]+?)\}\}/g,
'imports': { '_': _ }
};
// Setting wait to 59 instead of 60 to avoid timing conflicts with the
// webserver, which is often also set to 60 and might therefore sometimes
// return a 504 error page instead of passing through to the BOSH proxy.
const BOSH_WAIT = 59;
// Core plugins are whitelisted automatically
// These are just the @converse/headless plugins, for the full converse,
// the other plugins are whitelisted in src/converse.js
const CORE_PLUGINS = [
'converse-bookmarks',
'converse-bosh',
'converse-caps',
'converse-chatboxes',
'converse-disco',
'converse-mam',
'converse-muc',
'converse-ping',
'converse-pubsub',
'converse-roster',
'converse-rsm',
'converse-smacks',
'converse-vcard'
];
2018-10-23 03:41:38 +02:00
/**
2019-03-22 08:15:35 +01:00
* A private, closured object containing the private api (via {@link _converse.api})
2018-10-23 03:41:38 +02:00
* as well as private methods and internal data-structures.
*
* @namespace _converse
*/
const _converse = {
'templates': {},
'promises': {}
2018-10-23 03:41:38 +02:00
}
_converse.VERSION_NAME = 'v4.2.0';
2019-04-29 09:07:15 +02:00
Object.assign(_converse, Backbone.Events);
2018-10-23 03:41:38 +02:00
// Make converse pluggable
pluggable.enable(_converse, '_converse', 'pluggable');
2018-11-09 12:19:45 +01:00
2018-10-23 03:41:38 +02:00
_converse.keycodes = {
TAB: 9,
ENTER: 13,
SHIFT: 16,
CTRL: 17,
ALT: 18,
ESCAPE: 27,
UP_ARROW: 38,
DOWN_ARROW: 40,
FORWARD_SLASH: 47,
AT: 50,
META: 91,
META_RIGHT: 93
};
// Module-level constants
_converse.STATUS_WEIGHTS = {
'offline': 6,
'unavailable': 5,
'xa': 4,
'away': 3,
'dnd': 2,
'chat': 1, // We currently don't differentiate between "chat" and "online"
'online': 1
};
_converse.PRETTY_CHAT_STATUS = {
'offline': 'Offline',
'unavailable': 'Unavailable',
'xa': 'Extended Away',
'away': 'Away',
'dnd': 'Do not disturb',
'chat': 'Chattty',
'online': 'Online'
};
_converse.ANONYMOUS = 'anonymous';
2018-10-23 03:41:38 +02:00
_converse.CLOSED = 'closed';
_converse.EXTERNAL = 'external';
_converse.LOGIN = 'login';
_converse.LOGOUT = 'logout';
2018-10-23 03:41:38 +02:00
_converse.OPENED = 'opened';
_converse.PREBIND = 'prebind';
2018-10-23 03:41:38 +02:00
_converse.IQ_TIMEOUT = 20000;
_converse.CONNECTION_STATUS = {
0: 'ERROR',
1: 'CONNECTING',
2: 'CONNFAIL',
3: 'AUTHENTICATING',
4: 'AUTHFAIL',
5: 'CONNECTED',
6: 'DISCONNECTED',
7: 'DISCONNECTING',
8: 'ATTACHED',
9: 'REDIRECT',
10: 'RECONNECTING'
2018-10-23 03:41:38 +02:00
};
_converse.SUCCESS = 'success';
_converse.FAILURE = 'failure';
// Generated from css/images/user.svg
_converse.DEFAULT_IMAGE_TYPE = 'image/svg+xml';
_converse.DEFAULT_IMAGE = "PD94bWwgdmVyc2lvbj0iMS4wIj8+CjxzdmcgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIiB3aWR0aD0iMTI4IiBoZWlnaHQ9IjEyOCI+CiA8cmVjdCB3aWR0aD0iMTI4IiBoZWlnaHQ9IjEyOCIgZmlsbD0iIzU1NSIvPgogPGNpcmNsZSBjeD0iNjQiIGN5PSI0MSIgcj0iMjQiIGZpbGw9IiNmZmYiLz4KIDxwYXRoIGQ9Im0yOC41IDExMiB2LTEyIGMwLTEyIDEwLTI0IDI0LTI0IGgyMyBjMTQgMCAyNCAxMiAyNCAyNCB2MTIiIGZpbGw9IiNmZmYiLz4KPC9zdmc+Cg==";
2018-10-23 03:41:38 +02:00
_converse.TIMEOUTS = {
// Set as module attr so that we can override in tests.
PAUSED: 10000,
INACTIVE: 90000
2018-10-23 03:41:38 +02:00
};
// XEP-0085 Chat states
// https://xmpp.org/extensions/xep-0085.html
2018-10-23 03:41:38 +02:00
_converse.INACTIVE = 'inactive';
_converse.ACTIVE = 'active';
_converse.COMPOSING = 'composing';
_converse.PAUSED = 'paused';
_converse.GONE = 'gone';
// Chat types
_converse.PRIVATE_CHAT_TYPE = 'chatbox';
_converse.CHATROOMS_TYPE = 'chatroom';
_converse.HEADLINES_TYPE = 'headline';
_converse.CONTROLBOX_TYPE = 'controlbox';
_converse.default_connection_options = {'explicitResourceBinding': true};
2018-10-23 03:41:38 +02:00
// Default configuration values
// ----------------------------
_converse.default_settings = {
allow_non_roster_messaging: false,
authentication: 'login', // Available values are "login", "prebind", "anonymous" and "external".
auto_away: 0, // Seconds after which user status is set to 'away'
auto_login: false, // Currently only used in connection with anonymous login
auto_reconnect: true,
auto_xa: 0, // Seconds after which user status is set to 'xa'
blacklisted_plugins: [],
connection_options: {},
credentials_url: null, // URL from where login credentials can be fetched
csi_waiting_time: 0, // Support for XEP-0352. Seconds before client is considered idle and CSI is sent out.
debug: false,
default_state: 'online',
geouri_regex: /https:\/\/www.openstreetmap.org\/.*#map=[0-9]+\/([\-0-9.]+)\/([\-0-9.]+)\S*/g,
geouri_replacement: 'https://www.openstreetmap.org/?mlat=$1&mlon=$2#map=18/$1/$2',
idle_presence_timeout: 300, // Seconds after which an idle presence is sent
2018-10-23 03:41:38 +02:00
jid: undefined,
locales_url: 'locale/{{{locale}}}/LC_MESSAGES/converse.json',
locales: [
2019-01-29 05:12:41 +01:00
'af', 'ar', 'bg', 'ca', 'cs', 'de', 'eo', 'es', 'eu', 'en', 'fr', 'gl',
2018-11-15 12:23:41 +01:00
'he', 'hi', 'hu', 'id', 'it', 'ja', 'nb', 'nl',
2018-10-23 03:41:38 +02:00
'pl', 'pt_BR', 'ro', 'ru', 'tr', 'uk', 'zh_CN', 'zh_TW'
],
message_carbons: true,
nickname: undefined,
password: undefined,
priority: 0,
rid: undefined,
root: window.document,
sid: undefined,
singleton: false,
2018-10-23 03:41:38 +02:00
strict_plugin_dependencies: false,
trusted: true,
view_mode: 'overlayed', // Choices are 'overlayed', 'fullscreen', 'mobile'
websocket_url: undefined,
whitelisted_plugins: []
};
/**
* Logs messages to the browser's developer console.
* Available loglevels are 0 for 'debug', 1 for 'info', 2 for 'warn',
* 3 for 'error' and 4 for 'fatal'.
* When using the 'error' or 'warn' loglevels, a full stacktrace will be
* logged as well.
* @method log
* @private
* @memberOf _converse
* @param { string } message - The message to be logged
* @param { integer } level - The loglevel which allows for filtering of log messages
*/
2018-10-23 03:41:38 +02:00
_converse.log = function (message, level, style='') {
if (level === Strophe.LogLevel.ERROR || level === Strophe.LogLevel.FATAL) {
style = style || 'color: maroon';
}
if (message instanceof Error) {
message = message.stack;
} else if (_.isElement(message)) {
message = message.outerHTML;
}
const prefix = style ? '%c' : '';
2019-05-24 13:52:15 +02:00
const logger = Object.assign({
2018-10-23 03:41:38 +02:00
'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
}, console);
if (level === Strophe.LogLevel.ERROR) {
logger.error(`${prefix} ERROR: ${message}`, style);
} else if (level === Strophe.LogLevel.WARN) {
if (_converse.debug) {
logger.warn(`${prefix} ${(new Date()).toISOString()} WARNING: ${message}`, style);
2018-10-23 03:41:38 +02:00
}
} else if (level === Strophe.LogLevel.FATAL) {
logger.error(`${prefix} FATAL: ${message}`, style);
} else if (_converse.debug) {
if (level === Strophe.LogLevel.DEBUG) {
logger.debug(`${prefix} ${(new Date()).toISOString()} DEBUG: ${message}`, style);
2018-10-23 03:41:38 +02:00
} else {
logger.info(`${prefix} ${(new Date()).toISOString()} INFO: ${message}`, style);
2018-10-23 03:41:38 +02:00
}
2017-07-10 21:14:48 +02:00
}
2018-10-23 03:41:38 +02:00
};
2017-07-10 21:14:48 +02:00
2018-10-23 03:41:38 +02:00
Strophe.log = function (level, msg) { _converse.log(level+' '+msg, level); };
Strophe.error = function (msg) { _converse.log(msg, Strophe.LogLevel.ERROR); };
/**
* Translate the given string based on the current locale.
* Handles all MUC presence stanzas.
* @method __
* @private
* @memberOf _converse
* @param { String } str - The string to translate
*/
2018-10-23 03:41:38 +02:00
_converse.__ = function (str) {
if (_.isUndefined(i18n)) {
return str;
}
return i18n.translate.apply(i18n, arguments);
};
2018-10-23 03:41:38 +02:00
const __ = _converse.__;
2018-10-23 03:41:38 +02:00
const PROMISES = [
'afterResourceBinding',
2018-10-23 03:41:38 +02:00
'connectionInitialized',
'initialized',
2018-10-23 03:41:38 +02:00
'pluginsInitialized',
'statusInitialized'
];
2018-10-23 03:41:38 +02:00
function addPromise (promise) {
/* Private function, used to add a new promise to the ones already
* available via the `waitUntil` api method.
*/
_converse.promises[promise] = u.getResolveablePromise();
}
_converse.isTestEnv = function () {
2019-04-22 14:04:21 +02:00
return _.get(_converse.connection, 'service') === 'jasmine tests';
}
_converse.haveResumed = function () {
if (_converse.api.connection.isType('bosh')) {
return _converse.connfeedback.get('connection_status') === Strophe.Status.ATTACHED;
} else {
// XXX: Not binding means that the session was resumed.
// This seems very fragile. Perhaps a better way is possible.
return !_converse.connection.do_bind;
}
}
_converse.isUniView = function () {
/* We distinguish between UniView and MultiView instances.
*
* UniView means that only one chat is visible, even though there might be multiple ongoing chats.
* MultiView means that multiple chats may be visible simultaneously.
*/
2018-10-23 03:41:38 +02:00
return _.includes(['mobile', 'fullscreen', 'embedded'], _converse.view_mode);
};
2018-10-23 03:41:38 +02:00
_converse.router = new Backbone.Router();
function initPlugins () {
// 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 = [];
const whitelist = CORE_PLUGINS.concat(_converse.whitelisted_plugins);
if (_converse.singleton) {
[
'converse-bookmarks',
'converse-controlbox',
'converse-headline',
'converse-register'
].forEach(name => _converse.blacklisted_plugins.push(name));
}
_converse.pluggable.initializePlugins(
{ '_converse': _converse },
whitelist,
_converse.blacklisted_plugins
);
2019-03-22 08:15:35 +01:00
/**
2019-03-29 15:47:23 +01:00
* Triggered once all plugins have been initialized. This is a useful event if you want to
2019-03-22 08:15:35 +01:00
* register event handlers but would like your own handlers to be overridable by
* plugins. In that case, you need to first wait until all plugins have been
* initialized, so that their overrides are active. One example where this is used
* is in [converse-notifications.js](https://github.com/jcbrand/converse.js/blob/master/src/converse-notification.js)`.
*
* Also available as an [ES2015 Promise](http://es6-features.org/#PromiseUsage)
* which can be listened to with `_converse.api.waitUntil`.
*
* @event _converse#pluginsInitialized
* @memberOf _converse
2019-03-29 15:47:23 +01:00
* @example _converse.api.listen.on('pluginsInitialized', () => { ... });
* @example _converse.api.waitUntil('pluginsInitialized').then(() => { ... });
2019-03-22 08:15:35 +01:00
*/
_converse.api.trigger('pluginsInitialized');
}
function initClientConfig () {
/* The client config refers to configuration of the client which is
* independent of any particular user.
* What this means is that config values need to persist across
* user sessions.
*/
const id = 'converse.client-config';
_converse.config = new Backbone.Model({
'id': id,
'trusted': _converse.trusted && true || false,
'storage': _converse.trusted ? 'local' : 'session'
});
2019-05-23 14:26:20 +02:00
_converse.config.browserStorage = new BrowserStorage.session(id);
_converse.config.fetch();
2019-03-22 08:15:35 +01:00
/**
2019-03-29 15:47:23 +01:00
* Triggered once the XMPP-client configuration has been initialized.
2019-03-22 08:15:35 +01:00
* The client configuration is independent of any particular and its values
* persist across user sessions.
*
* @event _converse#clientConfigInitialized
* @example
* _converse.api.listen.on('clientConfigInitialized', () => { ... });
*/
_converse.api.trigger('clientConfigInitialized');
}
2019-06-01 15:37:18 +02:00
function tearDown () {
_converse.api.trigger('beforeTearDown');
window.removeEventListener('click', _converse.onUserActivity);
window.removeEventListener('focus', _converse.onUserActivity);
window.removeEventListener('keypress', _converse.onUserActivity);
window.removeEventListener('mousemove', _converse.onUserActivity);
window.removeEventListener(_converse.unloadevent, _converse.onUserActivity);
window.clearInterval(_converse.everySecondTrigger);
_converse.api.trigger('afterTearDown');
return _converse;
2019-06-01 15:37:18 +02:00
}
2019-06-01 19:36:23 +02:00
function reconnect () {
_converse.log('RECONNECTING: the connection has dropped, attempting to reconnect.');
_converse.setConnectionStatus(
Strophe.Status.RECONNECTING,
__('The connection has dropped, attempting to reconnect.')
);
/**
* Triggered when the connection has dropped, but Converse will attempt
* to reconnect again.
*
* @event _converse#will-reconnect
*/
_converse.api.trigger('will-reconnect');
_converse.connection.reconnecting = true;
tearDown();
_converse.api.user.login(null, null, true);
}
const debouncedReconnect = _.debounce(reconnect, 2000);
function clearSession () {
if (!_.isUndefined(_converse.session)) {
_converse.session.destroy();
delete _converse.session;
}
// TODO: Refactor so that we don't clear
if (!_converse.config.get('trusted') || _converse.isTestEnv()) {
window.localStorage.clear();
window.sessionStorage.clear();
}
/**
* Triggered once the session information has been cleared,
* for example when the user has logged out or when Converse has
* disconnected for some other reason.
* @event _converse#clearSession
*/
_converse.api.trigger('clearSession');
}
_converse.initConnection = function () {
/* Creates a new Strophe.Connection instance if we don't already have one.
*/
if (!_converse.connection) {
if (!_converse.bosh_service_url && ! _converse.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) && _converse.websocket_url) {
_converse.connection = new Strophe.Connection(
_converse.websocket_url,
Object.assign(_converse.default_connection_options, _converse.connection_options)
);
} else if (_converse.bosh_service_url) {
_converse.connection = new Strophe.Connection(
_converse.bosh_service_url,
Object.assign(
_converse.default_connection_options,
_converse.connection_options,
{'keepalive': true}
)
);
} else {
throw new Error("initConnection: this browser does not support websockets and bosh_service_url wasn't specified.");
}
}
setUpXMLLogging();
2019-03-22 08:15:35 +01:00
/**
2019-03-29 15:47:23 +01:00
* Triggered once the `Strophe.Connection` constructor has been initialized, which
2019-03-22 08:15:35 +01:00
* will be responsible for managing the connection to the XMPP server.
*
* @event _converse#connectionInitialized
*/
_converse.api.trigger('connectionInitialized');
};
async function initUserSession (jid) {
const bare_jid = Strophe.getBareJidFromJid(jid).toLowerCase();
const id = `converse.session-${bare_jid}`;
if (!_converse.session || _converse.session.get('id') !== id) {
_converse.session = new Backbone.Model({id});
_converse.session.browserStorage = new BrowserStorage.session(id);
await new Promise(r => _converse.session.fetch({'success': r, 'error': r}));
if (_converse.session.get('active')) {
_converse.session.clear();
}
/**
* Triggered once the user's session has been initialized. The session is a
* cache which stores information about the user's current session.
* @event _converse#userSessionInitialized
* @memberOf _converse
*/
_converse.api.trigger('userSessionInitialized');
}
}
2019-06-05 11:27:59 +02:00
async function setUserJID (jid) {
await initUserSession(jid);
jid = _converse.session.get('jid') || jid;
if (!Strophe.getResourceFromJid(jid)) {
jid = jid.toLowerCase() + _converse.generateResource();
}
// Set JID on the connection object so that when we call
// `connection.bind` the new resource is found by Strophe.js
// and sent to the XMPP server.
_converse.connection.jid = jid;
_converse.jid = jid;
_converse.bare_jid = Strophe.getBareJidFromJid(jid);
_converse.resource = Strophe.getResourceFromJid(jid);
_converse.domain = Strophe.getDomainFromJid(jid);
_converse.session.save({
'jid': jid,
'bare_jid': _converse.bare_jid,
'resource': _converse.resource,
'domain': _converse.domain,
'active': true
});
/**
* Triggered whenever the user's JID has been updated
* @event _converse#setUserJID
*/
_converse.api.trigger('setUserJID');
return jid;
}
async function onConnected (reconnecting) {
/* Called as soon as a new connection has been established, either
* by logging in or by attaching to an existing BOSH session.
*/
_converse.connection.flush(); // Solves problem of returned PubSub BOSH response not received by browser
2019-06-05 11:27:59 +02:00
await setUserJID(_converse.connection.jid);
/**
* Synchronous event triggered after we've sent an IQ to bind the
* user's JID resource for this session.
* @event _converse#afterResourceBinding
*/
await _converse.api.trigger('afterResourceBinding', {'synchronous': true});
_converse.enableCarbons();
_converse.initStatus(reconnecting)
}
function setUpXMLLogging () {
Strophe.log = function (level, msg) {
_converse.log(msg, level);
};
_converse.connection.xmlInput = function (body) {
if (_converse.debug) {
_converse.log(body.outerHTML, Strophe.LogLevel.DEBUG, 'color: darkgoldenrod');
}
};
_converse.connection.xmlOutput = function (body) {
if (_converse.debug) {
_converse.log(body.outerHTML, Strophe.LogLevel.DEBUG, 'color: darkcyan');
}
};
}
function finishInitialization () {
initClientConfig();
2019-02-21 09:19:02 +01:00
initPlugins();
_converse.initConnection();
_converse.api.user.login();
_converse.registerGlobalEventHandlers();
if (!Backbone.history.started) {
Backbone.history.start();
}
if (_converse.idle_presence_timeout > 0) {
2019-03-29 14:15:03 +01:00
_converse.api.listen.on('addClientFeatures', () => {
_converse.api.disco.own.features.add(Strophe.NS.IDLE);
});
}
}
function fetchLoginCredentials (wait=0) {
return new Promise(
_.debounce((resolve, reject) => {
const xhr = new XMLHttpRequest();
xhr.open('GET', _converse.credentials_url, true);
xhr.setRequestHeader('Accept', 'application/json, text/javascript');
xhr.onload = () => {
if (xhr.status >= 200 && xhr.status < 400) {
const data = JSON.parse(xhr.responseText);
setUserJID(data.jid);
resolve({
jid: data.jid,
password: data.password
});
} else {
reject(new Error(`${xhr.status}: ${xhr.responseText}`));
}
};
xhr.onerror = reject;
xhr.send();
}, wait)
);
}
async function getLoginCredentials () {
let credentials;
let wait = 0;
while (!credentials) {
try {
credentials = await fetchLoginCredentials(wait); // eslint-disable-line no-await-in-loop
} catch (e) {
_converse.log('Could not fetch login credentials', Strophe.LogLevel.ERROR);
_converse.log(e, Strophe.LogLevel.ERROR);
}
// If unsuccessful, we wait 2 seconds between subsequent attempts to
// fetch the credentials.
wait = 2000;
}
return credentials;
2019-04-23 10:50:48 +02:00
}
function unregisterGlobalEventHandlers () {
document.removeEventListener("visibilitychange", _converse.saveWindowState);
_converse.api.trigger('unregisteredGlobalEventHandlers');
}
function cleanup () {
// Looks like _converse.initialized was called again without logging
// out or disconnecting in the previous session.
// This happens in tests. We therefore first clean up.
Backbone.history.stop();
unregisterGlobalEventHandlers();
delete _converse.controlboxtoggle;
if (_converse.chatboxviews) {
delete _converse.chatboxviews;
}
_converse.stopListening();
_converse.off();
}
_converse.initialize = async function (settings, callback) {
2018-10-23 03:41:38 +02:00
settings = !_.isUndefined(settings) ? settings : {};
const init_promise = u.getResolveablePromise();
PROMISES.forEach(addPromise);
if (!_.isUndefined(_converse.connection)) {
cleanup();
}
2018-10-23 03:41:38 +02: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/
_converse.unloadevent = 'pagehide';
} else if ('onbeforeunload' in window) {
_converse.unloadevent = 'beforeunload';
} else if ('onunload' in window) {
_converse.unloadevent = 'unload';
}
2018-10-23 03:41:38 +02:00
_.assignIn(this, this.default_settings);
// Allow only whitelisted configuration attributes to be overwritten
2019-04-29 09:29:40 +02:00
_.assignIn(this, _.pick(settings, Object.keys(this.default_settings)));
2018-10-23 03:41:38 +02:00
if (this.authentication === _converse.ANONYMOUS) {
if (this.auto_login && !this.jid) {
throw new Error("Config Error: you need to provide the server's " +
"domain via the 'jid' option when using anonymous " +
"authentication with auto_login.");
}
2018-10-23 03:41:38 +02:00
}
_converse.router.route(/^converse\?debug=(true|false)$/, 'debug', debug => {
if (debug === 'true') {
_converse.debug = true;
} else {
_converse.debug = false;
}
});
2018-10-23 03:41:38 +02:00
/* Localisation */
if (!_.isUndefined(i18n)) {
i18n.setLocales(settings.i18n, _converse);
} else {
_converse.locale = 'en';
}
2018-10-23 03:41:38 +02:00
// Module-level variables
// ----------------------
this.callback = callback || _.noop;
/* When reloading the page:
* For new sessions, we need to send out a presence stanza to notify
* the server/network that we're online.
* When re-attaching to an existing session we don't need to again send out a presence stanza,
* because it's as if "we never left" (see onConnectStatusChanged).
2018-10-23 03:41:38 +02:00
* https://github.com/jcbrand/converse.js/issues/521
*/
this.send_initial_presence = true;
this.msg_counter = 0;
this.user_settings = settings; // Save the user settings so that they can be used by plugins
2018-10-23 03:41:38 +02:00
// Module-level functions
// ----------------------
2018-10-23 03:41:38 +02:00
this.generateResource = () => `/converse.js-${Math.floor(Math.random()*139749528).toString()}`;
/**
* Send out a Client State Indication (XEP-0352)
* @private
* @method sendCSI
* @memberOf _converse
* @param { String } stat - The user's chat status
*/
2018-10-23 03:41:38 +02:00
this.sendCSI = function (stat) {
_converse.api.send($build(stat, {xmlns: Strophe.NS.CSI}));
2018-10-23 03:41:38 +02:00
_converse.inactive = (stat === _converse.INACTIVE) ? true : false;
};
2018-10-23 03:41:38 +02:00
this.onUserActivity = function () {
/* Resets counters and flags relating to CSI and auto_away/auto_xa */
if (_converse.idle_seconds > 0) {
_converse.idle_seconds = 0;
}
2018-10-23 03:41:38 +02:00
if (!_converse.connection.authenticated) {
// We can't send out any stanzas when there's no authenticated connection.
// This can happen when the connection reconnects.
2018-10-23 03:41:38 +02:00
return;
}
2018-10-23 03:41:38 +02:00
if (_converse.inactive) {
_converse.sendCSI(_converse.ACTIVE);
}
if (_converse.idle) {
_converse.idle = false;
_converse.xmppstatus.sendPresence();
}
2018-10-23 03:41:38 +02:00
if (_converse.auto_changed_status === true) {
_converse.auto_changed_status = false;
// XXX: we should really remember the original state here, and
// then set it back to that...
_converse.xmppstatus.set('status', _converse.default_state);
}
2018-10-23 03:41:38 +02:00
};
2018-10-23 03:41:38 +02:00
this.onEverySecond = function () {
/* An interval handler running every second.
* Used for CSI and the auto_away and auto_xa features.
*/
2018-10-23 03:41:38 +02:00
if (!_converse.connection.authenticated) {
// We can't send out any stanzas when there's no authenticated connection.
// This can happen when the connection reconnects.
return;
}
const stat = _converse.xmppstatus.get('status');
_converse.idle_seconds++;
if (_converse.csi_waiting_time > 0 &&
_converse.idle_seconds > _converse.csi_waiting_time &&
!_converse.inactive) {
_converse.sendCSI(_converse.INACTIVE);
}
if (_converse.idle_presence_timeout > 0 &&
_converse.idle_seconds > _converse.idle_presence_timeout &&
!_converse.idle) {
_converse.idle = true;
_converse.xmppstatus.sendPresence();
}
2018-10-23 03:41:38 +02:00
if (_converse.auto_away > 0 &&
_converse.idle_seconds > _converse.auto_away &&
stat !== 'away' && stat !== 'xa' && stat !== 'dnd') {
_converse.auto_changed_status = true;
_converse.xmppstatus.set('status', 'away');
} else if (_converse.auto_xa > 0 &&
_converse.idle_seconds > _converse.auto_xa &&
stat !== 'xa' && stat !== 'dnd') {
_converse.auto_changed_status = true;
_converse.xmppstatus.set('status', 'xa');
}
};
2018-10-23 03:41:38 +02: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.
*/
if (
_converse.auto_away < 1 &&
_converse.auto_xa < 1 &&
_converse.csi_waiting_time < 1 &&
_converse.idle_presence_timeout < 1
) {
2018-10-23 03:41:38 +02:00
// Waiting time of less then one second means features aren't used.
return;
}
_converse.idle_seconds = 0;
_converse.auto_changed_status = false; // Was the user's status changed by Converse?
2018-10-23 03:41:38 +02:00
window.addEventListener('click', _converse.onUserActivity);
window.addEventListener('focus', _converse.onUserActivity);
window.addEventListener('keypress', _converse.onUserActivity);
window.addEventListener('mousemove', _converse.onUserActivity);
const options = {'once': true, 'passive': true};
window.addEventListener(_converse.unloadevent, _converse.onUserActivity, options);
window.addEventListener(_converse.unloadevent, () => {
if (_converse.session) {
_converse.session.save('active', false);
}
});
2018-10-23 03:41:38 +02:00
_converse.everySecondTrigger = window.setInterval(_converse.onEverySecond, 1000);
};
2018-10-23 03:41:38 +02:00
this.setConnectionStatus = function (connection_status, message) {
_converse.connfeedback.set({
'connection_status': connection_status,
'message': message
});
};
/**
* Reject or cancel another user's subscription to our presence updates.
* @method rejectPresenceSubscription
* @private
* @memberOf _converse
* @param { String } jid - The Jabber ID of the user whose subscription is being canceled
* @param { String } message - An optional message to the user
*/
2018-10-23 03:41:38 +02:00
this.rejectPresenceSubscription = function (jid, message) {
const pres = $pres({to: jid, type: "unsubscribed"});
if (message && message !== "") { pres.c("status").t(message); }
_converse.api.send(pres);
2018-10-23 03:41:38 +02:00
};
/**
* Properly tear down the session so that it's possible to manually connect again.
* @method finishDisconnection
* @private
* @memberOf _converse
*/
2019-04-22 14:04:21 +02:00
this.finishDisconnection = function () {
2018-10-23 03:41:38 +02:00
_converse.log('DISCONNECTED');
delete _converse.connection.reconnecting;
_converse.connection.reset();
2019-06-01 15:37:18 +02:00
tearDown();
clearSession();
2019-03-22 08:15:35 +01:00
/**
2019-03-29 15:47:23 +01:00
* Triggered after converse.js has disconnected from the XMPP server.
2019-03-22 08:15:35 +01:00
* @event _converse#disconnected
* @memberOf _converse
2019-03-29 15:47:23 +01:00
* @example _converse.api.listen.on('disconnected', () => { ... });
2019-03-22 08:15:35 +01:00
*/
_converse.api.trigger('disconnected');
2018-10-23 03:41:38 +02:00
};
/**
* Gets called once strophe's status reaches Strophe.Status.DISCONNECTED.
* Will either start a teardown process for converse.js or attempt
* to reconnect.
* @method onDisconnected
* @private
* @memberOf _converse
*/
2018-10-23 03:41:38 +02:00
this.onDisconnected = function () {
const reason = _converse.disconnection_reason;
if (_converse.disconnection_cause === Strophe.Status.AUTHFAIL) {
if (_converse.credentials_url && _converse.auto_reconnect) {
/* In this case, we reconnect, because we might be receiving
* expirable tokens from the credentials_url.
*/
2019-06-01 19:36:23 +02:00
return _converse.api.connection.reconnect();
2018-10-23 03:41:38 +02:00
} else {
2019-04-22 14:04:21 +02:00
return _converse.finishDisconnection();
}
2018-10-23 03:41:38 +02:00
} else if (_converse.disconnection_cause === _converse.LOGOUT ||
(!_.isUndefined(reason) && reason === _.get(Strophe, 'ErrorCondition.NO_AUTH_MECH')) ||
reason === "host-unknown" ||
reason === "remote-connection-failed" ||
!_converse.auto_reconnect) {
2019-04-22 14:04:21 +02:00
return _converse.finishDisconnection();
2018-10-23 03:41:38 +02:00
}
2019-06-01 19:36:23 +02:00
_converse.api.connection.reconnect();
2018-10-23 03:41:38 +02:00
};
2018-10-23 03:41:38 +02:00
this.setDisconnectionCause = function (cause, reason, override) {
/* Used to keep track of why we got disconnected, so that we can
* decide on what the next appropriate action is (in onDisconnected)
*/
if (_.isUndefined(cause)) {
delete _converse.disconnection_cause;
delete _converse.disconnection_reason;
} else if (_.isUndefined(_converse.disconnection_cause) || override) {
_converse.disconnection_cause = cause;
_converse.disconnection_reason = reason;
}
};
2018-10-23 03:41:38 +02:00
this.onConnectStatusChanged = function (status, message) {
/* Callback method called by Strophe as the Strophe.Connection goes
* through various states while establishing or tearing down a
* connection.
*/
_converse.log(`Status changed to: ${_converse.CONNECTION_STATUS[status]}`);
if (status === Strophe.Status.CONNECTED || status === Strophe.Status.ATTACHED) {
_converse.setConnectionStatus(status);
// By default we always want to send out an initial presence stanza.
_converse.send_initial_presence = true;
_converse.setDisconnectionCause();
if (_converse.connection.reconnecting) {
_converse.log(status === Strophe.Status.CONNECTED ? 'Reconnected' : 'Reattached');
onConnected(true);
2018-10-23 03:41:38 +02:00
} else {
_converse.log(status === Strophe.Status.CONNECTED ? 'Connected' : 'Attached');
if (_converse.connection.restored) {
// No need to send an initial presence stanza when
// we're restoring an existing session.
_converse.send_initial_presence = false;
}
onConnected();
}
2018-10-23 03:41:38 +02:00
} else if (status === Strophe.Status.DISCONNECTED) {
_converse.setDisconnectionCause(status, message);
_converse.onDisconnected();
} else if (status === Strophe.Status.BINDREQUIRED) {
_converse.bindResource();
2018-10-23 03:41:38 +02:00
} else if (status === Strophe.Status.ERROR) {
_converse.setConnectionStatus(
status,
__('An error occurred while connecting to the chat server.')
);
} else if (status === Strophe.Status.CONNECTING) {
_converse.setConnectionStatus(status);
} else if (status === Strophe.Status.AUTHENTICATING) {
_converse.setConnectionStatus(status);
} else if (status === Strophe.Status.AUTHFAIL) {
if (!message) {
message = __('Your Jabber ID and/or password is incorrect. Please try again.');
}
2018-10-23 03:41:38 +02:00
_converse.setConnectionStatus(status, message);
_converse.setDisconnectionCause(status, message, true);
_converse.onDisconnected();
} else if (status === Strophe.Status.CONNFAIL) {
let feedback = message;
if (message === "host-unknown" || message == "remote-connection-failed") {
feedback = __("Sorry, we could not connect to the XMPP host with domain: %1$s",
`\"${Strophe.getDomainFromJid(_converse.connection.jid)}\"`);
} else if (!_.isUndefined(message) && message === _.get(Strophe, 'ErrorCondition.NO_AUTH_MECH')) {
feedback = __("The XMPP server did not offer a supported authentication mechanism");
}
2018-10-23 03:41:38 +02:00
_converse.setConnectionStatus(status, feedback);
_converse.setDisconnectionCause(status, message);
} else if (status === Strophe.Status.DISCONNECTING) {
_converse.setDisconnectionCause(status, message);
}
};
2018-10-23 03:41:38 +02:00
this.incrementMsgCounter = function () {
this.msg_counter += 1;
const unreadMsgCount = this.msg_counter;
let title = document.title;
if (_.isNil(title)) {
return;
}
if (title.search(/^Messages \(\d+\) /) === -1) {
title = `Messages (${unreadMsgCount}) ${title}`;
} else {
title = title.replace(/^Messages \(\d+\) /, `Messages (${unreadMsgCount})`);
}
};
2018-10-23 03:41:38 +02:00
this.clearMsgCounter = function () {
this.msg_counter = 0;
let title = document.title;
if (_.isNil(title)) {
return;
}
2018-10-23 03:41:38 +02:00
if (title.search(/^Messages \(\d+\) /) !== -1) {
title = title.replace(/^Messages \(\d+\) /, "");
}
};
2018-10-23 03:41:38 +02: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 {
const id = `converse.xmppstatus-${_converse.bare_jid}`;
_converse.xmppstatus = new this.XMPPStatus({'id': id});
2019-05-23 14:26:20 +02:00
_converse.xmppstatus.browserStorage = new BrowserStorage.session(id);
_converse.xmppstatus.fetch({
2018-10-23 03:41:38 +02:00
'success': _.partial(_converse.onStatusInitialized, reconnecting),
'error': _.partial(_converse.onStatusInitialized, reconnecting),
'silent': true
});
2018-10-23 03:41:38 +02:00
}
}
this.saveWindowState = function (ev) {
2018-10-23 03:41:38 +02:00
// XXX: eventually we should be able to just use
// document.visibilityState (when we drop support for older
// browsers).
let state;
const event_map = {
'focus': "visible",
'focusin': "visible",
'pageshow': "visible",
'blur': "hidden",
'focusout': "hidden",
'pagehide': "hidden"
};
2018-10-23 03:41:38 +02:00
ev = ev || document.createEvent('Events');
if (ev.type in event_map) {
state = event_map[ev.type];
} else {
state = document.hidden ? "hidden" : "visible";
2018-10-23 03:41:38 +02:00
}
if (state === 'visible') {
_converse.clearMsgCounter();
}
_converse.windowState = state;
2019-03-29 15:47:23 +01:00
/**
* Triggered when window state has changed.
* Used to determine when a user left the page and when came back.
* @event _converse#windowStateChanged
* @type { object }
* @property{ string } state - Either "hidden" or "visible"
* @example _converse.api.listen.on('windowStateChanged', obj => { ... });
*/
_converse.api.trigger('windowStateChanged', {state});
2018-10-23 03:41:38 +02:00
};
2018-10-23 03:41:38 +02:00
this.registerGlobalEventHandlers = function () {
document.addEventListener("visibilitychange", _converse.saveWindowState);
_converse.saveWindowState({'type': document.hidden ? "blur" : "focus"}); // Set initial state
2019-03-29 15:47:23 +01:00
/**
* Called once Converse has registered its global event handlers
* (for events such as window resize or unload).
* Plugins can listen to this event as cue to register their own
* global event handlers.
* @event _converse#registeredGlobalEventHandlers
* @example _converse.api.listen.on('registeredGlobalEventHandlers', () => { ... });
*/
_converse.api.trigger('registeredGlobalEventHandlers');
2018-10-23 03:41:38 +02:00
};
2018-10-23 03:41:38 +02:00
this.enableCarbons = function () {
/* Ask the XMPP server to enable Message Carbons
* See XEP-0280 https://xmpp.org/extensions/xep-0280.html#enabling
*/
2019-06-12 06:40:53 +02:00
if (!this.message_carbons || !this.session || this.session.get('carbons_enabled')) {
2018-10-23 03:41:38 +02:00
return;
}
const carbons_iq = new Strophe.Builder('iq', {
'from': this.connection.jid,
'id': 'enablecarbons',
'type': 'set'
})
.c('enable', {xmlns: Strophe.NS.CARBONS});
this.connection.addHandler((iq) => {
if (iq.querySelectorAll('error').length > 0) {
_converse.log(
'An error occurred while trying to enable message carbons.',
Strophe.LogLevel.WARN);
} else {
2018-10-23 03:41:38 +02:00
this.session.save({'carbons_enabled': true});
_converse.log('Message carbons have been enabled.');
}
2018-10-23 03:41:38 +02:00
}, null, "iq", null, "enablecarbons");
this.connection.send(carbons_iq);
};
2018-10-23 03:41:38 +02:00
this.sendInitialPresence = function () {
if (_converse.send_initial_presence) {
_converse.xmppstatus.sendPresence();
}
};
2018-10-23 03:41:38 +02:00
this.onStatusInitialized = function (reconnecting) {
/**
* Triggered when the user's own chat status has been initialized.
* @event _converse#statusInitialized
* @example _converse.api.listen.on('statusInitialized', status => { ... });
* @example _converse.api.waitUntil('statusInitialized').then(() => { ... });
*/
_converse.api.trigger('statusInitialized', reconnecting);
2018-10-23 03:41:38 +02:00
if (reconnecting) {
2019-03-29 15:47:23 +01:00
/**
* After the connection has dropped and converse.js has reconnected.
* Any Strophe stanza handlers (as registered via `converse.listen.stanza`) will
* have to be registered anew.
* @event _converse#reconnected
* @example _converse.api.listen.on('reconnected', () => { ... });
*/
_converse.api.trigger('reconnected');
2018-10-23 03:41:38 +02:00
} else {
init_promise.resolve();
2019-03-22 08:15:35 +01:00
/**
2019-03-29 15:47:23 +01:00
* Triggered once converse.js has been initialized.
2019-03-22 08:15:35 +01:00
* See also {@link _converse#event:pluginsInitialized}.
* @event _converse#initialized
*/
_converse.api.trigger('initialized');
2019-03-22 08:15:35 +01:00
/**
2019-03-29 15:47:23 +01:00
* Triggered after the connection has been established and Converse
2019-03-22 08:15:35 +01:00
* has got all its ducks in a row.
* @event _converse#initialized
*/
_converse.api.trigger('connected');
2018-10-23 03:41:38 +02:00
}
};
this.bindResource = async function () {
/**
* Synchronous event triggered before we send an IQ to bind the user's
* JID resource for this session.
* @event _converse#beforeResourceBinding
*/
await _converse.api.trigger('beforeResourceBinding', {'synchronous': true});
_converse.connection.bind();
};
2018-10-23 03:41:38 +02:00
this.ConnectionFeedback = Backbone.Model.extend({
defaults: {
'connection_status': Strophe.Status.DISCONNECTED,
'message': ''
},
2018-10-23 03:41:38 +02:00
initialize () {
this.on('change', () => _converse.api.trigger('connfeedback', _converse.connfeedback));
2018-10-23 03:41:38 +02:00
}
});
this.connfeedback = new this.ConnectionFeedback();
2018-10-23 03:41:38 +02:00
this.XMPPStatus = Backbone.Model.extend({
defaults: {
"status": _converse.default_state
2018-10-23 03:41:38 +02:00
},
2018-10-23 03:41:38 +02:00
initialize () {
this.on('change', item => {
if (!_.isObject(item.changed)) {
return;
}
if ('status' in item.changed || 'status_message' in item.changed) {
this.sendPresence(this.get('status'), this.get('status_message'));
}
2018-10-23 03:41:38 +02:00
});
},
constructPresence (type, status_message) {
let presence;
type = _.isString(type) ? type : (this.get('status') || _converse.default_state);
status_message = _.isString(status_message) ? status_message : this.get('status_message');
// Most of these presence types are actually not explicitly sent,
// but I add all of them here for reference and future proofing.
if ((type === 'unavailable') ||
(type === 'probe') ||
(type === 'error') ||
(type === 'unsubscribe') ||
(type === 'unsubscribed') ||
(type === 'subscribe') ||
(type === 'subscribed')) {
presence = $pres({'type': type});
} else if (type === 'offline') {
presence = $pres({'type': 'unavailable'});
} else if (type === 'online') {
presence = $pres();
} else {
presence = $pres().c('show').t(type).up();
}
if (status_message) {
presence.c('status').t(status_message).up();
}
presence.c('priority').t(
_.isNaN(Number(_converse.priority)) ? 0 : _converse.priority
).up();
if (_converse.idle) {
const idle_since = new Date();
idle_since.setSeconds(idle_since.getSeconds() - _converse.idle_seconds);
presence.c('idle', {xmlns: Strophe.NS.IDLE, since: idle_since.toISOString()});
}
2018-10-23 03:41:38 +02:00
return presence;
},
sendPresence (type, status_message) {
_converse.api.send(this.constructPresence(type, status_message));
2018-10-23 03:41:38 +02:00
}
});
this.attemptNonPreboundSession = async function (credentials, reconnecting) {
2018-10-23 03:41:38 +02:00
if (credentials) {
this.autoLogin(credentials);
} else if (this.auto_login) {
if (this.credentials_url && _converse.authentication === _converse.LOGIN) {
const data = await getLoginCredentials();
this.autoLogin(data);
2018-10-23 03:41:38 +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 {
// Could be ANONYMOUS or EXTERNAL or manual passing in of JID and password
this.autoLogin();
}
2018-10-23 03:41:38 +02:00
} else if (reconnecting) {
this.autoLogin();
} else if (!_converse.isTestEnv() && window.PasswordCredential) {
const creds = await navigator.credentials.get({'password': true});
if (creds && creds.type == 'password' && u.isValidJID(creds.id)) {
2019-06-05 11:27:59 +02:00
await setUserJID(creds.id);
this.autoLogin({'jid': creds.id, 'password': creds.password});
}
2018-10-23 03:41:38 +02:00
}
};
2018-10-23 03:41:38 +02:00
this.autoLogin = function (credentials) {
if (
this.authentication === _converse.ANONYMOUS ||
this.authentication === _converse.EXTERNAL
) {
2018-10-23 03:41:38 +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 " +
"_converse.api.user.login.");
}
2018-10-23 03:41:38 +02:00
if (!this.connection.reconnecting) {
this.connection.reset();
}
this.connection.connect(
this.jid.toLowerCase(),
null,
this.onConnectStatusChanged,
BOSH_WAIT
);
2018-10-23 03:41:38 +02:00
} else if (this.authentication === _converse.LOGIN) {
const password = _.isNil(credentials)
? _converse.connection.pass || this.password
: credentials.password;
2018-10-23 03:41:38 +02:00
if (!password) {
if (this.auto_login) {
2019-04-22 14:04:21 +02:00
throw new Error("autoLogin: If you use auto_login and "+
2018-10-23 03:41:38 +02:00
"authentication='login' then you also need to provide a password.");
}
2018-10-23 03:41:38 +02:00
_converse.setDisconnectionCause(Strophe.Status.AUTHFAIL, undefined, true);
2019-04-27 15:01:15 +02:00
_converse.api.connection.disconnect();
2018-10-23 03:41:38 +02:00
return;
}
2018-10-23 03:41:38 +02:00
if (!this.connection.reconnecting) {
this.connection.reset();
}
2018-10-23 03:41:38 +02:00
this.connection.connect(this.jid, password, this.onConnectStatusChanged, BOSH_WAIT);
}
};
2018-10-23 03:41:38 +02:00
// Initialization
// --------------
// This is the end of the initialize method.
if (settings.connection) {
this.connection = settings.connection;
2018-10-23 03:41:38 +02:00
}
if (_converse.isTestEnv()) {
await finishInitialization();
2018-10-23 03:41:38 +02:00
return _converse;
} else if (!_.isUndefined(i18n)) {
const url = u.interpolate(_converse.locales_url, {'locale': _converse.locale});
try {
await i18n.fetchTranslations(_converse.locale, _converse.locales, url);
} catch (e) {
_converse.log(e.message, Strophe.LogLevel.FATAL);
}
2018-10-23 03:41:38 +02:00
}
await finishInitialization();
2018-10-23 03:41:38 +02:00
return init_promise;
};
/**
* ### The private API
*
* The private API methods are only accessible via the closured {@link _converse}
* object, which is only available to plugins.
*
* These methods are kept private (i.e. not global) because they may return
* sensitive data which should be kept off-limits to other 3rd-party scripts
* that might be running in the page.
*
* @namespace _converse.api
* @memberOf _converse
*/
_converse.api = {
/**
2018-10-23 03:41:38 +02:00
* This grouping collects API functions related to the XMPP connection.
*
2018-10-23 03:41:38 +02:00
* @namespace _converse.api.connection
* @memberOf _converse.api
*/
2019-06-01 19:36:23 +02:00
connection: {
2018-10-23 03:41:38 +02:00
/**
* @method _converse.api.connection.connected
* @memberOf _converse.api.connection
* @returns {boolean} Whether there is an established connection or not.
*/
2019-06-01 19:36:23 +02:00
connected () {
return (_converse.connection && _converse.connection.connected) || false;
2018-10-23 03:41:38 +02:00
},
2019-06-01 19:36:23 +02:00
2018-10-23 03:41:38 +02:00
/**
* Terminates the connection.
*
* @method _converse.api.connection.disconnect
* @memberOf _converse.api.connection
*/
2019-06-01 19:36:23 +02:00
disconnect () {
2019-04-22 14:04:21 +02:00
if (_converse.connection) {
_converse.connection.disconnect();
2019-04-22 14:04:21 +02:00
} else {
tearDown();
clearSession();
2019-04-22 14:04:21 +02:00
}
2018-10-23 03:41:38 +02:00
},
2019-06-01 19:36:23 +02:00
/**
* Can be called once the XMPP connection has dropped and we want
* to attempt reconnection.
* Only needs to be called once, if reconnect fails Converse will
* attempt to reconnect every two seconds, alternating between BOSH and
* Websocket if URLs for both were provided.
2019-06-01 19:36:23 +02:00
* @method reconnect
* @memberOf _converse.api.connection
*/
async reconnect () {
const conn_status = _converse.connfeedback.get('connection_status');
if (conn_status === Strophe.Status.CONNFAIL) {
// When reconnecting with a new transport, we call setUserJID
// so that a new resource is generated, to avoid multiple
// server-side sessions with the same resource.
//
// We also call `_proto._doDisconnect` so that connection event handlers
// for the old transport are removed.
if (_converse.api.connection.isType('websocket') && _converse.bosh_service_url) {
await setUserJID(_converse.bare_jid);
_converse.connection._proto._doDisconnect();
_converse.connection._proto = new Strophe.Bosh(_converse.connection);
_converse.connection.service = _converse.bosh_service_url;
} else if (_converse.api.connection.isType('bosh') && _converse.websocket_url) {
await setUserJID(_converse.bare_jid);
_converse.connection._proto._doDisconnect();
_converse.connection._proto = new Strophe.Websocket(_converse.connection);
_converse.connection.service = _converse.websocket_url;
}
}
if ([Strophe.Status.RECONNECTING, Strophe.Status.CONNFAIL].includes(conn_status)) {
debouncedReconnect();
2019-06-01 19:36:23 +02:00
} else {
reconnect();
2019-06-01 19:36:23 +02:00
}
},
/**
* Utility method to determine the type of connection we have
* @method isType
* @memberOf _converse.api.connection
* @returns {boolean}
*/
isType (type) {
if (type.toLowerCase() === 'websocket') {
return _converse.connection._proto instanceof Strophe.Websocket;
} else if (type.toLowerCase() === 'bosh') {
return _converse.connection._proto instanceof Strophe.Bosh;
}
2019-06-01 19:36:23 +02:00
}
2018-10-23 03:41:38 +02:00
},
/**
* Lets you trigger events, which can be listened to via
2018-10-23 03:41:38 +02:00
* {@link _converse.api.listen.on} or {@link _converse.api.listen.once}
* (see [_converse.api.listen](http://localhost:8000/docs/html/api/-_converse.api.listen.html)).
*
* @method _converse.api.trigger
* @param {string} name - The event name
* @param {...any} [argument] - Argument to be passed to the event handler
* @param {object} [options]
* @param {boolean} [options.synchronous] - Whether the event is synchronous or not.
* When a synchronous event is fired, Converse will wait for all
* promises returned by the event's handlers to finish before continuing.
2018-10-23 03:41:38 +02:00
*/
async trigger (name) {
/* Event emitter and promise resolver */
const args = Array.from(arguments);
const options = args.pop();
if (options && options.synchronous) {
const events = _converse._events[name] || [];
await Promise.all(events.map(e => e.callback.call(e.ctx, args)));
} else {
_converse.trigger.apply(_converse, arguments);
}
const promise = _converse.promises[name];
if (!_.isUndefined(promise)) {
promise.resolve();
}
2018-10-23 03:41:38 +02:00
},
/**
* This grouping collects API functions related to the current logged in user.
*
2018-10-23 03:41:38 +02:00
* @namespace _converse.api.user
* @memberOf _converse.api
*/
user: {
/**
2018-10-23 03:41:38 +02:00
* @method _converse.api.user.jid
* @returns {string} The current user's full JID (Jabber ID)
* @example _converse.api.user.jid())
*/
jid () {
2018-10-23 03:41:38 +02:00
return _converse.connection.jid;
},
2019-06-01 19:36:23 +02:00
2018-10-23 03:41:38 +02:00
/**
* Logs the user in.
*
* If called without any parameters, Converse will try
* to log the user in by calling the `prebind_url` or `credentials_url` depending
* on whether prebinding is used or not.
*
* Otherwise the user will be shown a login form.
*
2018-10-23 03:41:38 +02:00
* @method _converse.api.user.login
* @param {string} [jid]
* @param {string} [password]
* @param {boolean} [reconnecting]
*/
async login (jid, password, reconnecting) {
if (_converse.api.connection.isType('bosh')) {
if (reconnecting && _converse.prebind_url) {
return _converse.startNewBOSHSession();
} else if (await _converse.restoreBOSHSession()) {
return;
}
}
if (jid || _converse.jid) {
// Reassign because we might have gained a resource
jid = await setUserJID(jid || _converse.jid);
}
password = password || _converse.password;
const credentials = (jid && password) ? { jid, password } : null;
_converse.attemptNonPreboundSession(credentials, reconnecting);
},
/**
2018-10-23 03:41:38 +02:00
* Logs the user out of the current XMPP session.
*
2018-10-23 03:41:38 +02:00
* @method _converse.api.user.logout
* @example _converse.api.user.logout();
*/
logout () {
clearSession();
_converse.setDisconnectionCause(_converse.LOGOUT, undefined, true);
if (!_.isUndefined(_converse.connection)) {
_converse.connection.disconnect();
} else {
tearDown();
}
// Recreate all the promises
Object.keys(_converse.promises).forEach(addPromise);
/**
* Triggered once the user has logged out.
* @event _converse#logout
*/
_converse.api.trigger('logout');
},
/**
2018-10-23 03:41:38 +02:00
* Set and get the user's chat status, also called their *availability*.
*
2018-10-23 03:41:38 +02:00
* @namespace _converse.api.user.status
* @memberOf _converse.api.user
*/
status: {
2018-10-23 03:41:38 +02:00
/** Return the current user's availability status.
*
2018-10-23 03:41:38 +02:00
* @method _converse.api.user.status.get
* @example _converse.api.user.status.get();
*/
get () {
2018-10-23 03:41:38 +02:00
return _converse.xmppstatus.get('status');
},
/**
2018-10-23 03:41:38 +02:00
* The user's status can be set to one of the following values:
*
2018-10-23 03:41:38 +02:00
* @method _converse.api.user.status.set
* @param {string} value The user's chat status (e.g. 'away', 'dnd', 'offline', 'online', 'unavailable' or 'xa')
* @param {string} [message] A custom status message
*
* @example this._converse.api.user.status.set('dnd');
* @example this._converse.api.user.status.set('dnd', 'In a meeting');
*/
set (value, message) {
2018-10-23 03:41:38 +02:00
const data = {'status': value};
2019-04-29 09:29:40 +02:00
if (!_.includes(Object.keys(_converse.STATUS_WEIGHTS), value)) {
throw new Error(
'Invalid availability value. See https://xmpp.org/rfcs/rfc3921.html#rfc.section.2.2.2.1'
);
2018-10-23 03:41:38 +02:00
}
if (_.isString(message)) {
data.status_message = message;
}
_converse.xmppstatus.sendPresence(value);
_converse.xmppstatus.save(data);
},
2018-10-23 03:41:38 +02:00
/**
2018-10-23 03:41:38 +02:00
* Set and retrieve the user's custom status message.
*
2018-10-23 03:41:38 +02:00
* @namespace _converse.api.user.status.message
* @memberOf _converse.api.user.status
*/
message: {
/**
2018-10-23 03:41:38 +02:00
* @method _converse.api.user.status.message.get
* @returns {string} The status message
* @example const message = _converse.api.user.status.message.get()
*/
get () {
2018-10-23 03:41:38 +02:00
return _converse.xmppstatus.get('status_message');
},
/**
2018-10-23 03:41:38 +02:00
* @method _converse.api.user.status.message.set
* @param {string} status The status message
* @example _converse.api.user.status.message.set('In a meeting');
*/
set (status) {
_converse.xmppstatus.save({ status_message: status });
}
2018-10-23 03:41:38 +02:00
}
}
2018-10-23 03:41:38 +02:00
},
2018-10-23 03:41:38 +02:00
/**
* This grouping allows access to the
* [configuration settings](/docs/html/configuration.html#configuration-settings)
* of Converse.
*
* @namespace _converse.api.settings
* @memberOf _converse.api
*/
settings: {
/**
2018-10-23 03:41:38 +02:00
* Allows new configuration settings to be specified, or new default values for
* existing configuration settings to be specified.
*
* @method _converse.api.settings.update
* @param {object} settings The configuration settings
* @example
* _converse.api.settings.update({
* 'enable_foo': true
* });
*
2018-10-23 03:41:38 +02:00
* // The user can then override the default value of the configuration setting when
* // calling `converse.initialize`.
* converse.initialize({
* 'enable_foo': false
* });
*/
update (settings) {
2018-10-23 03:41:38 +02:00
u.merge(_converse.default_settings, settings);
u.merge(_converse, settings);
u.applyUserSettings(_converse, settings, _converse.user_settings);
},
/**
* @method _converse.api.settings.get
* @returns {*} Value of the particular configuration setting.
* @example _converse.api.settings.get("play_sounds");
*/
get (key) {
2019-04-29 09:29:40 +02:00
if (_.includes(Object.keys(_converse.default_settings), key)) {
2018-10-23 03:41:38 +02:00
return _converse[key];
}
},
/**
2018-10-23 03:41:38 +02:00
* Set one or many configuration settings.
*
2018-10-23 03:41:38 +02:00
* Note, this is not an alternative to calling {@link converse.initialize}, which still needs
* to be called. Generally, you'd use this method after Converse is already
* running and you want to change the configuration on-the-fly.
*
2018-10-23 03:41:38 +02:00
* @method _converse.api.settings.set
* @param {Object} [settings] An object containing configuration settings.
* @param {string} [key] Alternatively to passing in an object, you can pass in a key and a value.
* @param {string} [value]
* @example _converse.api.settings.set("play_sounds", true);
* @example
* _converse.api.settings.set({
* "play_sounds", true,
* "hide_offline_users" true
* });
*/
set (key, val) {
2018-10-23 03:41:38 +02:00
const o = {};
if (_.isObject(key)) {
2019-04-29 09:29:40 +02:00
_.assignIn(_converse, _.pick(key, Object.keys(_converse.default_settings)));
} else if (_.isString('string')) {
2018-10-23 03:41:38 +02:00
o[key] = val;
2019-04-29 09:29:40 +02:00
_.assignIn(_converse, _.pick(o, Object.keys(_converse.default_settings)));
2018-10-23 03:41:38 +02:00
}
}
},
/**
* Converse and its plugins trigger various events which you can listen to via the
2018-10-23 03:41:38 +02:00
* {@link _converse.api.listen} namespace.
*
* Some of these events are also available as [ES2015 Promises](http://es6-features.org/#PromiseUsage)
* although not all of them could logically act as promises, since some events
* might be fired multpile times whereas promises are to be resolved (or
* rejected) only once.
*
* Events which are also promises include:
*
* * [cachedRoster](/docs/html/events.html#cachedroster)
* * [chatBoxesFetched](/docs/html/events.html#chatBoxesFetched)
* * [pluginsInitialized](/docs/html/events.html#pluginsInitialized)
* * [roster](/docs/html/events.html#roster)
* * [rosterContactsFetched](/docs/html/events.html#rosterContactsFetched)
* * [rosterGroupsFetched](/docs/html/events.html#rosterGroupsFetched)
* * [rosterInitialized](/docs/html/events.html#rosterInitialized)
* * [statusInitialized](/docs/html/events.html#statusInitialized)
* * [roomsPanelRendered](/docs/html/events.html#roomsPanelRendered)
*
* The various plugins might also provide promises, and they do this by using the
* `promises.add` api method.
*
* @namespace _converse.api.promises
* @memberOf _converse.api
*/
promises: {
2018-10-23 03:41:38 +02:00
/**
* By calling `promises.add`, a new [Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise)
* is made available for other code or plugins to depend on via the
* {@link _converse.api.waitUntil} method.
*
2018-10-23 03:41:38 +02:00
* Generally, it's the responsibility of the plugin which adds the promise to
* also resolve it.
*
* This is done by calling {@link _converse.api.trigger}, which not only resolves the
2018-10-23 03:41:38 +02:00
* promise, but also emits an event with the same name (which can be listened to
* via {@link _converse.api.listen}).
*
2018-10-23 03:41:38 +02:00
* @method _converse.api.promises.add
* @param {string|array} [name|names] The name or an array of names for the promise(s) to be added
* @example _converse.api.promises.add('foo-completed');
*/
add (promises) {
promises = Array.isArray(promises) ? promises : [promises];
promises.forEach(addPromise);
2018-10-23 03:41:38 +02:00
}
},
2018-10-23 03:41:38 +02:00
/**
* Converse emits events to which you can subscribe to.
*
* The `listen` namespace exposes methods for creating event listeners
* (aka handlers) for these events.
*
* @namespace _converse.api.listen
* @memberOf _converse
*/
listen: {
/**
2018-10-23 03:41:38 +02:00
* Lets you listen to an event exactly once.
*
2018-10-23 03:41:38 +02:00
* @method _converse.api.listen.once
* @param {string} name The event's name
* @param {function} callback The callback method to be called when the event is emitted.
* @param {object} [context] The value of the `this` parameter for the callback.
* @example _converse.api.listen.once('message', function (messageXML) { ... });
*/
once: _converse.once.bind(_converse),
/**
2018-10-23 03:41:38 +02:00
* Lets you subscribe to an event.
*
2018-10-23 03:41:38 +02:00
* Every time the event fires, the callback method specified by `callback` will be called.
*
* @method _converse.api.listen.on
* @param {string} name The event's name
* @param {function} callback The callback method to be called when the event is emitted.
* @param {object} [context] The value of the `this` parameter for the callback.
* @example _converse.api.listen.on('message', function (messageXML) { ... });
*/
on: _converse.on.bind(_converse),
2018-09-02 15:07:14 +02:00
/**
2018-10-23 03:41:38 +02:00
* To stop listening to an event, you can use the `not` method.
*
2018-10-23 03:41:38 +02:00
* Every time the event fires, the callback method specified by `callback` will be called.
*
* @method _converse.api.listen.not
* @param {string} name The event's name
* @param {function} callback The callback method that is to no longer be called when the event fires
* @example _converse.api.listen.not('message', function (messageXML);
*/
not: _converse.off.bind(_converse),
2018-09-02 15:07:14 +02:00
/**
2018-10-23 03:41:38 +02:00
* Subscribe to an incoming stanza
2019-03-29 15:47:23 +01:00
* Every a matched stanza is received, the callback method specified by
* `callback` will be called.
2018-10-23 03:41:38 +02:00
* @method _converse.api.listen.stanza
* @param {string} name The stanza's name
2019-03-29 15:47:23 +01:00
* @param {object} options Matching options (e.g. 'ns' for namespace, 'type' for stanza type, also 'id' and 'from');
2018-10-23 03:41:38 +02:00
* @param {function} handler The callback method to be called when the stanza appears
*/
stanza (name, options, handler) {
2018-10-23 03:41:38 +02:00
if (_.isFunction(options)) {
handler = options;
options = {};
} else {
options = options || {};
}
_converse.connection.addHandler(
handler,
options.ns,
name,
options.type,
options.id,
options.from,
options
);
}
2018-10-23 03:41:38 +02:00
},
/**
* Wait until a promise is resolved
* @method _converse.api.waitUntil
* @param {string} name The name of the promise
* @returns {Promise}
*/
waitUntil (name) {
2018-10-23 03:41:38 +02:00
const promise = _converse.promises[name];
if (_.isUndefined(promise)) {
return null;
}
2018-10-23 03:41:38 +02:00
return promise;
},
/**
2018-10-23 03:41:38 +02:00
* Allows you to send XML stanzas.
* @method _converse.api.send
* @example
* const msg = converse.env.$msg({
* 'from': 'juliet@example.com/balcony',
* 'to': 'romeo@example.net',
* 'type':'chat'
* });
* _converse.api.send(msg);
*/
send (stanza) {
if (_.isString(stanza)) {
stanza = u.toStanza(stanza);
}
if (stanza.tagName === 'iq') {
return _converse.api.sendIQ(stanza);
} else {
_converse.connection.send(stanza);
_converse.api.trigger('send', stanza);
}
2018-10-23 03:41:38 +02:00
},
/**
* Send an IQ stanza and receive a promise
* @method _converse.api.sendIQ
* @returns {Promise} A promise which resolves when we receive a `result` stanza
* or is rejected when we receive an `error` stanza.
*/
sendIQ (stanza, timeout) {
2018-10-23 03:41:38 +02:00
return new Promise((resolve, reject) => {
_converse.connection.sendIQ(stanza, resolve, reject, timeout || _converse.IQ_TIMEOUT);
_converse.api.trigger('send', stanza);
2018-10-23 03:41:38 +02:00
});
}
};
/**
* ### The Public API
*
* This namespace contains public API methods which are are
* accessible on the global `converse` object.
* They are public, because any JavaScript in the
* page can call them. Public methods therefore dont expose any sensitive
* or closured data. To do that, youll need to create a plugin, which has
* access to the private API method.
*
* @namespace converse
*/
const converse = {
/**
* Public API method which initializes Converse.
* This method must always be called when using Converse.
* @memberOf converse
* @method initialize
* @param {object} config A map of [configuration-settings](https://conversejs.org/docs/html/configuration.html#configuration-settings).
* @example
* converse.initialize({
* auto_list_rooms: false,
* auto_subscribe: false,
* bosh_service_url: 'https://bind.example.com',
* hide_muc_server: false,
* i18n: locales['en'],
* play_sounds: true,
* show_controlbox_by_default: true,
* debug: false,
* roster_groups: true
* });
*/
initialize (settings, callback) {
2018-10-23 03:41:38 +02:00
return _converse.initialize(settings, callback);
},
/**
* Exposes methods for adding and removing plugins. You'll need to write a plugin
* if you want to have access to the private API methods defined further down below.
*
* For more information on plugins, read the documentation on [writing a plugin](/docs/html/plugin_development.html).
* @namespace plugins
* @memberOf converse
*/
plugins: {
2018-10-23 03:41:38 +02:00
/** Registers a new plugin.
*
2018-10-23 03:41:38 +02:00
* @method converse.plugins.add
* @param {string} name The name of the plugin
* @param {object} plugin The plugin object
* @example
2018-10-23 03:41:38 +02:00
* const plugin = {
* initialize: function () {
* // Gets called as soon as the plugin has been loaded.
*
2018-10-23 03:41:38 +02:00
* // Inside this method, you have access to the private
* // API via `_covnerse.api`.
*
2018-10-23 03:41:38 +02:00
* // The private _converse object contains the core logic
* // and data-structures of Converse.
* }
* }
* converse.plugins.add('myplugin', plugin);
*/
add (name, plugin) {
2018-10-23 03:41:38 +02:00
plugin.__name__ = name;
if (!_.isUndefined(_converse.pluggable.plugins[name])) {
throw new TypeError(
`Error: plugin with name "${name}" has already been ` + 'registered!'
);
2018-10-23 03:41:38 +02:00
} else {
_converse.pluggable.plugins[name] = plugin;
}
}
2018-10-23 03:41:38 +02:00
},
/**
* Utility methods and globals from bundled 3rd party libraries.
* @memberOf converse
*
* @property {function} converse.env.$build - Creates a Strophe.Builder, for creating stanza objects.
* @property {function} converse.env.$iq - Creates a Strophe.Builder with an <iq/> element as the root.
* @property {function} converse.env.$msg - Creates a Strophe.Builder with an <message/> element as the root.
* @property {function} converse.env.$pres - Creates a Strophe.Builder with an <presence/> element as the root.
* @property {object} converse.env.Backbone - The [Backbone](http://backbonejs.org) object used by Converse to create models and views.
* @property {function} converse.env.Promise - The Promise implementation used by Converse.
* @property {function} converse.env.Strophe - The [Strophe](http://strophe.im/strophejs) XMPP library used by Converse.
* @property {object} converse.env._ - The instance of [lodash](http://lodash.com) used by Converse.
* @property {function} converse.env.f - And instance of Lodash with its methods wrapped to produce immutable auto-curried iteratee-first data-last methods.
2019-05-06 11:16:56 +02:00
* @property {object} converse.env.dayjs - [DayJS](https://github.com/iamkun/dayjs) date manipulation library.
2018-10-23 03:41:38 +02:00
* @property {function} converse.env.sizzle - [Sizzle](https://sizzlejs.com) CSS selector engine.
* @property {object} converse.env.utils - Module containing common utility methods used by Converse.
*/
'env': {
'$build': $build,
'$iq': $iq,
'$msg': $msg,
'$pres': $pres,
'Backbone': Backbone,
'Promise': Promise,
'Strophe': Strophe,
'_': _,
2019-05-06 11:16:56 +02:00
'dayjs': dayjs,
2018-10-23 03:41:38 +02:00
'sizzle': sizzle,
'utils': u
}
};
2019-03-22 08:15:35 +01:00
2018-10-23 03:41:38 +02:00
window.converse = converse;
2019-03-22 08:15:35 +01:00
/**
2019-03-29 15:47:23 +01:00
* Once Converse.js has loaded, it'll dispatch a custom event with the name `converse-loaded`.
* You can listen for this event in order to be informed as soon as converse.js has been
* loaded and parsed, which would mean it's safe to call `converse.initialize`.
2019-03-22 08:15:35 +01:00
* @event converse-loaded
2019-03-29 15:47:23 +01:00
* @example window.addEventListener('converse-loaded', () => converse.initialize());
2019-03-22 08:15:35 +01:00
*/
2018-10-23 03:41:38 +02:00
window.dispatchEvent(new CustomEvent('converse-loaded'));
export default converse;