2018-04-16 18:08:00 +02:00
|
|
|
|
// Converse.js
|
|
|
|
|
// https://conversejs.org
|
2016-02-16 08:46:47 +01:00
|
|
|
|
//
|
2019-02-18 19:17:06 +01:00
|
|
|
|
// Copyright (c) 2013-2019, the Converse.js developers
|
2016-02-16 08:46:47 +01:00
|
|
|
|
// Licensed under the Mozilla Public License (MPLv2)
|
2019-07-11 10:48:52 +02:00
|
|
|
|
/**
|
|
|
|
|
* @module converse-core
|
|
|
|
|
*/
|
2020-01-12 01:00:57 +01:00
|
|
|
|
import { assignIn, debounce, get, invoke, isFunction, isObject, isString, pick } from 'lodash';
|
2019-07-11 12:30:14 +02:00
|
|
|
|
import 'strophe.js/src/websocket';
|
2019-10-09 16:01:38 +02:00
|
|
|
|
import './polyfill';
|
2019-05-29 17:40:16 +02:00
|
|
|
|
import * as strophe from 'strophe.js/src/core';
|
2019-06-05 06:57:20 +02:00
|
|
|
|
import Backbone from 'backbone';
|
|
|
|
|
import BrowserStorage from 'backbone.browserStorage';
|
|
|
|
|
import _ from './lodash.noconflict';
|
2019-05-21 16:18:14 +02:00
|
|
|
|
import advancedFormat from 'dayjs/plugin/advancedFormat';
|
2019-06-05 06:57:20 +02:00
|
|
|
|
import dayjs from 'dayjs';
|
|
|
|
|
import i18n from './i18n';
|
2019-11-06 11:01:34 +01:00
|
|
|
|
import log from '@converse/headless/log';
|
2019-06-05 06:57:20 +02:00
|
|
|
|
import pluggable from 'pluggable.js/src/pluggable';
|
|
|
|
|
import sizzle from 'sizzle';
|
2019-11-05 14:40:05 +01:00
|
|
|
|
import stanza_utils from "@converse/headless/utils/stanza";
|
2019-06-05 06:57:20 +02:00
|
|
|
|
import u from '@converse/headless/utils/core';
|
2018-10-23 03:41:38 +02:00
|
|
|
|
|
2019-05-29 17:40:16 +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();
|
2019-10-24 14:29:15 +02:00
|
|
|
|
BrowserStorage.patch(Backbone);
|
2018-10-23 03:41:38 +02:00
|
|
|
|
|
2019-05-21 16:18:14 +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');
|
2019-09-24 15:33:41 +02:00
|
|
|
|
Strophe.addNamespace('FASTEN', 'urn:xmpp:fasten:0');
|
2018-10-23 03:41:38 +02:00
|
|
|
|
Strophe.addNamespace('FORWARD', 'urn:xmpp:forward:0');
|
|
|
|
|
Strophe.addNamespace('HINTS', 'urn:xmpp:hints');
|
|
|
|
|
Strophe.addNamespace('HTTPUPLOAD', 'urn:xmpp:http:upload:0');
|
2018-11-12 18:46:03 +01:00
|
|
|
|
Strophe.addNamespace('IDLE', 'urn:xmpp:idle:1');
|
2018-10-23 03:41:38 +02:00
|
|
|
|
Strophe.addNamespace('MAM', 'urn:xmpp:mam:2');
|
2019-09-24 15:33:41 +02:00
|
|
|
|
Strophe.addNamespace('MODERATE', 'urn:xmpp:message-moderate:0');
|
2018-10-23 03:41:38 +02:00
|
|
|
|
Strophe.addNamespace('NICK', 'http://jabber.org/protocol/nick');
|
2019-06-05 06:57:20 +02:00
|
|
|
|
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');
|
2019-09-24 15:33:41 +02:00
|
|
|
|
Strophe.addNamespace('RETRACT', 'urn:xmpp:message-retract:0');
|
2018-10-23 03:41:38 +02:00
|
|
|
|
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');
|
2019-04-10 22:03:00 +02:00
|
|
|
|
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': { '_': _ }
|
|
|
|
|
};
|
|
|
|
|
|
2018-11-15 22:59:55 +01:00
|
|
|
|
// 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;
|
|
|
|
|
|
2019-06-04 14:26:32 +02:00
|
|
|
|
// 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',
|
2019-06-05 10:01:55 +02:00
|
|
|
|
'converse-bosh',
|
2019-06-04 14:26:32 +02:00
|
|
|
|
'converse-caps',
|
|
|
|
|
'converse-chatboxes',
|
2019-11-01 16:04:55 +01:00
|
|
|
|
'converse-chat',
|
2019-06-04 14:26:32 +02:00
|
|
|
|
'converse-disco',
|
2019-07-11 12:30:51 +02:00
|
|
|
|
'converse-emoji',
|
2019-06-04 14:26:32 +02:00
|
|
|
|
'converse-mam',
|
|
|
|
|
'converse-muc',
|
2019-11-01 16:04:55 +01:00
|
|
|
|
'converse-headlines',
|
2019-06-04 14:26:32 +02:00
|
|
|
|
'converse-ping',
|
|
|
|
|
'converse-pubsub',
|
|
|
|
|
'converse-roster',
|
|
|
|
|
'converse-rsm',
|
|
|
|
|
'converse-smacks',
|
2019-10-15 12:55:11 +02:00
|
|
|
|
'converse-status',
|
2019-09-24 15:33:41 +02:00
|
|
|
|
'converse-vcard'
|
2019-06-04 14:26:32 +02:00
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
|
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.
|
2019-07-25 06:46:05 +02:00
|
|
|
|
* @global
|
2018-10-23 03:41:38 +02:00
|
|
|
|
* @namespace _converse
|
|
|
|
|
*/
|
2019-09-24 15:33:41 +02:00
|
|
|
|
// Strictly speaking _converse is not a global, but we need to set it as
|
2019-07-25 06:46:05 +02:00
|
|
|
|
// such to get JSDoc to create the correct document site strucure.
|
2018-10-23 03:41:38 +02:00
|
|
|
|
const _converse = {
|
|
|
|
|
'templates': {},
|
2018-11-20 17:31:53 +01:00
|
|
|
|
'promises': {}
|
2018-10-23 03:41:38 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-01-10 13:04:05 +01:00
|
|
|
|
_converse.VERSION_NAME = "v6.0.1dev";
|
2018-11-15 22:59:55 +01:00
|
|
|
|
|
2019-04-29 09:07:15 +02:00
|
|
|
|
Object.assign(_converse, Backbone.Events);
|
2018-10-23 03:41:38 +02:00
|
|
|
|
|
2019-08-01 10:26:35 +02:00
|
|
|
|
_converse.Collection = Backbone.Collection.extend({
|
2019-10-24 14:29:15 +02:00
|
|
|
|
async clearSession (options={}) {
|
|
|
|
|
await Promise.all(Array.from(this.models).map(m => {
|
|
|
|
|
return new Promise(
|
2019-12-11 15:11:22 +01:00
|
|
|
|
resolve => {
|
|
|
|
|
m.destroy(Object.assign(options, {
|
|
|
|
|
'success': resolve,
|
|
|
|
|
'error': (m, e) => { log.error(e); resolve() }
|
|
|
|
|
}));
|
|
|
|
|
}
|
2019-10-24 14:29:15 +02:00
|
|
|
|
);
|
|
|
|
|
}));
|
2019-12-11 15:11:22 +01:00
|
|
|
|
await this.browserStorage.clear();
|
2019-08-01 10:26:35 +02:00
|
|
|
|
this.reset();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
2019-08-12 20:16:34 +02:00
|
|
|
|
/**
|
|
|
|
|
* Custom error for indicating timeouts
|
|
|
|
|
* @namespace _converse
|
|
|
|
|
*/
|
|
|
|
|
class TimeoutError extends Error {}
|
|
|
|
|
_converse.TimeoutError = TimeoutError;
|
|
|
|
|
|
|
|
|
|
|
2019-09-24 15:33:41 +02:00
|
|
|
|
class IllegalMessage extends Error {}
|
|
|
|
|
_converse.IllegalMessage = IllegalMessage;
|
|
|
|
|
|
|
|
|
|
|
2018-11-15 22:59:55 +01: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
|
|
|
|
// 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'
|
|
|
|
|
};
|
2019-06-05 06:57:20 +02:00
|
|
|
|
_converse.ANONYMOUS = 'anonymous';
|
2018-10-23 03:41:38 +02:00
|
|
|
|
_converse.CLOSED = 'closed';
|
2019-06-05 06:57:20 +02:00
|
|
|
|
_converse.EXTERNAL = 'external';
|
|
|
|
|
_converse.LOGIN = 'login';
|
|
|
|
|
_converse.LOGOUT = 'logout';
|
2018-10-23 03:41:38 +02:00
|
|
|
|
_converse.OPENED = 'opened';
|
2019-06-05 06:57:20 +02:00
|
|
|
|
_converse.PREBIND = 'prebind';
|
2018-10-23 03:41:38 +02:00
|
|
|
|
|
2019-09-24 15:33:41 +02:00
|
|
|
|
_converse.STANZA_TIMEOUT = 10000;
|
2018-10-23 03:41:38 +02:00
|
|
|
|
|
|
|
|
|
_converse.CONNECTION_STATUS = {
|
|
|
|
|
0: 'ERROR',
|
|
|
|
|
1: 'CONNECTING',
|
|
|
|
|
2: 'CONNFAIL',
|
|
|
|
|
3: 'AUTHENTICATING',
|
|
|
|
|
4: 'AUTHFAIL',
|
|
|
|
|
5: 'CONNECTED',
|
|
|
|
|
6: 'DISCONNECTED',
|
|
|
|
|
7: 'DISCONNECTING',
|
|
|
|
|
8: 'ATTACHED',
|
|
|
|
|
9: 'REDIRECT',
|
2019-06-05 06:57:20 +02:00
|
|
|
|
10: 'RECONNECTING'
|
2018-10-23 03:41:38 +02:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
_converse.SUCCESS = 'success';
|
|
|
|
|
_converse.FAILURE = 'failure';
|
|
|
|
|
|
2018-10-28 20:56:44 +01:00
|
|
|
|
// 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
|
|
|
|
|
2019-06-05 06:57:20 +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
|
2019-03-04 17:49:44 +01:00
|
|
|
|
// 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';
|
|
|
|
|
|
2018-05-29 12:00:23 +02:00
|
|
|
|
_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.
|
2019-11-22 14:23:19 +01:00
|
|
|
|
loglevel: 'info',
|
2018-10-23 03:41:38 +02:00
|
|
|
|
default_state: 'online',
|
2018-11-18 19:14:22 +01:00
|
|
|
|
discover_connection_methods: false,
|
2019-07-11 12:30:14 +02:00
|
|
|
|
geouri_regex: /https\:\/\/www.openstreetmap.org\/.*#map=[0-9]+\/([\-0-9.]+)\/([\-0-9.]+)\S*/g,
|
2018-10-23 03:41:38 +02:00
|
|
|
|
geouri_replacement: 'https://www.openstreetmap.org/?mlat=$1&mlon=$2#map=18/$1/$2',
|
2018-11-12 18:46:03 +01:00
|
|
|
|
idle_presence_timeout: 300, // Seconds after which an idle presence is sent
|
2018-10-23 03:41:38 +02:00
|
|
|
|
jid: undefined,
|
2019-07-26 17:53:18 +02:00
|
|
|
|
keepalive: true,
|
2018-10-23 03:41:38 +02:00
|
|
|
|
locales: [
|
2019-01-29 05:12:41 +01:00
|
|
|
|
'af', 'ar', 'bg', 'ca', 'cs', 'de', 'eo', 'es', 'eu', 'en', 'fr', 'gl',
|
2020-01-09 15:56:54 +01:00
|
|
|
|
'he', 'hi', 'hu', 'id', 'it', 'ja', 'nb', 'nl', 'mr', 'oc',
|
2019-10-06 11:04:47 +02:00
|
|
|
|
'pl', 'pt', 'pt_BR', 'ro', 'ru', 'tr', 'uk', 'vi', 'zh_CN', 'zh_TW'
|
2018-10-23 03:41:38 +02:00
|
|
|
|
],
|
|
|
|
|
message_carbons: true,
|
|
|
|
|
nickname: undefined,
|
|
|
|
|
password: undefined,
|
2019-12-03 13:51:57 +01:00
|
|
|
|
persistent_store: 'localStorage',
|
2018-10-23 03:41:38 +02:00
|
|
|
|
priority: 0,
|
|
|
|
|
rid: undefined,
|
|
|
|
|
root: window.document,
|
|
|
|
|
sid: undefined,
|
2018-12-04 12:52:25 +01:00
|
|
|
|
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: []
|
|
|
|
|
};
|
|
|
|
|
|
2018-08-10 14:09:21 +02:00
|
|
|
|
|
2019-03-29 23:47:56 +01:00
|
|
|
|
/**
|
|
|
|
|
* 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) {
|
2019-07-29 10:19:05 +02:00
|
|
|
|
if (i18n === undefined) {
|
2018-10-23 03:41:38 +02:00
|
|
|
|
return str;
|
|
|
|
|
}
|
|
|
|
|
return i18n.translate.apply(i18n, arguments);
|
2019-06-05 06:57:20 +02:00
|
|
|
|
};
|
2016-02-29 21:54:34 +01:00
|
|
|
|
|
2019-10-16 17:17:29 +02:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* A no-op method which is used to signal to gettext that the passed in string
|
|
|
|
|
* should be included in the pot translation file.
|
|
|
|
|
*
|
|
|
|
|
* In contrast to the double-underscore method, the triple underscore method
|
|
|
|
|
* doesn't actually translate the strings.
|
|
|
|
|
*
|
|
|
|
|
* One reason for this method might be because we're using strings we cannot
|
|
|
|
|
* send to the translation function because they require variable interpolation
|
|
|
|
|
* and we don't yet have the variables at scan time.
|
|
|
|
|
*
|
|
|
|
|
* @method ___
|
|
|
|
|
* @private
|
|
|
|
|
* @memberOf _converse
|
|
|
|
|
* @param { String } str
|
|
|
|
|
*/
|
|
|
|
|
_converse.___ = function (str) {
|
|
|
|
|
return str;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2018-10-23 03:41:38 +02:00
|
|
|
|
const __ = _converse.__;
|
2018-04-17 15:17:39 +02:00
|
|
|
|
|
2018-10-23 03:41:38 +02:00
|
|
|
|
const PROMISES = [
|
2018-05-29 12:00:23 +02:00
|
|
|
|
'afterResourceBinding',
|
2018-10-23 03:41:38 +02:00
|
|
|
|
'connectionInitialized',
|
2018-05-29 12:00:23 +02:00
|
|
|
|
'initialized',
|
2018-10-23 03:41:38 +02:00
|
|
|
|
'pluginsInitialized',
|
|
|
|
|
'statusInitialized'
|
|
|
|
|
];
|
2017-01-23 11:15:07 +01:00
|
|
|
|
|
2019-12-18 16:09:44 +01:00
|
|
|
|
|
|
|
|
|
function replacePromise (name) {
|
|
|
|
|
const existing_promise = _converse.promises[name];
|
|
|
|
|
if (!existing_promise) {
|
|
|
|
|
throw new Error(`Tried to replace non-existing promise: ${name}`);
|
|
|
|
|
}
|
|
|
|
|
if (existing_promise.replace) {
|
|
|
|
|
const promise = u.getResolveablePromise();
|
2019-12-20 16:02:53 +01:00
|
|
|
|
promise.replace = existing_promise.replace;
|
2019-12-18 16:09:44 +01:00
|
|
|
|
_converse.promises[name] = promise;
|
|
|
|
|
} else {
|
|
|
|
|
log.debug(`Not replacing promise "${name}"`);
|
|
|
|
|
}
|
2018-10-23 03:41:38 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-06-14 11:28:31 +02:00
|
|
|
|
_converse.isTestEnv = function () {
|
2018-11-18 19:14:22 +01:00
|
|
|
|
return Strophe.Connection.name === 'MockConnection';
|
2019-04-22 14:04:21 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2019-06-05 11:31:34 +02:00
|
|
|
|
_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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-04 12:14:52 +01:00
|
|
|
|
_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.
|
|
|
|
|
*/
|
2020-01-12 01:00:57 +01:00
|
|
|
|
return ['mobile', 'fullscreen', 'embedded'].includes(_converse.view_mode);
|
2019-06-05 06:57:20 +02:00
|
|
|
|
};
|
2018-06-01 12:21:41 +02:00
|
|
|
|
|
2019-10-24 14:29:15 +02:00
|
|
|
|
|
2019-12-17 13:39:32 +01:00
|
|
|
|
async function initSessionStorage () {
|
2019-10-24 14:29:15 +02:00
|
|
|
|
await BrowserStorage.sessionStorageInitialized;
|
2019-12-17 13:39:32 +01:00
|
|
|
|
_converse.storage = {
|
|
|
|
|
'session': BrowserStorage.localForage.createInstance({
|
|
|
|
|
'name': _converse.isTestEnv() ? 'converse-test-session' : 'converse-session',
|
|
|
|
|
'description': 'sessionStorage instance',
|
|
|
|
|
'driver': ['sessionStorageWrapper']
|
|
|
|
|
})
|
|
|
|
|
};
|
|
|
|
|
}
|
2019-10-24 14:29:15 +02:00
|
|
|
|
|
|
|
|
|
|
2019-12-17 13:39:32 +01:00
|
|
|
|
function initPersistentStorage () {
|
|
|
|
|
if (_converse.config.get('storage') !== 'persistent') {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
const config = {
|
|
|
|
|
'name': _converse.isTestEnv() ? 'converse-test-persistent' : 'converse-persistent',
|
|
|
|
|
'storeName': _converse.bare_jid
|
2019-10-24 14:29:15 +02:00
|
|
|
|
}
|
2019-12-17 13:39:32 +01:00
|
|
|
|
if (_converse.persistent_store === 'localStorage') {
|
|
|
|
|
config['description'] = 'localStorage instance';
|
|
|
|
|
config['driver'] = [BrowserStorage.localForage.LOCALSTORAGE];
|
|
|
|
|
} else if (_converse.persistent_store === 'IndexedDB') {
|
|
|
|
|
config['description'] = 'indexedDB instance';
|
|
|
|
|
config['driver'] = [BrowserStorage.localForage.INDEXEDDB];
|
|
|
|
|
}
|
|
|
|
|
_converse.storage['persistent'] = BrowserStorage.localForage.createInstance(config);
|
2019-10-24 14:29:15 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2019-10-11 20:29:12 +02:00
|
|
|
|
_converse.createStore = function (id, storage) {
|
2019-10-24 14:29:15 +02:00
|
|
|
|
const s = _converse.storage[storage ? storage : _converse.config.get('storage')];
|
|
|
|
|
return new Backbone.BrowserStorage(id, s);
|
2019-10-11 20:29:12 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2018-10-23 03:41:38 +02:00
|
|
|
|
_converse.router = new Backbone.Router();
|
2018-06-01 12:21:41 +02:00
|
|
|
|
|
2019-06-05 06:57:20 +02:00
|
|
|
|
function initPlugins () {
|
2018-11-15 22:59:55 +01:00
|
|
|
|
// 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 = [];
|
2019-06-04 14:26:32 +02:00
|
|
|
|
const whitelist = CORE_PLUGINS.concat(_converse.whitelisted_plugins);
|
2018-11-15 22:59:55 +01:00
|
|
|
|
|
2018-12-04 12:52:25 +01:00
|
|
|
|
if (_converse.singleton) {
|
2019-06-05 06:57:20 +02:00
|
|
|
|
[
|
|
|
|
|
'converse-bookmarks',
|
|
|
|
|
'converse-controlbox',
|
|
|
|
|
'converse-headline',
|
|
|
|
|
'converse-register'
|
|
|
|
|
].forEach(name => _converse.blacklisted_plugins.push(name));
|
2018-11-15 22:59:55 +01:00
|
|
|
|
}
|
|
|
|
|
|
2019-04-16 16:56:49 +02:00
|
|
|
|
_converse.pluggable.initializePlugins(
|
2019-06-05 06:57:20 +02:00
|
|
|
|
{ '_converse': _converse },
|
|
|
|
|
whitelist,
|
|
|
|
|
_converse.blacklisted_plugins
|
2019-04-16 16:56:49 +02:00
|
|
|
|
);
|
2019-05-21 11:12:34 +02:00
|
|
|
|
|
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
|
|
|
|
*/
|
2019-03-29 21:10:45 +01:00
|
|
|
|
_converse.api.trigger('pluginsInitialized');
|
2018-11-15 22:59:55 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
*/
|
2019-02-18 19:06:11 +01:00
|
|
|
|
const id = 'converse.client-config';
|
2018-11-15 22:59:55 +01:00
|
|
|
|
_converse.config = new Backbone.Model({
|
|
|
|
|
'id': id,
|
|
|
|
|
'trusted': _converse.trusted && true || false,
|
2019-12-17 13:39:32 +01:00
|
|
|
|
'storage': _converse.trusted ? 'persistent' : 'session'
|
2018-11-15 22:59:55 +01:00
|
|
|
|
});
|
2019-10-11 20:29:12 +02:00
|
|
|
|
_converse.config.browserStorage = _converse.createStore(id, "session");
|
2018-11-15 22:59:55 +01:00
|
|
|
|
_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', () => { ... });
|
|
|
|
|
*/
|
2019-03-29 21:10:45 +01:00
|
|
|
|
_converse.api.trigger('clientConfigInitialized');
|
2018-11-15 22:59:55 +01:00
|
|
|
|
}
|
|
|
|
|
|
2018-05-29 12:00:23 +02:00
|
|
|
|
|
2019-10-24 14:29:15 +02:00
|
|
|
|
async function tearDown () {
|
|
|
|
|
await _converse.api.trigger('beforeTearDown', {'synchronous': true});
|
2019-06-05 06:57:20 +02:00
|
|
|
|
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-07-29 16:17:06 +02:00
|
|
|
|
async function attemptNonPreboundSession (credentials, automatic) {
|
2019-07-23 10:18:02 +02:00
|
|
|
|
if (_converse.authentication === _converse.LOGIN) {
|
2019-07-29 16:17:06 +02:00
|
|
|
|
// XXX: If EITHER ``keepalive`` or ``auto_login`` is ``true`` and
|
|
|
|
|
// ``authentication`` is set to ``login``, then Converse will try to log the user in,
|
|
|
|
|
// since we don't have a way to distinguish between wether we're
|
|
|
|
|
// restoring a previous session (``keepalive``) or whether we're
|
|
|
|
|
// automatically setting up a new session (``auto_login``).
|
|
|
|
|
// So we can't do the check (!automatic || _converse.auto_login) here.
|
2019-07-23 10:18:02 +02:00
|
|
|
|
if (credentials) {
|
|
|
|
|
connect(credentials);
|
|
|
|
|
} else if (_converse.credentials_url) {
|
2019-07-26 20:03:27 +02:00
|
|
|
|
// We give credentials_url preference, because
|
|
|
|
|
// _converse.connection.pass might be an expired token.
|
2019-07-23 10:18:02 +02:00
|
|
|
|
connect(await getLoginCredentials());
|
2019-07-26 20:03:27 +02:00
|
|
|
|
} else if (_converse.jid && (_converse.password || _converse.connection.pass)) {
|
|
|
|
|
connect();
|
2020-01-12 01:01:38 +01:00
|
|
|
|
} else if (!_converse.isTestEnv() && 'credentials' in navigator) {
|
2019-07-23 10:18:02 +02:00
|
|
|
|
connect(await getLoginCredentialsFromBrowser());
|
|
|
|
|
} else {
|
2019-11-06 11:01:34 +01:00
|
|
|
|
log.warn("attemptNonPreboundSession: Could not find any credentials to log in with");
|
2019-07-23 10:18:02 +02:00
|
|
|
|
}
|
2019-07-29 16:17:06 +02:00
|
|
|
|
} else if ([_converse.ANONYMOUS, _converse.EXTERNAL].includes(_converse.authentication) && (!automatic || _converse.auto_login)) {
|
2019-07-23 11:51:49 +02:00
|
|
|
|
connect();
|
2019-07-23 10:18:02 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function connect (credentials) {
|
2019-07-23 11:51:49 +02:00
|
|
|
|
if ([_converse.ANONYMOUS, _converse.EXTERNAL].includes(_converse.authentication)) {
|
2019-07-23 10:18:02 +02:00
|
|
|
|
if (!_converse.jid) {
|
|
|
|
|
throw new Error("Config Error: when using anonymous login " +
|
|
|
|
|
"you need to provide the server's domain via the 'jid' option. " +
|
|
|
|
|
"Either when calling converse.initialize, or when calling " +
|
|
|
|
|
"_converse.api.user.login.");
|
|
|
|
|
}
|
|
|
|
|
if (!_converse.connection.reconnecting) {
|
|
|
|
|
_converse.connection.reset();
|
|
|
|
|
}
|
|
|
|
|
_converse.connection.connect(
|
|
|
|
|
_converse.jid.toLowerCase(),
|
|
|
|
|
null,
|
|
|
|
|
_converse.onConnectStatusChanged,
|
|
|
|
|
BOSH_WAIT
|
|
|
|
|
);
|
|
|
|
|
} else if (_converse.authentication === _converse.LOGIN) {
|
2020-01-12 01:00:57 +01:00
|
|
|
|
const password = credentials ? credentials.password : (get(_converse.connection, 'pass') || _converse.password);
|
2019-07-23 10:18:02 +02:00
|
|
|
|
if (!password) {
|
|
|
|
|
if (_converse.auto_login) {
|
|
|
|
|
throw new Error("autoLogin: If you use auto_login and "+
|
|
|
|
|
"authentication='login' then you also need to provide a password.");
|
|
|
|
|
}
|
|
|
|
|
_converse.setDisconnectionCause(Strophe.Status.AUTHFAIL, undefined, true);
|
|
|
|
|
_converse.api.connection.disconnect();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (!_converse.connection.reconnecting) {
|
|
|
|
|
_converse.connection.reset();
|
|
|
|
|
}
|
|
|
|
|
_converse.connection.connect(_converse.jid, password, _converse.onConnectStatusChanged, BOSH_WAIT);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2019-10-24 14:29:15 +02:00
|
|
|
|
async function reconnect () {
|
2019-11-06 11:01:34 +01:00
|
|
|
|
log.debug('RECONNECTING: the connection has dropped, attempting to reconnect.');
|
2019-06-01 19:36:23 +02:00
|
|
|
|
_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;
|
2019-10-24 14:29:15 +02:00
|
|
|
|
await tearDown();
|
2019-07-29 10:04:09 +02:00
|
|
|
|
return _converse.api.user.login();
|
2019-06-01 19:36:23 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-01-12 01:00:57 +01:00
|
|
|
|
const debouncedReconnect = debounce(reconnect, 2000);
|
2019-06-01 19:36:23 +02:00
|
|
|
|
|
|
|
|
|
|
2018-11-18 19:14:22 +01:00
|
|
|
|
_converse.shouldClearCache = () => (!_converse.config.get('trusted') || _converse.isTestEnv());
|
2019-08-01 10:26:35 +02:00
|
|
|
|
|
2018-05-29 12:00:23 +02:00
|
|
|
|
function clearSession () {
|
2019-07-29 10:19:05 +02:00
|
|
|
|
if (_converse.session !== undefined) {
|
2019-06-05 06:57:20 +02:00
|
|
|
|
_converse.session.destroy();
|
|
|
|
|
delete _converse.session;
|
2018-05-29 12:00:23 +02:00
|
|
|
|
}
|
|
|
|
|
/**
|
2019-08-01 10:26:35 +02:00
|
|
|
|
* Triggered once the user session has been cleared,
|
2018-05-29 12:00:23 +02:00
|
|
|
|
* for example when the user has logged out or when Converse has
|
|
|
|
|
* disconnected for some other reason.
|
|
|
|
|
* @event _converse#clearSession
|
|
|
|
|
*/
|
|
|
|
|
_converse.api.trigger('clearSession');
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-05 10:01:55 +02:00
|
|
|
|
|
2018-11-18 19:14:22 +01:00
|
|
|
|
async function onDomainDiscovered (response) {
|
|
|
|
|
const text = await response.text();
|
|
|
|
|
const xrd = (new window.DOMParser()).parseFromString(text, "text/xml").firstElementChild;
|
|
|
|
|
if (xrd.nodeName != "XRD" || xrd.namespaceURI != "http://docs.oasis-open.org/ns/xri/xrd-1.0") {
|
2019-11-06 11:01:34 +01:00
|
|
|
|
return log.warn("Could not discover XEP-0156 connection methods");
|
2018-11-18 19:14:22 +01:00
|
|
|
|
}
|
|
|
|
|
const bosh_links = sizzle(`Link[rel="urn:xmpp:alt-connections:xbosh"]`, xrd);
|
|
|
|
|
const ws_links = sizzle(`Link[rel="urn:xmpp:alt-connections:websocket"]`, xrd);
|
|
|
|
|
const bosh_methods = bosh_links.map(el => el.getAttribute('href'));
|
|
|
|
|
const ws_methods = ws_links.map(el => el.getAttribute('href'));
|
|
|
|
|
// TODO: support multiple endpoints
|
|
|
|
|
_converse.websocket_url = ws_methods.pop();
|
|
|
|
|
_converse.bosh_service_url = bosh_methods.pop();
|
|
|
|
|
if (bosh_methods.length === 0 && ws_methods.length === 0) {
|
2019-11-06 11:01:34 +01:00
|
|
|
|
log.warn(
|
|
|
|
|
"onDomainDiscovered: neither BOSH nor WebSocket connection methods have been specified with XEP-0156."
|
2018-11-18 19:14:22 +01:00
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Use XEP-0156 to check whether this host advertises websocket or BOSH connection methods.
|
2019-07-26 16:42:13 +02:00
|
|
|
|
*/
|
2018-11-18 19:14:22 +01:00
|
|
|
|
async function discoverConnectionMethods (domain) {
|
|
|
|
|
const options = {
|
|
|
|
|
'mode': 'cors',
|
|
|
|
|
'headers': {
|
|
|
|
|
'Accept': 'application/xrd+xml, text/xml'
|
2018-11-20 17:31:53 +01:00
|
|
|
|
}
|
2018-11-18 19:14:22 +01:00
|
|
|
|
};
|
|
|
|
|
const url = `https://${domain}/.well-known/host-meta`;
|
|
|
|
|
let response;
|
|
|
|
|
try {
|
|
|
|
|
response = await fetch(url, options);
|
|
|
|
|
} catch (e) {
|
2019-11-06 11:01:34 +01:00
|
|
|
|
log.error(`Failed to discover alternative connection methods at ${url}`);
|
|
|
|
|
log.error(e);
|
|
|
|
|
return;
|
2018-11-18 19:14:22 +01:00
|
|
|
|
}
|
|
|
|
|
if (response.status >= 200 && response.status < 400) {
|
|
|
|
|
await onDomainDiscovered(response);
|
|
|
|
|
} else {
|
2019-11-06 11:01:34 +01:00
|
|
|
|
log.warn("Could not discover XEP-0156 connection methods");
|
2018-11-18 19:14:22 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
_converse.initConnection = async function (domain) {
|
|
|
|
|
if (_converse.discover_connection_methods) {
|
|
|
|
|
await discoverConnectionMethods(domain);
|
|
|
|
|
}
|
|
|
|
|
if (! _converse.bosh_service_url) {
|
|
|
|
|
if (_converse.authentication === _converse.PREBIND) {
|
|
|
|
|
throw new Error("authentication is set to 'prebind' but we don't have a BOSH connection");
|
2018-11-20 17:31:53 +01:00
|
|
|
|
}
|
2018-11-18 19:14:22 +01:00
|
|
|
|
if (! _converse.websocket_url) {
|
|
|
|
|
throw new Error("initConnection: you must supply a value for either the bosh_service_url or websocket_url or both.");
|
2019-07-26 16:42:13 +02:00
|
|
|
|
}
|
2018-11-20 17:31:53 +01:00
|
|
|
|
}
|
2018-11-18 19:14:22 +01:00
|
|
|
|
|
|
|
|
|
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': _converse.keepalive}
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
throw new Error("initConnection: this browser does not support "+
|
|
|
|
|
"websockets and bosh_service_url wasn't specified.");
|
|
|
|
|
}
|
2019-03-14 21:05:39 +01:00
|
|
|
|
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
|
|
|
|
|
*/
|
2019-03-29 21:10:45 +01:00
|
|
|
|
_converse.api.trigger('connectionInitialized');
|
2018-11-18 19:14:22 +01:00
|
|
|
|
}
|
2018-11-15 22:59:55 +01:00
|
|
|
|
|
2018-05-29 12:00:23 +02:00
|
|
|
|
|
2018-11-18 19:14:22 +01:00
|
|
|
|
async function initSession (jid) {
|
2019-06-27 15:12:10 +02:00
|
|
|
|
const bare_jid = Strophe.getBareJidFromJid(jid).toLowerCase();
|
2019-06-27 15:09:06 +02:00
|
|
|
|
const id = `converse.session-${bare_jid}`;
|
2019-06-05 06:57:20 +02:00
|
|
|
|
if (!_converse.session || _converse.session.get('id') !== id) {
|
|
|
|
|
_converse.session = new Backbone.Model({id});
|
2019-10-11 20:29:12 +02:00
|
|
|
|
_converse.session.browserStorage = _converse.createStore(id, "session");
|
2019-06-05 06:57:20 +02:00
|
|
|
|
await new Promise(r => _converse.session.fetch({'success': r, 'error': r}));
|
2019-06-27 15:12:10 +02:00
|
|
|
|
if (_converse.session.get('active')) {
|
|
|
|
|
_converse.session.clear();
|
2019-10-24 14:29:15 +02:00
|
|
|
|
_converse.session.save({id});
|
2019-06-27 15:12:10 +02:00
|
|
|
|
}
|
2019-08-01 08:55:44 +02:00
|
|
|
|
saveJIDtoSession(jid);
|
2019-12-17 13:39:32 +01:00
|
|
|
|
initPersistentStorage();
|
2019-06-05 06:57:20 +02:00
|
|
|
|
/**
|
|
|
|
|
* 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-08-01 08:55:44 +02:00
|
|
|
|
} else {
|
|
|
|
|
saveJIDtoSession(jid);
|
2019-06-05 06:57:20 +02:00
|
|
|
|
}
|
2018-05-29 12:00:23 +02:00
|
|
|
|
}
|
|
|
|
|
|
2018-11-18 19:14:22 +01:00
|
|
|
|
|
2019-08-01 08:55:44 +02:00
|
|
|
|
function saveJIDtoSession (jid) {
|
2019-06-27 15:12:10 +02:00
|
|
|
|
jid = _converse.session.get('jid') || jid;
|
2019-07-22 14:43:30 +02:00
|
|
|
|
if (_converse.authentication !== _converse.ANONYMOUS && !Strophe.getResourceFromJid(jid)) {
|
2019-06-05 06:57:20 +02:00
|
|
|
|
jid = jid.toLowerCase() + _converse.generateResource();
|
|
|
|
|
}
|
|
|
|
|
_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,
|
2019-06-27 15:12:10 +02:00
|
|
|
|
'domain': _converse.domain,
|
|
|
|
|
'active': true
|
2019-06-05 06:57:20 +02:00
|
|
|
|
});
|
2018-11-18 19:14:22 +01:00
|
|
|
|
// 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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Stores the passed in JID for the current user, potentially creating a
|
|
|
|
|
* resource if the JID is bare.
|
|
|
|
|
*
|
|
|
|
|
* Given that we can only create an XMPP connection if we know the domain of
|
|
|
|
|
* the server connect to and we only know this once we know the JID, we also
|
|
|
|
|
* call {@link _converse.initConnection } (if necessary) to make sure that the
|
|
|
|
|
* connection is set up.
|
|
|
|
|
*
|
|
|
|
|
* @method _converse#setUserJID
|
|
|
|
|
* @emits _converse#setUserJID
|
|
|
|
|
* @params { String } jid
|
|
|
|
|
*/
|
|
|
|
|
_converse.setUserJID = async function (jid) {
|
|
|
|
|
if (!_converse.connection || !u.isSameDomain(_converse.connection.jid, jid)) {
|
|
|
|
|
const domain = Strophe.getDomainFromJid(jid)
|
|
|
|
|
await _converse.initConnection(domain);
|
|
|
|
|
}
|
|
|
|
|
await initSession(jid);
|
|
|
|
|
/**
|
|
|
|
|
* Triggered whenever the user's JID has been updated
|
|
|
|
|
* @event _converse#setUserJID
|
|
|
|
|
*/
|
|
|
|
|
_converse.api.trigger('setUserJID');
|
|
|
|
|
return jid;
|
2018-05-29 12:00:23 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2019-10-15 12:55:11 +02:00
|
|
|
|
function enableCarbons () {
|
|
|
|
|
/* Ask the XMPP server to enable Message Carbons
|
|
|
|
|
* See XEP-0280 https://xmpp.org/extensions/xep-0280.html#enabling
|
|
|
|
|
*/
|
|
|
|
|
if (!_converse.message_carbons || !_converse.session || _converse.session.get('carbons_enabled')) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
const carbons_iq = new Strophe.Builder('iq', {
|
|
|
|
|
'from': _converse.connection.jid,
|
|
|
|
|
'id': 'enablecarbons',
|
|
|
|
|
'type': 'set'
|
|
|
|
|
})
|
|
|
|
|
.c('enable', {xmlns: Strophe.NS.CARBONS});
|
|
|
|
|
_converse.connection.addHandler((iq) => {
|
|
|
|
|
if (iq.querySelectorAll('error').length > 0) {
|
2019-11-06 11:01:34 +01:00
|
|
|
|
log.warn('An error occurred while trying to enable message carbons.');
|
2019-10-15 12:55:11 +02:00
|
|
|
|
} else {
|
|
|
|
|
_converse.session.save({'carbons_enabled': true});
|
2019-11-06 11:01:34 +01:00
|
|
|
|
log.debug('Message carbons have been enabled.');
|
2019-10-15 12:55:11 +02:00
|
|
|
|
}
|
|
|
|
|
}, null, "iq", null, "enablecarbons");
|
|
|
|
|
_converse.connection.send(carbons_iq);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2018-05-29 12:00:23 +02:00
|
|
|
|
async function onConnected (reconnecting) {
|
2019-06-05 06:57:20 +02:00
|
|
|
|
/* Called as soon as a new connection has been established, either
|
|
|
|
|
* by logging in or by attaching to an existing BOSH session.
|
|
|
|
|
*/
|
2019-07-26 20:00:59 +02:00
|
|
|
|
delete _converse.connection.reconnecting;
|
2019-06-05 06:57:20 +02:00
|
|
|
|
_converse.connection.flush(); // Solves problem of returned PubSub BOSH response not received by browser
|
2018-11-18 19:14:22 +01:00
|
|
|
|
await _converse.setUserJID(_converse.connection.jid);
|
2020-01-12 01:01:38 +01:00
|
|
|
|
|
2019-06-05 06:57:20 +02:00
|
|
|
|
/**
|
|
|
|
|
* Synchronous event triggered after we've sent an IQ to bind the
|
|
|
|
|
* user's JID resource for this session.
|
|
|
|
|
* @event _converse#afterResourceBinding
|
|
|
|
|
*/
|
2019-10-15 12:55:11 +02:00
|
|
|
|
await _converse.api.trigger('afterResourceBinding', reconnecting, {'synchronous': true});
|
|
|
|
|
enableCarbons();
|
|
|
|
|
|
|
|
|
|
if (reconnecting) {
|
|
|
|
|
/**
|
|
|
|
|
* 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');
|
|
|
|
|
} else {
|
|
|
|
|
/**
|
|
|
|
|
* Triggered once converse.js has been initialized.
|
|
|
|
|
* See also {@link _converse#event:pluginsInitialized}.
|
|
|
|
|
* @event _converse#initialized
|
|
|
|
|
*/
|
|
|
|
|
_converse.api.trigger('initialized');
|
|
|
|
|
/**
|
|
|
|
|
* Triggered after the connection has been established and Converse
|
|
|
|
|
* has got all its ducks in a row.
|
|
|
|
|
* @event _converse#initialized
|
|
|
|
|
*/
|
|
|
|
|
_converse.api.trigger('connected');
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-22 14:13:23 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2018-11-15 22:59:55 +01:00
|
|
|
|
function setUpXMLLogging () {
|
2019-11-22 14:23:19 +01:00
|
|
|
|
const lmap = {}
|
|
|
|
|
lmap[Strophe.LogLevel.DEBUG] = 'debug';
|
|
|
|
|
lmap[Strophe.LogLevel.INFO] = 'info';
|
|
|
|
|
lmap[Strophe.LogLevel.WARN] = 'warn';
|
|
|
|
|
lmap[Strophe.LogLevel.ERROR] = 'error';
|
|
|
|
|
lmap[Strophe.LogLevel.FATAL] = 'fatal';
|
|
|
|
|
|
|
|
|
|
Strophe.log = (level, msg) => log.log(msg, lmap[level]);
|
|
|
|
|
Strophe.error = (msg) => log.error(msg);
|
|
|
|
|
|
|
|
|
|
_converse.connection.xmlInput = body => log.debug(body.outerHTML, 'color: darkgoldenrod');
|
|
|
|
|
_converse.connection.xmlOutput = body => log.debug(body.outerHTML, 'color: darkcyan');
|
2018-11-15 22:59:55 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2019-07-26 16:42:13 +02:00
|
|
|
|
async function finishInitialization () {
|
2019-12-17 13:39:32 +01:00
|
|
|
|
await initSessionStorage();
|
2018-11-15 22:59:55 +01:00
|
|
|
|
initClientConfig();
|
2019-02-21 09:19:02 +01:00
|
|
|
|
initPlugins();
|
2019-12-05 21:02:06 +01:00
|
|
|
|
registerGlobalEventHandlers();
|
2018-11-18 19:14:22 +01:00
|
|
|
|
|
|
|
|
|
if (!Backbone.History.started) {
|
2018-11-15 22:59:55 +01:00
|
|
|
|
Backbone.history.start();
|
|
|
|
|
}
|
|
|
|
|
if (_converse.idle_presence_timeout > 0) {
|
2019-03-29 14:15:03 +01:00
|
|
|
|
_converse.api.listen.on('addClientFeatures', () => {
|
2018-11-15 22:59:55 +01:00
|
|
|
|
_converse.api.disco.own.features.add(Strophe.NS.IDLE);
|
|
|
|
|
});
|
|
|
|
|
}
|
2018-11-18 19:14:22 +01:00
|
|
|
|
if (_converse.auto_login ||
|
2020-01-12 01:00:57 +01:00
|
|
|
|
_converse.keepalive && invoke(_converse.pluggable.plugins['converse-bosh'], 'enabled')) {
|
2018-11-18 19:14:22 +01:00
|
|
|
|
await _converse.api.user.login(null, null, true);
|
|
|
|
|
}
|
2018-11-15 22:59:55 +01:00
|
|
|
|
}
|
|
|
|
|
|
2019-08-01 10:26:35 +02:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Properly tear down the session so that it's possible to manually connect again.
|
|
|
|
|
* @method finishDisconnection
|
|
|
|
|
* @emits _converse#disconnected
|
|
|
|
|
* @private
|
|
|
|
|
*/
|
|
|
|
|
function finishDisconnection () {
|
2019-11-06 11:01:34 +01:00
|
|
|
|
log.debug('DISCONNECTED');
|
2019-08-01 10:26:35 +02:00
|
|
|
|
delete _converse.connection.reconnecting;
|
|
|
|
|
_converse.connection.reset();
|
|
|
|
|
tearDown();
|
|
|
|
|
clearSession();
|
2018-11-18 19:14:22 +01:00
|
|
|
|
delete _converse.connection;
|
2019-08-01 10:26:35 +02:00
|
|
|
|
/**
|
|
|
|
|
* Triggered after converse.js has disconnected from the XMPP server.
|
|
|
|
|
* @event _converse#disconnected
|
|
|
|
|
* @memberOf _converse
|
|
|
|
|
* @example _converse.api.listen.on('disconnected', () => { ... });
|
|
|
|
|
*/
|
|
|
|
|
_converse.api.trigger('disconnected');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2019-04-29 17:29:07 +02:00
|
|
|
|
function fetchLoginCredentials (wait=0) {
|
2019-06-05 06:57:20 +02:00
|
|
|
|
return new Promise(
|
2020-01-12 01:00:57 +01:00
|
|
|
|
debounce((resolve, reject) => {
|
2019-06-05 06:57:20 +02:00
|
|
|
|
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);
|
2018-11-18 19:14:22 +01:00
|
|
|
|
_converse.setUserJID(data.jid).then(() => {
|
2019-07-23 15:26:46 +02:00
|
|
|
|
resolve({
|
|
|
|
|
jid: data.jid,
|
|
|
|
|
password: data.password
|
|
|
|
|
});
|
2019-06-05 06:57:20 +02:00
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
reject(new Error(`${xhr.status}: ${xhr.responseText}`));
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
xhr.onerror = reject;
|
|
|
|
|
xhr.send();
|
|
|
|
|
}, wait)
|
|
|
|
|
);
|
2019-04-29 17:29:07 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function getLoginCredentials () {
|
2019-06-05 06:57:20 +02:00
|
|
|
|
let credentials;
|
|
|
|
|
let wait = 0;
|
|
|
|
|
while (!credentials) {
|
|
|
|
|
try {
|
|
|
|
|
credentials = await fetchLoginCredentials(wait); // eslint-disable-line no-await-in-loop
|
|
|
|
|
} catch (e) {
|
2019-11-06 11:01:34 +01:00
|
|
|
|
log.error('Could not fetch login credentials');
|
|
|
|
|
log.error(e);
|
2019-06-05 06:57:20 +02:00
|
|
|
|
}
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
|
2019-07-23 08:08:25 +02:00
|
|
|
|
async function getLoginCredentialsFromBrowser () {
|
2020-01-12 01:01:38 +01:00
|
|
|
|
try {
|
|
|
|
|
const creds = await navigator.credentials.get({'password': true});
|
|
|
|
|
if (creds && creds.type == 'password' && u.isValidJID(creds.id)) {
|
|
|
|
|
await _converse.setUserJID(creds.id);
|
|
|
|
|
return {'jid': creds.id, 'password': creds.password};
|
|
|
|
|
}
|
|
|
|
|
} catch (e) {
|
|
|
|
|
log.error(e);
|
2019-07-23 08:08:25 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-05 22:17:27 +01:00
|
|
|
|
_converse.saveWindowState = function (ev) {
|
|
|
|
|
// 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"
|
|
|
|
|
};
|
|
|
|
|
ev = ev || document.createEvent('Events');
|
|
|
|
|
if (ev.type in event_map) {
|
|
|
|
|
state = event_map[ev.type];
|
|
|
|
|
} else {
|
|
|
|
|
state = document.hidden ? "hidden" : "visible";
|
|
|
|
|
}
|
|
|
|
|
_converse.windowState = state;
|
|
|
|
|
/**
|
|
|
|
|
* 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-11-15 22:59:55 +01:00
|
|
|
|
|
2019-12-05 21:02:06 +01:00
|
|
|
|
function registerGlobalEventHandlers () {
|
|
|
|
|
document.addEventListener("visibilitychange", _converse.saveWindowState);
|
|
|
|
|
_converse.saveWindowState({'type': document.hidden ? "blur" : "focus"}); // Set initial state
|
|
|
|
|
/**
|
|
|
|
|
* 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-12-15 19:52:45 +01:00
|
|
|
|
function unregisterGlobalEventHandlers () {
|
|
|
|
|
document.removeEventListener("visibilitychange", _converse.saveWindowState);
|
2019-03-29 21:10:45 +01:00
|
|
|
|
_converse.api.trigger('unregisteredGlobalEventHandlers');
|
2018-12-15 19:52:45 +01:00
|
|
|
|
}
|
|
|
|
|
|
2019-12-17 13:39:32 +01:00
|
|
|
|
|
|
|
|
|
function cleanup () {
|
2019-10-24 14:29:15 +02:00
|
|
|
|
// Make sure everything is reset in case this is a subsequent call to
|
|
|
|
|
// convesre.initialize (happens during tests).
|
2019-06-05 06:57:20 +02:00
|
|
|
|
Backbone.history.stop();
|
|
|
|
|
unregisterGlobalEventHandlers();
|
|
|
|
|
delete _converse.controlboxtoggle;
|
|
|
|
|
if (_converse.chatboxviews) {
|
|
|
|
|
delete _converse.chatboxviews;
|
|
|
|
|
}
|
2018-11-18 19:14:22 +01:00
|
|
|
|
if (_converse.connection) {
|
|
|
|
|
_converse.connection.reset();
|
|
|
|
|
}
|
2019-06-05 06:57:20 +02:00
|
|
|
|
_converse.stopListening();
|
|
|
|
|
_converse.off();
|
2018-11-15 22:59:55 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2019-02-12 14:21:45 +01:00
|
|
|
|
_converse.initialize = async function (settings, callback) {
|
2019-12-17 13:39:32 +01:00
|
|
|
|
cleanup();
|
2018-11-18 19:14:22 +01:00
|
|
|
|
|
2019-07-29 10:19:05 +02:00
|
|
|
|
settings = settings !== undefined ? settings : {};
|
2019-12-18 16:09:44 +01:00
|
|
|
|
PROMISES.forEach(name => _converse.api.promises.add(name));
|
2017-09-26 18:27:41 +02:00
|
|
|
|
|
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';
|
2017-07-11 15:22:05 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-01-12 01:00:57 +01:00
|
|
|
|
assignIn(this, this.default_settings);
|
2018-10-23 03:41:38 +02:00
|
|
|
|
// Allow only whitelisted configuration attributes to be overwritten
|
2020-01-12 01:00:57 +01:00
|
|
|
|
assignIn(this, pick(settings, Object.keys(this.default_settings)));
|
2019-07-26 18:50:19 +02:00
|
|
|
|
this.settings = {};
|
2020-01-12 01:00:57 +01:00
|
|
|
|
assignIn(this.settings, pick(settings, Object.keys(this.default_settings)));
|
2018-10-23 03:41:38 +02:00
|
|
|
|
|
2019-11-22 14:23:19 +01:00
|
|
|
|
log.setLogLevel(_converse.loglevel);
|
|
|
|
|
_converse.log = log.log;
|
|
|
|
|
|
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.");
|
2017-07-11 15:22:05 +02:00
|
|
|
|
}
|
2018-10-23 03:41:38 +02:00
|
|
|
|
}
|
2017-07-11 15:22:05 +02:00
|
|
|
|
|
2019-11-22 14:23:19 +01:00
|
|
|
|
_converse.router.route(
|
|
|
|
|
/^converse\?loglevel=(debug|info|warn|error|fatal)$/, 'loglevel',
|
|
|
|
|
l => log.setLogLevel(l)
|
|
|
|
|
);
|
2019-02-27 16:02:55 +01:00
|
|
|
|
|
2018-10-23 03:41:38 +02:00
|
|
|
|
/* Localisation */
|
2019-08-23 14:03:52 +02:00
|
|
|
|
if (i18n === undefined || _converse.isTestEnv()) {
|
2018-10-23 03:41:38 +02:00
|
|
|
|
_converse.locale = 'en';
|
2019-08-23 14:03:52 +02:00
|
|
|
|
} else {
|
|
|
|
|
try {
|
|
|
|
|
_converse.locale = i18n.getLocale(settings.i18n, _converse.locales);
|
|
|
|
|
await i18n.fetchTranslations(_converse);
|
|
|
|
|
} catch (e) {
|
2019-11-06 11:01:34 +01:00
|
|
|
|
log.fatal(e.message);
|
2019-08-23 14:03:52 +02:00
|
|
|
|
}
|
2018-07-02 23:29:57 +02:00
|
|
|
|
}
|
|
|
|
|
|
2018-10-23 03:41:38 +02:00
|
|
|
|
// Module-level variables
|
|
|
|
|
// ----------------------
|
2019-08-05 01:39:57 +02:00
|
|
|
|
this.callback = callback || function noop () {};
|
2018-10-23 03:41:38 +02: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.
|
2019-06-05 10:01:55 +02:00
|
|
|
|
* 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.user_settings = settings; // Save the user settings so that they can be used by plugins
|
2017-10-31 21:47:27 +01:00
|
|
|
|
|
2018-10-23 03:41:38 +02:00
|
|
|
|
// Module-level functions
|
|
|
|
|
// ----------------------
|
2017-10-31 21:47:27 +01:00
|
|
|
|
|
2018-10-23 03:41:38 +02:00
|
|
|
|
this.generateResource = () => `/converse.js-${Math.floor(Math.random()*139749528).toString()}`;
|
2016-12-06 12:07:52 +01:00
|
|
|
|
|
2018-10-23 03:41:38 +02:00
|
|
|
|
this.setConnectionStatus = function (connection_status, message) {
|
|
|
|
|
_converse.connfeedback.set({
|
|
|
|
|
'connection_status': connection_status,
|
|
|
|
|
'message': message
|
|
|
|
|
});
|
|
|
|
|
};
|
2016-02-16 08:46:47 +01:00
|
|
|
|
|
2019-05-14 10:31:41 +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) {
|
2019-07-26 18:50:19 +02:00
|
|
|
|
if (_converse.auto_reconnect && (_converse.credentials_url || _converse.authentication === _converse.ANONYMOUS)) {
|
|
|
|
|
/**
|
|
|
|
|
* If `credentials_url` is set, we reconnect, because we might
|
|
|
|
|
* be receiving expirable tokens from the credentials_url.
|
|
|
|
|
*
|
|
|
|
|
* If `authentication` is anonymous, we reconnect because we
|
|
|
|
|
* might have tried to attach with stale BOSH session tokens
|
|
|
|
|
* or with a cached JID and password
|
2018-10-23 03:41:38 +02:00
|
|
|
|
*/
|
2019-06-01 19:36:23 +02:00
|
|
|
|
return _converse.api.connection.reconnect();
|
2018-10-23 03:41:38 +02:00
|
|
|
|
} else {
|
2019-08-01 10:26:35 +02:00
|
|
|
|
return finishDisconnection();
|
2016-08-18 21:43:18 +02:00
|
|
|
|
}
|
2018-10-23 03:41:38 +02:00
|
|
|
|
} else if (_converse.disconnection_cause === _converse.LOGOUT ||
|
2020-01-12 01:00:57 +01:00
|
|
|
|
(reason !== undefined && reason === get(Strophe, 'ErrorCondition.NO_AUTH_MECH')) ||
|
2018-10-23 03:41:38 +02:00
|
|
|
|
reason === "host-unknown" ||
|
|
|
|
|
reason === "remote-connection-failed" ||
|
|
|
|
|
!_converse.auto_reconnect) {
|
2019-08-01 10:26:35 +02:00
|
|
|
|
return 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
|
|
|
|
};
|
2016-08-18 21:43:18 +02:00
|
|
|
|
|
2019-05-14 10:31:41 +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)
|
|
|
|
|
*/
|
2019-07-29 10:19:05 +02:00
|
|
|
|
if (cause === undefined) {
|
2018-10-23 03:41:38 +02:00
|
|
|
|
delete _converse.disconnection_cause;
|
|
|
|
|
delete _converse.disconnection_reason;
|
2019-07-29 10:19:05 +02:00
|
|
|
|
} else if (_converse.disconnection_cause === undefined || override) {
|
2018-10-23 03:41:38 +02:00
|
|
|
|
_converse.disconnection_cause = cause;
|
|
|
|
|
_converse.disconnection_reason = reason;
|
|
|
|
|
}
|
|
|
|
|
};
|
2016-03-31 14:23:45 +02:00
|
|
|
|
|
2019-10-15 12:55:11 +02:00
|
|
|
|
/**
|
|
|
|
|
* Callback method called by Strophe as the Strophe.Connection goes
|
|
|
|
|
* through various states while establishing or tearing down a
|
|
|
|
|
* connection.
|
|
|
|
|
* @method _converse#onConnectStatusChanged
|
|
|
|
|
* @private
|
|
|
|
|
* @memberOf _converse
|
|
|
|
|
*/
|
2018-10-23 03:41:38 +02:00
|
|
|
|
this.onConnectStatusChanged = function (status, message) {
|
2019-11-06 11:01:34 +01:00
|
|
|
|
log.debug(`Status changed to: ${_converse.CONNECTION_STATUS[status]}`);
|
2018-10-23 03:41:38 +02:00
|
|
|
|
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) {
|
2019-11-06 11:01:34 +01:00
|
|
|
|
log.debug(status === Strophe.Status.CONNECTED ? 'Reconnected' : 'Reattached');
|
2018-05-29 12:00:23 +02:00
|
|
|
|
onConnected(true);
|
2018-10-23 03:41:38 +02:00
|
|
|
|
} else {
|
2019-11-06 11:01:34 +01:00
|
|
|
|
log.debug(status === Strophe.Status.CONNECTED ? 'Connected' : 'Attached');
|
2018-10-23 03:41:38 +02:00
|
|
|
|
if (_converse.connection.restored) {
|
|
|
|
|
// No need to send an initial presence stanza when
|
|
|
|
|
// we're restoring an existing session.
|
|
|
|
|
_converse.send_initial_presence = false;
|
2017-09-08 14:24:38 +02:00
|
|
|
|
}
|
2018-05-29 12:00:23 +02:00
|
|
|
|
onConnected();
|
2016-02-16 08:46:47 +01:00
|
|
|
|
}
|
2018-10-23 03:41:38 +02:00
|
|
|
|
} else if (status === Strophe.Status.DISCONNECTED) {
|
|
|
|
|
_converse.setDisconnectionCause(status, message);
|
|
|
|
|
_converse.onDisconnected();
|
2019-05-30 09:45:41 +02:00
|
|
|
|
} 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-02-09 16:02:56 +01:00
|
|
|
|
}
|
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)}\"`);
|
2020-01-12 01:00:57 +01:00
|
|
|
|
} else if (message !== undefined && message === get(Strophe, 'ErrorCondition.NO_AUTH_MECH')) {
|
2018-10-23 03:41:38 +02:00
|
|
|
|
feedback = __("The XMPP server did not offer a supported authentication mechanism");
|
2017-05-03 09:06:28 +02:00
|
|
|
|
}
|
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);
|
|
|
|
|
}
|
|
|
|
|
};
|
2016-02-16 08:46:47 +01:00
|
|
|
|
|
2019-05-30 09:45:41 +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-02-07 17:30:44 +01:00
|
|
|
|
|
2018-10-23 03:41:38 +02:00
|
|
|
|
initialize () {
|
2019-03-29 21:10:45 +01:00
|
|
|
|
this.on('change', () => _converse.api.trigger('connfeedback', _converse.connfeedback));
|
2018-10-23 03:41:38 +02:00
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
this.connfeedback = new this.ConnectionFeedback();
|
2018-05-08 19:24:50 +02:00
|
|
|
|
|
2018-10-23 03:41:38 +02:00
|
|
|
|
// Initialization
|
|
|
|
|
// --------------
|
2019-08-23 14:03:52 +02:00
|
|
|
|
await finishInitialization();
|
2019-06-14 11:28:31 +02:00
|
|
|
|
if (_converse.isTestEnv()) {
|
2018-10-23 03:41:38 +02:00
|
|
|
|
return _converse;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* ### 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-08 01:10:01 +02:00
|
|
|
|
/**
|
2018-10-23 03:41:38 +02:00
|
|
|
|
* This grouping collects API functions related to the XMPP connection.
|
2018-09-02 12:15:26 +02:00
|
|
|
|
*
|
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 () {
|
2020-01-12 01:00:57 +01:00
|
|
|
|
return get(_converse, 'connection', {}).connected && true;
|
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.
|
|
|
|
|
*
|
2019-12-05 22:17:27 +01:00
|
|
|
|
* @method _converse.api.connection.disconnectkjjjkk
|
2018-10-23 03:41:38 +02:00
|
|
|
|
* @memberOf _converse.api.connection
|
|
|
|
|
*/
|
2019-06-01 19:36:23 +02:00
|
|
|
|
disconnect () {
|
2019-04-22 14:04:21 +02:00
|
|
|
|
if (_converse.connection) {
|
2019-06-05 06:57:20 +02:00
|
|
|
|
_converse.connection.disconnect();
|
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
|
2019-06-04 15:10:03 +02:00
|
|
|
|
* 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
|
|
|
|
|
*/
|
2019-06-14 11:54:19 +02:00
|
|
|
|
async reconnect () {
|
2019-06-04 15:10:03 +02:00
|
|
|
|
const conn_status = _converse.connfeedback.get('connection_status');
|
2019-07-26 20:00:59 +02:00
|
|
|
|
|
|
|
|
|
if (_converse.authentication === _converse.ANONYMOUS) {
|
2019-10-24 14:29:15 +02:00
|
|
|
|
await tearDown();
|
2019-07-26 20:00:59 +02:00
|
|
|
|
clearSession();
|
|
|
|
|
}
|
2019-06-04 15:10:03 +02:00
|
|
|
|
if (conn_status === Strophe.Status.CONNFAIL) {
|
2019-06-14 11:54:19 +02:00
|
|
|
|
// 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.
|
2019-06-05 06:57:20 +02:00
|
|
|
|
if (_converse.api.connection.isType('websocket') && _converse.bosh_service_url) {
|
2018-11-18 19:14:22 +01:00
|
|
|
|
await _converse.setUserJID(_converse.bare_jid);
|
2019-06-14 11:54:19 +02:00
|
|
|
|
_converse.connection._proto._doDisconnect();
|
2019-06-05 06:57:20 +02:00
|
|
|
|
_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) {
|
2019-07-26 18:50:19 +02:00
|
|
|
|
if (_converse.authentication === _converse.ANONYMOUS) {
|
|
|
|
|
// When reconnecting anonymously, we need to connect with only
|
|
|
|
|
// the domain, not the full JID that we had in our previous
|
|
|
|
|
// (now failed) session.
|
2018-11-18 19:14:22 +01:00
|
|
|
|
await _converse.setUserJID(_converse.settings.jid);
|
2019-07-26 18:50:19 +02:00
|
|
|
|
} else {
|
2018-11-18 19:14:22 +01:00
|
|
|
|
await _converse.setUserJID(_converse.bare_jid);
|
2019-07-26 18:50:19 +02:00
|
|
|
|
}
|
2019-06-14 11:54:19 +02:00
|
|
|
|
_converse.connection._proto._doDisconnect();
|
2019-06-05 06:57:20 +02:00
|
|
|
|
_converse.connection._proto = new Strophe.Websocket(_converse.connection);
|
|
|
|
|
_converse.connection.service = _converse.websocket_url;
|
|
|
|
|
}
|
2019-06-04 15:10:03 +02:00
|
|
|
|
}
|
2019-07-26 18:50:19 +02:00
|
|
|
|
if (conn_status === Strophe.Status.AUTHFAIL && _converse.authentication === _converse.ANONYMOUS) {
|
|
|
|
|
// When reconnecting anonymously, we need to connect with only
|
|
|
|
|
// the domain, not the full JID that we had in our previous
|
|
|
|
|
// (now failed) session.
|
2018-11-18 19:14:22 +01:00
|
|
|
|
await _converse.setUserJID(_converse.settings.jid);
|
2019-07-26 18:50:19 +02:00
|
|
|
|
}
|
2019-07-26 20:00:59 +02:00
|
|
|
|
if (_converse.connection.reconnecting) {
|
2019-06-05 06:57:20 +02:00
|
|
|
|
debouncedReconnect();
|
2019-06-01 19:36:23 +02:00
|
|
|
|
} else {
|
2019-07-26 20:00:59 +02:00
|
|
|
|
return reconnect();
|
2019-06-01 19:36:23 +02:00
|
|
|
|
}
|
2019-06-04 15:10:03 +02:00
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Utility method to determine the type of connection we have
|
|
|
|
|
* @method isType
|
|
|
|
|
* @memberOf _converse.api.connection
|
|
|
|
|
* @returns {boolean}
|
|
|
|
|
*/
|
|
|
|
|
isType (type) {
|
2019-06-05 06:57:20 +02:00
|
|
|
|
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
|
|
|
|
},
|
|
|
|
|
|
2019-03-29 21:10:45 +01: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)).
|
2018-10-08 01:10:01 +02:00
|
|
|
|
*
|
2019-08-13 12:03:37 +02:00
|
|
|
|
* Some events also double as promises and can be waited on via {@link _converse.api.waitUntil}.
|
|
|
|
|
*
|
2019-03-29 21:10:45 +01:00
|
|
|
|
* @method _converse.api.trigger
|
2019-05-30 09:42:18 +02:00
|
|
|
|
* @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.
|
2019-08-13 12:03:37 +02:00
|
|
|
|
* When a synchronous event is fired, a promise will be returned
|
|
|
|
|
* by {@link _converse.api.trigger} which resolves once all the
|
|
|
|
|
* event handlers' promises have been resolved.
|
2018-10-23 03:41:38 +02:00
|
|
|
|
*/
|
2019-05-30 09:42:18 +02:00
|
|
|
|
async trigger (name) {
|
2019-06-05 06:57:20 +02:00
|
|
|
|
const args = Array.from(arguments);
|
|
|
|
|
const options = args.pop();
|
|
|
|
|
if (options && options.synchronous) {
|
2019-05-30 09:42:18 +02:00
|
|
|
|
const events = _converse._events[name] || [];
|
2019-10-12 00:17:18 +02:00
|
|
|
|
await Promise.all(events.map(e => e.callback.apply(e.ctx, args.splice(1))));
|
2019-06-05 06:57:20 +02:00
|
|
|
|
} else {
|
2019-05-30 09:42:18 +02:00
|
|
|
|
_converse.trigger.apply(_converse, arguments);
|
2019-06-05 06:57:20 +02:00
|
|
|
|
}
|
|
|
|
|
const promise = _converse.promises[name];
|
2019-07-29 10:19:05 +02:00
|
|
|
|
if (promise !== undefined) {
|
2019-03-29 21:10:45 +01:00
|
|
|
|
promise.resolve();
|
2019-06-05 06:57:20 +02:00
|
|
|
|
}
|
2018-10-23 03:41:38 +02:00
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* This grouping collects API functions related to the current logged in user.
|
2018-09-02 12:15:26 +02:00
|
|
|
|
*
|
2018-10-23 03:41:38 +02:00
|
|
|
|
* @namespace _converse.api.user
|
|
|
|
|
* @memberOf _converse.api
|
2018-09-02 12:15:26 +02:00
|
|
|
|
*/
|
2019-06-05 06:57:20 +02:00
|
|
|
|
user: {
|
2018-09-02 12:15:26 +02:00
|
|
|
|
/**
|
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())
|
|
|
|
|
*/
|
2019-06-03 11:36:55 +02:00
|
|
|
|
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.
|
2018-09-02 12:15:26 +02:00
|
|
|
|
*
|
2018-10-23 03:41:38 +02:00
|
|
|
|
* @method _converse.api.user.login
|
2018-05-29 12:00:23 +02:00
|
|
|
|
* @param {string} [jid]
|
|
|
|
|
* @param {string} [password]
|
2019-07-29 16:17:06 +02:00
|
|
|
|
* @param {boolean} [automatic=false] - An internally used flag that indicates whether
|
|
|
|
|
* this method was called automatically once the connection has been
|
|
|
|
|
* initialized. It's used together with the `auto_login` configuration flag
|
|
|
|
|
* to determine whether Converse should try to log the user in if it
|
|
|
|
|
* fails to restore a previous auth'd session.
|
2018-09-02 12:15:26 +02:00
|
|
|
|
*/
|
2019-07-29 16:17:06 +02:00
|
|
|
|
async login (jid, password, automatic=false) {
|
2018-11-18 19:14:22 +01:00
|
|
|
|
if (jid || _converse.jid) {
|
|
|
|
|
jid = await _converse.setUserJID(jid || _converse.jid);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// See whether there is a BOSH session to re-attach to
|
2019-12-03 11:31:28 +01:00
|
|
|
|
const bosh_plugin = _converse.pluggable.plugins['converse-bosh'];
|
|
|
|
|
if (bosh_plugin && bosh_plugin.enabled()) {
|
2019-07-29 10:04:09 +02:00
|
|
|
|
if (await _converse.restoreBOSHSession()) {
|
2019-06-05 10:01:55 +02:00
|
|
|
|
return;
|
2019-07-29 16:17:06 +02:00
|
|
|
|
} else if (_converse.authentication === _converse.PREBIND && (!automatic || _converse.auto_login)) {
|
2019-07-29 10:08:38 +02:00
|
|
|
|
return _converse.startNewPreboundBOSHSession();
|
2019-06-05 06:57:20 +02:00
|
|
|
|
}
|
2018-05-29 12:00:23 +02:00
|
|
|
|
}
|
2019-07-23 11:51:49 +02:00
|
|
|
|
|
2019-06-26 13:48:10 +02:00
|
|
|
|
password = password || _converse.password;
|
|
|
|
|
const credentials = (jid && password) ? { jid, password } : null;
|
2019-07-29 16:17:06 +02:00
|
|
|
|
attemptNonPreboundSession(credentials, automatic);
|
2017-02-14 15:08:39 +01:00
|
|
|
|
},
|
2018-05-29 12:00:23 +02:00
|
|
|
|
|
2018-09-02 12:15:26 +02:00
|
|
|
|
/**
|
2018-10-23 03:41:38 +02:00
|
|
|
|
* Logs the user out of the current XMPP session.
|
|
|
|
|
* @method _converse.api.user.logout
|
|
|
|
|
* @example _converse.api.user.logout();
|
2018-09-02 12:15:26 +02:00
|
|
|
|
*/
|
2018-05-29 12:00:23 +02:00
|
|
|
|
logout () {
|
2019-10-11 12:06:13 +02:00
|
|
|
|
const promise = u.getResolveablePromise();
|
|
|
|
|
const complete = () => {
|
|
|
|
|
// Recreate all the promises
|
2019-12-18 16:09:44 +01:00
|
|
|
|
Object.keys(_converse.promises).forEach(replacePromise);
|
2020-01-09 11:05:37 +01:00
|
|
|
|
delete _converse.jid
|
2019-10-11 12:06:13 +02:00
|
|
|
|
/**
|
|
|
|
|
* Triggered once the user has logged out.
|
|
|
|
|
* @event _converse#logout
|
|
|
|
|
*/
|
|
|
|
|
_converse.api.trigger('logout');
|
|
|
|
|
promise.resolve();
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-29 12:00:23 +02:00
|
|
|
|
_converse.setDisconnectionCause(_converse.LOGOUT, undefined, true);
|
2019-07-29 10:19:05 +02:00
|
|
|
|
if (_converse.connection !== undefined) {
|
2019-10-11 12:06:13 +02:00
|
|
|
|
_converse.api.listen.once('disconnected', () => complete());
|
2019-06-05 06:57:20 +02:00
|
|
|
|
_converse.connection.disconnect();
|
2019-10-11 12:06:13 +02:00
|
|
|
|
} else {
|
|
|
|
|
complete();
|
2018-05-29 12:00:23 +02:00
|
|
|
|
}
|
2019-10-11 12:06:13 +02:00
|
|
|
|
return promise;
|
2019-10-24 14:29:15 +02:00
|
|
|
|
}
|
2018-10-23 03:41:38 +02:00
|
|
|
|
},
|
2018-09-02 12:15:26 +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
|
|
|
|
|
*/
|
2019-06-05 06:57:20 +02:00
|
|
|
|
settings: {
|
2018-09-02 12:15:26 +02:00
|
|
|
|
/**
|
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-09-02 12:15:26 +02:00
|
|
|
|
*
|
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
|
|
|
|
|
* });
|
2018-09-02 12:15:26 +02:00
|
|
|
|
*/
|
2019-06-05 06:57:20 +02:00
|
|
|
|
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");
|
|
|
|
|
*/
|
2019-06-05 06:57:20 +02:00
|
|
|
|
get (key) {
|
2020-01-12 01:00:57 +01:00
|
|
|
|
if (Object.keys(_converse.default_settings).includes(key)) {
|
2018-10-23 03:41:38 +02:00
|
|
|
|
return _converse[key];
|
2017-02-14 15:08:39 +01:00
|
|
|
|
}
|
|
|
|
|
},
|
2018-09-02 12:15:26 +02:00
|
|
|
|
/**
|
2018-10-23 03:41:38 +02:00
|
|
|
|
* Set one or many configuration settings.
|
2018-10-08 01:10:01 +02:00
|
|
|
|
*
|
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-08 01:10:01 +02:00
|
|
|
|
*
|
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
|
|
|
|
|
* });
|
|
|
|
|
*/
|
2019-06-05 06:57:20 +02:00
|
|
|
|
set (key, val) {
|
2018-10-23 03:41:38 +02:00
|
|
|
|
const o = {};
|
2020-01-12 01:00:57 +01:00
|
|
|
|
if (isObject(key)) {
|
|
|
|
|
assignIn(_converse, pick(key, Object.keys(_converse.default_settings)));
|
|
|
|
|
} else if (isString('string')) {
|
2018-10-23 03:41:38 +02:00
|
|
|
|
o[key] = val;
|
2020-01-12 01:00:57 +01:00
|
|
|
|
assignIn(_converse, pick(o, Object.keys(_converse.default_settings)));
|
2018-10-23 03:41:38 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/**
|
2019-03-29 21:10:45 +01: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
|
|
|
|
|
*/
|
2019-06-05 06:57:20 +02:00
|
|
|
|
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-08 01:10:01 +02:00
|
|
|
|
*
|
2018-10-23 03:41:38 +02:00
|
|
|
|
* Generally, it's the responsibility of the plugin which adds the promise to
|
|
|
|
|
* also resolve it.
|
2018-10-08 01:10:01 +02:00
|
|
|
|
*
|
2019-03-29 21:10:45 +01:00
|
|
|
|
* 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-09-02 12:15:26 +02:00
|
|
|
|
*
|
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
|
2019-12-18 16:09:44 +01:00
|
|
|
|
* @param {boolean} [replace=true] Whether this promise should be replaced with a new one when the user logs out.
|
2018-10-23 03:41:38 +02:00
|
|
|
|
* @example _converse.api.promises.add('foo-completed');
|
2018-09-02 12:15:26 +02:00
|
|
|
|
*/
|
2019-12-18 16:09:44 +01:00
|
|
|
|
add (promises, replace=true) {
|
2019-06-05 06:57:20 +02:00
|
|
|
|
promises = Array.isArray(promises) ? promises : [promises];
|
2019-12-18 16:09:44 +01:00
|
|
|
|
promises.forEach(name => {
|
|
|
|
|
const promise = u.getResolveablePromise();
|
|
|
|
|
promise.replace = replace;
|
|
|
|
|
_converse.promises[name] = promise;
|
|
|
|
|
});
|
2018-10-23 03:41:38 +02:00
|
|
|
|
}
|
|
|
|
|
},
|
2018-09-02 12:15:26 +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
|
|
|
|
|
*/
|
2019-06-05 06:57:20 +02:00
|
|
|
|
listen: {
|
2018-09-02 12:15:26 +02:00
|
|
|
|
/**
|
2018-10-23 03:41:38 +02:00
|
|
|
|
* Lets you listen to an event exactly once.
|
2018-10-08 01:10:01 +02:00
|
|
|
|
*
|
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) { ... });
|
2018-09-02 12:15:26 +02:00
|
|
|
|
*/
|
2019-06-05 06:57:20 +02:00
|
|
|
|
once: _converse.once.bind(_converse),
|
2018-09-02 12:15:26 +02:00
|
|
|
|
|
|
|
|
|
/**
|
2018-10-23 03:41:38 +02:00
|
|
|
|
* Lets you subscribe to an event.
|
2018-09-02 12:15:26 +02:00
|
|
|
|
*
|
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) { ... });
|
2018-09-02 12:15:26 +02:00
|
|
|
|
*/
|
2019-06-05 06:57:20 +02:00
|
|
|
|
on: _converse.on.bind(_converse),
|
2018-09-02 15:07:14 +02:00
|
|
|
|
|
2018-09-02 12:15:26 +02:00
|
|
|
|
/**
|
2018-10-23 03:41:38 +02:00
|
|
|
|
* To stop listening to an event, you can use the `not` method.
|
2018-10-08 01:10:01 +02:00
|
|
|
|
*
|
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);
|
2018-09-02 12:15:26 +02:00
|
|
|
|
*/
|
2019-06-05 06:57:20 +02:00
|
|
|
|
not: _converse.off.bind(_converse),
|
2018-09-02 15:07:14 +02:00
|
|
|
|
|
2018-09-02 12:15:26 +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
|
2018-09-02 12:15:26 +02:00
|
|
|
|
*/
|
2019-06-05 06:57:20 +02:00
|
|
|
|
stanza (name, options, handler) {
|
2020-01-12 01:00:57 +01:00
|
|
|
|
if (isFunction(options)) {
|
2018-10-23 03:41:38 +02:00
|
|
|
|
handler = options;
|
|
|
|
|
options = {};
|
|
|
|
|
} else {
|
|
|
|
|
options = options || {};
|
|
|
|
|
}
|
|
|
|
|
_converse.connection.addHandler(
|
|
|
|
|
handler,
|
|
|
|
|
options.ns,
|
|
|
|
|
name,
|
|
|
|
|
options.type,
|
|
|
|
|
options.id,
|
|
|
|
|
options.from,
|
|
|
|
|
options
|
|
|
|
|
);
|
2019-06-05 06:57:20 +02:00
|
|
|
|
}
|
2018-10-23 03:41:38 +02:00
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/**
|
2019-07-11 22:50:30 +02:00
|
|
|
|
* Wait until a promise is resolved or until the passed in function returns
|
|
|
|
|
* a truthy value.
|
2018-10-23 03:41:38 +02:00
|
|
|
|
* @method _converse.api.waitUntil
|
2019-07-11 22:50:30 +02:00
|
|
|
|
* @param {string|function} condition - The name of the promise to wait for,
|
|
|
|
|
* or a function which should eventually return a truthy value.
|
2018-10-23 03:41:38 +02:00
|
|
|
|
* @returns {Promise}
|
|
|
|
|
*/
|
2019-07-11 22:50:30 +02:00
|
|
|
|
waitUntil (condition) {
|
2020-01-12 01:00:57 +01:00
|
|
|
|
if (isFunction(condition)) {
|
2019-07-11 22:50:30 +02:00
|
|
|
|
return u.waitUntil(condition);
|
|
|
|
|
} else {
|
|
|
|
|
const promise = _converse.promises[condition];
|
2019-07-29 10:19:05 +02:00
|
|
|
|
if (promise === undefined) {
|
2019-07-11 22:50:30 +02:00
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
return promise;
|
2018-06-06 11:07:59 +02:00
|
|
|
|
}
|
2018-10-23 03:41:38 +02:00
|
|
|
|
},
|
2017-02-14 15:08:39 +01:00
|
|
|
|
|
2018-09-02 10:11:37 +02:00
|
|
|
|
/**
|
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);
|
|
|
|
|
*/
|
2018-05-29 12:00:23 +02:00
|
|
|
|
send (stanza) {
|
2018-11-18 19:14:22 +01:00
|
|
|
|
if (!_converse.api.connection.connected()) {
|
2019-11-06 11:01:34 +01:00
|
|
|
|
log.warn("Not sending stanza because we're not connected!");
|
|
|
|
|
log.warn(Strophe.serialize(stanza));
|
2018-11-18 19:14:22 +01:00
|
|
|
|
return;
|
|
|
|
|
}
|
2020-01-12 01:00:57 +01:00
|
|
|
|
if (isString(stanza)) {
|
2018-05-29 12:00:23 +02:00
|
|
|
|
stanza = u.toStanza(stanza);
|
|
|
|
|
}
|
|
|
|
|
if (stanza.tagName === 'iq') {
|
2019-06-05 06:57:20 +02:00
|
|
|
|
return _converse.api.sendIQ(stanza);
|
2018-05-29 12:00:23 +02:00
|
|
|
|
} else {
|
2019-06-05 06:57:20 +02:00
|
|
|
|
_converse.connection.send(stanza);
|
|
|
|
|
_converse.api.trigger('send', stanza);
|
2018-05-29 12:00:23 +02:00
|
|
|
|
}
|
2018-10-23 03:41:38 +02:00
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Send an IQ stanza and receive a promise
|
|
|
|
|
* @method _converse.api.sendIQ
|
2019-07-29 14:32:57 +02:00
|
|
|
|
* @param { XMLElement } stanza
|
|
|
|
|
* @param { Integer } timeout
|
|
|
|
|
* @param { Boolean } reject - Whether an error IQ should cause the promise
|
|
|
|
|
* to be rejected. If `false`, the promise will resolve instead of being rejected.
|
2018-10-23 03:41:38 +02:00
|
|
|
|
* @returns {Promise} A promise which resolves when we receive a `result` stanza
|
|
|
|
|
* or is rejected when we receive an `error` stanza.
|
2018-09-02 10:11:37 +02:00
|
|
|
|
*/
|
2019-07-29 14:32:57 +02:00
|
|
|
|
sendIQ (stanza, timeout, reject=true) {
|
2019-09-24 15:33:41 +02:00
|
|
|
|
timeout = timeout || _converse.STANZA_TIMEOUT;
|
2019-07-29 14:32:57 +02:00
|
|
|
|
let promise;
|
|
|
|
|
if (reject) {
|
|
|
|
|
promise = new Promise((resolve, reject) => _converse.connection.sendIQ(stanza, resolve, reject, timeout));
|
|
|
|
|
} else {
|
2019-10-08 16:47:30 +02:00
|
|
|
|
promise = new Promise(resolve => _converse.connection.sendIQ(stanza, resolve, resolve, timeout));
|
2019-07-29 14:32:57 +02:00
|
|
|
|
}
|
|
|
|
|
_converse.api.trigger('send', stanza);
|
|
|
|
|
return promise;
|
2018-10-23 03:41:38 +02:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2019-10-08 16:47:30 +02:00
|
|
|
|
|
|
|
|
|
window.converse = window.converse || {};
|
|
|
|
|
|
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 don’t expose any sensitive
|
|
|
|
|
* or closured data. To do that, you’ll need to create a plugin, which has
|
|
|
|
|
* access to the private API method.
|
|
|
|
|
*
|
2019-07-20 21:55:42 +02:00
|
|
|
|
* @global
|
2018-10-23 03:41:38 +02:00
|
|
|
|
* @namespace converse
|
|
|
|
|
*/
|
2019-10-06 12:48:05 +02:00
|
|
|
|
Object.assign(window.converse, {
|
2019-09-11 15:23:58 +02:00
|
|
|
|
keycodes: {
|
|
|
|
|
TAB: 9,
|
|
|
|
|
ENTER: 13,
|
|
|
|
|
SHIFT: 16,
|
|
|
|
|
CTRL: 17,
|
|
|
|
|
ALT: 18,
|
|
|
|
|
ESCAPE: 27,
|
|
|
|
|
LEFT_ARROW: 37,
|
|
|
|
|
UP_ARROW: 38,
|
|
|
|
|
RIGHT_ARROW: 39,
|
|
|
|
|
DOWN_ARROW: 40,
|
|
|
|
|
FORWARD_SLASH: 47,
|
|
|
|
|
AT: 50,
|
|
|
|
|
META: 91,
|
|
|
|
|
META_RIGHT: 93
|
|
|
|
|
},
|
|
|
|
|
|
2018-10-23 03:41:38 +02:00
|
|
|
|
/**
|
|
|
|
|
* 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,
|
2019-08-07 10:28:49 +02:00
|
|
|
|
* i18n: 'en',
|
2018-10-23 03:41:38 +02:00
|
|
|
|
* play_sounds: true,
|
|
|
|
|
* show_controlbox_by_default: true,
|
|
|
|
|
* debug: false,
|
|
|
|
|
* roster_groups: true
|
|
|
|
|
* });
|
|
|
|
|
*/
|
2019-06-05 06:57:20 +02:00
|
|
|
|
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
|
|
|
|
|
*/
|
2019-06-05 06:57:20 +02:00
|
|
|
|
plugins: {
|
2019-08-16 15:44:58 +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
|
2018-09-02 10:11:37 +02:00
|
|
|
|
* @example
|
2018-10-23 03:41:38 +02:00
|
|
|
|
* const plugin = {
|
|
|
|
|
* initialize: function () {
|
|
|
|
|
* // Gets called as soon as the plugin has been loaded.
|
2018-09-02 10:11:37 +02:00
|
|
|
|
*
|
2018-10-23 03:41:38 +02:00
|
|
|
|
* // Inside this method, you have access to the private
|
|
|
|
|
* // API via `_covnerse.api`.
|
2018-09-02 10:11:37 +02:00
|
|
|
|
*
|
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);
|
2018-09-02 10:11:37 +02:00
|
|
|
|
*/
|
2019-06-05 06:57:20 +02:00
|
|
|
|
add (name, plugin) {
|
2018-10-23 03:41:38 +02:00
|
|
|
|
plugin.__name__ = name;
|
2019-07-29 10:19:05 +02:00
|
|
|
|
if (_converse.pluggable.plugins[name] !== undefined) {
|
2018-10-23 03:41:38 +02:00
|
|
|
|
throw new TypeError(
|
2019-06-05 06:57:20 +02:00
|
|
|
|
`Error: plugin with name "${name}" has already been ` + 'registered!'
|
|
|
|
|
);
|
2018-10-23 03:41:38 +02:00
|
|
|
|
} else {
|
|
|
|
|
_converse.pluggable.plugins[name] = plugin;
|
|
|
|
|
}
|
2017-02-14 15:08:39 +01:00
|
|
|
|
}
|
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.
|
|
|
|
|
*/
|
2019-12-17 13:39:32 +01:00
|
|
|
|
'env': { $build, $iq, $msg, $pres, Backbone, BrowserStorage, Promise, Strophe, _, dayjs, log, sizzle, stanza_utils, u, 'utils': u }
|
2019-10-06 12:48:05 +02:00
|
|
|
|
});
|
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;
|