UNPKG

strophe.js

Version:

Strophe.js is an XMPP library for JavaScript

1,483 lines (1,406 loc) 113 kB
/* This program is distributed under the terms of the MIT license. Please see the LICENSE file for details. Copyright 2006-2018, OGG, LLC */ /*global define, document, sessionStorage, setTimeout, clearTimeout, ActiveXObject, DOMParser, btoa, atob */ import * as shims from './shims'; import MD5 from './md5'; import SASLAnonymous from './sasl-anon.js'; import SASLExternal from './sasl-external.js'; import SASLMechanism from './sasl.js'; import SASLOAuthBearer from './sasl-oauthbearer.js'; import SASLPlain from './sasl-plain.js'; import SASLSHA1 from './sasl-sha1.js'; import SASLXOAuth2 from './sasl-xoauth2.js'; import SHA1 from './sha1'; import utils from './utils'; import { atob, btoa } from 'abab' /** Function: $build * Create a Strophe.Builder. * This is an alias for 'new Strophe.Builder(name, attrs)'. * * Parameters: * (String) name - The root element name. * (Object) attrs - The attributes for the root element in object notation. * * Returns: * A new Strophe.Builder object. */ export function $build(name, attrs) { return new Strophe.Builder(name, attrs); } /** Function: $msg * Create a Strophe.Builder with a <message/> element as the root. * * Parameters: * (Object) attrs - The <message/> element attributes in object notation. * * Returns: * A new Strophe.Builder object. */ export function $msg(attrs) { return new Strophe.Builder("message", attrs); } /** Function: $iq * Create a Strophe.Builder with an <iq/> element as the root. * * Parameters: * (Object) attrs - The <iq/> element attributes in object notation. * * Returns: * A new Strophe.Builder object. */ export function $iq(attrs) { return new Strophe.Builder("iq", attrs); } /** Function: $pres * Create a Strophe.Builder with a <presence/> element as the root. * * Parameters: * (Object) attrs - The <presence/> element attributes in object notation. * * Returns: * A new Strophe.Builder object. */ export function $pres(attrs) { return new Strophe.Builder("presence", attrs); } /** Class: Strophe * An object container for all Strophe library functions. * * This class is just a container for all the objects and constants * used in the library. It is not meant to be instantiated, but to * provide a namespace for library objects, constants, and functions. */ export const Strophe = { /** Constant: VERSION */ VERSION: "1.4.2", /** Constants: XMPP Namespace Constants * Common namespace constants from the XMPP RFCs and XEPs. * * NS.HTTPBIND - HTTP BIND namespace from XEP 124. * NS.BOSH - BOSH namespace from XEP 206. * NS.CLIENT - Main XMPP client namespace. * NS.AUTH - Legacy authentication namespace. * NS.ROSTER - Roster operations namespace. * NS.PROFILE - Profile namespace. * NS.DISCO_INFO - Service discovery info namespace from XEP 30. * NS.DISCO_ITEMS - Service discovery items namespace from XEP 30. * NS.MUC - Multi-User Chat namespace from XEP 45. * NS.SASL - XMPP SASL namespace from RFC 3920. * NS.STREAM - XMPP Streams namespace from RFC 3920. * NS.BIND - XMPP Binding namespace from RFC 3920 and RFC 6120. * NS.SESSION - XMPP Session namespace from RFC 3920. * NS.XHTML_IM - XHTML-IM namespace from XEP 71. * NS.XHTML - XHTML body namespace from XEP 71. */ NS: { HTTPBIND: "http://jabber.org/protocol/httpbind", BOSH: "urn:xmpp:xbosh", CLIENT: "jabber:client", AUTH: "jabber:iq:auth", ROSTER: "jabber:iq:roster", PROFILE: "jabber:iq:profile", DISCO_INFO: "http://jabber.org/protocol/disco#info", DISCO_ITEMS: "http://jabber.org/protocol/disco#items", MUC: "http://jabber.org/protocol/muc", SASL: "urn:ietf:params:xml:ns:xmpp-sasl", STREAM: "http://etherx.jabber.org/streams", FRAMING: "urn:ietf:params:xml:ns:xmpp-framing", BIND: "urn:ietf:params:xml:ns:xmpp-bind", SESSION: "urn:ietf:params:xml:ns:xmpp-session", VERSION: "jabber:iq:version", STANZAS: "urn:ietf:params:xml:ns:xmpp-stanzas", XHTML_IM: "http://jabber.org/protocol/xhtml-im", XHTML: "http://www.w3.org/1999/xhtml" }, /** Constants: XHTML_IM Namespace * contains allowed tags, tag attributes, and css properties. * Used in the createHtml function to filter incoming html into the allowed XHTML-IM subset. * See http://xmpp.org/extensions/xep-0071.html#profile-summary for the list of recommended * allowed tags and their attributes. */ XHTML: { tags: ['a','blockquote','br','cite','em','img','li','ol','p','span','strong','ul','body'], attributes: { 'a': ['href'], 'blockquote': ['style'], 'br': [], 'cite': ['style'], 'em': [], 'img': ['src', 'alt', 'style', 'height', 'width'], 'li': ['style'], 'ol': ['style'], 'p': ['style'], 'span': ['style'], 'strong': [], 'ul': ['style'], 'body': [] }, css: ['background-color','color','font-family','font-size','font-style','font-weight','margin-left','margin-right','text-align','text-decoration'], /** Function: XHTML.validTag * * Utility method to determine whether a tag is allowed * in the XHTML_IM namespace. * * XHTML tag names are case sensitive and must be lower case. */ validTag (tag) { for (let i=0; i<Strophe.XHTML.tags.length; i++) { if (tag === Strophe.XHTML.tags[i]) { return true; } } return false; }, /** Function: XHTML.validAttribute * * Utility method to determine whether an attribute is allowed * as recommended per XEP-0071 * * XHTML attribute names are case sensitive and must be lower case. */ validAttribute (tag, attribute) { if (typeof Strophe.XHTML.attributes[tag] !== 'undefined' && Strophe.XHTML.attributes[tag].length > 0) { for (let i=0; i<Strophe.XHTML.attributes[tag].length; i++) { if (attribute === Strophe.XHTML.attributes[tag][i]) { return true; } } } return false; }, validCSS (style) { for (let i=0; i<Strophe.XHTML.css.length; i++) { if (style === Strophe.XHTML.css[i]) { return true; } } return false; } }, /** Constants: Connection Status Constants * Connection status constants for use by the connection handler * callback. * * Status.ERROR - An error has occurred * Status.CONNECTING - The connection is currently being made * Status.CONNFAIL - The connection attempt failed * Status.AUTHENTICATING - The connection is authenticating * Status.AUTHFAIL - The authentication attempt failed * Status.CONNECTED - The connection has succeeded * Status.DISCONNECTED - The connection has been terminated * Status.DISCONNECTING - The connection is currently being terminated * Status.ATTACHED - The connection has been attached * Status.REDIRECT - The connection has been redirected * Status.CONNTIMEOUT - The connection has timed out */ Status: { ERROR: 0, CONNECTING: 1, CONNFAIL: 2, AUTHENTICATING: 3, AUTHFAIL: 4, CONNECTED: 5, DISCONNECTED: 6, DISCONNECTING: 7, ATTACHED: 8, REDIRECT: 9, CONNTIMEOUT: 10, BINDREQUIRED: 11, ATTACHFAIL: 12 }, ErrorCondition: { BAD_FORMAT: "bad-format", CONFLICT: "conflict", MISSING_JID_NODE: "x-strophe-bad-non-anon-jid", NO_AUTH_MECH: "no-auth-mech", UNKNOWN_REASON: "unknown", }, /** Constants: Log Level Constants * Logging level indicators. * * LogLevel.DEBUG - Debug output * LogLevel.INFO - Informational output * LogLevel.WARN - Warnings * LogLevel.ERROR - Errors * LogLevel.FATAL - Fatal errors */ LogLevel: { DEBUG: 0, INFO: 1, WARN: 2, ERROR: 3, FATAL: 4 }, /** PrivateConstants: DOM Element Type Constants * DOM element types. * * ElementType.NORMAL - Normal element. * ElementType.TEXT - Text data element. * ElementType.FRAGMENT - XHTML fragment element. */ ElementType: { NORMAL: 1, TEXT: 3, CDATA: 4, FRAGMENT: 11 }, /** PrivateConstants: Timeout Values * Timeout values for error states. These values are in seconds. * These should not be changed unless you know exactly what you are * doing. * * TIMEOUT - Timeout multiplier. A waiting request will be considered * failed after Math.floor(TIMEOUT * wait) seconds have elapsed. * This defaults to 1.1, and with default wait, 66 seconds. * SECONDARY_TIMEOUT - Secondary timeout multiplier. In cases where * Strophe can detect early failure, it will consider the request * failed if it doesn't return after * Math.floor(SECONDARY_TIMEOUT * wait) seconds have elapsed. * This defaults to 0.1, and with default wait, 6 seconds. */ TIMEOUT: 1.1, SECONDARY_TIMEOUT: 0.1, /** Function: addNamespace * This function is used to extend the current namespaces in * Strophe.NS. It takes a key and a value with the key being the * name of the new namespace, with its actual value. * For example: * Strophe.addNamespace('PUBSUB', "http://jabber.org/protocol/pubsub"); * * Parameters: * (String) name - The name under which the namespace will be * referenced under Strophe.NS * (String) value - The actual namespace. */ addNamespace (name, value) { Strophe.NS[name] = value; }, /** Function: forEachChild * Map a function over some or all child elements of a given element. * * This is a small convenience function for mapping a function over * some or all of the children of an element. If elemName is null, all * children will be passed to the function, otherwise only children * whose tag names match elemName will be passed. * * Parameters: * (XMLElement) elem - The element to operate on. * (String) elemName - The child element tag name filter. * (Function) func - The function to apply to each child. This * function should take a single argument, a DOM element. */ forEachChild (elem, elemName, func) { for (let i=0; i<elem.childNodes.length; i++) { const childNode = elem.childNodes[i]; if (childNode.nodeType === Strophe.ElementType.NORMAL && (!elemName || this.isTagEqual(childNode, elemName))) { func(childNode); } } }, /** Function: isTagEqual * Compare an element's tag name with a string. * * This function is case sensitive. * * Parameters: * (XMLElement) el - A DOM element. * (String) name - The element name. * * Returns: * true if the element's tag name matches _el_, and false * otherwise. */ isTagEqual (el, name) { return el.tagName === name; }, /** PrivateVariable: _xmlGenerator * _Private_ variable that caches a DOM document to * generate elements. */ _xmlGenerator: null, /** Function: xmlGenerator * Get the DOM document to generate elements. * * Returns: * The currently used DOM document. */ xmlGenerator () { if (!Strophe._xmlGenerator) { Strophe._xmlGenerator = shims.getDummyXMLDOMDocument() } return Strophe._xmlGenerator; }, /** Function: xmlElement * Create an XML DOM element. * * This function creates an XML DOM element correctly across all * implementations. Note that these are not HTML DOM elements, which * aren't appropriate for XMPP stanzas. * * Parameters: * (String) name - The name for the element. * (Array|Object) attrs - An optional array or object containing * key/value pairs to use as element attributes. The object should * be in the format {'key': 'value'} or {key: 'value'}. The array * should have the format [['key1', 'value1'], ['key2', 'value2']]. * (String) text - The text child data for the element. * * Returns: * A new XML DOM element. */ xmlElement (name) { if (!name) { return null; } const node = Strophe.xmlGenerator().createElement(name); // FIXME: this should throw errors if args are the wrong type or // there are more than two optional args for (let a=1; a<arguments.length; a++) { const arg = arguments[a]; if (!arg) { continue; } if (typeof(arg) === "string" || typeof(arg) === "number") { node.appendChild(Strophe.xmlTextNode(arg)); } else if (typeof(arg) === "object" && typeof(arg.sort) === "function") { for (let i=0; i<arg.length; i++) { const attr = arg[i]; if (typeof(attr) === "object" && typeof(attr.sort) === "function" && attr[1] !== undefined && attr[1] !== null) { node.setAttribute(attr[0], attr[1]); } } } else if (typeof(arg) === "object") { for (const k in arg) { if (Object.prototype.hasOwnProperty.call(arg, k) && arg[k] !== undefined && arg[k] !== null) { node.setAttribute(k, arg[k]); } } } } return node; }, /* Function: xmlescape * Excapes invalid xml characters. * * Parameters: * (String) text - text to escape. * * Returns: * Escaped text. */ xmlescape (text) { text = text.replace(/\&/g, "&amp;"); text = text.replace(/</g, "&lt;"); text = text.replace(/>/g, "&gt;"); text = text.replace(/'/g, "&apos;"); text = text.replace(/"/g, "&quot;"); return text; }, /* Function: xmlunescape * Unexcapes invalid xml characters. * * Parameters: * (String) text - text to unescape. * * Returns: * Unescaped text. */ xmlunescape (text) { text = text.replace(/\&amp;/g, "&"); text = text.replace(/&lt;/g, "<"); text = text.replace(/&gt;/g, ">"); text = text.replace(/&apos;/g, "'"); text = text.replace(/&quot;/g, "\""); return text; }, /** Function: xmlTextNode * Creates an XML DOM text node. * * Provides a cross implementation version of document.createTextNode. * * Parameters: * (String) text - The content of the text node. * * Returns: * A new XML DOM text node. */ xmlTextNode (text) { return Strophe.xmlGenerator().createTextNode(text); }, /** Function: xmlHtmlNode * Creates an XML DOM html node. * * Parameters: * (String) html - The content of the html node. * * Returns: * A new XML DOM text node. */ xmlHtmlNode (html) { let node; //ensure text is escaped if (shims.DOMParser) { const parser = new shims.DOMParser(); node = parser.parseFromString(html, "text/xml"); } else { node = new ActiveXObject("Microsoft.XMLDOM"); node.async="false"; node.loadXML(html); } return node; }, /** Function: getText * Get the concatenation of all text children of an element. * * Parameters: * (XMLElement) elem - A DOM element. * * Returns: * A String with the concatenated text of all text element children. */ getText (elem) { if (!elem) { return null; } let str = ""; if (elem.childNodes.length === 0 && elem.nodeType === Strophe.ElementType.TEXT) { str += elem.nodeValue; } for (let i=0; i<elem.childNodes.length; i++) { if (elem.childNodes[i].nodeType === Strophe.ElementType.TEXT) { str += elem.childNodes[i].nodeValue; } } return Strophe.xmlescape(str); }, /** Function: copyElement * Copy an XML DOM element. * * This function copies a DOM element and all its descendants and returns * the new copy. * * Parameters: * (XMLElement) elem - A DOM element. * * Returns: * A new, copied DOM element tree. */ copyElement (elem) { let el; if (elem.nodeType === Strophe.ElementType.NORMAL) { el = Strophe.xmlElement(elem.tagName); for (let i=0; i<elem.attributes.length; i++) { el.setAttribute(elem.attributes[i].nodeName, elem.attributes[i].value); } for (let i=0; i<elem.childNodes.length; i++) { el.appendChild(Strophe.copyElement(elem.childNodes[i])); } } else if (elem.nodeType === Strophe.ElementType.TEXT) { el = Strophe.xmlGenerator().createTextNode(elem.nodeValue); } return el; }, /** Function: createHtml * Copy an HTML DOM element into an XML DOM. * * This function copies a DOM element and all its descendants and returns * the new copy. * * Parameters: * (HTMLElement) elem - A DOM element. * * Returns: * A new, copied DOM element tree. */ createHtml (elem) { let el; if (elem.nodeType === Strophe.ElementType.NORMAL) { const tag = elem.nodeName.toLowerCase(); // XHTML tags must be lower case. if (Strophe.XHTML.validTag(tag)) { try { el = Strophe.xmlElement(tag); for (let i=0; i < Strophe.XHTML.attributes[tag].length; i++) { const attribute = Strophe.XHTML.attributes[tag][i]; let value = elem.getAttribute(attribute); if (typeof value === 'undefined' || value === null || value === '' || value === false || value === 0) { continue; } if (attribute === 'style' && typeof value === 'object' && typeof value.cssText !== 'undefined') { value = value.cssText; // we're dealing with IE, need to get CSS out } // filter out invalid css styles if (attribute === 'style') { const css = []; const cssAttrs = value.split(';'); for (let j=0; j < cssAttrs.length; j++) { const attr = cssAttrs[j].split(':'); const cssName = attr[0].replace(/^\s*/, "").replace(/\s*$/, "").toLowerCase(); if(Strophe.XHTML.validCSS(cssName)) { const cssValue = attr[1].replace(/^\s*/, "").replace(/\s*$/, ""); css.push(cssName + ': ' + cssValue); } } if (css.length > 0) { value = css.join('; '); el.setAttribute(attribute, value); } } else { el.setAttribute(attribute, value); } } for (let i=0; i < elem.childNodes.length; i++) { el.appendChild(Strophe.createHtml(elem.childNodes[i])); } } catch(e) { // invalid elements el = Strophe.xmlTextNode(''); } } else { el = Strophe.xmlGenerator().createDocumentFragment(); for (let i=0; i < elem.childNodes.length; i++) { el.appendChild(Strophe.createHtml(elem.childNodes[i])); } } } else if (elem.nodeType === Strophe.ElementType.FRAGMENT) { el = Strophe.xmlGenerator().createDocumentFragment(); for (let i=0; i < elem.childNodes.length; i++) { el.appendChild(Strophe.createHtml(elem.childNodes[i])); } } else if (elem.nodeType === Strophe.ElementType.TEXT) { el = Strophe.xmlTextNode(elem.nodeValue); } return el; }, /** Function: escapeNode * Escape the node part (also called local part) of a JID. * * Parameters: * (String) node - A node (or local part). * * Returns: * An escaped node (or local part). */ escapeNode (node) { if (typeof node !== "string") { return node; } return node.replace(/^\s+|\s+$/g, '') .replace(/\\/g, "\\5c") .replace(/ /g, "\\20") .replace(/\"/g, "\\22") .replace(/\&/g, "\\26") .replace(/\'/g, "\\27") .replace(/\//g, "\\2f") .replace(/:/g, "\\3a") .replace(/</g, "\\3c") .replace(/>/g, "\\3e") .replace(/@/g, "\\40"); }, /** Function: unescapeNode * Unescape a node part (also called local part) of a JID. * * Parameters: * (String) node - A node (or local part). * * Returns: * An unescaped node (or local part). */ unescapeNode (node) { if (typeof node !== "string") { return node; } return node.replace(/\\20/g, " ") .replace(/\\22/g, '"') .replace(/\\26/g, "&") .replace(/\\27/g, "'") .replace(/\\2f/g, "/") .replace(/\\3a/g, ":") .replace(/\\3c/g, "<") .replace(/\\3e/g, ">") .replace(/\\40/g, "@") .replace(/\\5c/g, "\\"); }, /** Function: getNodeFromJid * Get the node portion of a JID String. * * Parameters: * (String) jid - A JID. * * Returns: * A String containing the node. */ getNodeFromJid (jid) { if (jid.indexOf("@") < 0) { return null; } return jid.split("@")[0]; }, /** Function: getDomainFromJid * Get the domain portion of a JID String. * * Parameters: * (String) jid - A JID. * * Returns: * A String containing the domain. */ getDomainFromJid (jid) { const bare = Strophe.getBareJidFromJid(jid); if (bare.indexOf("@") < 0) { return bare; } else { const parts = bare.split("@"); parts.splice(0, 1); return parts.join('@'); } }, /** Function: getResourceFromJid * Get the resource portion of a JID String. * * Parameters: * (String) jid - A JID. * * Returns: * A String containing the resource. */ getResourceFromJid (jid) { if (!jid) { return null; } const s = jid.split("/"); if (s.length < 2) { return null; } s.splice(0, 1); return s.join('/'); }, /** Function: getBareJidFromJid * Get the bare JID from a JID String. * * Parameters: * (String) jid - A JID. * * Returns: * A String containing the bare JID. */ getBareJidFromJid (jid) { return jid ? jid.split("/")[0] : null; }, /** PrivateFunction: _handleError * _Private_ function that properly logs an error to the console */ _handleError (e) { if (typeof e.stack !== "undefined") { Strophe.fatal(e.stack); } if (e.sourceURL) { Strophe.fatal("error: " + this.handler + " " + e.sourceURL + ":" + e.line + " - " + e.name + ": " + e.message); } else if (e.fileName) { Strophe.fatal("error: " + this.handler + " " + e.fileName + ":" + e.lineNumber + " - " + e.name + ": " + e.message); } else { Strophe.fatal("error: " + e.message); } }, /** Function: log * User overrideable logging function. * * This function is called whenever the Strophe library calls any * of the logging functions. The default implementation of this * function logs only fatal errors. If client code wishes to handle the logging * messages, it should override this with * > Strophe.log = function (level, msg) { * > (user code here) * > }; * * Please note that data sent and received over the wire is logged * via Strophe.Connection.rawInput() and Strophe.Connection.rawOutput(). * * The different levels and their meanings are * * DEBUG - Messages useful for debugging purposes. * INFO - Informational messages. This is mostly information like * 'disconnect was called' or 'SASL auth succeeded'. * WARN - Warnings about potential problems. This is mostly used * to report transient connection errors like request timeouts. * ERROR - Some error occurred. * FATAL - A non-recoverable fatal error occurred. * * Parameters: * (Integer) level - The log level of the log message. This will * be one of the values in Strophe.LogLevel. * (String) msg - The log message. */ log (level, msg) { if (level === this.LogLevel.FATAL) { console?.error(msg); } }, /** Function: debug * Log a message at the Strophe.LogLevel.DEBUG level. * * Parameters: * (String) msg - The log message. */ debug (msg) { this.log(this.LogLevel.DEBUG, msg); }, /** Function: info * Log a message at the Strophe.LogLevel.INFO level. * * Parameters: * (String) msg - The log message. */ info (msg) { this.log(this.LogLevel.INFO, msg); }, /** Function: warn * Log a message at the Strophe.LogLevel.WARN level. * * Parameters: * (String) msg - The log message. */ warn (msg) { this.log(this.LogLevel.WARN, msg); }, /** Function: error * Log a message at the Strophe.LogLevel.ERROR level. * * Parameters: * (String) msg - The log message. */ error (msg) { this.log(this.LogLevel.ERROR, msg); }, /** Function: fatal * Log a message at the Strophe.LogLevel.FATAL level. * * Parameters: * (String) msg - The log message. */ fatal (msg) { this.log(this.LogLevel.FATAL, msg); }, /** Function: serialize * Render a DOM element and all descendants to a String. * * Parameters: * (XMLElement) elem - A DOM element. * * Returns: * The serialized element tree as a String. */ serialize (elem) { if (!elem) { return null; } if (typeof(elem.tree) === "function") { elem = elem.tree(); } const names = [...Array(elem.attributes.length).keys()].map(i => elem.attributes[i].nodeName); names.sort(); let result = names.reduce( (a, n) => `${a} ${n}="${Strophe.xmlescape(elem.attributes.getNamedItem(n).value)}"`, `<${elem.nodeName}` ); if (elem.childNodes.length > 0) { result += ">"; for (let i=0; i < elem.childNodes.length; i++) { const child = elem.childNodes[i]; switch (child.nodeType) { case Strophe.ElementType.NORMAL: // normal element, so recurse result += Strophe.serialize(child); break; case Strophe.ElementType.TEXT: // text element to escape values result += Strophe.xmlescape(child.nodeValue); break; case Strophe.ElementType.CDATA: // cdata section so don't escape values result += "<![CDATA["+child.nodeValue+"]]>"; } } result += "</" + elem.nodeName + ">"; } else { result += "/>"; } return result; }, /** PrivateVariable: _requestId * _Private_ variable that keeps track of the request ids for * connections. */ _requestId: 0, /** PrivateVariable: Strophe.connectionPlugins * _Private_ variable Used to store plugin names that need * initialization on Strophe.Connection construction. */ _connectionPlugins: {}, /** Function: addConnectionPlugin * Extends the Strophe.Connection object with the given plugin. * * Parameters: * (String) name - The name of the extension. * (Object) ptype - The plugin's prototype. */ addConnectionPlugin (name, ptype) { Strophe._connectionPlugins[name] = ptype; } }; /** Class: Strophe.Builder * XML DOM builder. * * This object provides an interface similar to JQuery but for building * DOM elements easily and rapidly. All the functions except for toString() * and tree() return the object, so calls can be chained. Here's an * example using the $iq() builder helper. * > $iq({to: 'you', from: 'me', type: 'get', id: '1'}) * > .c('query', {xmlns: 'strophe:example'}) * > .c('example') * > .toString() * * The above generates this XML fragment * > <iq to='you' from='me' type='get' id='1'> * > <query xmlns='strophe:example'> * > <example/> * > </query> * > </iq> * The corresponding DOM manipulations to get a similar fragment would be * a lot more tedious and probably involve several helper variables. * * Since adding children makes new operations operate on the child, up() * is provided to traverse up the tree. To add two children, do * > builder.c('child1', ...).up().c('child2', ...) * The next operation on the Builder will be relative to the second child. */ /** Constructor: Strophe.Builder * Create a Strophe.Builder object. * * The attributes should be passed in object notation. For example * > let b = new Builder('message', {to: 'you', from: 'me'}); * or * > let b = new Builder('messsage', {'xml:lang': 'en'}); * * Parameters: * (String) name - The name of the root element. * (Object) attrs - The attributes for the root element in object notation. * * Returns: * A new Strophe.Builder. */ Strophe.Builder = class Builder { constructor (name, attrs) { // Set correct namespace for jabber:client elements if (name === "presence" || name === "message" || name === "iq") { if (attrs && !attrs.xmlns) { attrs.xmlns = Strophe.NS.CLIENT; } else if (!attrs) { attrs = {xmlns: Strophe.NS.CLIENT}; } } // Holds the tree being built. this.nodeTree = Strophe.xmlElement(name, attrs); // Points to the current operation node. this.node = this.nodeTree; } /** Function: tree * Return the DOM tree. * * This function returns the current DOM tree as an element object. This * is suitable for passing to functions like Strophe.Connection.send(). * * Returns: * The DOM tree as a element object. */ tree () { return this.nodeTree; } /** Function: toString * Serialize the DOM tree to a String. * * This function returns a string serialization of the current DOM * tree. It is often used internally to pass data to a * Strophe.Request object. * * Returns: * The serialized DOM tree in a String. */ toString () { return Strophe.serialize(this.nodeTree); } /** Function: up * Make the current parent element the new current element. * * This function is often used after c() to traverse back up the tree. * For example, to add two children to the same element * > builder.c('child1', {}).up().c('child2', {}); * * Returns: * The Stophe.Builder object. */ up () { this.node = this.node.parentNode; return this; } /** Function: root * Make the root element the new current element. * * When at a deeply nested element in the tree, this function can be used * to jump back to the root of the tree, instead of having to repeatedly * call up(). * * Returns: * The Stophe.Builder object. */ root () { this.node = this.nodeTree; return this; } /** Function: attrs * Add or modify attributes of the current element. * * The attributes should be passed in object notation. This function * does not move the current element pointer. * * Parameters: * (Object) moreattrs - The attributes to add/modify in object notation. * * Returns: * The Strophe.Builder object. */ attrs (moreattrs) { for (const k in moreattrs) { if (Object.prototype.hasOwnProperty.call(moreattrs, k)) { if (moreattrs[k] === undefined) { this.node.removeAttribute(k); } else { this.node.setAttribute(k, moreattrs[k]); } } } return this; } /** Function: c * Add a child to the current element and make it the new current * element. * * This function moves the current element pointer to the child, * unless text is provided. If you need to add another child, it * is necessary to use up() to go back to the parent in the tree. * * Parameters: * (String) name - The name of the child. * (Object) attrs - The attributes of the child in object notation. * (String) text - The text to add to the child. * * Returns: * The Strophe.Builder object. */ c (name, attrs, text) { const child = Strophe.xmlElement(name, attrs, text); this.node.appendChild(child); if (typeof text !== "string" && typeof text !=="number") { this.node = child; } return this; } /** Function: cnode * Add a child to the current element and make it the new current * element. * * This function is the same as c() except that instead of using a * name and an attributes object to create the child it uses an * existing DOM element object. * * Parameters: * (XMLElement) elem - A DOM element. * * Returns: * The Strophe.Builder object. */ cnode (elem) { let impNode; const xmlGen = Strophe.xmlGenerator(); try { impNode = (xmlGen.importNode !== undefined); } catch (e) { impNode = false; } const newElem = impNode ? xmlGen.importNode(elem, true) : Strophe.copyElement(elem); this.node.appendChild(newElem); this.node = newElem; return this; } /** Function: t * Add a child text element. * * This *does not* make the child the new current element since there * are no children of text elements. * * Parameters: * (String) text - The text data to append to the current element. * * Returns: * The Strophe.Builder object. */ t (text) { const child = Strophe.xmlTextNode(text); this.node.appendChild(child); return this; } /** Function: h * Replace current element contents with the HTML passed in. * * This *does not* make the child the new current element * * Parameters: * (String) html - The html to insert as contents of current element. * * Returns: * The Strophe.Builder object. */ h (html) { const fragment = Strophe.xmlGenerator().createElement('body'); // force the browser to try and fix any invalid HTML tags fragment.innerHTML = html; // copy cleaned html into an xml dom const xhtml = Strophe.createHtml(fragment); while (xhtml.childNodes.length > 0) { this.node.appendChild(xhtml.childNodes[0]); } return this; } }; /** PrivateClass: Strophe.Handler * _Private_ helper class for managing stanza handlers. * * A Strophe.Handler encapsulates a user provided callback function to be * executed when matching stanzas are received by the connection. * Handlers can be either one-off or persistant depending on their * return value. Returning true will cause a Handler to remain active, and * returning false will remove the Handler. * * Users will not use Strophe.Handler objects directly, but instead they * will use Strophe.Connection.addHandler() and * Strophe.Connection.deleteHandler(). */ /** PrivateConstructor: Strophe.Handler * Create and initialize a new Strophe.Handler. * * Parameters: * (Function) handler - A function to be executed when the handler is run. * (String) ns - The namespace to match. * (String) name - The element name to match. * (String) type - The element type to match. * (String) id - The element id attribute to match. * (String) from - The element from attribute to match. * (Object) options - Handler options * * Returns: * A new Strophe.Handler object. */ Strophe.Handler = function (handler, ns, name, type, id, from, options) { this.handler = handler; this.ns = ns; this.name = name; this.type = type; this.id = id; this.options = options || {'matchBareFromJid': false, 'ignoreNamespaceFragment': false}; // BBB: Maintain backward compatibility with old `matchBare` option if (this.options.matchBare) { Strophe.warn('The "matchBare" option is deprecated, use "matchBareFromJid" instead.'); this.options.matchBareFromJid = this.options.matchBare; delete this.options.matchBare; } if (this.options.matchBareFromJid) { this.from = from ? Strophe.getBareJidFromJid(from) : null; } else { this.from = from; } // whether the handler is a user handler or a system handler this.user = true; }; Strophe.Handler.prototype = { /** PrivateFunction: getNamespace * Returns the XML namespace attribute on an element. * If `ignoreNamespaceFragment` was passed in for this handler, then the * URL fragment will be stripped. * * Parameters: * (XMLElement) elem - The XML element with the namespace. * * Returns: * The namespace, with optionally the fragment stripped. */ getNamespace (elem) { let elNamespace = elem.getAttribute("xmlns"); if (elNamespace && this.options.ignoreNamespaceFragment) { elNamespace = elNamespace.split('#')[0]; } return elNamespace; }, /** PrivateFunction: namespaceMatch * Tests if a stanza matches the namespace set for this Strophe.Handler. * * Parameters: * (XMLElement) elem - The XML element to test. * * Returns: * true if the stanza matches and false otherwise. */ namespaceMatch (elem) { let nsMatch = false; if (!this.ns) { return true; } else { Strophe.forEachChild(elem, null, (elem) => { if (this.getNamespace(elem) === this.ns) { nsMatch = true; } }); return nsMatch || this.getNamespace(elem) === this.ns; } }, /** PrivateFunction: isMatch * Tests if a stanza matches the Strophe.Handler. * * Parameters: * (XMLElement) elem - The XML element to test. * * Returns: * true if the stanza matches and false otherwise. */ isMatch (elem) { let from = elem.getAttribute('from'); if (this.options.matchBareFromJid) { from = Strophe.getBareJidFromJid(from); } const elem_type = elem.getAttribute("type"); if (this.namespaceMatch(elem) && (!this.name || Strophe.isTagEqual(elem, this.name)) && (!this.type || (Array.isArray(this.type) ? this.type.indexOf(elem_type) !== -1 : elem_type === this.type)) && (!this.id || elem.getAttribute("id") === this.id) && (!this.from || from === this.from)) { return true; } return false; }, /** PrivateFunction: run * Run the callback on a matching stanza. * * Parameters: * (XMLElement) elem - The DOM element that triggered the * Strophe.Handler. * * Returns: * A boolean indicating if the handler should remain active. */ run (elem) { let result = null; try { result = this.handler(elem); } catch (e) { Strophe._handleError(e); throw e; } return result; }, /** PrivateFunction: toString * Get a String representation of the Strophe.Handler object. * * Returns: * A String. */ toString () { return "{Handler: " + this.handler + "(" + this.name + "," + this.id + "," + this.ns + ")}"; } }; /** PrivateClass: Strophe.TimedHandler * _Private_ helper class for managing timed handlers. * * A Strophe.TimedHandler encapsulates a user provided callback that * should be called after a certain period of time or at regular * intervals. The return value of the callback determines whether the * Strophe.TimedHandler will continue to fire. * * Users will not use Strophe.TimedHandler objects directly, but instead * they will use Strophe.Connection.addTimedHandler() and * Strophe.Connection.deleteTimedHandler(). */ Strophe.TimedHandler = class TimedHandler { /** PrivateConstructor: Strophe.TimedHandler * Create and initialize a new Strophe.TimedHandler object. * * Parameters: * (Integer) period - The number of milliseconds to wait before the * handler is called. * (Function) handler - The callback to run when the handler fires. This * function should take no arguments. * * Returns: * A new Strophe.TimedHandler object. */ constructor (period, handler) { this.period = period; this.handler = handler; this.lastCalled = new Date().getTime(); this.user = true; } /** PrivateFunction: run * Run the callback for the Strophe.TimedHandler. * * Returns: * true if the Strophe.TimedHandler should be called again, and false * otherwise. */ run () { this.lastCalled = new Date().getTime(); return this.handler(); } /** PrivateFunction: reset * Reset the last called time for the Strophe.TimedHandler. */ reset () { this.lastCalled = new Date().getTime(); } /** PrivateFunction: toString * Get a string representation of the Strophe.TimedHandler object. * * Returns: * The string representation. */ toString () { return "{TimedHandler: " + this.handler + "(" + this.period +")}"; } }; /** Class: Strophe.Connection * XMPP Connection manager. * * This class is the main part of Strophe. It manages a BOSH or websocket * connection to an XMPP server and dispatches events to the user callbacks * as data arrives. It supports SASL PLAIN, SASL SCRAM-SHA-1 * and legacy authentication. * * After creating a Strophe.Connection object, the user will typically * call connect() with a user supplied callback to handle connection level * events like authentication failure, disconnection, or connection * complete. * * The user will also have several event handlers defined by using * addHandler() and addTimedHandler(). These will allow the user code to * respond to interesting stanzas or do something periodically with the * connection. These handlers will be active once authentication is * finished. * * To send data to the connection, use send(). */ /** Constructor: Strophe.Connection * Create and initialize a Strophe.Connection object. * * The transport-protocol for this connection will be chosen automatically * based on the given service parameter. URLs starting with "ws://" or * "wss://" will use WebSockets, URLs starting with "http://", "https://" * or without a protocol will use BOSH. * * To make Strophe connect to the current host you can leave out the protocol * and host part and just pass the path, e.g. * * > let conn = new Strophe.Connection("/http-bind/"); * * Options common to both Websocket and BOSH: * ------------------------------------------ * * cookies: * * The *cookies* option allows you to pass in cookies to be added to the * document. These cookies will then be included in the BOSH XMLHttpRequest * or in the websocket connection. * * The passed in value must be a map of cookie names and string values. * * > { "myCookie": { * > "value": "1234", * > "domain": ".example.org", * > "path": "/", * > "expires": expirationDate * > } * > } * * Note that cookies can't be set in this way for other domains (i.e. cross-domain). * Those cookies need to be set under those domains, for example they can be * set server-side by making a XHR call to that domain to ask it to set any * necessary cookies. * * mechanisms: * * The *mechanisms* option allows you to specify the SASL mechanisms that this * instance of Strophe.Connection (and therefore your XMPP client) will * support. * * The value must be an array of objects with Strophe.SASLMechanism * prototypes. * * If nothing is specified, then the following mechanisms (and their * priorities) are registered: * * SCRAM-SHA-1 - 60 * PLAIN - 50 * OAUTHBEARER - 40 * X-OAUTH2 - 30 * ANONYMOUS - 20 * EXTERNAL - 10 * * explicitResourceBinding: * * If `explicitResourceBinding` is set to a truthy value, then the XMPP client * needs to explicitly call `Strophe.Connection.prototype.bind` once the XMPP * server has advertised the "urn:ietf:params:xml:ns:xmpp-bind" feature. * * Making this step explicit allows client authors to first finish other * stream related tasks, such as setting up an XEP-0198 Stream Management * session, before binding the JID resource for this session. * * WebSocket options: * ------------------ * * protocol: * * If you want to connect to the current host with a WebSocket connection you * can tell Strophe to use WebSockets through a "protocol" attribute in the * optional options parameter. Valid values are "ws" for WebSocket and "wss" * for Secure WebSocket. * So to connect to "wss://CURRENT_HOSTNAME/xmpp-websocket" you would call * * > let conn = new Strophe.Connection("/xmpp-websocket/", {protocol: "wss"}); * * Note that relative URLs _NOT_ starting with a "/" will also include the path * of the current site. * * Also because downgrading security is not permitted by browsers, when using * relative URLs both BOSH and WebSocket connections will use their secure * variants if the current connection to the site is also secure (https). * * worker: * * Set this option to URL from where the shared worker script should be loaded. * * To run the websocket connection inside a shared worker. * This allows you to share a single websocket-based connection between * multiple Strophe.Connection instances, for example one per browser tab. * * The script to use is the one in `src/shared-connection-worker.js`. * * BOSH options: * ------------- * * By adding "sync" to the options, you can control if requests will * be made synchronously or not. The default behaviour is asynchronous. * If you want to make requests synchronous, make "sync" evaluate to true. * > let conn = new Strophe.Connection("/http-bind/", {sync: true}); * * You can also toggle this on an already established connection. * > conn.options.sync = true; * * The *customHeaders* option can be used to provide custom HTTP headers to be * included in the XMLHttpRequests made. * * The *keepalive* option can be used to instruct Strophe to maintain the * current BOSH session across interruptions such as webpage reloads. * * It will do this by caching the sessions tokens in sessionStorage, and when * "restore" is called it will chec