diff --git a/types/strophe/index.d.ts b/types/strophe/index.d.ts index ff93346f6c..9cff5d3e02 100644 --- a/types/strophe/index.d.ts +++ b/types/strophe/index.d.ts @@ -3,1094 +3,1070 @@ // Definitions by: David Deutsch // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped -declare namespace wrapper { - /** Function: $build - * Create a Strophe.Builder. - * This is an alias for 'new Strophe.Builder(name, attrs)'. +declare namespace Strophe { + /** Constant: VERSION + * The version of the Strophe library. Unreleased builds will have + * a version of head-HASH where HASH is a partial revision. + */ + var VERSION: string; + + /** 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. + * 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. + */ + var NS: { + HTTPBIND: string; + BOSH: string; + CLIENT: string; + AUTH: string; + ROSTER: string; + PROFILE: string; + DISCO_INFO: string; + DISCO_ITEMS: string; + MUC: string; + SASL: string; + STREAM: string; + FRAMING: string; + BIND: string; + SESSION: string; + VERSION: string; + STANZAS: string; + XHTML_IM: string; + XHTML: string; + } + + /** 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 + */ + enum Status { + ERROR, + CONNECTING, + CONNFAIL, + AUTHENTICATING, + AUTHFAIL, + CONNECTED, + DISCONNECTED, + DISCONNECTING, + ATTACHED, + REDIRECT, + CONNTIMEOUT + } + + /** 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 + */ + enum LogLevel { + DEBUG, + INFO, + WARN, + ERROR, + FATAL + } + + /** 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 root element name. - * (Object) attrs - The attributes for the root element in object notation. - * - * Returns: - * A new Strophe.Builder object. + * (String) name - The name under which the namespace will be + * referenced under Strophe.NS + * (String) value - The actual namespace. */ - function $build(name: string, attrs?: any): Strophe.Builder; + function addNamespace(name: string, value: string): void; - /** Function: $msg - * Create a Strophe.Builder with a element as the root. + /** Function: forEachChild + * Map a function over some or all child elements of a given element. * - * Parmaeters: - * (Object) attrs - The element attributes in object notation. - * - * Returns: - * A new Strophe.Builder object. - */ - function $msg(attrs?: any): Strophe.Builder; - - /** Function: $iq - * Create a Strophe.Builder with an element as the root. + * 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: - * (Object) attrs - The element attributes in object notation. - * - * Returns: - * A new Strophe.Builder object. + * (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. */ - function $iq(attrs?: any): Strophe.Builder; + function forEachChild(elem: Element, elemName: string, func: (child: Element) => any): void; - /** Function: $pres - * Create a Strophe.Builder with a element as the root. + /** Function: isTagEqual + * Compare an element's tag name with a string. + * + * This function is case sensitive. * * Parameters: - * (Object) attrs - The element attributes in object notation. + * (XMLElement) el - A DOM element. + * (String) name - The element name. * * Returns: - * A new Strophe.Builder object. + * true if the element's tag name matches _el_, and false + * otherwise. */ - function $pres(attrs?: any): Strophe.Builder; + function isTagEqual(el: Element, name: string): boolean; - namespace Strophe { - /** Constant: VERSION - * The version of the Strophe library. Unreleased builds will have - * a version of head-HASH where HASH is a partial revision. - */ - var VERSION: string; + /** Function: xmlGenerator + * Get the DOM document to generate elements. + * + * Returns: + * The currently used DOM document. + */ + function xmlGenerator(): Document; - /** 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. - * 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. - */ - var NS: { - HTTPBIND: string; - BOSH: string; - CLIENT: string; - AUTH: string; - ROSTER: string; - PROFILE: string; - DISCO_INFO: string; - DISCO_ITEMS: string; - MUC: string; - SASL: string; - STREAM: string; - FRAMING: string; - BIND: string; - SESSION: string; - VERSION: string; - STANZAS: string; - XHTML_IM: string; - XHTML: string; - } + /** 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. + */ + function xmlElement(name: string, attrs?: any, text?: string): Element; + function xmlElement(name: string, text?: string, attrs?: any): Element; - /** 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 - */ - enum Status { - ERROR, - CONNECTING, - CONNFAIL, - AUTHENTICATING, - AUTHFAIL, - CONNECTED, - DISCONNECTED, - DISCONNECTING, - ATTACHED, - REDIRECT - } + /* Function: xmlescape + * Excapes invalid xml characters. + * + * Parameters: + * (String) text - text to escape. + * + * Returns: + * Escaped text. + */ + function xmlescape(text: string): string; - /** 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 - */ - enum LogLevel { - DEBUG, - INFO, - WARN, - ERROR, - FATAL - } + /* Function: xmlunescape + * Unexcapes invalid xml characters. + * + * Parameters: + * (String) text - text to unescape. + * + * Returns: + * Unescaped text. + */ + function xmlunescape(text: string): string; - /** 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"); + /** 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. + */ + function xmlTextNode(text: string): 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. + */ + function xmlHtmlNode(html: string): Document; + + /** Function: getText + * Get the concatenation of all text children of an element. * * Parameters: - * (String) name - The name under which the namespace will be - * referenced under Strophe.NS - * (String) value - The actual namespace. - */ - function addNamespace(name: string, value: string): void; - - /** 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. - */ - function forEachChild(elem: Element, elemName: string, func: (child: Element) => any): void; - - /** 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. + * (XMLElement) elem - A DOM element. * * Returns: - * true if the element's tag name matches _el_, and false - * otherwise. + * A String with the concatenated text of all text element children. */ - function isTagEqual(el: Element, name: string): boolean; + function getText(elem: Element): string; - /** Function: xmlGenerator - * Get the DOM document to generate elements. - * - * Returns: - * The currently used DOM document. - */ - function xmlGenerator(): Document; + /** 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. + */ + function copyElement(elem: Element): Element; - /** Function: xmlElement - * Create an XML DOM element. + /** 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: + * (Element) elem - A DOM element. + * + * Returns: + * A new, copied DOM element tree. + */ + function createHtml(elem: Element): Element; + + /** 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). + */ + function escapeNode(node: string): string; + + /** 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). + */ + function unescapeNode(node: string): string; + + /** Function: getNodeFromJid + * Get the node portion of a JID String. + * + * Parameters: + * (String) jid - A JID. + * + * Returns: + * A String containing the node. + */ + function getNodeFromJid(jid: string): string; + + /** Function: getDomainFromJid + * Get the domain portion of a JID String. + * + * Parameters: + * (String) jid - A JID. + * + * Returns: + * A String containing the domain. + */ + function getDomainFromJid(jid: string): string; + + /** Function: getResourceFromJid + * Get the resource portion of a JID String. + * + * Parameters: + * (String) jid - A JID. + * + * Returns: + * A String containing the resource. + */ + function getResourceFromJid(jid: string): string; + + /** Function: getBareJidFromJid + * Get the bare JID from a JID String. + * + * Parameters: + * (String) jid - A JID. + * + * Returns: + * A String containing the bare JID. + */ + function getBareJidFromJid(jid: string): string; + + /** 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 does nothing. 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. + */ + function log(level: LogLevel, msg: string): void; + + /** Functions: debug, info, warn, error + * Log a message at the appropriate Strophe.LogLevel + * + * Parameters: + * (String) msg - The log message. + */ + function debug(msg: string): void; + function info(msg: string): void; + function warn(msg: string): void; + function error(msg: string): void; + function fatal(msg: string): void; + + /** 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. + */ + function serialize(elem: Element | Builder): string; + + /** 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. + */ + function addConnectionPlugin(name: string, ptype: any): void; + + /** Class: Strophe.Builder + * XML DOM builder. + * + * This object provides an interface similar to JQuery but for building + * DOM element 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 + * > + * > + * > + * > + * > + * 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. + */ + class Builder { + + /** Constructor: Strophe.Builder + * Create a Strophe.Builder object. * - * 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. + * The attributes should be passed in object notation. For example + * > var b = new Builder('message', {to: 'you', from: 'me'}); + * or + * > var b = new Builder('messsage', {'xml:lang': 'en'}); * * 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. + * (String) name - The name of the root element. + * (Object) attrs - The attributes for the root element in object notation. * * Returns: - * A new XML DOM element. + * A new Strophe.Builder. */ - function xmlElement(name: string, attrs?: any, text?: string): Element; - function xmlElement(name: string, text?: string, attrs?: any): Element; + constructor(name: string, attrs?: any); - /* Function: xmlescape - * Excapes invalid xml characters. - * - * Parameters: - * (String) text - text to escape. - * - * Returns: - * Escaped text. - */ - function xmlescape(text: string): string; - - /* Function: xmlunescape - * Unexcapes invalid xml characters. + /** Function: tree + * Return the DOM tree. * - * Parameters: - * (String) text - text to unescape. + * This function returns the current DOM tree as an element object. This + * is suitable for passing to functions like Strophe.Connection.send(). * * Returns: - * Unescaped text. + * The DOM tree as a element object. */ - function xmlunescape(text: string): string; + tree(): Element; - /** Function: xmlTextNode - * Creates an XML DOM text node. + /** Function: toString + * Serialize the DOM tree to a String. * - * Provides a cross implementation version of document.createTextNode. - * - * Parameters: - * (String) text - The content of the text node. + * 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: - * A new XML DOM text node. + * The serialized DOM tree in a String. */ - function xmlTextNode(text: string): Text; + toString(): string; - /** Function: xmlHtmlNode - * Creates an XML DOM html node. + /** Function: up + * Make the current parent element the new current element. * - * Parameters: - * (String) html - The content of the html node. + * 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: - * A new XML DOM text node. + * The Stophe.Builder object. */ - function xmlHtmlNode(html: string): Document; + up(): Builder; - /** 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. - */ - function getText(elem: Element): string; - - /** Function: copyElement - * Copy an XML DOM element. + /** Function: attrs + * Add or modify attributes of the current element. * - * This function copies a DOM element and all its descendants and returns - * the new copy. + * 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: any): Builder; + + /** 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: string, attrs?: any, text?: string): Builder; + + /** 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: - * A new, copied DOM element tree. + * The Strophe.Builder object. */ - function copyElement(elem: Element): Element; + cnode(elem: Node): Builder; - /** Function: createHtml - * Copy an HTML DOM element into an XML DOM. + /** Function: t + * Add a child text element. * - * This function copies a DOM element and all its descendants and returns - * the new copy. + * This *does not* make the child the new current element since there + * are no children of text elements. * * Parameters: - * (Element) elem - A DOM element. + * (String) text - The text data to append to the current element. * * Returns: - * A new, copied DOM element tree. + * The Strophe.Builder object. */ - function createHtml(elem: Element): Element; + t(text: string): Builder; - /** Function: escapeNode - * Escape the node part (also called local part) of a JID. + /** Function: h + * Replace current element contents with the HTML passed in. + * + * This *does not* make the child the new current element * * Parameters: - * (String) node - A node (or local part). + * (String) html - The html to insert as contents of current element. * * Returns: - * An escaped node (or local part). + * The Strophe.Builder object. */ - function escapeNode(node: string): string; + h(html: string): Builder; + } - /** Function: unescapeNode - * Unescape a node part (also called local part) of a JID. + interface ConnectionOptions { + keepalive?: boolean; + protocol?: string; + sync?: boolean; + } + + /** Class: Strophe.Connection + * XMPP Connection manager. + * + * This class is the main part of Strophe. It manages a BOSH connection + * to an XMPP server and dispatches events to the user callbacks as + * data arrives. It supports SASL PLAIN, SASL DIGEST-MD5, SASL SCRAM-SHA1 + * 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(). + */ + class Connection { + + jid: string; + authzid: string; + pass: string; + authcid: string; + domain: string; + servtype: string; + maxRetries: number; + //todo: what other members are meant to be public? + + /** 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. + * + * > var conn = new Strophe.Connection("/http-bind/"); + * + * WebSocket options: + * + * 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 + * + * > var 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). + * + * 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: + * > var conn = new Strophe.Connection("/http-bind/", {sync: true}); + * You can also toggle this on an already established connection: + * > conn.options.sync = true; + * * * Parameters: - * (String) node - A node (or local part). + * (String) service - The BOSH or WebSocket service URL. + * (Object) options - A hash of configuration options * * Returns: - * An unescaped node (or local part). + * A new Strophe.Connection object. */ - function unescapeNode(node: string): string; + constructor(service: string, options?: ConnectionOptions); - /** Function: getNodeFromJid - * Get the node portion of a JID String. + /** Function: reset + * Reset the connection. + * + * This function should be called after a connection is disconnected + * before that connection is reused. + */ + reset(): void; + + /** Function: pause + * Pause the request manager. + * + * This will prevent Strophe from sending any more requests to the + * server. This is very useful for temporarily pausing + * BOSH-Connections while a lot of send() calls are happening quickly. + * This causes Strophe to send the data in a single request, saving + * many request trips. + */ + pause(): void; + + /** Function: resume + * Resume the request manager. + * + * This resumes after pause() has been called. + */ + resume(): void; + + /** Function: getUniqueId + * Generate a unique ID for use in elements. + * + * All stanzas are required to have unique id attributes. This + * function makes creating these easy. Each connection instance has + * a counter which starts from zero, and the value of this counter + * plus a colon followed by the suffix becomes the unique id. If no + * suffix is supplied, the counter is used as the unique id. + * + * Suffixes are used to make debugging easier when reading the stream + * data, and their use is recommended. The counter resets to 0 for + * every new connection for the same reason. For connections to the + * same server that authenticate the same way, all the ids should be + * the same, which makes it easy to see changes. This is useful for + * automated testing as well. * * Parameters: - * (String) jid - A JID. + * (String) suffix - A optional suffix to append to the id. * * Returns: - * A String containing the node. + * A unique string to be used for the id attribute. */ - function getNodeFromJid(jid: string): string; + getUniqueId(suffix?: string | number): string; - /** Function: getDomainFromJid - * Get the domain portion of a JID String. + /** Function: connect + * Starts the connection process. + * + * As the connection process proceeds, the user supplied callback will + * be triggered multiple times with status updates. The callback + * should take two arguments - the status code and the error condition. + * + * The status code will be one of the values in the Strophe.Status + * constants. The error condition will be one of the conditions + * defined in RFC 3920 or the condition 'strophe-parsererror'. + * + * The Parameters _wait_, _hold_ and _route_ are optional and only relevant + * for BOSH connections. Please see XEP 124 for a more detailed explanation + * of the optional parameters. * * Parameters: - * (String) jid - A JID. - * - * Returns: - * A String containing the domain. + * (String) jid - The user's JID. This may be a bare JID, + * or a full JID. If a node is not supplied, SASL ANONYMOUS + * authentication will be attempted. + * (String) pass - The user's password. + * (Function) callback - The connect callback function. + * (Integer) wait - The optional HTTPBIND wait value. This is the + * time the server will wait before returning an empty result for + * a request. The default setting of 60 seconds is recommended. + * (Integer) hold - The optional HTTPBIND hold value. This is the + * number of connections the server will hold at one time. This + * should almost always be set to 1 (the default). + * (String) route - The optional route value. */ - function getDomainFromJid(jid: string): string; + connect(jid?: string, pass?: string, callback?: (status: Status, condition: string) => any, wait?: number, hold?: number, route?: string): void; - /** Function: getResourceFromJid - * Get the resource portion of a JID String. + /** + * Function: restore + * Attempt to restore a cached BOSH session. + * + * This function is only useful in conjunction with providing the + * “keepalive”:true option when instantiating a new Strophe.Connection. + * When “keepalive” is set to true, Strophe will cache the BOSH tokens + * RID (Request ID) and SID (Session ID) and then when this function is called, + * it will attempt to restore the session from those cached tokens. + * This function must therefore be called instead of connect or attach. + * For an example on how to use it, please see examples/restore.js + * + * Parameters: + * (String) jid - The user’s JID. This may be a bare JID or a full JID. + * (Function) callback - The connect callback function. + * (Integer) wait - The optional HTTPBIND wait value. + * This is the time the server will wait before returning an empty result for a request. + * The default setting of 60 seconds is recommended. + * (Integer) hold - The optional HTTPBIND hold value. + * This is the number of connections the server will hold at one time. + * This should almost always be set to 1 (the default). + * (Integer) wind - The optional HTTBIND window value. + * This is the allowed range of request ids that are valid. + * The default is 5. + */ + restore(jid?: string, callback?: (status: Status, condition: string) => any, wait?: number, hold?: number, route?: string): void; + + /** Function: attach + * Attach to an already created and authenticated BOSH session. + * + * This function is provided to allow Strophe to attach to BOSH + * sessions which have been created externally, perhaps by a Web + * application. This is often used to support auto-login type features + * without putting user credentials into the page. * * Parameters: - * (String) jid - A JID. - * - * Returns: - * A String containing the resource. + * (String) jid - The full JID that is bound by the session. + * (String) sid - The SID of the BOSH session. + * (String) rid - The current RID of the BOSH session. This RID + * will be used by the next request. + * (Function) callback The connect callback function. + * (Integer) wait - The optional HTTPBIND wait value. This is the + * time the server will wait before returning an empty result for + * a request. The default setting of 60 seconds is recommended. + * Other settings will require tweaks to the Strophe.TIMEOUT value. + * (Integer) hold - The optional HTTPBIND hold value. This is the + * number of connections the server will hold at one time. This + * should almost always be set to 1 (the default). + * (Integer) wind - The optional HTTBIND window value. This is the + * allowed range of request ids that are valid. The default is 5. */ - function getResourceFromJid(jid: string): string; + attach(jid: string, sid: string, rid: string, callback?: (status: Status, condition: string) => any, wait?: number, hold?: number, wind?: number): void; - /** Function: getBareJidFromJid - * Get the bare JID from a JID String. + /** Function: xmlInput + * User overrideable function that receives XML data coming into the + * connection. * - * Parameters: - * (String) jid - A JID. - * - * Returns: - * A String containing the bare JID. - */ - function getBareJidFromJid(jid: string): string; - - /** 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 does nothing. If client code wishes to handle the logging - * messages, it should override this with - * > Strophe.log = function (level, msg) { - * > (user code here) + * The default function does nothing. User code can override this with + * > Strophe.Connection.xmlInput = function (elem) { + * > (user code) * > }; * - * Please note that data sent and received over the wire is logged - * via Strophe.Connection.rawInput() and Strophe.Connection.rawOutput(). + * Due to limitations of current Browsers' XML-Parsers the opening and closing + * tag for WebSocket-Connoctions will be passed as selfclosing here. * - * 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. + * BOSH-Connections will have all stanzas wrapped in a tag. See + * if you want to strip this tag. * * 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. + * (XMLElement) elem - The XML data received by the connection. */ - function log(level: LogLevel, msg: string): void; + xmlInput(elem: Element): void; - /** Functions: debug, info, warn, error - * Log a message at the appropriate Strophe.LogLevel + /** Function: xmlOutput + * User overrideable function that receives XML data sent to the + * connection. + * + * The default function does nothing. User code can override this with + * > Strophe.Connection.xmlOutput = function (elem) { + * > (user code) + * > }; + * + * Due to limitations of current Browsers' XML-Parsers the opening and closing + * tag for WebSocket-Connoctions will be passed as selfclosing here. + * + * BOSH-Connections will have all stanzas wrapped in a tag. See + * if you want to strip this tag. * * Parameters: - * (String) msg - The log message. + * (XMLElement) elem - The XMLdata sent by the connection. */ - function debug(msg: string): void; - function info(msg: string): void; - function warn(msg: string): void; - function error(msg: string): void; - function fatal(msg: string): void; + xmlOutput(elem: Element): void; - /** Function: serialize - * Render a DOM element and all descendants to a String. + /** Function: rawInput + * User overrideable function that receives raw data coming into the + * connection. + * + * The default function does nothing. User code can override this with + * > Strophe.Connection.rawInput = function (data) { + * > (user code) + * > }; * * Parameters: - * (XMLElement) elem - A DOM element. + * (String) data - The data received by the connection. + */ + rawInput(data: string): void; + + /** Function: rawOutput + * User overrideable function that receives raw data sent to the + * connection. + * + * The default function does nothing. User code can override this with + * > Strophe.Connection.rawOutput = function (data) { + * > (user code) + * > }; + * + * Parameters: + * (String) data - The data sent by the connection. + */ + rawOutput(data: string): void; + + /** Function: send + * Send a stanza. + * + * This function is called to push data onto the send queue to + * go out over the wire. Whenever a request is sent to the BOSH + * server, all pending data is sent and the queue is flushed. + * + * Parameters: + * (XMLElement | + * [XMLElement] | + * Strophe.Builder) elem - The stanza to send. + */ + send(elem: Element | Element[]| Builder): void; + + /** Function: flush + * Immediately send any pending outgoing data. + * + * Normally send() queues outgoing data until the next idle period + * (100ms), which optimizes network use in the common cases when + * several send()s are called in succession. flush() can be used to + * immediately send all pending data. + */ + flush(): void; + + /** Function: sendIQ + * Helper function to send IQ stanzas. + * + * Parameters: + * (XMLElement) elem - The stanza to send. + * (Function) callback - The callback function for a successful request. + * (Function) errback - The callback function for a failed or timed + * out request. On timeout, the stanza will be null. + * (Integer) timeout - The time specified in milliseconds for a + * timeout to occur. * * Returns: - * The serialized element tree as a String. - */ - function serialize(elem: Element | Builder): string; + * The id used to send the IQ. + */ + sendIQ(elem: Element | Builder, callback?: (stanza: Element) => any, errback?: (stanza: Element) => any, timeout?: number): string; //todo: Is callback correct? - /** Function: addConnectionPlugin - * Extends the Strophe.Connection object with the given plugin. + /** Function: addTimedHandler + * Add a timed handler to the connection. + * + * This function adds a timed handler. The provided handler will + * be called every period milliseconds until it returns false, + * the connection is terminated, or the handler is removed. Handlers + * that wish to continue being invoked should return true. + * + * Because of method binding it is necessary to save the result of + * this function if you wish to remove a handler with + * deleteTimedHandler(). + * + * Note that user handlers are not active until authentication is + * successful. * * Parameters: - * (String) name - The name of the extension. - * (Object) ptype - The plugin's prototype. + * (Integer) period - The period of the handler. + * (Function) handler - The callback function. + * + * Returns: + * A reference to the handler that can be used to remove it. */ - function addConnectionPlugin(name: string, ptype: any): void; + addTimedHandler(period: number, handler: () => boolean): any; - var Builder: { - /** Constructor: Strophe.Builder - * Create a Strophe.Builder object. - * - * The attributes should be passed in object notation. For example - * > var b = new Builder('message', {to: 'you', from: 'me'}); - * or - * > var 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. - */ - new (name: string, attrs?: any): Builder; - prototype: any; - } - - - /** Class: Strophe.Builder - * XML DOM builder. + /** Function: deleteTimedHandler + * Delete a timed handler for a connection. * - * This object provides an interface similar to JQuery but for building - * DOM element 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 - * > - * > - * > - * > - * > - * The corresponding DOM manipulations to get a similar fragment would be - * a lot more tedious and probably involve several helper variables. + * This function removes a timed handler from the connection. The + * handRef parameter is *not* the function passed to addTimedHandler(), + * but is the reference returned from addTimedHandler(). * - * 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. + * Parameters: + * (Strophe.TimedHandler) handRef - The handler reference. */ - interface Builder { - /** 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(): Element; + deleteTimedHandler(handRef: any): void; - /** 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(): string; - /** 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(): Builder; - - /** 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: any): Builder; - - /** 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: string, attrs?: any, text?: string): Builder; - - /** 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: Node): Builder; - - /** 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: string): Builder; - - /** 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: string): Builder; - } - - interface ConnectionOptions { - protocol?: string; - sync?: boolean; - } - var Connection: { - /** 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. - * - * > var conn = new Strophe.Connection("/http-bind/"); - * - * WebSocket options: - * - * 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 - * - * > var 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). - * - * 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: - * > var conn = new Strophe.Connection("/http-bind/", {sync: true}); - * You can also toggle this on an already established connection: - * > conn.options.sync = true; - * - * - * Parameters: - * (String) service - The BOSH or WebSocket service URL. - * (Object) options - A hash of configuration options - * - * Returns: - * A new Strophe.Connection object. - */ - new (service: string, options?: ConnectionOptions): Connection; - prototype: any; - } - - /** Class: Strophe.Connection - * XMPP Connection manager. + /** Function: addHandler + * Add a stanza handler for the connection. * - * This class is the main part of Strophe. It manages a BOSH connection - * to an XMPP server and dispatches events to the user callbacks as - * data arrives. It supports SASL PLAIN, SASL DIGEST-MD5, SASL SCRAM-SHA1 - * and legacy authentication. + * This function adds a stanza handler to the connection. The + * handler callback will be called for any stanza that matches + * the parameters. Note that if multiple parameters are supplied, + * they must all match for the handler to be invoked. * - * 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 handler will receive the stanza that triggered it as its argument. + * *The handler should return true if it is to be invoked again; + * returning false will remove the handler after it returns.* * - * 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. + * As a convenience, the ns parameters applies to the top level element + * and also any of its immediate children. This is primarily to make + * matching /iq/query elements easy. * - * To send data to the connection, use send(). + * The options argument contains handler matching flags that affect how + * matches are determined. Currently the only flag is matchBare (a + * boolean). When matchBare is true, the from parameter and the from + * attribute on the stanza will be matched as bare JIDs instead of + * full JIDs. To use this, pass {matchBare: true} as the value of + * options. The default value for matchBare is false. + * + * The return value should be saved if you wish to remove the handler + * with deleteHandler(). + * + * Parameters: + * (Function) handler - The user callback. + * (String) ns - The namespace to match. + * (String) name - The stanza name to match. + * (String) type - The stanza type attribute to match. + * (String) id - The stanza id attribute to match. + * (String) from - The stanza from attribute to match. + * (String) options - The handler options + * + * Returns: + * A reference to the handler that can be used to remove it. */ - interface Connection { + addHandler(handler: (stanza: Element) => boolean, ns: string, name: string, type?: string, id?: string, from?: string, options?: { matchBare: boolean }): any; //todo: is callback correct? Also, are the elements specified as optional truly optional? - jid: string; - authzid: string; - pass: string; - authcid: string; - domain: string; - servtype: string; - maxRetries: number; - //todo: what other members are meant to be public? - - /** Function: reset - * Reset the connection. - * - * This function should be called after a connection is disconnected - * before that connection is reused. - */ - reset(): void; - - /** Function: pause - * Pause the request manager. - * - * This will prevent Strophe from sending any more requests to the - * server. This is very useful for temporarily pausing - * BOSH-Connections while a lot of send() calls are happening quickly. - * This causes Strophe to send the data in a single request, saving - * many request trips. - */ - pause(): void; - - /** Function: resume - * Resume the request manager. - * - * This resumes after pause() has been called. - */ - resume(): void; - - /** Function: getUniqueId - * Generate a unique ID for use in elements. - * - * All stanzas are required to have unique id attributes. This - * function makes creating these easy. Each connection instance has - * a counter which starts from zero, and the value of this counter - * plus a colon followed by the suffix becomes the unique id. If no - * suffix is supplied, the counter is used as the unique id. - * - * Suffixes are used to make debugging easier when reading the stream - * data, and their use is recommended. The counter resets to 0 for - * every new connection for the same reason. For connections to the - * same server that authenticate the same way, all the ids should be - * the same, which makes it easy to see changes. This is useful for - * automated testing as well. - * - * Parameters: - * (String) suffix - A optional suffix to append to the id. - * - * Returns: - * A unique string to be used for the id attribute. - */ - getUniqueId(suffix?: string | number): string; - - /** Function: connect - * Starts the connection process. - * - * As the connection process proceeds, the user supplied callback will - * be triggered multiple times with status updates. The callback - * should take two arguments - the status code and the error condition. - * - * The status code will be one of the values in the Strophe.Status - * constants. The error condition will be one of the conditions - * defined in RFC 3920 or the condition 'strophe-parsererror'. - * - * The Parameters _wait_, _hold_ and _route_ are optional and only relevant - * for BOSH connections. Please see XEP 124 for a more detailed explanation - * of the optional parameters. - * - * Parameters: - * (String) jid - The user's JID. This may be a bare JID, - * or a full JID. If a node is not supplied, SASL ANONYMOUS - * authentication will be attempted. - * (String) pass - The user's password. - * (Function) callback - The connect callback function. - * (Integer) wait - The optional HTTPBIND wait value. This is the - * time the server will wait before returning an empty result for - * a request. The default setting of 60 seconds is recommended. - * (Integer) hold - The optional HTTPBIND hold value. This is the - * number of connections the server will hold at one time. This - * should almost always be set to 1 (the default). - * (String) route - The optional route value. - */ - connect(jid?: string, pass?: string, callback?: (status: Status, condition: string) => any, wait?: number, hold?: number, route?: string): void; - - /** Function: attach - * Attach to an already created and authenticated BOSH session. - * - * This function is provided to allow Strophe to attach to BOSH - * sessions which have been created externally, perhaps by a Web - * application. This is often used to support auto-login type features - * without putting user credentials into the page. - * - * Parameters: - * (String) jid - The full JID that is bound by the session. - * (String) sid - The SID of the BOSH session. - * (String) rid - The current RID of the BOSH session. This RID - * will be used by the next request. - * (Function) callback The connect callback function. - * (Integer) wait - The optional HTTPBIND wait value. This is the - * time the server will wait before returning an empty result for - * a request. The default setting of 60 seconds is recommended. - * Other settings will require tweaks to the Strophe.TIMEOUT value. - * (Integer) hold - The optional HTTPBIND hold value. This is the - * number of connections the server will hold at one time. This - * should almost always be set to 1 (the default). - * (Integer) wind - The optional HTTBIND window value. This is the - * allowed range of request ids that are valid. The default is 5. - */ - attach(jid: string, sid: string, rid: string, callback?: (status: Status, condition: string) => any, wait?: number, hold?: number, wind?: number): void; - - /** Function: xmlInput - * User overrideable function that receives XML data coming into the - * connection. - * - * The default function does nothing. User code can override this with - * > Strophe.Connection.xmlInput = function (elem) { - * > (user code) - * > }; - * - * Due to limitations of current Browsers' XML-Parsers the opening and closing - * tag for WebSocket-Connoctions will be passed as selfclosing here. - * - * BOSH-Connections will have all stanzas wrapped in a tag. See - * if you want to strip this tag. - * - * Parameters: - * (XMLElement) elem - The XML data received by the connection. - */ - xmlInput(elem: Element): void; - - /** Function: xmlOutput - * User overrideable function that receives XML data sent to the - * connection. - * - * The default function does nothing. User code can override this with - * > Strophe.Connection.xmlOutput = function (elem) { - * > (user code) - * > }; - * - * Due to limitations of current Browsers' XML-Parsers the opening and closing - * tag for WebSocket-Connoctions will be passed as selfclosing here. - * - * BOSH-Connections will have all stanzas wrapped in a tag. See - * if you want to strip this tag. - * - * Parameters: - * (XMLElement) elem - The XMLdata sent by the connection. - */ - xmlOutput(elem: Element): void; - - /** Function: rawInput - * User overrideable function that receives raw data coming into the - * connection. - * - * The default function does nothing. User code can override this with - * > Strophe.Connection.rawInput = function (data) { - * > (user code) - * > }; - * - * Parameters: - * (String) data - The data received by the connection. - */ - rawInput(data: string): void; - - /** Function: rawOutput - * User overrideable function that receives raw data sent to the - * connection. - * - * The default function does nothing. User code can override this with - * > Strophe.Connection.rawOutput = function (data) { - * > (user code) - * > }; - * - * Parameters: - * (String) data - The data sent by the connection. - */ - rawOutput(data: string): void; - - /** Function: send - * Send a stanza. - * - * This function is called to push data onto the send queue to - * go out over the wire. Whenever a request is sent to the BOSH - * server, all pending data is sent and the queue is flushed. - * - * Parameters: - * (XMLElement | - * [XMLElement] | - * Strophe.Builder) elem - The stanza to send. - */ - send(elem: Element | Element[]| Builder): void; - - /** Function: flush - * Immediately send any pending outgoing data. - * - * Normally send() queues outgoing data until the next idle period - * (100ms), which optimizes network use in the common cases when - * several send()s are called in succession. flush() can be used to - * immediately send all pending data. - */ - flush(): void; - - /** Function: sendIQ - * Helper function to send IQ stanzas. - * - * Parameters: - * (XMLElement) elem - The stanza to send. - * (Function) callback - The callback function for a successful request. - * (Function) errback - The callback function for a failed or timed - * out request. On timeout, the stanza will be null. - * (Integer) timeout - The time specified in milliseconds for a - * timeout to occur. - * - * Returns: - * The id used to send the IQ. - */ - sendIQ(elem: Element | Builder, callback?: (stanza: Element) => any, errback?: (stanza: Element) => any, timeout?: number): string; //todo: Is callback correct? - - /** Function: addTimedHandler - * Add a timed handler to the connection. - * - * This function adds a timed handler. The provided handler will - * be called every period milliseconds until it returns false, - * the connection is terminated, or the handler is removed. Handlers - * that wish to continue being invoked should return true. - * - * Because of method binding it is necessary to save the result of - * this function if you wish to remove a handler with - * deleteTimedHandler(). - * - * Note that user handlers are not active until authentication is - * successful. - * - * Parameters: - * (Integer) period - The period of the handler. - * (Function) handler - The callback function. - * - * Returns: - * A reference to the handler that can be used to remove it. - */ - addTimedHandler(period: number, handler: () => boolean): any; - - /** Function: deleteTimedHandler - * Delete a timed handler for a connection. - * - * This function removes a timed handler from the connection. The - * handRef parameter is *not* the function passed to addTimedHandler(), - * but is the reference returned from addTimedHandler(). - * - * Parameters: - * (Strophe.TimedHandler) handRef - The handler reference. - */ - deleteTimedHandler(handRef: any): void; - - - /** Function: addHandler - * Add a stanza handler for the connection. - * - * This function adds a stanza handler to the connection. The - * handler callback will be called for any stanza that matches - * the parameters. Note that if multiple parameters are supplied, - * they must all match for the handler to be invoked. - * - * The handler will receive the stanza that triggered it as its argument. - * *The handler should return true if it is to be invoked again; - * returning false will remove the handler after it returns.* - * - * As a convenience, the ns parameters applies to the top level element - * and also any of its immediate children. This is primarily to make - * matching /iq/query elements easy. - * - * The options argument contains handler matching flags that affect how - * matches are determined. Currently the only flag is matchBare (a - * boolean). When matchBare is true, the from parameter and the from - * attribute on the stanza will be matched as bare JIDs instead of - * full JIDs. To use this, pass {matchBare: true} as the value of - * options. The default value for matchBare is false. - * - * The return value should be saved if you wish to remove the handler - * with deleteHandler(). - * - * Parameters: - * (Function) handler - The user callback. - * (String) ns - The namespace to match. - * (String) name - The stanza name to match. - * (String) type - The stanza type attribute to match. - * (String) id - The stanza id attribute to match. - * (String) from - The stanza from attribute to match. - * (String) options - The handler options - * - * Returns: - * A reference to the handler that can be used to remove it. - */ - addHandler(handler: (stanza: Element) => boolean, ns: string, name: string, type?: string, id?: string, from?: string, options?: { matchBare: boolean }): any; //todo: is callback correct? Also, are the elements specified as optional truly optional? - - /** Function: deleteHandler - * Delete a stanza handler for a connection. - * - * This function removes a stanza handler from the connection. The - * handRef parameter is *not* the function passed to addHandler(), - * but is the reference returned from addHandler(). - * - * Parameters: - * (Strophe.Handler) handRef - The handler reference. - */ - deleteHandler(handRef: any): void; - - /** Function: disconnect - * Start the graceful disconnection process. - * - * This function starts the disconnection process. This process starts - * by sending unavailable presence and sending BOSH body of type - * terminate. A timeout handler makes sure that disconnection happens - * even if the BOSH server does not respond. - * If the Connection object isn't connected, at least tries to abort all pending requests - * so the connection object won't generate successful requests (which were already opened). - * - * The user supplied connection callback will be notified of the - * progress as this process happens. - * - * Parameters: - * (String) reason - The reason the disconnect is occuring. - */ - disconnect(reason: string): void; - } - - /** Interface: Strophe.SASLMechanism + /** Function: deleteHandler + * Delete a stanza handler for a connection. * - * encapsulates SASL authentication mechanisms. + * This function removes a stanza handler from the connection. The + * handRef parameter is *not* the function passed to addHandler(), + * but is the reference returned from addHandler(). * - * User code may override the priority for each mechanism or disable it completely. - * See for information about changing priority and for informatian on - * how to disable a mechanism. - * - * By default, all mechanisms are enabled and the priorities are - * - * SCRAM-SHA1 - 40 - * DIGEST-MD5 - 30 - * Plain - 20 + * Parameters: + * (Strophe.Handler) handRef - The handler reference. */ - interface SASLMechanism { - /** - * Function: test - * Checks if mechanism able to run. - * To disable a mechanism, make this return false; - * - * To disable plain authentication run - * > Strophe.SASLPlain.test = function() { - * > return false; - * > } - * - * See for a list of available mechanisms. - * - * Parameters: - * (Strophe.Connection) connection - Target Connection. - * - * Returns: - * (Boolean) If mechanism was able to run. - */ - test(connection: Connection): boolean; + deleteHandler(handRef: any): void; - /** Variable: priority - * Determines which is chosen for authentication (Higher is better). - * Users may override this to prioritize mechanisms differently. - * - * In the default configuration the priorities are - * - * SCRAM-SHA1 - 40 - * DIGEST-MD5 - 30 - * Plain - 20 - * - * Example: (This will cause Strophe to choose the mechanism that the server sent first) - * - * > Strophe.SASLMD5.priority = Strophe.SASLSHA1.priority; - * - * See for a list of available mechanisms. - * - */ - priority: number; - } - - /** Constants: SASL mechanisms - * Available authentication mechanisms - * - * Strophe.SASLAnonymous - SASL Anonymous authentication. - * Strophe.SASLPlain - SASL Plain authentication. - * Strophe.SASLMD5 - SASL Digest-MD5 authentication - * Strophe.SASLSHA1 - SASL SCRAM-SHA1 authentication - */ - var SASLAnonymous: SASLMechanism; - var SASLPlain: SASLMechanism; - var SASLSHA1: SASLMechanism; - var SASLMD5: SASLMechanism; + /** Function: disconnect + * Start the graceful disconnection process. + * + * This function starts the disconnection process. This process starts + * by sending unavailable presence and sending BOSH body of type + * terminate. A timeout handler makes sure that disconnection happens + * even if the BOSH server does not respond. + * If the Connection object isn't connected, at least tries to abort all pending requests + * so the connection object won't generate successful requests (which were already opened). + * + * The user supplied connection callback will be notified of the + * progress as this process happens. + * + * Parameters: + * (String) reason - The reason the disconnect is occuring. + */ + disconnect(reason: string): void; } + + /** Interface: Strophe.SASLMechanism + * + * encapsulates SASL authentication mechanisms. + * + * User code may override the priority for each mechanism or disable it completely. + * See for information about changing priority and for informatian on + * how to disable a mechanism. + * + * By default, all mechanisms are enabled and the priorities are + * + * SCRAM-SHA1 - 40 + * DIGEST-MD5 - 30 + * Plain - 20 + */ + interface SASLMechanism { + /** + * Function: test + * Checks if mechanism able to run. + * To disable a mechanism, make this return false; + * + * To disable plain authentication run + * > Strophe.SASLPlain.test = function() { + * > return false; + * > } + * + * See for a list of available mechanisms. + * + * Parameters: + * (Strophe.Connection) connection - Target Connection. + * + * Returns: + * (Boolean) If mechanism was able to run. + */ + test(connection: Connection): boolean; + + /** Variable: priority + * Determines which is chosen for authentication (Higher is better). + * Users may override this to prioritize mechanisms differently. + * + * In the default configuration the priorities are + * + * SCRAM-SHA1 - 40 + * DIGEST-MD5 - 30 + * Plain - 20 + * + * Example: (This will cause Strophe to choose the mechanism that the server sent first) + * + * > Strophe.SASLMD5.priority = Strophe.SASLSHA1.priority; + * + * See for a list of available mechanisms. + * + */ + priority: number; + } + + /** Constants: SASL mechanisms + * Available authentication mechanisms + * + * Strophe.SASLAnonymous - SASL Anonymous authentication. + * Strophe.SASLPlain - SASL Plain authentication. + * Strophe.SASLMD5 - SASL Digest-MD5 authentication + * Strophe.SASLSHA1 - SASL SCRAM-SHA1 authentication + */ + var SASLAnonymous: SASLMechanism; + var SASLPlain: SASLMechanism; + var SASLSHA1: SASLMechanism; + var SASLMD5: SASLMechanism; } /** 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. - */ -declare function $build(name: string, attrs?: any): wrapper.Strophe.Builder; + * 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. + */ +declare function $build(name: string, attrs?: any): Strophe.Builder; /** Function: $msg - * Create a Strophe.Builder with a element as the root. - * - * Parmaeters: - * (Object) attrs - The element attributes in object notation. - * - * Returns: - * A new Strophe.Builder object. - */ -declare function $msg(attrs?: any): wrapper.Strophe.Builder; + * Create a Strophe.Builder with a element as the root. + * + * Parmaeters: + * (Object) attrs - The element attributes in object notation. + * + * Returns: + * A new Strophe.Builder object. + */ +declare function $msg(attrs?: any): Strophe.Builder; /** Function: $iq * Create a Strophe.Builder with an element as the root. @@ -1101,7 +1077,7 @@ declare function $msg(attrs?: any): wrapper.Strophe.Builder; * Returns: * A new Strophe.Builder object. */ -declare function $iq(attrs?: any): wrapper.Strophe.Builder; +declare function $iq(attrs?: any): Strophe.Builder; /** Function: $pres * Create a Strophe.Builder with a element as the root. @@ -1112,26 +1088,29 @@ declare function $iq(attrs?: any): wrapper.Strophe.Builder; * Returns: * A new Strophe.Builder object. */ -declare function $pres(attrs?: any): wrapper.Strophe.Builder; - -import Strophe = wrapper.Strophe; +declare function $pres(attrs?: any): Strophe.Builder; // Support AMD require -declare module '$build' { - export = wrapper.$build; +declare module "strophe" { + export = { Strophe, $build, $msg, $iq, $pres }; } -declare module '$msg' { - export = wrapper.$msg; + +declare module "Strophe" { + export = Strophe; } -declare module '$iq' { - export = wrapper.$iq; + +declare module "$build" { + export = $build; } -declare module '$pres' { - export = wrapper.$pres; + +declare module "$msg" { + export = $msg; } -declare module 'Strophe' { - export = wrapper.Strophe; + +declare module "$iq" { + export = $iq; } -declare module 'strophe' { - export = wrapper; + +declare module "$pres" { + export = $pres; } diff --git a/types/strophe/muc.d.ts b/types/strophe/muc.d.ts new file mode 100644 index 0000000000..7d377c2302 --- /dev/null +++ b/types/strophe/muc.d.ts @@ -0,0 +1,837 @@ +// Type definitions for strophe.muc.js v1.2.0 +// Project: https://github.com/metajack/strophejs-plugins +// Definitions by: Ilia Choly +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +declare namespace Strophe { + + interface Connection { + muc: MUC.Plugin; + } + + namespace MUC { + + interface Plugin { + + /** + * Initialize the MUC plugin. Sets the correct connection object and + * extends the namesace. + * + * @param conn - the connection instance. + */ + init(conn: Connection): void; + + /** + * Join a multi-user chat room + * + * @param room - The multi-user chat room to join. + * @param nick - The nickname to use in the chat room. Optional + * @param msg_handler_cb - The function call to handle messages from the specified chat room. + * @param pres_handler_cb - The function call back to handle presence in the chat room. + * @param roster_cb - The function call to handle roster info in the chat room + * @param password - The optional password to use. (password protected rooms only) + * @param history_attrs - Optional attributes for retrieving history + * @param extended_presence - Optional XML for extending presence + */ + join( + room: string, + nick: string, + msg_handler_cb: (stanza: Element, room: XmppRoom) => boolean, + pres_handler_cb: (stanza: Element, room: XmppRoom) => boolean, + roster_cb: (occupants: OccupantMap, room: XmppRoom) => boolean, + password?: string, + history_attrs?: any, + extended_presence?: Element + ): void; + + + /** + * Leave a multi-user chat room + * + * @param room - The multi-user chat room to leave. + * @param nick - The nick name used in the room. + * @param handler_cb - Optional function to handle the successful leave. + * @param exit_msg - optional exit message. + * @return iqid - The unique id for the room leave.* + */ + leave( + room: string, + nick: string, + handler_cb?: Function, + exit_msg?: string + ): string; + + + /** + * Send a message to a room. + * + * @param) room - The multi-user chat room name. + * @param nick - The nick name used in the chat room. + * @param message - The plaintext message to send to the room. + * @param html_message - The message to send to the room with html markup. + * @param type - "groupchat" for group chat messages o "chat" for private chat messages + * @return msgiq - the unique id used to send the message + */ + message( + room: string, + nick: string, + message: string, + html_message: string, + type: "groupchat"|"chat" + ): string; + + + /** + * Convenience Function to send a Message to all Occupants + * + * @param room - The multi-user chat room name. + * @param message - The plaintext message to send to the room. + * @param html_message - Optional message to send to the room with html markup. + * @param msgid - Optional unique ID which will be set as the 'id' attribute of the stanza + * @return msgiq - the unique id used to send the message + */ + groupchat( + room: string, + message: string, + html_message?: string, + msgid?: string + ): string; + + + /** + * Send a mediated invitation. + * + * @param room - The multi-user chat room name. + * @param receiver - The invitation's receiver. + * @param reason - Optional reason for joining the room. + * @return msgiq - the unique id used to send the invitation + */ + invite( + room: string, + receiver: string, + reason?: string + ): string; + + + /** + * Send a mediated multiple invitation. + * + * @param room - The multi-user chat room name. + * @param receivers - The invitation's receivers. + * @param reason - Optional reason for joining the room. + * @return msgiq - the unique id used to send the invitation + */ + multipleInvites( + room: string, + receivers: string[], + reason?: string + ): string; + + + /** + * Send a direct invitation. + * + * @param room - The multi-user chat room name. + * @param receiver - The invitation's receiver. + * @param reason - Optional reason for joining the room. + * @param password - Optional password for the room. + * @return msgiq - the unique id used to send the invitation + */ + directInvite( + room: string, + receiver: string, + reason?: string, + password?: string + ): string; + + + /** + * Queries a room for a list of occupants + * + * @param room - The multi-user chat room name. + * @param success_cb - Optional function to handle the info. + * @param error_cb - Optional function to handle an error. + * @return id - the unique id used to send the info request + */ + queryOccupants( + room: string, + success_cb?: (stanza: Element) => any, + error_cb?: (error: any) => any + ): string; + + + /** + * Start a room configuration. + * + * @param room - The multi-user chat room name. + * @param handler_cb - Optional function to handle the config form. + * @param error_cb - Optional function to handle an error. + * @return id - the unique id used to send the configuration request + */ + configure( + room: string, + handler_cb?: Function, + error_cb?: Function + ): string; + + + /** + * Cancel the room configuration + * + * @param room - The multi-user chat room name. + * @return id - the unique id used to cancel the configuration. + */ + cancelConfigure(room: string): string; + + + /** + * Save a room configuration. + * + * @param room - The multi-user chat room name. + * @param config- Form Object or an array of form elements used to configure the room. + * @param success_db - Optional function to handle success. + * @param error_cb - Optional function to handle an error. + * @return id - the unique id used to save the configuration. + */ + saveConfiguration( + room: string, + config: any, + success_cb?: Function, + error_cb?: Function + ): string; + + + /** + * Create an instance room. + * + * @param room - The multi-user chat room name. + * @param success_db - Optional function to handle success. + * @param error_cb - Optional function to handle an error. + * @return id - the unique id used to create the chat room. + */ + createInstantRoom( + room: string, + success_cb?: Function, + error_cb?: Function + ): string; + + + /** + * Create a configured room. + * + * @param room - The multi-user chat room name. + * @param config - the configuration. ex: {"muc#roomconfig_publicroom": "0", "muc#roomconfig_persistentroom": "1"} + * @param success_db - Optional function to handle success. + * @param error_cb - Optional function to handle an error. + * @return id - the unique id used to create the chat room. + */ + createConfiguredRoom( + room: string, + config: any, + success_cb: Function, + error_cb: Function + ): string; + + + /** + * Set the topic of the chat room. + * + * @param room - The multi-user chat room name. + * @param topic - Topic message. + */ + setTopic(room: string, topic: string): void; + + + /** + * Changes the role of a member of a MUC room. + * The modification can only be done by a room moderator. An error will be + * returned if the user doesn't have permission. + * + * @param room - The multi-user chat room name. + * @param nick - The nick name of the user to modify. + * @param role - The new role of the user. + * @param reason - Optional reason for the change. + * @param handler_cb - Optional callback for success + * @param error_cb - Optional callback for error + * @return iq - the id of the mode change request. + */ + modifyRole( + room: string, + nick: string, + role: string, + reason?: string, + handler_cb?: Function, + error_cb?: Function + ): string; + + + /** + * Kick a user. + * + * @param room - The multi-user chat room name. + * @param nick - The nick name of the user to modify. + * @param reason - Optional reason for the change. + * @param handler_cb - Optional callback for success + * @param error_cb - Optional callback for error + * @return iq - the id of the mode change request. + */ + kick( + room: string, + nick: string, + reason?: string, + handler_cb?: Function, + error_cb?: Function + ): string; + + + /** + * Voice a user. + * + * @param room - The multi-user chat room name. + * @param nick - The nick name of the user to modify. + * @param reason - Optional reason for the change. + * @param handler_cb - Optional callback for success + * @param error_cb - Optional callback for error + * @return iq - the id of the mode change request. + */ + voice( + room: string, + nick: string, + reason?: string, + handler_cb?: Function, + error_cb?: Function + ): string; + + + /** + * Mute a user. + * + * @param room - The multi-user chat room name. + * @param nick - The nick name of the user to modify. + * @param reason - Optional reason for the change. + * @param handler_cb - Optional callback for success + * @param error_cb - Optional callback for error + * @return iq - the id of the mode change request. + */ + mute( + room: string, + nick: string, + reason?: string, + handler_cb?: Function, + error_cb?: Function + ): string; + + + /** + * Op a user. + * + * @param room - The multi-user chat room name. + * @param nick - The nick name of the user to modify. + * @param reason - Optional reason for the change. + * @param handler_cb - Optional callback for success + * @param error_cb - Optional callback for error + * @return iq - the id of the mode change request. + */ + op( + room: string, + nick: string, + reason?: string, + handler_cb?: Function, + error_cb?: Function + ): string; + + + /** + * De-Op a user. + * + * @param room - The multi-user chat room name. + * @param nick - The nick name of the user to modify. + * @param reason - Optional reason for the change. + * @param handler_cb - Optional callback for success + * @param error_cb - Optional callback for error + * @return iq - the id of the mode change request. + */ + deop( + room: string, + nick: string, + reason?: string, + handler_cb?: Function, + error_cb?: Function + ): string; + + + /** + * Changes the affiliation of a member of a MUC room. + * The modification can only be done by a room moderator. An error will be + * returned if the user doesn't have permission. + * Parameters: + * @param room - The multi-user chat room name. + * @param jid - The jid of the user to modify. + * @param affiliation - The new affiliation of the user. + * @param reason - Optional reason for the change. + * @param handler_cb - Optional callback for success + * @param error_cb - Optional callback for error + * @return iq - the id of the mode change request. + */ + modifyAffiliation( + room: string, + jid: string, + affiliation: string, + reason?: string, + handler_cb?: Function, + error_cb?: Function + ): string; + + + /** + * Ban a user. + * + * @param room - The multi-user chat room name. + * @param jid - The jid of the user to modify. + * @param reason - Optional reason for the change. + * @param handler_cb - Optional callback for success + * @param error_cb - Optional callback for error + * @return iq - the id of the mode change request. + */ + ban( + room: string, + jid: string, + reason?: string, + handler_cb?: Function, + error_cb?: Function + ): string; + + + /** + * Member a user. + * + * @param room - The multi-user chat room name. + * @param jid - The jid of the user to modify. + * @param reason - Optional reason for the change. + * @param handler_cb - Optional callback for success + * @param error_cb - Optional callback for error + * @return iq - the id of the mode change request. + */ + member( + room: string, + jid: string, + reason?: string, + handler_cb?: Function, + error_cb?: Function + ): string; + + + /** + * Revoke a user. + * + * @param room - The multi-user chat room name. + * @param jid - The jid of the user to modify. + * @param reason - Optional reason for the change. + * @param handler_cb - Optional callback for success + * @param error_cb - Optional callback for error + * @return iq - the id of the mode change request. + */ + revoke( + room: string, + jid: string, + reason?: string, + handler_cb?: Function, + error_cb?: Function + ): string; + + + /** + * Owner a user. + * + * @param room - The multi-user chat room name. + * @param jid - The jid of the user to modify. + * @param reason - Optional reason for the change. + * @param handler_cb - Optional callback for success + * @param error_cb - Optional callback for error + * @return iq - the id of the mode change request. + */ + owner( + room: string, + jid: string, + reason?: string, + handler_cb?: Function, + error_cb?: Function + ): string; + + + /** + * Admin a user. + * + * @param room - The multi-user chat room name. + * @param jid - The jid of the user to modify. + * @param reason - Optional reason for the change. + * @param handler_cb - Optional callback for success + * @param error_cb - Optional callback for error + * @return iq - the id of the mode change request. + */ + owner( + room: string, + jid: string, + reason?: string, + handler_cb?: Function, + error_cb?: Function + ): string; + + + /** + * Change the current users nick name. + * + * @param room - The multi-user chat room name. + * @param user - The new nick name. + */ + changeNick(room: string, user: string): void; + + /** + * Change the current users status. + * + * @param room - The multi-user chat room name. + * @param user - The current nick. + * @param show - The new show-text. + * @param status - The new status-text. + */ + setStatus( + room: string, + user: string, + show: string, + status: string + ): void; + + /** + * Registering with a room. + * @see http://xmpp.org/extensions/xep-0045.html#register + * + * @param room - The multi-user chat room name. + * @param handle_cb - Function to call for room list return. + * @param error_cb - Function to call on error. + */ + registrationRequest( + room: string, + handle_cb: Function, + error_cb: Function + ): void; + + + /** + * Submits registration form. + * + * @param room - The multi-user chat room name. + * @param fields - The form fields. + * @param handle_cb - Function to call for room list return. + * @param error_cb - Function to call on error. + */ + submitRegistrationForm( + room: string, + fields: any, + handle_cb: Function, + error_cb: Function + ): void; + + /** + * List all chat room available on a server. + * + * @param server - name of chat server. + * @param handle_cb - Function to call for room list return. + * @param error_cb - Function to call on error. + */ + listRooms( + server: string, + handle_cb: (stanza: Element) => any, + error_cb: (error: any) => any + ): void; + } + + interface XmppRoom { + + join( + msg_handler_cb: Function, + pres_handler_cb: Function, + roster_cb: Function + ): void; + + + leave( + handler_cb?: Function, + exit_msg?: string + ): void; + + + message( + message: string, + html_message: string, + type: "groupchat"|"chat" + ): string; + + + groupchat(message: string, html_message?: string): string; + + + invite(receiver: string, reason?: string): string; + + + multipleInvites(receivers: string[], reason?: string): string; + + + directInvite(receiver: string, reason?: string): string; + + + configure(handler_cb: Function): string; + + + cancelConfigure(): string; + + + saveConfiguration(config: any): string; + + + queryOccupants(success_cb: Function, error_cb: Function): void; + + + setTopic(topic: string): string; + + + modifyRole( + nick: string, + role: string, + reason?: string, + success_cb?: Function, + error_cb?: Function + ): string; + + + kick( + nick: string, + reason?: string, + handler_cb?: Function, + error_cb?: Function + ): string; + + + voice( + nick: string, + reason?: string, + handler_cb?: Function, + error_cb?: Function + ): string; + + + mute( + nick: string, + reason?: string, + handler_cb?: Function, + error_cb?: Function + ): string; + + + op( + nick: string, + reason?: string, + handler_cb?: Function, + error_cb?: Function + ): string; + + + deop( + nick: string, + reason?: string, + handler_cb?: Function, + error_cb?: Function + ): string; + + + modifyAffiliation( + jid: string, + affiliation: string, + reason?: string, + success_cb?: Function, + error_cb?: Function + ): string; + + + ban( + jid: string, + reason?: string, + handler_cb?: Function, + error_cb?: Function + ): string; + + + member( + jid: string, + reason?: string, + handler_cb?: Function, + error_cb?: Function + ): string; + + + revoke( + jid: string, + reason?: string, + handler_cb?: Function, + error_cb?: Function + ): string; + + + owner( + jid: string, + reason?: string, + handler_cb?: Function, + error_cb?: Function + ): string; + + + admin( + jid: string, + reason?: string, + handler_cb?: Function, + error_cb?: Function + ): string; + + + changeNick(nick: string): string; + + + setStatus(show: string, status: string): string; + + + /** + * Adds a handler to the MUC room. + * Parameters: + * @param handler_type - 'message', 'presence' or 'roster'. + * @param handler - The handler function. + * @return id - the id of handler. + */ + addHandler( + handler_type: "presence"|"message"|"roster", + handler: Function + ): number; + + + /** + * Removes a handler from the MUC room. + * This function takes ONLY ids returned by the addHandler function + * of this room. passing handler ids returned by connection.addHandler + * may brake things! + * + * @param id - the id of the handler + */ + removeHandler(id: number): void; + + } + + + interface RoomConfig { + parse(info?: Element): any; + } + + + interface OccupantInfo { + nick?: string; + affiliation?: string; + role?: string; + jid?: string; + status?: string; + show?: string + } + + + interface Occupant extends OccupantInfo { + + modifyRole( + role: string, + reason?: string, + success_cb?: Function, + error_cb?: Function + ): string; + + + kick( + reason?: string, + handler_cb?: Function, + error_cb?: Function + ): string; + + + voice( + reason?: string, + handler_cb?: Function, + error_cb?: Function + ): string; + + mute( + reason?: string, + handler_cb?: Function, + error_cb?: Function + ): string; + + + op( + reason?: string, + handler_cb?: Function, + error_cb?: Function + ): string; + + + deop( + reason?: string, + handler_cb?: Function, + error_cb?: Function + ): string; + + + modifyAffiliation( + affiliation: string, + reason?: string, + success_cb?: Function, + error_cb?: Function + ): string; + + + ban( + reason?: string, + success_cb?: Function, + error_cb?: Function + ): string; + + + member( + reason?: string, + success_cb?: Function, + error_cb?: Function + ): string; + + + revoke( + reason?: string, + success_cb?: Function, + error_cb?: Function + ): string; + + + owner( + reason?: string, + success_cb?: Function, + error_cb?: Function + ): string; + + + admin( + reason?: string, + success_cb?: Function, + error_cb?: Function + ): string; + + update(data: OccupantInfo): void; + } + + interface OccupantMap { + [jid: string]: Occupant; + } + + } +} + diff --git a/types/strophe/strophe-tests.ts b/types/strophe/strophe-tests.ts index 45611ae04d..c425a4efee 100644 --- a/types/strophe/strophe-tests.ts +++ b/types/strophe/strophe-tests.ts @@ -98,3 +98,26 @@ function onConnect(status: Strophe.Status): void { connection.send($pres().tree()); } } + +function onRoomMessage(stanza: Element, room: Strophe.MUC.XmppRoom): boolean { + console.log(Strophe.serialize(stanza)); + room.groupchat("hello"); + return true; +} + +function onRoomPresence(stanza: Element, room: Strophe.MUC.XmppRoom): boolean { + let from = stanza.getAttribute("from"); + console.log(`${from} precense updated`); + return true; +} + +function onRoomRoster(occupants: Strophe.MUC.OccupantMap, room: Strophe.MUC.XmppRoom): boolean { + for (let nick of Object.keys(occupants)) { + let occupant = occupants[nick]; + console.log(occupant.nick, occupant.show, occupant.status); + } + return true; +} + +connection.muc.init(connection); +connection.muc.join("room", "nick", onRoomMessage, onRoomPresence, onRoomRoster); diff --git a/types/strophe/tsconfig.json b/types/strophe/tsconfig.json index 64f2d4ab19..eee9184d3b 100644 --- a/types/strophe/tsconfig.json +++ b/types/strophe/tsconfig.json @@ -18,6 +18,7 @@ }, "files": [ "index.d.ts", + "muc.d.ts", "strophe-tests.ts" ] -} \ No newline at end of file +}