js/strophe/strophe.js

changeset 18
9e4230bb66e4
parent 17
6344a9a20da2
child 19
41a3c9d41e6a
equal deleted inserted replaced
17:6344a9a20da2 18:9e4230bb66e4
1 /*
2 This program is distributed under the terms of the MIT license.
3 Please see the LICENSE file for details.
4
5 Copyright 2006-2008, OGG, LLC
6 */
7
8 /** File: strophe.js
9 * A JavaScript library for XMPP BOSH.
10 *
11 * This is the JavaScript version of the Strophe library. Since JavaScript
12 * has no facilities for persistent TCP connections, this library uses
13 * Bidirectional-streams Over Synchronous HTTP (BOSH) to emulate
14 * a persistent, stateful, two-way connection to an XMPP server. More
15 * information on BOSH can be found in XEP 124.
16 */
17
18 /** PrivateFunction: Function.prototype.bind
19 * Bind a function to an instance.
20 *
21 * This Function object extension method creates a bound method similar
22 * to those in Python. This means that the 'this' object will point
23 * to the instance you want. See
24 * <a href='http://benjamin.smedbergs.us/blog/2007-01-03/bound-functions-and-function-imports-in-javascript/'>Bound Functions and Function Imports in JavaScript</a>
25 * for a complete explanation.
26 *
27 * This extension already exists in some browsers (namely, Firefox 3), but
28 * we provide it to support those that don't.
29 *
30 * Parameters:
31 * (Object) obj - The object that will become 'this' in the bound function.
32 *
33 * Returns:
34 * The bound function.
35 */
36 if (!Function.prototype.bind) {
37 Function.prototype.bind = function (obj)
38 {
39 var func = this;
40 return function () { return func.apply(obj, arguments); };
41 };
42 }
43
44 /** PrivateFunction: Function.prototype.prependArg
45 * Prepend an argument to a function.
46 *
47 * This Function object extension method returns a Function that will
48 * invoke the original function with an argument prepended. This is useful
49 * when some object has a callback that needs to get that same object as
50 * an argument. The following fragment illustrates a simple case of this
51 * > var obj = new Foo(this.someMethod);</code></blockquote>
52 *
53 * Foo's constructor can now use func.prependArg(this) to ensure the
54 * passed in callback function gets the instance of Foo as an argument.
55 * Doing this without prependArg would mean not setting the callback
56 * from the constructor.
57 *
58 * This is used inside Strophe for passing the Strophe.Request object to
59 * the onreadystatechange handler of XMLHttpRequests.
60 *
61 * Parameters:
62 * arg - The argument to pass as the first parameter to the function.
63 *
64 * Returns:
65 * A new Function which calls the original with the prepended argument.
66 */
67 if (!Function.prototype.prependArg) {
68 Function.prototype.prependArg = function (arg)
69 {
70 var func = this;
71
72 return function () {
73 var newargs = [arg];
74 for (var i = 0; i < arguments.length; i++)
75 newargs.push(arguments[i]);
76 return func.apply(this, newargs);
77 };
78 };
79 }
80
81 /** PrivateFunction: Array.prototype.indexOf
82 * Return the index of an object in an array.
83 *
84 * This function is not supplied by some JavaScript implementations, so
85 * we provide it if it is missing. This code is from:
86 * http://developer.mozilla.org/En/Core_JavaScript_1.5_Reference:Objects:Array:indexOf
87 *
88 * Parameters:
89 * (Object) elt - The object to look for.
90 * (Integer) from - The index from which to start looking. (optional).
91 *
92 * Returns:
93 * The index of elt in the array or -1 if not found.
94 */
95 if (!Array.prototype.indexOf)
96 {
97 Array.prototype.indexOf = function(elt /*, from*/)
98 {
99 var len = this.length;
100
101 var from = Number(arguments[1]) || 0;
102 from = (from < 0) ? Math.ceil(from) : Math.floor(from);
103 if (from < 0)
104 from += len;
105
106 for (; from < len; from++) {
107 if (from in this && this[from] === elt)
108 return from;
109 }
110
111 return -1;
112 };
113 }
114
115
116 /** Function: $build
117 * Create a Strophe.Builder.
118 * This is an alias for 'new Strophe.Builder(name, attrs)'.
119 *
120 * Parameters:
121 * (String) name - The root element name.
122 * (Object) attrs - The attributes for the root element in object notation.
123 *
124 * Returns:
125 * A new Strophe.Builder object.
126 */
127 function $build(name, attrs) { return new Strophe.Builder(name, attrs); }
128 /** Function: $msg
129 * Create a Strophe.Builder with a <message/> element as the root.
130 *
131 * Parmaeters:
132 * (Object) attrs - The <message/> element attributes in object notation.
133 *
134 * Returns:
135 * A new Strophe.Builder object.
136 */
137 function $msg(attrs) { return new Strophe.Builder("message", attrs); }
138 /** Function: $iq
139 * Create a Strophe.Builder with an <iq/> element as the root.
140 *
141 * Parameters:
142 * (Object) attrs - The <iq/> element attributes in object notation.
143 *
144 * Returns:
145 * A new Strophe.Builder object.
146 */
147 function $iq(attrs) { return new Strophe.Builder("iq", attrs); }
148 /** Function: $pres
149 * Create a Strophe.Builder with a <presence/> element as the root.
150 *
151 * Parameters:
152 * (Object) attrs - The <presence/> element attributes in object notation.
153 *
154 * Returns:
155 * A new Strophe.Builder object.
156 */
157 function $pres(attrs) { return new Strophe.Builder("presence", attrs); }
158
159 /** Class: Strophe
160 * An object container for all Strophe library functions.
161 *
162 * This class is just a container for all the objects and constants
163 * used in the library. It is not meant to be instantiated, but to
164 * provide a namespace for library objects, constants, and functions.
165 */
166 Strophe = {
167 /** Constants: XMPP Namespace Constants
168 * Common namespace constants from the XMPP RFCs and XEPs.
169 *
170 * NS.HTTPBIND - HTTP BIND namespace from XEP 124.
171 * NS.BOSH - BOSH namespace from XEP 206.
172 * NS.CLIENT - Main XMPP client namespace.
173 * NS.AUTH - Legacy authentication namespace.
174 * NS.ROSTER - Roster operations namespace.
175 * NS.PROFILE - Profile namespace.
176 * NS.DISCO_INFO - Service discovery info namespace from XEP 30.
177 * NS.DISCO_ITEMS - Service discovery items namespace from XEP 30.
178 * NS.MUC - Multi-User Chat namespace from XEP 45.
179 * NS.SASL - XMPP SASL namespace from RFC 3920.
180 * NS.STREAM - XMPP Streams namespace from RFC 3920.
181 * NS.BIND - XMPP Binding namespace from RFC 3920.
182 * NS.SESSION - XMPP Session namespace from RFC 3920.
183 */
184 NS: {
185 HTTPBIND: "http://jabber.org/protocol/httpbind",
186 BOSH: "urn:xmpp:xbosh",
187 CLIENT: "jabber:client",
188 AUTH: "jabber:iq:auth",
189 ROSTER: "jabber:iq:roster",
190 PROFILE: "jabber:iq:profile",
191 DISCO_INFO: "http://jabber.org/protocol/disco#info",
192 DISCO_ITEMS: "http://jabber.org/protocol/disco#items",
193 MUC: "http://jabber.org/protocol/muc",
194 SASL: "urn:ietf:params:xml:ns:xmpp-sasl",
195 STREAM: "http://etherx.jabber.org/streams",
196 BIND: "urn:ietf:params:xml:ns:xmpp-bind",
197 SESSION: "urn:ietf:params:xml:ns:xmpp-session",
198 VERSION: "jabber:iq:version",
199 STANZAS: "urn:ietf:params:xml:ns:xmpp-stanzas"
200 },
201
202 /** Function: addNamespace
203 * This function is used to extend the current namespaces in
204 * Strophe.NS. It takes a key and a value with the key being the
205 * name of the new namespace, with its actual value.
206 * For example:
207 * Strophe.addNamespace('PUBSUB', "http://jabber.org/protocol/pubsub");
208 *
209 * Parameters:
210 * (String) name - The name under which the namespace will be
211 * referenced under Strophe.NS
212 * (String) value - The actual namespace.
213 */
214 addNamespace: function (name, value)
215 {
216 Strophe.NS[name] = value;
217 },
218
219 /** Constants: Connection Status Constants
220 * Connection status constants for use by the connection handler
221 * callback.
222 *
223 * Status.ERROR - An error has occurred
224 * Status.CONNECTING - The connection is currently being made
225 * Status.CONNFAIL - The connection attempt failed
226 * Status.AUTHENTICATING - The connection is authenticating
227 * Status.AUTHFAIL - The authentication attempt failed
228 * Status.CONNECTED - The connection has succeeded
229 * Status.DISCONNECTED - The connection has been terminated
230 * Status.DISCONNECTING - The connection is currently being terminated
231 */
232 Status: {
233 ERROR: 0,
234 CONNECTING: 1,
235 CONNFAIL: 2,
236 AUTHENTICATING: 3,
237 AUTHFAIL: 4,
238 CONNECTED: 5,
239 DISCONNECTED: 6,
240 DISCONNECTING: 7
241 },
242
243 /** Constants: Log Level Constants
244 * Logging level indicators.
245 *
246 * LogLevel.DEBUG - Debug output
247 * LogLevel.INFO - Informational output
248 * LogLevel.WARN - Warnings
249 * LogLevel.ERROR - Errors
250 * LogLevel.FATAL - Fatal errors
251 */
252 LogLevel: {
253 DEBUG: 0,
254 INFO: 1,
255 WARN: 2,
256 ERROR: 3,
257 FATAL: 4
258 },
259
260 /** PrivateConstants: DOM Element Type Constants
261 * DOM element types.
262 *
263 * ElementType.NORMAL - Normal element.
264 * ElementType.TEXT - Text data element.
265 */
266 ElementType: {
267 NORMAL: 1,
268 TEXT: 3
269 },
270
271 /** PrivateConstants: Timeout Values
272 * Timeout values for error states. These values are in seconds.
273 * These should not be changed unless you know exactly what you are
274 * doing.
275 *
276 * TIMEOUT - Time to wait for a request to return. This defaults to
277 * 70 seconds.
278 * SECONDARY_TIMEOUT - Time to wait for immediate request return. This
279 * defaults to 7 seconds.
280 */
281 TIMEOUT: 70,
282 SECONDARY_TIMEOUT: 7,
283
284 /** Function: forEachChild
285 * Map a function over some or all child elements of a given element.
286 *
287 * This is a small convenience function for mapping a function over
288 * some or all of the children of an element. If elemName is null, all
289 * children will be passed to the function, otherwise only children
290 * whose tag names match elemName will be passed.
291 *
292 * Parameters:
293 * (XMLElement) elem - The element to operate on.
294 * (String) elemName - The child element tag name filter.
295 * (Function) func - The function to apply to each child. This
296 * function should take a single argument, a DOM element.
297 */
298 forEachChild: function (elem, elemName, func)
299 {
300 var i, childNode;
301
302 for (i = 0; i < elem.childNodes.length; i++) {
303 childNode = elem.childNodes[i];
304 if (childNode.nodeType == Strophe.ElementType.NORMAL &&
305 (!elemName || this.isTagEqual(childNode, elemName))) {
306 func(childNode);
307 }
308 }
309 },
310
311 /** Function: isTagEqual
312 * Compare an element's tag name with a string.
313 *
314 * This function is case insensitive.
315 *
316 * Parameters:
317 * (XMLElement) el - A DOM element.
318 * (String) name - The element name.
319 *
320 * Returns:
321 * true if the element's tag name matches _el_, and false
322 * otherwise.
323 */
324 isTagEqual: function (el, name)
325 {
326 return el.tagName.toLowerCase() == name.toLowerCase();
327 },
328
329 /** Function: xmlElement
330 * Create an XML DOM element.
331 *
332 * This function creates an XML DOM element correctly across all
333 * implementations. Specifically the Microsoft implementation of
334 * document.createElement makes DOM elements with 43+ default attributes
335 * unless elements are created with the ActiveX object Microsoft.XMLDOM.
336 *
337 * Most DOMs force element names to lowercase, so we use the
338 * _realname attribute on the created element to store the case
339 * sensitive name. This is required to generate proper XML for
340 * things like vCard avatars (XEP 153). This attribute is stripped
341 * out before being sent over the wire or serialized, but you may
342 * notice it during debugging.
343 *
344 * Parameters:
345 * (String) name - The name for the element.
346 * (Array) attrs - An optional array of key/value pairs to use as
347 * element attributes in the following format [['key1', 'value1'],
348 * ['key2', 'value2']]
349 * (String) text - The text child data for the element.
350 *
351 * Returns:
352 * A new XML DOM element.
353 */
354 xmlElement: function (name)
355 {
356 // FIXME: this should also support attrs argument in object notation
357 if (!name) { return null; }
358
359 var node = null;
360 if (window.ActiveXObject) {
361 node = new ActiveXObject("Microsoft.XMLDOM").createElement(name);
362 } else {
363 node = document.createElement(name);
364 }
365 // use node._realname to store the case-sensitive version of the tag
366 // name, since some browsers will force tagnames to all lowercase.
367 // this is needed for the <vCard/> tag in XMPP specifically.
368 if (node.tagName != name)
369 node.setAttribute("_realname", name);
370
371 // FIXME: this should throw errors if args are the wrong type or
372 // there are more than two optional args
373 var a, i;
374 for (a = 1; a < arguments.length; a++) {
375 if (!arguments[a]) { continue; }
376 if (typeof(arguments[a]) == "string" ||
377 typeof(arguments[a]) == "number") {
378 node.appendChild(Strophe.xmlTextNode(arguments[a]));
379 } else if (typeof(arguments[a]) == "object" &&
380 typeof(arguments[a]['sort']) == "function") {
381 for (i = 0; i < arguments[a].length; i++) {
382 if (typeof(arguments[a][i]) == "object" &&
383 typeof(arguments[a][i]['sort']) == "function") {
384 node.setAttribute(arguments[a][i][0],
385 arguments[a][i][1]);
386 }
387 }
388 }
389 }
390
391 return node;
392 },
393 /* Function: xmlescape
394 * Excapes invalid xml characters.
395 *
396 * Parameters:
397 * (String) text - text to escape.
398 *
399 * Returns:
400 * Escaped text.
401 */
402 xmlescape: function(text)
403 {
404 text = text.replace(/\&/g, "&amp;");
405 text = text.replace(/</g, "&lt;");
406 text = text.replace(/>/g, "&gt;");
407 return text;
408 },
409 /** Function: xmlTextNode
410 * Creates an XML DOM text node.
411 *
412 * Provides a cross implementation version of document.createTextNode.
413 *
414 * Parameters:
415 * (String) text - The content of the text node.
416 *
417 * Returns:
418 * A new XML DOM text node.
419 */
420 xmlTextNode: function (text)
421 {
422 //ensure text is escaped
423 text = Strophe.xmlescape(text);
424 if (window.ActiveXObject) {
425 return new ActiveXObject("Microsoft.XMLDOM").createTextNode(text);
426 } else {
427 return document.createTextNode(text);
428 }
429 },
430
431 /** Function: getText
432 * Get the concatenation of all text children of an element.
433 *
434 * Parameters:
435 * (XMLElement) elem - A DOM element.
436 *
437 * Returns:
438 * A String with the concatenated text of all text element children.
439 */
440 getText: function (elem)
441 {
442 if (!elem) return null;
443
444 var str = "";
445 if (elem.childNodes.length === 0 && elem.nodeType ==
446 Strophe.ElementType.TEXT) {
447 str += elem.nodeValue;
448 }
449
450 for (var i = 0; i < elem.childNodes.length; i++) {
451 if (elem.childNodes[i].nodeType == Strophe.ElementType.TEXT) {
452 str += elem.childNodes[i].nodeValue;
453 }
454 }
455
456 return str;
457 },
458
459 /** Function: copyElement
460 * Copy an XML DOM element.
461 *
462 * This function copies a DOM element and all its descendants and returns
463 * the new copy.
464 *
465 * Parameters:
466 * (XMLElement) elem - A DOM element.
467 *
468 * Returns:
469 * A new, copied DOM element tree.
470 */
471 copyElement: function (elem)
472 {
473 var i, el;
474 if (elem.nodeType == Strophe.ElementType.NORMAL) {
475 el = Strophe.xmlElement(elem.tagName);
476
477 for (i = 0; i < elem.attributes.length; i++) {
478 el.setAttribute(elem.attributes[i].nodeName.toLowerCase(),
479 elem.attributes[i].value);
480 }
481
482 for (i = 0; i < elem.childNodes.length; i++) {
483 el.appendChild(Strophe.copyElement(elem.childNodes[i]));
484 }
485 } else if (elem.nodeType == Strophe.ElementType.TEXT) {
486 el = Strophe.xmlTextNode(elem.nodeValue);
487 }
488
489 return el;
490 },
491
492 /** Function: escapeJid
493 * Escape a JID.
494 *
495 * Parameters:
496 * (String) jid - A JID.
497 *
498 * Returns:
499 * An escaped JID String.
500 */
501 escapeJid: function (jid)
502 {
503 var user = jid.split("@");
504 if (user.length == 1)
505 // no user so nothing to escape
506 return jid;
507
508 var host = user.splice(user.length - 1, 1)[0];
509 user = user.join("@")
510 .replace(/^\s+|\s+$/g, '')
511 .replace(/\\/g, "\\5c")
512 .replace(/ /g, "\\20")
513 .replace(/\"/g, "\\22")
514 .replace(/\&/g, "\\26")
515 .replace(/\'/g, "\\27")
516 .replace(/\//g, "\\2f")
517 .replace(/:/g, "\\3a")
518 .replace(/</g, "\\3c")
519 .replace(/>/g, "\\3e")
520 .replace(/@/g, "\\40");
521
522 return [user, host].join("@");
523 },
524
525 /** Function: unescapeJid
526 * Unescape a JID.
527 *
528 * Parameters:
529 * (String) jid - A JID.
530 *
531 * Returns:
532 * An unescaped JID String.
533 */
534 unescapeJid: function (jid)
535 {
536 return jid.replace(/\\20/g, " ")
537 .replace(/\\22/g, '"')
538 .replace(/\\26/g, "&")
539 .replace(/\\27/g, "'")
540 .replace(/\\2f/g, "/")
541 .replace(/\\3a/g, ":")
542 .replace(/\\3c/g, "<")
543 .replace(/\\3e/g, ">")
544 .replace(/\\40/g, "@")
545 .replace(/\\5c/g, "\\");
546 },
547
548 /** Function: getNodeFromJid
549 * Get the node portion of a JID String.
550 *
551 * Parameters:
552 * (String) jid - A JID.
553 *
554 * Returns:
555 * A String containing the node.
556 */
557 getNodeFromJid: function (jid)
558 {
559 if (jid.indexOf("@") < 0)
560 return null;
561 return Strophe.escapeJid(jid).split("@")[0];
562 },
563
564 /** Function: getDomainFromJid
565 * Get the domain portion of a JID String.
566 *
567 * Parameters:
568 * (String) jid - A JID.
569 *
570 * Returns:
571 * A String containing the domain.
572 */
573 getDomainFromJid: function (jid)
574 {
575 var bare = Strophe.escapeJid(Strophe.getBareJidFromJid(jid));
576 if (bare.indexOf("@") < 0)
577 return bare;
578 else
579 return bare.split("@")[1];
580 },
581
582 /** Function: getResourceFromJid
583 * Get the resource portion of a JID String.
584 *
585 * Parameters:
586 * (String) jid - A JID.
587 *
588 * Returns:
589 * A String containing the resource.
590 */
591 getResourceFromJid: function (jid)
592 {
593 var j = Strophe.escapeJid(jid);
594 var s = j.indexOf("/");
595 if (s < 0) return null;
596 return j.substr(s+1);
597 },
598
599 /** Function: getBareJidFromJid
600 * Get the bare JID from a JID String.
601 *
602 * Parameters:
603 * (String) jid - A JID.
604 *
605 * Returns:
606 * A String containing the bare JID.
607 */
608 getBareJidFromJid: function (jid)
609 {
610 return this.escapeJid(jid).split("/")[0];
611 },
612
613 /** Function: log
614 * User overrideable logging function.
615 *
616 * This function is called whenever the Strophe library calls any
617 * of the logging functions. The default implementation of this
618 * function does nothing. If client code wishes to handle the logging
619 * messages, it should override this with
620 * > Strophe.log = function (level, msg) {
621 * > (user code here)
622 * > };
623 *
624 * Please note that data sent and received over the wire is logged
625 * via Strophe.Connection.rawInput() and Strophe.Connection.rawOutput().
626 *
627 * The different levels and their meanings are
628 *
629 * DEBUG - Messages useful for debugging purposes.
630 * INFO - Informational messages. This is mostly information like
631 * 'disconnect was called' or 'SASL auth succeeded'.
632 * WARN - Warnings about potential problems. This is mostly used
633 * to report transient connection errors like request timeouts.
634 * ERROR - Some error occurred.
635 * FATAL - A non-recoverable fatal error occurred.
636 *
637 * Parameters:
638 * (Integer) level - The log level of the log message. This will
639 * be one of the values in Strophe.LogLevel.
640 * (String) msg - The log message.
641 */
642 log: function (level, msg)
643 {
644 return;
645 },
646
647 /** Function: debug
648 * Log a message at the Strophe.LogLevel.DEBUG level.
649 *
650 * Parameters:
651 * (String) msg - The log message.
652 */
653 debug: function(msg)
654 {
655 this.log(this.LogLevel.DEBUG, msg);
656 },
657
658 /** Function: info
659 * Log a message at the Strophe.LogLevel.INFO level.
660 *
661 * Parameters:
662 * (String) msg - The log message.
663 */
664 info: function (msg)
665 {
666 this.log(this.LogLevel.INFO, msg);
667 },
668
669 /** Function: warn
670 * Log a message at the Strophe.LogLevel.WARN level.
671 *
672 * Parameters:
673 * (String) msg - The log message.
674 */
675 warn: function (msg)
676 {
677 this.log(this.LogLevel.WARN, msg);
678 },
679
680 /** Function: error
681 * Log a message at the Strophe.LogLevel.ERROR level.
682 *
683 * Parameters:
684 * (String) msg - The log message.
685 */
686 error: function (msg)
687 {
688 this.log(this.LogLevel.ERROR, msg);
689 },
690
691 /** Function: fatal
692 * Log a message at the Strophe.LogLevel.FATAL level.
693 *
694 * Parameters:
695 * (String) msg - The log message.
696 */
697 fatal: function (msg)
698 {
699 this.log(this.LogLevel.FATAL, msg);
700 },
701
702 /** Function: serialize
703 * Render a DOM element and all descendants to a String.
704 *
705 * Parameters:
706 * (XMLElement) elem - A DOM element.
707 *
708 * Returns:
709 * The serialized element tree as a String.
710 */
711 serialize: function (elem)
712 {
713 var result;
714
715 if (!elem) return null;
716
717 if (typeof(elem["tree"]) === "function") {
718 elem = elem.tree();
719 }
720
721 var nodeName = elem.nodeName;
722 var i, child;
723
724 if (elem.getAttribute("_realname")) {
725 nodeName = elem.getAttribute("_realname");
726 }
727
728 result = "<" + nodeName;
729 for (i = 0; i < elem.attributes.length; i++) {
730 if(elem.attributes[i].nodeName != "_realname") {
731 result += " " + elem.attributes[i].nodeName.toLowerCase() +
732 "='" + elem.attributes[i].value
733 .replace("'", "&#39;").replace("&", "&#x26;") + "'";
734 }
735 }
736
737 if (elem.childNodes.length > 0) {
738 result += ">";
739 for (i = 0; i < elem.childNodes.length; i++) {
740 child = elem.childNodes[i];
741 if (child.nodeType == Strophe.ElementType.NORMAL) {
742 // normal element, so recurse
743 result += Strophe.serialize(child);
744 } else if (child.nodeType == Strophe.ElementType.TEXT) {
745 // text element
746 result += child.nodeValue;
747 }
748 }
749 result += "</" + nodeName + ">";
750 } else {
751 result += "/>";
752 }
753
754 return result;
755 },
756
757 /** PrivateVariable: _requestId
758 * _Private_ variable that keeps track of the request ids for
759 * connections.
760 */
761 _requestId: 0,
762
763 /** PrivateVariable: Strophe.connectionPlugins
764 * _Private_ variable Used to store plugin names that need
765 * initialization on Strophe.Connection construction.
766 */
767 _connectionPlugins: {},
768
769 /** Function: addConnectionPlugin
770 * Extends the Strophe.Connection object with the given plugin.
771 *
772 * Paramaters:
773 * (String) name - The name of the extension.
774 * (Object) ptype - The plugin's prototype.
775 */
776 addConnectionPlugin: function (name, ptype)
777 {
778 Strophe._connectionPlugins[name] = ptype;
779 }
780 };
781
782 /** Class: Strophe.Builder
783 * XML DOM builder.
784 *
785 * This object provides an interface similar to JQuery but for building
786 * DOM element easily and rapidly. All the functions except for toString()
787 * and tree() return the object, so calls can be chained. Here's an
788 * example using the $iq() builder helper.
789 * > $iq({to: 'you': from: 'me': type: 'get', id: '1'})
790 * > .c('query', {xmlns: 'strophe:example'})
791 * > .c('example')
792 * > .toString()
793 * The above generates this XML fragment
794 * > <iq to='you' from='me' type='get' id='1'>
795 * > <query xmlns='strophe:example'>
796 * > <example/>
797 * > </query>
798 * > </iq>
799 * The corresponding DOM manipulations to get a similar fragment would be
800 * a lot more tedious and probably involve several helper variables.
801 *
802 * Since adding children makes new operations operate on the child, up()
803 * is provided to traverse up the tree. To add two children, do
804 * > builder.c('child1', ...).up().c('child2', ...)
805 * The next operation on the Builder will be relative to the second child.
806 */
807
808 /** Constructor: Strophe.Builder
809 * Create a Strophe.Builder object.
810 *
811 * The attributes should be passed in object notation. For example
812 * > var b = new Builder('message', {to: 'you', from: 'me'});
813 * or
814 * > var b = new Builder('messsage', {'xml:lang': 'en'});
815 *
816 * Parameters:
817 * (String) name - The name of the root element.
818 * (Object) attrs - The attributes for the root element in object notation.
819 *
820 * Returns:
821 * A new Strophe.Builder.
822 */
823 Strophe.Builder = function (name, attrs)
824 {
825 // Set correct namespace for jabber:client elements
826 if (name == "presence" || name == "message" || name == "iq") {
827 if (attrs && !attrs.xmlns)
828 attrs.xmlns = Strophe.NS.CLIENT;
829 else if (!attrs)
830 attrs = {xmlns: Strophe.NS.CLIENT};
831 }
832
833 // Holds the tree being built.
834 this.nodeTree = this._makeNode(name, attrs);
835
836 // Points to the current operation node.
837 this.node = this.nodeTree;
838 };
839
840 Strophe.Builder.prototype = {
841 /** Function: tree
842 * Return the DOM tree.
843 *
844 * This function returns the current DOM tree as an element object. This
845 * is suitable for passing to functions like Strophe.Connection.send().
846 *
847 * Returns:
848 * The DOM tree as a element object.
849 */
850 tree: function ()
851 {
852 return this.nodeTree;
853 },
854
855 /** Function: toString
856 * Serialize the DOM tree to a String.
857 *
858 * This function returns a string serialization of the current DOM
859 * tree. It is often used internally to pass data to a
860 * Strophe.Request object.
861 *
862 * Returns:
863 * The serialized DOM tree in a String.
864 */
865 toString: function ()
866 {
867 return Strophe.serialize(this.nodeTree);
868 },
869
870 /** Function: up
871 * Make the current parent element the new current element.
872 *
873 * This function is often used after c() to traverse back up the tree.
874 * For example, to add two children to the same element
875 * > builder.c('child1', {}).up().c('child2', {});
876 *
877 * Returns:
878 * The Stophe.Builder object.
879 */
880 up: function ()
881 {
882 this.node = this.node.parentNode;
883 return this;
884 },
885
886 /** Function: attrs
887 * Add or modify attributes of the current element.
888 *
889 * The attributes should be passed in object notation. This function
890 * does not move the current element pointer.
891 *
892 * Parameters:
893 * (Object) moreattrs - The attributes to add/modify in object notation.
894 *
895 * Returns:
896 * The Strophe.Builder object.
897 */
898 attrs: function (moreattrs)
899 {
900 for (var k in moreattrs)
901 this.node.setAttribute(k, moreattrs[k]);
902 return this;
903 },
904
905 /** Function: c
906 * Add a child to the current element and make it the new current
907 * element.
908 *
909 * This function moves the current element pointer to the child. If you
910 * need to add another child, it is necessary to use up() to go back
911 * to the parent in the tree.
912 *
913 * Parameters:
914 * (String) name - The name of the child.
915 * (Object) attrs - The attributes of the child in object notation.
916 *
917 * Returns:
918 * The Strophe.Builder object.
919 */
920 c: function (name, attrs)
921 {
922 var child = this._makeNode(name, attrs);
923 this.node.appendChild(child);
924 this.node = child;
925 return this;
926 },
927
928 /** Function: cnode
929 * Add a child to the current element and make it the new current
930 * element.
931 *
932 * This function is the same as c() except that instead of using a
933 * name and an attributes object to create the child it uses an
934 * existing DOM element object.
935 *
936 * Parameters:
937 * (XMLElement) elem - A DOM element.
938 *
939 * Returns:
940 * The Strophe.Builder object.
941 */
942 cnode: function (elem)
943 {
944 this.node.appendChild(elem);
945 this.node = elem;
946 return this;
947 },
948
949 /** Function: t
950 * Add a child text element.
951 *
952 * This *does not* make the child the new current element since there
953 * are no children of text elements.
954 *
955 * Parameters:
956 * (String) text - The text data to append to the current element.
957 *
958 * Returns:
959 * The Strophe.Builder object.
960 */
961 t: function (text)
962 {
963 var child = Strophe.xmlTextNode(text);
964 this.node.appendChild(child);
965 return this;
966 },
967
968 /** PrivateFunction: _makeNode
969 * _Private_ helper function to create a DOM element.
970 *
971 * Parameters:
972 * (String) name - The name of the new element.
973 * (Object) attrs - The attributes for the new element in object
974 * notation.
975 *
976 * Returns:
977 * A new DOM element.
978 */
979 _makeNode: function (name, attrs)
980 {
981 var node = Strophe.xmlElement(name);
982 for (var k in attrs)
983 node.setAttribute(k, attrs[k]);
984 return node;
985 }
986 };
987
988
989 /** PrivateClass: Strophe.Handler
990 * _Private_ helper class for managing stanza handlers.
991 *
992 * A Strophe.Handler encapsulates a user provided callback function to be
993 * executed when matching stanzas are received by the connection.
994 * Handlers can be either one-off or persistant depending on their
995 * return value. Returning true will cause a Handler to remain active, and
996 * returning false will remove the Handler.
997 *
998 * Users will not use Strophe.Handler objects directly, but instead they
999 * will use Strophe.Connection.addHandler() and
1000 * Strophe.Connection.deleteHandler().
1001 */
1002
1003 /** PrivateConstructor: Strophe.Handler
1004 * Create and initialize a new Strophe.Handler.
1005 *
1006 * Parameters:
1007 * (Function) handler - A function to be executed when the handler is run.
1008 * (String) ns - The namespace to match.
1009 * (String) name - The element name to match.
1010 * (String) type - The element type to match.
1011 * (String) id - The element id attribute to match.
1012 * (String) from - The element from attribute to match.
1013 *
1014 * Returns:
1015 * A new Strophe.Handler object.
1016 */
1017 Strophe.Handler = function (handler, ns, name, type, id, from)
1018 {
1019 this.handler = handler;
1020 this.ns = ns;
1021 this.name = name;
1022 this.type = type;
1023 this.id = id;
1024 this.from = from;
1025
1026 // whether the handler is a user handler or a system handler
1027 this.user = true;
1028 };
1029
1030 Strophe.Handler.prototype = {
1031 /** PrivateFunction: isMatch
1032 * Tests if a stanza matches the Strophe.Handler.
1033 *
1034 * Parameters:
1035 * (XMLElement) elem - The XML element to test.
1036 *
1037 * Returns:
1038 * true if the stanza matches and false otherwise.
1039 */
1040 isMatch: function (elem)
1041 {
1042 var nsMatch, i;
1043
1044 nsMatch = false;
1045 if (!this.ns) {
1046 nsMatch = true;
1047 } else {
1048 var self = this;
1049 Strophe.forEachChild(elem, null, function (elem) {
1050 if (elem.getAttribute("xmlns") == self.ns)
1051 nsMatch = true;
1052 });
1053
1054 nsMatch = nsMatch || elem.getAttribute("xmlns") == this.ns;
1055 }
1056
1057 if (nsMatch &&
1058 (!this.name || Strophe.isTagEqual(elem, this.name)) &&
1059 (!this.type || elem.getAttribute("type") == this.type) &&
1060 (!this.id || elem.getAttribute("id") == this.id) &&
1061 (!this.from || elem.getAttribute("from") == this.from)) {
1062 return true;
1063 }
1064
1065 return false;
1066 },
1067
1068 /** PrivateFunction: run
1069 * Run the callback on a matching stanza.
1070 *
1071 * Parameters:
1072 * (XMLElement) elem - The DOM element that triggered the
1073 * Strophe.Handler.
1074 *
1075 * Returns:
1076 * A boolean indicating if the handler should remain active.
1077 */
1078 run: function (elem)
1079 {
1080 var result = null;
1081 try {
1082 result = this.handler(elem);
1083 } catch (e) {
1084 if (e.sourceURL) {
1085 Strophe.fatal("error: " + this.handler +
1086 " " + e.sourceURL + ":" +
1087 e.line + " - " + e.name + ": " + e.message);
1088 } else if (e.fileName) {
1089 if (typeof(console) != "undefined") {
1090 console.trace();
1091 console.error(this.handler, " - error - ", e, e.message);
1092 }
1093 Strophe.fatal("error: " + this.handler + " " +
1094 e.fileName + ":" + e.lineNumber + " - " +
1095 e.name + ": " + e.message);
1096 } else {
1097 Strophe.fatal("error: " + this.handler);
1098 }
1099
1100 throw e;
1101 }
1102
1103 return result;
1104 },
1105
1106 /** PrivateFunction: toString
1107 * Get a String representation of the Strophe.Handler object.
1108 *
1109 * Returns:
1110 * A String.
1111 */
1112 toString: function ()
1113 {
1114 return "{Handler: " + this.handler + "(" + this.name + "," +
1115 this.id + "," + this.ns + ")}";
1116 }
1117 };
1118
1119 /** PrivateClass: Strophe.TimedHandler
1120 * _Private_ helper class for managing timed handlers.
1121 *
1122 * A Strophe.TimedHandler encapsulates a user provided callback that
1123 * should be called after a certain period of time or at regular
1124 * intervals. The return value of the callback determines whether the
1125 * Strophe.TimedHandler will continue to fire.
1126 *
1127 * Users will not use Strophe.TimedHandler objects directly, but instead
1128 * they will use Strophe.Connection.addTimedHandler() and
1129 * Strophe.Connection.deleteTimedHandler().
1130 */
1131
1132 /** PrivateConstructor: Strophe.TimedHandler
1133 * Create and initialize a new Strophe.TimedHandler object.
1134 *
1135 * Parameters:
1136 * (Integer) period - The number of milliseconds to wait before the
1137 * handler is called.
1138 * (Function) handler - The callback to run when the handler fires. This
1139 * function should take no arguments.
1140 *
1141 * Returns:
1142 * A new Strophe.TimedHandler object.
1143 */
1144 Strophe.TimedHandler = function (period, handler)
1145 {
1146 this.period = period;
1147 this.handler = handler;
1148
1149 this.lastCalled = new Date().getTime();
1150 this.user = true;
1151 };
1152
1153 Strophe.TimedHandler.prototype = {
1154 /** PrivateFunction: run
1155 * Run the callback for the Strophe.TimedHandler.
1156 *
1157 * Returns:
1158 * true if the Strophe.TimedHandler should be called again, and false
1159 * otherwise.
1160 */
1161 run: function ()
1162 {
1163 this.lastCalled = new Date().getTime();
1164 return this.handler();
1165 },
1166
1167 /** PrivateFunction: reset
1168 * Reset the last called time for the Strophe.TimedHandler.
1169 */
1170 reset: function ()
1171 {
1172 this.lastCalled = new Date().getTime();
1173 },
1174
1175 /** PrivateFunction: toString
1176 * Get a string representation of the Strophe.TimedHandler object.
1177 *
1178 * Returns:
1179 * The string representation.
1180 */
1181 toString: function ()
1182 {
1183 return "{TimedHandler: " + this.handler + "(" + this.period +")}";
1184 }
1185 };
1186
1187 /** PrivateClass: Strophe.Request
1188 * _Private_ helper class that provides a cross implementation abstraction
1189 * for a BOSH related XMLHttpRequest.
1190 *
1191 * The Strophe.Request class is used internally to encapsulate BOSH request
1192 * information. It is not meant to be used from user's code.
1193 */
1194
1195 /** PrivateConstructor: Strophe.Request
1196 * Create and initialize a new Strophe.Request object.
1197 *
1198 * Parameters:
1199 * (XMLElement) elem - The XML data to be sent in the request.
1200 * (Function) func - The function that will be called when the
1201 * XMLHttpRequest readyState changes.
1202 * (Integer) rid - The BOSH rid attribute associated with this request.
1203 * (Integer) sends - The number of times this same request has been
1204 * sent.
1205 */
1206 Strophe.Request = function (elem, func, rid, sends)
1207 {
1208 this.id = ++Strophe._requestId;
1209 this.xmlData = elem;
1210 this.data = Strophe.serialize(elem);
1211 // save original function in case we need to make a new request
1212 // from this one.
1213 this.origFunc = func;
1214 this.func = func;
1215 this.rid = rid;
1216 this.date = NaN;
1217 this.sends = sends || 0;
1218 this.abort = false;
1219 this.dead = null;
1220 this.age = function () {
1221 if (!this.date) return 0;
1222 var now = new Date();
1223 return (now - this.date) / 1000;
1224 };
1225 this.timeDead = function () {
1226 if (!this.dead) return 0;
1227 var now = new Date();
1228 return (now - this.dead) / 1000;
1229 };
1230 this.xhr = this._newXHR();
1231 };
1232
1233 Strophe.Request.prototype = {
1234 /** PrivateFunction: getResponse
1235 * Get a response from the underlying XMLHttpRequest.
1236 *
1237 * This function attempts to get a response from the request and checks
1238 * for errors.
1239 *
1240 * Throws:
1241 * "parsererror" - A parser error occured.
1242 *
1243 * Returns:
1244 * The DOM element tree of the response.
1245 */
1246 getResponse: function ()
1247 {
1248 var node = null;
1249 if (this.xhr.responseXML && this.xhr.responseXML.documentElement) {
1250 node = this.xhr.responseXML.documentElement;
1251 if (node.tagName == "parsererror") {
1252 Strophe.error("invalid response received");
1253 Strophe.error("responseText: " + this.xhr.responseText);
1254 Strophe.error("responseXML: " +
1255 Strophe.serialize(this.xhr.responseXML));
1256 throw "parsererror";
1257 }
1258 } else if (this.xhr.responseText) {
1259 Strophe.error("invalid response received");
1260 Strophe.error("responseText: " + this.xhr.responseText);
1261 Strophe.error("responseXML: " +
1262 Strophe.serialize(this.xhr.responseXML));
1263 }
1264
1265 return node;
1266 },
1267
1268 /** PrivateFunction: _newXHR
1269 * _Private_ helper function to create XMLHttpRequests.
1270 *
1271 * This function creates XMLHttpRequests across all implementations.
1272 *
1273 * Returns:
1274 * A new XMLHttpRequest.
1275 */
1276 _newXHR: function ()
1277 {
1278 var xhr = null;
1279 if (window.XMLHttpRequest) {
1280 xhr = new XMLHttpRequest();
1281 if (xhr.overrideMimeType) {
1282 xhr.overrideMimeType("text/xml");
1283 }
1284 } else if (window.ActiveXObject) {
1285 xhr = new ActiveXObject("Microsoft.XMLHTTP");
1286 }
1287
1288 xhr.onreadystatechange = this.func.prependArg(this);
1289
1290 return xhr;
1291 }
1292 };
1293
1294 /** Class: Strophe.Connection
1295 * XMPP Connection manager.
1296 *
1297 * Thie class is the main part of Strophe. It manages a BOSH connection
1298 * to an XMPP server and dispatches events to the user callbacks as
1299 * data arrives. It supports SASL PLAIN, SASL DIGEST-MD5, and legacy
1300 * authentication.
1301 *
1302 * After creating a Strophe.Connection object, the user will typically
1303 * call connect() with a user supplied callback to handle connection level
1304 * events like authentication failure, disconnection, or connection
1305 * complete.
1306 *
1307 * The user will also have several event handlers defined by using
1308 * addHandler() and addTimedHandler(). These will allow the user code to
1309 * respond to interesting stanzas or do something periodically with the
1310 * connection. These handlers will be active once authentication is
1311 * finished.
1312 *
1313 * To send data to the connection, use send().
1314 */
1315
1316 /** Constructor: Strophe.Connection
1317 * Create and initialize a Strophe.Connection object.
1318 *
1319 * Parameters:
1320 * (String) service - The BOSH service URL.
1321 *
1322 * Returns:
1323 * A new Strophe.Connection object.
1324 */
1325 Strophe.Connection = function (service)
1326 {
1327 /* The path to the httpbind service. */
1328 this.service = service;
1329 /* The connected JID. */
1330 this.jid = "";
1331 /* request id for body tags */
1332 this.rid = Math.floor(Math.random() * 4294967295);
1333 /* The current session ID. */
1334 this.sid = null;
1335 this.streamId = null;
1336
1337 // SASL
1338 this.do_session = false;
1339 this.do_bind = false;
1340
1341 // handler lists
1342 this.timedHandlers = [];
1343 this.handlers = [];
1344 this.removeTimeds = [];
1345 this.removeHandlers = [];
1346 this.addTimeds = [];
1347 this.addHandlers = [];
1348
1349 this._idleTimeout = null;
1350 this._disconnectTimeout = null;
1351
1352 this.authenticated = false;
1353 this.disconnecting = false;
1354 this.connected = false;
1355
1356 this.errors = 0;
1357
1358 this.paused = false;
1359
1360 // default BOSH window
1361 this.window = 5;
1362
1363 this._data = [];
1364 this._requests = [];
1365 this._uniqueId = Math.round(Math.random() * 10000);
1366
1367 this._sasl_success_handler = null;
1368 this._sasl_failure_handler = null;
1369 this._sasl_challenge_handler = null;
1370
1371 // setup onIdle callback every 1/10th of a second
1372 this._idleTimeout = setTimeout(this._onIdle.bind(this), 100);
1373
1374 // initialize plugins
1375 for (var k in Strophe._connectionPlugins) {
1376 ptype = Strophe._connectionPlugins[k];
1377 var F = function () {};
1378 F.prototype = ptype;
1379 this[k] = new F();
1380 this[k].init(this);
1381 }
1382 };
1383
1384 Strophe.Connection.prototype = {
1385 /** Function: reset
1386 * Reset the connection.
1387 *
1388 * This function should be called after a connection is disconnected
1389 * before that connection is reused.
1390 */
1391 reset: function ()
1392 {
1393 this.rid = Math.floor(Math.random() * 4294967295);
1394
1395 this.sid = null;
1396 this.streamId = null;
1397
1398 // SASL
1399 this.do_session = false;
1400 this.do_bind = false;
1401
1402 // handler lists
1403 this.timedHandlers = [];
1404 this.handlers = [];
1405 this.removeTimeds = [];
1406 this.removeHandlers = [];
1407 this.addTimeds = [];
1408 this.addHandlers = [];
1409
1410 this.authenticated = false;
1411 this.disconnecting = false;
1412 this.connected = false;
1413
1414 this.errors = 0;
1415
1416 this._requests = [];
1417 this._uniqueId = Math.round(Math.random()*10000);
1418 },
1419
1420 /** Function: pause
1421 * Pause the request manager.
1422 *
1423 * This will prevent Strophe from sending any more requests to the
1424 * server. This is very useful for temporarily pausing while a lot
1425 * of send() calls are happening quickly. This causes Strophe to
1426 * send the data in a single request, saving many request trips.
1427 */
1428 pause: function ()
1429 {
1430 this.paused = true;
1431 },
1432
1433 /** Function: resume
1434 * Resume the request manager.
1435 *
1436 * This resumes after pause() has been called.
1437 */
1438 resume: function ()
1439 {
1440 this.paused = false;
1441 },
1442
1443 /** Function: getUniqueId
1444 * Generate a unique ID for use in <iq/> elements.
1445 *
1446 * All <iq/> stanzas are required to have unique id attributes. This
1447 * function makes creating these easy. Each connection instance has
1448 * a counter which starts from zero, and the value of this counter
1449 * plus a colon followed by the suffix becomes the unique id. If no
1450 * suffix is supplied, the counter is used as the unique id.
1451 *
1452 * Suffixes are used to make debugging easier when reading the stream
1453 * data, and their use is recommended. The counter resets to 0 for
1454 * every new connection for the same reason. For connections to the
1455 * same server that authenticate the same way, all the ids should be
1456 * the same, which makes it easy to see changes. This is useful for
1457 * automated testing as well.
1458 *
1459 * Parameters:
1460 * (String) suffix - A optional suffix to append to the id.
1461 *
1462 * Returns:
1463 * A unique string to be used for the id attribute.
1464 */
1465 getUniqueId: function (suffix)
1466 {
1467 if (typeof(suffix) == "string" || typeof(suffix) == "number") {
1468 return ++this._uniqueId + ":" + suffix;
1469 } else {
1470 return ++this._uniqueId + "";
1471 }
1472 },
1473
1474 /** Function: connect
1475 * Starts the connection process.
1476 *
1477 * As the connection process proceeds, the user supplied callback will
1478 * be triggered multiple times with status updates. The callback
1479 * should take two arguments - the status code and the error condition.
1480 *
1481 * The status code will be one of the values in the Strophe.Status
1482 * constants. The error condition will be one of the conditions
1483 * defined in RFC 3920 or the condition 'strophe-parsererror'.
1484 *
1485 * Please see XEP 124 for a more detailed explanation of the optional
1486 * parameters below.
1487 *
1488 * Parameters:
1489 * (String) jid - The user's JID. This may be a bare JID,
1490 * or a full JID. If a node is not supplied, SASL ANONYMOUS
1491 * authentication will be attempted.
1492 * (String) pass - The user's password.
1493 * (Function) callback The connect callback function.
1494 * (Integer) wait - The optional HTTPBIND wait value. This is the
1495 * time the server will wait before returning an empty result for
1496 * a request. The default setting of 60 seconds is recommended.
1497 * Other settings will require tweaks to the Strophe.TIMEOUT value.
1498 * (Integer) hold - The optional HTTPBIND hold value. This is the
1499 * number of connections the server will hold at one time. This
1500 * should almost always be set to 1 (the default).
1501 * (Integer) wind - The optional HTTBIND window value. This is the
1502 * allowed range of request ids that are valid. The default is 5.
1503 */
1504 connect: function (jid, pass, callback, wait, hold, wind)
1505 {
1506 this.jid = jid;
1507 this.pass = pass;
1508 this.connect_callback = callback;
1509 this.disconnecting = false;
1510 this.connected = false;
1511 this.authenticated = false;
1512 this.errors = 0;
1513
1514 if (!wait) wait = 60;
1515 if (!hold) hold = 1;
1516 if (wind) this.window = wind;
1517
1518 // parse jid for domain and resource
1519 this.domain = Strophe.getDomainFromJid(this.jid);
1520
1521 // build the body tag
1522 var body = this._buildBody().attrs({
1523 to: this.domain,
1524 "xml:lang": "en",
1525 wait: wait,
1526 hold: hold,
1527 window: this.window,
1528 content: "text/xml; charset=utf-8",
1529 ver: "1.6",
1530 "xmpp:version": "1.0",
1531 "xmlns:xmpp": Strophe.NS.BOSH
1532 });
1533
1534 this._changeConnectStatus(Strophe.Status.CONNECTING, null);
1535
1536 this._requests.push(
1537 new Strophe.Request(body.tree(),
1538 this._onRequestStateChange.bind(this)
1539 .prependArg(this._connect_cb.bind(this)),
1540 body.tree().getAttribute("rid")));
1541 this._throttledRequestHandler();
1542 },
1543
1544 /** Function: attach
1545 * Attach to an already created and authenticated BOSH session.
1546 *
1547 * This function is provided to allow Strophe to attach to BOSH
1548 * sessions which have been created externally, perhaps by a Web
1549 * application. This is often used to support auto-login type features
1550 * without putting user credentials into the page.
1551 *
1552 * Parameters:
1553 * (String) jid - The full JID that is bound by the session.
1554 * (String) sid - The SID of the BOSH session.
1555 * (String) rid - The current RID of the BOSH session. This RID
1556 * will be used by the next request.
1557 * (Function) callback The connect callback function.
1558 */
1559 attach: function (jid, sid, rid, callback)
1560 {
1561 this.jid = jid;
1562 this.sid = sid;
1563 this.rid = rid;
1564 this.connect_callback = callback;
1565
1566 this.domain = Strophe.getDomainFromJid(this.jid);
1567
1568 this.authenticated = true;
1569 this.connected = true;
1570 },
1571
1572 /** Function: xmlInput
1573 * User overrideable function that receives XML data coming into the
1574 * connection.
1575 *
1576 * The default function does nothing. User code can override this with
1577 * > Strophe.Connection.xmlInput = function (elem) {
1578 * > (user code)
1579 * > };
1580 *
1581 * Parameters:
1582 * (XMLElement) elem - The XML data received by the connection.
1583 */
1584 xmlInput: function (elem)
1585 {
1586 return;
1587 },
1588
1589 /** Function: xmlOutput
1590 * User overrideable function that receives XML data sent to the
1591 * connection.
1592 *
1593 * The default function does nothing. User code can override this with
1594 * > Strophe.Connection.xmlOutput = function (elem) {
1595 * > (user code)
1596 * > };
1597 *
1598 * Parameters:
1599 * (XMLElement) elem - The XMLdata sent by the connection.
1600 */
1601 xmlOutput: function (elem)
1602 {
1603 return;
1604 },
1605
1606 /** Function: rawInput
1607 * User overrideable function that receives raw data coming into the
1608 * connection.
1609 *
1610 * The default function does nothing. User code can override this with
1611 * > Strophe.Connection.rawInput = function (data) {
1612 * > (user code)
1613 * > };
1614 *
1615 * Parameters:
1616 * (String) data - The data received by the connection.
1617 */
1618 rawInput: function (data)
1619 {
1620 return;
1621 },
1622
1623 /** Function: rawOutput
1624 * User overrideable function that receives raw data sent to the
1625 * connection.
1626 *
1627 * The default function does nothing. User code can override this with
1628 * > Strophe.Connection.rawOutput = function (data) {
1629 * > (user code)
1630 * > };
1631 *
1632 * Parameters:
1633 * (String) data - The data sent by the connection.
1634 */
1635 rawOutput: function (data)
1636 {
1637 return;
1638 },
1639
1640 /** Function: send
1641 * Send a stanza.
1642 *
1643 * This function is called to push data onto the send queue to
1644 * go out over the wire. Whenever a request is sent to the BOSH
1645 * server, all pending data is sent and the queue is flushed.
1646 *
1647 * Parameters:
1648 * (XMLElement |
1649 * [XMLElement] |
1650 * Strophe.Builder) elem - The stanza to send.
1651 */
1652 send: function (elem)
1653 {
1654 if (elem === null) { return ; }
1655 if (typeof(elem["sort"]) === "function") {
1656 for (var i = 0; i < elem.length; i++) {
1657 this._queueData(elem[i]);
1658 }
1659 } else if (typeof(elem["tree"]) === "function") {
1660 this._queueData(elem.tree());
1661 } else {
1662 this._queueData(elem);
1663 }
1664
1665 this._throttledRequestHandler();
1666 clearTimeout(this._idleTimeout);
1667 this._idleTimeout = setTimeout(this._onIdle.bind(this), 100);
1668 },
1669
1670 /** Function: sendIQ
1671 * Helper function to send IQ stanzas.
1672 *
1673 * Parameters:
1674 * (XMLElement) elem - The stanza to send.
1675 * (Function) callback - The callback function for a successful request.
1676 * (Function) errback - The callback function for a failed or timed
1677 * out request. On timeout, the stanza will be null.
1678 * (Integer) timeout - The time specified in milliseconds for a
1679 * timeout to occur.
1680 *
1681 * Returns:
1682 * The id used to send the IQ.
1683 */
1684 sendIQ: function(elem, callback, errback, timeout) {
1685 var timeoutHandler = null, handler = null;
1686 var that = this;
1687
1688 if (typeof(elem["tree"]) === "function") {
1689 elem = elem.tree();
1690 }
1691 var id = elem.getAttribute('id');
1692
1693 // inject id if not found
1694 if (!id) {
1695 id = this.getUniqueId("sendIQ");
1696 elem.setAttribute("id", id);
1697 }
1698
1699 var handler = this.addHandler(function (stanza) {
1700 // remove timeout handler if there is one
1701 if (timeoutHandler) {
1702 that.deleteTimedHandler(timeoutHandler);
1703 }
1704
1705 var iqtype = stanza.getAttribute('type');
1706 if (iqtype === 'result') {
1707 if (callback) {
1708 callback(stanza);
1709 }
1710 } else if (iqtype === 'error') {
1711 if (errback) {
1712 errback(stanza);
1713 }
1714 } else {
1715 throw {
1716 name: "StropheError",
1717 message: "Got bad IQ type of " + iqtype
1718 };
1719 }
1720 }, null, 'iq', null, id);
1721
1722 // if timeout specified, setup timeout handler.
1723 if (timeout) {
1724 timeoutHandler = this.addTimedHandler(timeout, function () {
1725 // get rid of normal handler
1726 that.deleteHandler(handler);
1727
1728 // call errback on timeout with null stanza
1729 if (errback) {
1730 errback(null);
1731 }
1732 return false;
1733 });
1734 }
1735
1736 this.send(elem);
1737
1738 return id;
1739 },
1740
1741 /** PrivateFunction: _queueData
1742 * Queue outgoing data for later sending. Also ensures that the data
1743 * is a DOMElement.
1744 */
1745 _queueData: function (element) {
1746 if (element === null ||
1747 !element["tagName"] ||
1748 !element["childNodes"]) {
1749 throw {
1750 name: "StropheError",
1751 message: "Cannot queue non-DOMElement."
1752 };
1753 }
1754
1755 this._data.push(element);
1756 },
1757
1758 /** PrivateFunction: _sendRestart
1759 * Send an xmpp:restart stanza.
1760 */
1761 _sendRestart: function ()
1762 {
1763 this._data.push("restart");
1764
1765 this._throttledRequestHandler();
1766 clearTimeout(this._idleTimeout);
1767 this._idleTimeout = setTimeout(this._onIdle.bind(this), 100);
1768 },
1769
1770 /** Function: addTimedHandler
1771 * Add a timed handler to the connection.
1772 *
1773 * This function adds a timed handler. The provided handler will
1774 * be called every period milliseconds until it returns false,
1775 * the connection is terminated, or the handler is removed. Handlers
1776 * that wish to continue being invoked should return true.
1777 *
1778 * Because of method binding it is necessary to save the result of
1779 * this function if you wish to remove a handler with
1780 * deleteTimedHandler().
1781 *
1782 * Note that user handlers are not active until authentication is
1783 * successful.
1784 *
1785 * Parameters:
1786 * (Integer) period - The period of the handler.
1787 * (Function) handler - The callback function.
1788 *
1789 * Returns:
1790 * A reference to the handler that can be used to remove it.
1791 */
1792 addTimedHandler: function (period, handler)
1793 {
1794 var thand = new Strophe.TimedHandler(period, handler);
1795 this.addTimeds.push(thand);
1796 return thand;
1797 },
1798
1799 /** Function: deleteTimedHandler
1800 * Delete a timed handler for a connection.
1801 *
1802 * This function removes a timed handler from the connection. The
1803 * handRef parameter is *not* the function passed to addTimedHandler(),
1804 * but is the reference returned from addTimedHandler().
1805 *
1806 * Parameters:
1807 * (Strophe.TimedHandler) handRef - The handler reference.
1808 */
1809 deleteTimedHandler: function (handRef)
1810 {
1811 // this must be done in the Idle loop so that we don't change
1812 // the handlers during iteration
1813 this.removeTimeds.push(handRef);
1814 },
1815
1816 /** Function: addHandler
1817 * Add a stanza handler for the connection.
1818 *
1819 * This function adds a stanza handler to the connection. The
1820 * handler callback will be called for any stanza that matches
1821 * the parameters. Note that if multiple parameters are supplied,
1822 * they must all match for the handler to be invoked.
1823 *
1824 * The handler will receive the stanza that triggered it as its argument.
1825 * The handler should return true if it is to be invoked again;
1826 * returning false will remove the handler after it returns.
1827 *
1828 * As a convenience, the ns parameters applies to the top level element
1829 * and also any of its immediate children. This is primarily to make
1830 * matching /iq/query elements easy.
1831 *
1832 * The return value should be saved if you wish to remove the handler
1833 * with deleteHandler().
1834 *
1835 * Parameters:
1836 * (Function) handler - The user callback.
1837 * (String) ns - The namespace to match.
1838 * (String) name - The stanza name to match.
1839 * (String) type - The stanza type attribute to match.
1840 * (String) id - The stanza id attribute to match.
1841 * (String) from - The stanza from attribute to match.
1842 *
1843 * Returns:
1844 * A reference to the handler that can be used to remove it.
1845 */
1846 addHandler: function (handler, ns, name, type, id, from)
1847 {
1848 var hand = new Strophe.Handler(handler, ns, name, type, id, from);
1849 this.addHandlers.push(hand);
1850 return hand;
1851 },
1852
1853 /** Function: deleteHandler
1854 * Delete a stanza handler for a connection.
1855 *
1856 * This function removes a stanza handler from the connection. The
1857 * handRef parameter is *not* the function passed to addHandler(),
1858 * but is the reference returned from addHandler().
1859 *
1860 * Parameters:
1861 * (Strophe.Handler) handRef - The handler reference.
1862 */
1863 deleteHandler: function (handRef)
1864 {
1865 // this must be done in the Idle loop so that we don't change
1866 // the handlers during iteration
1867 this.removeHandlers.push(handRef);
1868 },
1869
1870 /** Function: disconnect
1871 * Start the graceful disconnection process.
1872 *
1873 * This function starts the disconnection process. This process starts
1874 * by sending unavailable presence and sending BOSH body of type
1875 * terminate. A timeout handler makes sure that disconnection happens
1876 * even if the BOSH server does not respond.
1877 *
1878 * The user supplied connection callback will be notified of the
1879 * progress as this process happens.
1880 *
1881 * Parameters:
1882 * (String) reason - The reason the disconnect is occuring.
1883 */
1884 disconnect: function (reason)
1885 {
1886 this._changeConnectStatus(Strophe.Status.DISCONNECTING, reason);
1887
1888 Strophe.info("Disconnect was called because: " + reason);
1889 if (this.connected) {
1890 // setup timeout handler
1891 this._disconnectTimeout = this._addSysTimedHandler(
1892 3000, this._onDisconnectTimeout.bind(this));
1893 this._sendTerminate();
1894 }
1895 },
1896
1897 /** PrivateFunction: _changeConnectStatus
1898 * _Private_ helper function that makes sure plugins and the user's
1899 * callback are notified of connection status changes.
1900 *
1901 * Parameters:
1902 * (Integer) status - the new connection status, one of the values
1903 * in Strophe.Status
1904 * (String) condition - the error condition or null
1905 */
1906 _changeConnectStatus: function (status, condition)
1907 {
1908 // notify all plugins listening for status changes
1909 for (var k in Strophe._connectionPlugins) {
1910 var plugin = this[k];
1911 if (plugin.statusChanged) {
1912 try {
1913 plugin.statusChanged(status, condition);
1914 } catch (err) {
1915 Strophe.error("" + k + " plugin caused an exception " +
1916 "changing status: " + err);
1917 }
1918 }
1919 }
1920
1921 // notify the user's callback
1922 if (this.connect_callback) {
1923 try {
1924 this.connect_callback(status, condition);
1925 } catch (err) {
1926 Strophe.error("User connection callback caused an " +
1927 "exception: " + err);
1928 }
1929 }
1930 },
1931
1932 /** PrivateFunction: _buildBody
1933 * _Private_ helper function to generate the <body/> wrapper for BOSH.
1934 *
1935 * Returns:
1936 * A Strophe.Builder with a <body/> element.
1937 */
1938 _buildBody: function ()
1939 {
1940 var bodyWrap = $build('body', {
1941 rid: this.rid++,
1942 xmlns: Strophe.NS.HTTPBIND
1943 });
1944
1945 if (this.sid !== null) {
1946 bodyWrap.attrs({sid: this.sid});
1947 }
1948
1949 return bodyWrap;
1950 },
1951
1952 /** PrivateFunction: _removeRequest
1953 * _Private_ function to remove a request from the queue.
1954 *
1955 * Parameters:
1956 * (Strophe.Request) req - The request to remove.
1957 */
1958 _removeRequest: function (req)
1959 {
1960 Strophe.debug("removing request");
1961
1962 var i;
1963 for (i = this._requests.length - 1; i >= 0; i--) {
1964 if (req == this._requests[i]) {
1965 this._requests.splice(i, 1);
1966 }
1967 }
1968
1969 // set the onreadystatechange handler to a null function so
1970 // that we don't get any misfires
1971 req.xhr.onreadystatechange = function () {};
1972
1973 this._throttledRequestHandler();
1974 },
1975
1976 /** PrivateFunction: _restartRequest
1977 * _Private_ function to restart a request that is presumed dead.
1978 *
1979 * Parameters:
1980 * (Integer) i - The index of the request in the queue.
1981 */
1982 _restartRequest: function (i)
1983 {
1984 var req = this._requests[i];
1985 if (req.dead === null) {
1986 req.dead = new Date();
1987 }
1988
1989 this._processRequest(i);
1990 },
1991
1992 /** PrivateFunction: _processRequest
1993 * _Private_ function to process a request in the queue.
1994 *
1995 * This function takes requests off the queue and sends them and
1996 * restarts dead requests.
1997 *
1998 * Parameters:
1999 * (Integer) i - The index of the request in the queue.
2000 */
2001 _processRequest: function (i)
2002 {
2003 var req = this._requests[i];
2004 var reqStatus = -1;
2005
2006 try {
2007 if (req.xhr.readyState == 4) {
2008 reqStatus = req.xhr.status;
2009 }
2010 } catch (e) {
2011 Strophe.error("caught an error in _requests[" + i +
2012 "], reqStatus: " + reqStatus);
2013 }
2014
2015 if (typeof(reqStatus) == "undefined") {
2016 reqStatus = -1;
2017 }
2018
2019 var now = new Date();
2020 var time_elapsed = req.age();
2021 var primaryTimeout = (!isNaN(time_elapsed) &&
2022 time_elapsed > Strophe.TIMEOUT);
2023 var secondaryTimeout = (req.dead !== null &&
2024 req.timeDead() > Strophe.SECONDARY_TIMEOUT);
2025 var requestCompletedWithServerError = (req.xhr.readyState == 4 &&
2026 (reqStatus < 1 ||
2027 reqStatus >= 500));
2028 var oldreq;
2029
2030 if (primaryTimeout || secondaryTimeout ||
2031 requestCompletedWithServerError) {
2032 if (secondaryTimeout) {
2033 Strophe.error("Request " +
2034 this._requests[i].id +
2035 " timed out (secondary), restarting");
2036 }
2037 req.abort = true;
2038 req.xhr.abort();
2039 oldreq = req;
2040 this._requests[i] = new Strophe.Request(req.xmlData,
2041 req.origFunc,
2042 req.rid,
2043 req.sends);
2044 req = this._requests[i];
2045 }
2046
2047 if (req.xhr.readyState === 0) {
2048 Strophe.debug("request id " + req.id +
2049 "." + req.sends + " posting");
2050
2051 req.date = new Date();
2052 try {
2053 req.xhr.open("POST", this.service, true);
2054 } catch (e) {
2055 Strophe.error("XHR open failed.");
2056 if (!this.connected) {
2057 this._changeConnectStatus(Strophe.Status.CONNFAIL,
2058 "bad-service");
2059 }
2060 this.disconnect();
2061 return;
2062 }
2063
2064 // Fires the XHR request -- may be invoked immediately
2065 // or on a gradually expanding retry window for reconnects
2066 var sendFunc = function () {
2067 req.xhr.send(req.data);
2068 };
2069
2070 // Implement progressive backoff for reconnects --
2071 // First retry (send == 1) should also be instantaneous
2072 if (req.sends > 1) {
2073 // Using a cube of the retry number creats a nicely
2074 // expanding retry window
2075 var backoff = Math.pow(req.sends, 3) * 1000;
2076 setTimeout(sendFunc, backoff);
2077 } else {
2078 sendFunc();
2079 }
2080
2081 req.sends++;
2082
2083 this.xmlOutput(req.xmlData);
2084 this.rawOutput(req.data);
2085 } else {
2086 Strophe.debug("_throttledRequestHandler: " +
2087 (i === 0 ? "first" : "second") +
2088 " request has readyState of " +
2089 req.xhr.readyState);
2090 }
2091 },
2092
2093 /** PrivateFunction: _throttledRequestHandler
2094 * _Private_ function to throttle requests to the connection window.
2095 *
2096 * This function makes sure we don't send requests so fast that the
2097 * request ids overflow the connection window in the case that one
2098 * request died.
2099 */
2100 _throttledRequestHandler: function ()
2101 {
2102 if (!this._requests) {
2103 Strophe.debug("_throttledRequestHandler called with " +
2104 "undefined requests");
2105 } else {
2106 Strophe.debug("_throttledRequestHandler called with " +
2107 this._requests.length + " requests");
2108 }
2109
2110 if (!this._requests || this._requests.length === 0) {
2111 return;
2112 }
2113
2114 if (this._requests.length > 0) {
2115 this._processRequest(0);
2116 }
2117
2118 if (this._requests.length > 1 &&
2119 Math.abs(this._requests[0].rid -
2120 this._requests[1].rid) < this.window - 1) {
2121 this._processRequest(1);
2122 }
2123 },
2124
2125 /** PrivateFunction: _onRequestStateChange
2126 * _Private_ handler for Strophe.Request state changes.
2127 *
2128 * This function is called when the XMLHttpRequest readyState changes.
2129 * It contains a lot of error handling logic for the many ways that
2130 * requests can fail, and calls the request callback when requests
2131 * succeed.
2132 *
2133 * Parameters:
2134 * (Function) func - The handler for the request.
2135 * (Strophe.Request) req - The request that is changing readyState.
2136 */
2137 _onRequestStateChange: function (func, req)
2138 {
2139 Strophe.debug("request id " + req.id +
2140 "." + req.sends + " state changed to " +
2141 req.xhr.readyState);
2142
2143 if (req.abort) {
2144 req.abort = false;
2145 return;
2146 }
2147
2148 // request complete
2149 var reqStatus;
2150 if (req.xhr.readyState == 4) {
2151 reqStatus = 0;
2152 try {
2153 reqStatus = req.xhr.status;
2154 } catch (e) {
2155 // ignore errors from undefined status attribute. works
2156 // around a browser bug
2157 }
2158
2159 if (typeof(reqStatus) == "undefined") {
2160 reqStatus = 0;
2161 }
2162
2163 if (this.disconnecting) {
2164 if (reqStatus >= 400) {
2165 this._hitError(reqStatus);
2166 return;
2167 }
2168 }
2169
2170 var reqIs0 = (this._requests[0] == req);
2171 var reqIs1 = (this._requests[1] == req);
2172
2173 if ((reqStatus > 0 && reqStatus < 500) || req.sends > 5) {
2174 // remove from internal queue
2175 this._removeRequest(req);
2176 Strophe.debug("request id " +
2177 req.id +
2178 " should now be removed");
2179 }
2180
2181 // request succeeded
2182 if (reqStatus == 200) {
2183 // if request 1 finished, or request 0 finished and request
2184 // 1 is over Strophe.SECONDARY_TIMEOUT seconds old, we need to
2185 // restart the other - both will be in the first spot, as the
2186 // completed request has been removed from the queue already
2187 if (reqIs1 ||
2188 (reqIs0 && this._requests.length > 0 &&
2189 this._requests[0].age() > Strophe.SECONDARY_TIMEOUT)) {
2190 this._restartRequest(0);
2191 }
2192 // call handler
2193 Strophe.debug("request id " +
2194 req.id + "." +
2195 req.sends + " got 200");
2196 func(req);
2197 this.errors = 0;
2198 } else {
2199 Strophe.error("request id " +
2200 req.id + "." +
2201 req.sends + " error " + reqStatus +
2202 " happened");
2203 if (reqStatus === 0 ||
2204 (reqStatus >= 400 && reqStatus < 600) ||
2205 reqStatus >= 12000) {
2206 this._hitError(reqStatus);
2207 if (reqStatus >= 400 && reqStatus < 500) {
2208 this._changeConnectStatus(Strophe.Status.DISCONNECTING,
2209 null);
2210 this._doDisconnect();
2211 }
2212 }
2213 }
2214
2215 if (!((reqStatus > 0 && reqStatus < 10000) ||
2216 req.sends > 5)) {
2217 this._throttledRequestHandler();
2218 }
2219 }
2220 },
2221
2222 /** PrivateFunction: _hitError
2223 * _Private_ function to handle the error count.
2224 *
2225 * Requests are resent automatically until their error count reaches
2226 * 5. Each time an error is encountered, this function is called to
2227 * increment the count and disconnect if the count is too high.
2228 *
2229 * Parameters:
2230 * (Integer) reqStatus - The request status.
2231 */
2232 _hitError: function (reqStatus)
2233 {
2234 this.errors++;
2235 Strophe.warn("request errored, status: " + reqStatus +
2236 ", number of errors: " + this.errors);
2237 if (this.errors > 4) {
2238 this._onDisconnectTimeout();
2239 }
2240 },
2241
2242 /** PrivateFunction: _doDisconnect
2243 * _Private_ function to disconnect.
2244 *
2245 * This is the last piece of the disconnection logic. This resets the
2246 * connection and alerts the user's connection callback.
2247 */
2248 _doDisconnect: function ()
2249 {
2250 Strophe.info("_doDisconnect was called");
2251 this.authenticated = false;
2252 this.disconnecting = false;
2253 this.sid = null;
2254 this.streamId = null;
2255 this.rid = Math.floor(Math.random() * 4294967295);
2256
2257 // tell the parent we disconnected
2258 if (this.connected) {
2259 this._changeConnectStatus(Strophe.Status.DISCONNECTED, null);
2260 this.connected = false;
2261 }
2262
2263 // delete handlers
2264 this.handlers = [];
2265 this.timedHandlers = [];
2266 this.removeTimeds = [];
2267 this.removeHandlers = [];
2268 this.addTimeds = [];
2269 this.addHandlers = [];
2270 },
2271
2272 /** PrivateFunction: _dataRecv
2273 * _Private_ handler to processes incoming data from the the connection.
2274 *
2275 * Except for _connect_cb handling the initial connection request,
2276 * this function handles the incoming data for all requests. This
2277 * function also fires stanza handlers that match each incoming
2278 * stanza.
2279 *
2280 * Parameters:
2281 * (Strophe.Request) req - The request that has data ready.
2282 */
2283 _dataRecv: function (req)
2284 {
2285 try {
2286 var elem = req.getResponse();
2287 } catch (e) {
2288 if (e != "parsererror") throw e;
2289 this.disconnect("strophe-parsererror");
2290 }
2291 if (elem === null) return;
2292
2293 this.xmlInput(elem);
2294 this.rawInput(Strophe.serialize(elem));
2295
2296 // remove handlers scheduled for deletion
2297 var i, hand;
2298 while (this.removeHandlers.length > 0) {
2299 hand = this.removeHandlers.pop();
2300 i = this.handlers.indexOf(hand);
2301 if (i >= 0)
2302 this.handlers.splice(i, 1);
2303 }
2304
2305 // add handlers scheduled for addition
2306 while (this.addHandlers.length > 0) {
2307 this.handlers.push(this.addHandlers.pop());
2308 }
2309
2310 // handle graceful disconnect
2311 if (this.disconnecting && this._requests.length == 0) {
2312 this.deleteTimedHandler(this._disconnectTimeout);
2313 this._disconnectTimeout = null;
2314 this._doDisconnect();
2315 return;
2316 }
2317
2318 var typ = elem.getAttribute("type");
2319 var cond, conflict;
2320 if (typ !== null && typ == "terminate") {
2321 // an error occurred
2322 cond = elem.getAttribute("condition");
2323 conflict = elem.getElementsByTagName("conflict");
2324 if (cond !== null) {
2325 if (cond == "remote-stream-error" && conflict.length > 0) {
2326 cond = "conflict";
2327 }
2328 this._changeConnectStatus(Strophe.Status.CONNFAIL, cond);
2329 } else {
2330 this._changeConnectStatus(Strophe.Status.CONNFAIL, "unknown");
2331 }
2332 this.disconnect();
2333 return;
2334 }
2335
2336 // send each incoming stanza through the handler chain
2337 var self = this;
2338 Strophe.forEachChild(elem, null, function (child) {
2339 var i, newList;
2340 // process handlers
2341 newList = self.handlers;
2342 self.handlers = [];
2343 for (i = 0; i < newList.length; i++) {
2344 var hand = newList[i];
2345 if (hand.isMatch(child) &&
2346 (self.authenticated || !hand.user)) {
2347 if (hand.run(child)) {
2348 self.handlers.push(hand);
2349 }
2350 } else {
2351 self.handlers.push(hand);
2352 }
2353 }
2354 });
2355 },
2356
2357 /** PrivateFunction: _sendTerminate
2358 * _Private_ function to send initial disconnect sequence.
2359 *
2360 * This is the first step in a graceful disconnect. It sends
2361 * the BOSH server a terminate body and includes an unavailable
2362 * presence if authentication has completed.
2363 */
2364 _sendTerminate: function ()
2365 {
2366 Strophe.info("_sendTerminate was called");
2367 var body = this._buildBody().attrs({type: "terminate"});
2368
2369 var presence, i;
2370 if (this.authenticated) {
2371 body.c('presence', {
2372 xmlns: Strophe.NS.CLIENT,
2373 type: 'unavailable'
2374 });
2375 }
2376
2377 this.disconnecting = true;
2378
2379 var req = new Strophe.Request(body.tree(),
2380 this._onRequestStateChange.bind(this)
2381 .prependArg(this._dataRecv.bind(this)),
2382 body.tree().getAttribute("rid"));
2383
2384 // abort and clear all waiting requests
2385 var r;
2386 while (this._requests.length > 0) {
2387 r = this._requests.pop();
2388 r.xhr.abort();
2389 r.abort = true;
2390 }
2391
2392 this._requests.push(req);
2393 this._throttledRequestHandler();
2394 },
2395
2396 /** PrivateFunction: _connect_cb
2397 * _Private_ handler for initial connection request.
2398 *
2399 * This handler is used to process the initial connection request
2400 * response from the BOSH server. It is used to set up authentication
2401 * handlers and start the authentication process.
2402 *
2403 * SASL authentication will be attempted if available, otherwise
2404 * the code will fall back to legacy authentication.
2405 *
2406 * Parameters:
2407 * (Strophe.Request) req - The current request.
2408 */
2409 _connect_cb: function (req)
2410 {
2411 Strophe.info("_connect_cb was called");
2412
2413 this.connected = true;
2414 var bodyWrap = req.getResponse();
2415 if (!bodyWrap) return;
2416
2417 this.xmlInput(bodyWrap);
2418 this.rawInput(Strophe.serialize(bodyWrap));
2419
2420 var typ = bodyWrap.getAttribute("type");
2421 var cond, conflict;
2422 if (typ !== null && typ == "terminate") {
2423 // an error occurred
2424 cond = bodyWrap.getAttribute("condition");
2425 conflict = bodyWrap.getElementsByTagName("conflict");
2426 if (cond !== null) {
2427 if (cond == "remote-stream-error" && conflict.length > 0) {
2428 cond = "conflict";
2429 }
2430 this._changeConnectStatus(Strophe.Status.CONNFAIL, cond);
2431 } else {
2432 this._changeConnectStatus(Strophe.Status.CONNFAIL, "unknown");
2433 }
2434 return;
2435 }
2436
2437 this.sid = bodyWrap.getAttribute("sid");
2438 this.stream_id = bodyWrap.getAttribute("authid");
2439
2440 // TODO - add SASL anonymous for guest accounts
2441 var do_sasl_plain = false;
2442 var do_sasl_digest_md5 = false;
2443 var do_sasl_anonymous = false;
2444
2445 var mechanisms = bodyWrap.getElementsByTagName("mechanism");
2446 var i, mech, auth_str, hashed_auth_str;
2447 if (mechanisms.length > 0) {
2448 for (i = 0; i < mechanisms.length; i++) {
2449 mech = Strophe.getText(mechanisms[i]);
2450 if (mech == 'DIGEST-MD5') {
2451 do_sasl_digest_md5 = true;
2452 } else if (mech == 'PLAIN') {
2453 do_sasl_plain = true;
2454 } else if (mech == 'ANONYMOUS') {
2455 do_sasl_anonymous = true;
2456 }
2457 }
2458 }
2459
2460 if (Strophe.getNodeFromJid(this.jid) === null &&
2461 do_sasl_anonymous) {
2462 this._changeConnectStatus(Strophe.Status.AUTHENTICATING, null);
2463 this._sasl_success_handler = this._addSysHandler(
2464 this._sasl_success_cb.bind(this), null,
2465 "success", null, null);
2466 this._sasl_failure_handler = this._addSysHandler(
2467 this._sasl_failure_cb.bind(this), null,
2468 "failure", null, null);
2469
2470 this.send($build("auth", {
2471 xmlns: Strophe.NS.SASL,
2472 mechanism: "ANONYMOUS"
2473 }).tree());
2474 } else if (Strophe.getNodeFromJid(this.jid) === null) {
2475 // we don't have a node, which is required for non-anonymous
2476 // client connections
2477 this._changeConnectStatus(Strophe.Status.CONNFAIL,
2478 'x-strophe-bad-non-anon-jid');
2479 this.disconnect();
2480 } else if (do_sasl_digest_md5) {
2481 this._changeConnectStatus(Strophe.Status.AUTHENTICATING, null);
2482 this._sasl_challenge_handler = this._addSysHandler(
2483 this._sasl_challenge1_cb.bind(this), null,
2484 "challenge", null, null);
2485 this._sasl_failure_handler = this._addSysHandler(
2486 this._sasl_failure_cb.bind(this), null,
2487 "failure", null, null);
2488
2489 this.send($build("auth", {
2490 xmlns: Strophe.NS.SASL,
2491 mechanism: "DIGEST-MD5"
2492 }).tree());
2493 } else if (do_sasl_plain) {
2494 // Build the plain auth string (barejid null
2495 // username null password) and base 64 encoded.
2496 auth_str = Strophe.escapeJid(
2497 Strophe.getBareJidFromJid(this.jid));
2498 auth_str = auth_str + "\u0000";
2499 auth_str = auth_str + Strophe.getNodeFromJid(this.jid);
2500 auth_str = auth_str + "\u0000";
2501 auth_str = auth_str + this.pass;
2502
2503 this._changeConnectStatus(Strophe.Status.AUTHENTICATING, null);
2504 this._sasl_success_handler = this._addSysHandler(
2505 this._sasl_success_cb.bind(this), null,
2506 "success", null, null);
2507 this._sasl_failure_handler = this._addSysHandler(
2508 this._sasl_failure_cb.bind(this), null,
2509 "failure", null, null);
2510
2511 hashed_auth_str = encode64(auth_str);
2512 this.send($build("auth", {
2513 xmlns: Strophe.NS.SASL,
2514 mechanism: "PLAIN"
2515 }).t(hashed_auth_str).tree());
2516 } else {
2517 this._changeConnectStatus(Strophe.Status.AUTHENTICATING, null);
2518 this._addSysHandler(this._auth1_cb.bind(this), null, null,
2519 null, "_auth_1");
2520
2521 this.send($iq({
2522 type: "get",
2523 to: this.domain,
2524 id: "_auth_1"
2525 }).c("query", {
2526 xmlns: Strophe.NS.AUTH
2527 }).c("username", {}).t(Strophe.getNodeFromJid(this.jid)).tree());
2528 }
2529 },
2530
2531 /** PrivateFunction: _sasl_challenge1_cb
2532 * _Private_ handler for DIGEST-MD5 SASL authentication.
2533 *
2534 * Parameters:
2535 * (XMLElement) elem - The challenge stanza.
2536 *
2537 * Returns:
2538 * false to remove the handler.
2539 */
2540 _sasl_challenge1_cb: function (elem)
2541 {
2542 var attribMatch = /([a-z]+)=("[^"]+"|[^,"]+)(?:,|$)/;
2543
2544 var challenge = decode64(Strophe.getText(elem));
2545 var cnonce = hex_md5(Math.random() * 1234567890);
2546 var realm = "";
2547 var host = null;
2548 var nonce = "";
2549 var qop = "";
2550 var matches;
2551
2552 // remove unneeded handlers
2553 this.deleteHandler(this._sasl_failure_handler);
2554
2555 while (challenge.match(attribMatch)) {
2556 matches = challenge.match(attribMatch);
2557 challenge = challenge.replace(matches[0], "");
2558 matches[2] = matches[2].replace(/^"(.+)"$/, "$1");
2559 switch (matches[1]) {
2560 case "realm":
2561 realm = matches[2];
2562 break;
2563 case "nonce":
2564 nonce = matches[2];
2565 break;
2566 case "qop":
2567 qop = matches[2];
2568 break;
2569 case "host":
2570 host = matches[2];
2571 break;
2572 }
2573 }
2574
2575 var digest_uri = "xmpp/" + this.domain;
2576 if (host !== null) {
2577 digest_uri = digest_uri + "/" + host;
2578 }
2579
2580 var A1 = str_md5(Strophe.getNodeFromJid(this.jid) +
2581 ":" + realm + ":" + this.pass) +
2582 ":" + nonce + ":" + cnonce;
2583 var A2 = 'AUTHENTICATE:' + digest_uri;
2584
2585 var responseText = "";
2586 responseText += 'username="' +
2587 Strophe.getNodeFromJid(this.jid) + '",';
2588 responseText += 'realm="' + realm + '",';
2589 responseText += 'nonce="' + nonce + '",';
2590 responseText += 'cnonce="' + cnonce + '",';
2591 responseText += 'nc="00000001",';
2592 responseText += 'qop="auth",';
2593 responseText += 'digest-uri="' + digest_uri + '",';
2594 responseText += 'response="' + hex_md5(hex_md5(A1) + ":" +
2595 nonce + ":00000001:" +
2596 cnonce + ":auth:" +
2597 hex_md5(A2)) + '",';
2598 responseText += 'charset="utf-8"';
2599
2600 this._sasl_challenge_handler = this._addSysHandler(
2601 this._sasl_challenge2_cb.bind(this), null,
2602 "challenge", null, null);
2603 this._sasl_success_handler = this._addSysHandler(
2604 this._sasl_success_cb.bind(this), null,
2605 "success", null, null);
2606 this._sasl_failure_handler = this._addSysHandler(
2607 this._sasl_failure_cb.bind(this), null,
2608 "failure", null, null);
2609
2610 this.send($build('response', {
2611 xmlns: Strophe.NS.SASL
2612 }).t(encode64(responseText)).tree());
2613
2614 return false;
2615 },
2616
2617 /** PrivateFunction: _sasl_challenge2_cb
2618 * _Private_ handler for second step of DIGEST-MD5 SASL authentication.
2619 *
2620 * Parameters:
2621 * (XMLElement) elem - The challenge stanza.
2622 *
2623 * Returns:
2624 * false to remove the handler.
2625 */
2626 _sasl_challenge2_cb: function (elem)
2627 {
2628 // remove unneeded handlers
2629 this.deleteHandler(this._sasl_success_handler);
2630 this.deleteHandler(this._sasl_failure_handler);
2631
2632 this._sasl_success_handler = this._addSysHandler(
2633 this._sasl_success_cb.bind(this), null,
2634 "success", null, null);
2635 this._sasl_failure_handler = this._addSysHandler(
2636 this._sasl_failure_cb.bind(this), null,
2637 "failure", null, null);
2638 this.send($build('response', {xmlns: Strophe.NS.SASL}).tree());
2639 return false;
2640 },
2641
2642 /** PrivateFunction: _auth1_cb
2643 * _Private_ handler for legacy authentication.
2644 *
2645 * This handler is called in response to the initial <iq type='get'/>
2646 * for legacy authentication. It builds an authentication <iq/> and
2647 * sends it, creating a handler (calling back to _auth2_cb()) to
2648 * handle the result
2649 *
2650 * Parameters:
2651 * (XMLElement) elem - The stanza that triggered the callback.
2652 *
2653 * Returns:
2654 * false to remove the handler.
2655 */
2656 _auth1_cb: function (elem)
2657 {
2658 var use_digest = false;
2659 var check_query, check_digest;
2660
2661 if (elem.getAttribute("type") == "result") {
2662 // Find digest
2663 check_query = elem.childNodes[0];
2664 if (check_query) {
2665 check_digest = check_query.getElementsByTagName("digest")[0];
2666 if (check_digest) {
2667 use_digest = true;
2668 }
2669 }
2670 }
2671
2672 // Use digest or plaintext depending on the server features
2673 var iq = $iq({type: "set", id: "_auth_2"})
2674 .c('query', {xmlns: Strophe.NS.AUTH})
2675 .c('username', {}).t(Strophe.getNodeFromJid(this.jid));
2676 if (use_digest) {
2677 iq.up().c("digest", {})
2678 .t(hex_sha1(this.stream_id + this.pass));
2679 } else {
2680 iq.up().c('password', {}).t(this.pass);
2681 }
2682 if (!Strophe.getResourceFromJid(this.jid)) {
2683 // since the user has not supplied a resource, we pick
2684 // a default one here. unlike other auth methods, the server
2685 // cannot do this for us.
2686 this.jid = Strophe.getBareJidFromJid(this.jid) + '/strophe';
2687 }
2688 iq.up().c('resource', {}).t(Strophe.getResourceFromJid(this.jid));
2689
2690 this._addSysHandler(this._auth2_cb.bind(this), null,
2691 null, null, "_auth_2");
2692
2693 this.send(iq.tree());
2694
2695 return false;
2696 },
2697
2698 /** PrivateFunction: _sasl_success_cb
2699 * _Private_ handler for succesful SASL authentication.
2700 *
2701 * Parameters:
2702 * (XMLElement) elem - The matching stanza.
2703 *
2704 * Returns:
2705 * false to remove the handler.
2706 */
2707 _sasl_success_cb: function (elem)
2708 {
2709 Strophe.info("SASL authentication succeeded.");
2710
2711 // remove old handlers
2712 this.deleteHandler(this._sasl_failure_handler);
2713 this._sasl_failure_handler = null;
2714 if (this._sasl_challenge_handler) {
2715 this.deleteHandler(this._sasl_challenge_handler);
2716 this._sasl_challenge_handler = null;
2717 }
2718
2719 this._addSysHandler(this._sasl_auth1_cb.bind(this), null,
2720 "stream:features", null, null);
2721
2722 // we must send an xmpp:restart now
2723 this._sendRestart();
2724
2725 return false;
2726 },
2727
2728 /** PrivateFunction: _sasl_auth1_cb
2729 * _Private_ handler to start stream binding.
2730 *
2731 * Parameters:
2732 * (XMLElement) elem - The matching stanza.
2733 *
2734 * Returns:
2735 * false to remove the handler.
2736 */
2737 _sasl_auth1_cb: function (elem)
2738 {
2739 var i, child;
2740
2741 for (i = 0; i < elem.childNodes.length; i++) {
2742 child = elem.childNodes[i];
2743 if (child.nodeName == 'bind') {
2744 this.do_bind = true;
2745 }
2746
2747 if (child.nodeName == 'session') {
2748 this.do_session = true;
2749 }
2750 }
2751
2752 if (!this.do_bind) {
2753 this._changeConnectStatus(Strophe.Status.AUTHFAIL, null);
2754 return false;
2755 } else {
2756 this._addSysHandler(this._sasl_bind_cb.bind(this), null, null,
2757 null, "_bind_auth_2");
2758
2759 var resource = Strophe.getResourceFromJid(this.jid);
2760 if (resource)
2761 this.send($iq({type: "set", id: "_bind_auth_2"})
2762 .c('bind', {xmlns: Strophe.NS.BIND})
2763 .c('resource', {}).t(resource).tree());
2764 else
2765 this.send($iq({type: "set", id: "_bind_auth_2"})
2766 .c('bind', {xmlns: Strophe.NS.BIND})
2767 .tree());
2768 }
2769
2770 return false;
2771 },
2772
2773 /** PrivateFunction: _sasl_bind_cb
2774 * _Private_ handler for binding result and session start.
2775 *
2776 * Parameters:
2777 * (XMLElement) elem - The matching stanza.
2778 *
2779 * Returns:
2780 * false to remove the handler.
2781 */
2782 _sasl_bind_cb: function (elem)
2783 {
2784 if (elem.getAttribute("type") == "error") {
2785 Strophe.info("SASL binding failed.");
2786 this._changeConnectStatus(Strophe.Status.AUTHFAIL, null);
2787 return false;
2788 }
2789
2790 // TODO - need to grab errors
2791 var bind = elem.getElementsByTagName("bind");
2792 var jidNode;
2793 if (bind.length > 0) {
2794 // Grab jid
2795 jidNode = bind[0].getElementsByTagName("jid");
2796 if (jidNode.length > 0) {
2797 this.jid = Strophe.getText(jidNode[0]);
2798
2799 if (this.do_session) {
2800 this._addSysHandler(this._sasl_session_cb.bind(this),
2801 null, null, null, "_session_auth_2");
2802
2803 this.send($iq({type: "set", id: "_session_auth_2"})
2804 .c('session', {xmlns: Strophe.NS.SESSION})
2805 .tree());
2806 }
2807 }
2808 } else {
2809 Strophe.info("SASL binding failed.");
2810 this._changeConnectStatus(Strophe.Status.AUTHFAIL, null);
2811 return false;
2812 }
2813 },
2814
2815 /** PrivateFunction: _sasl_session_cb
2816 * _Private_ handler to finish successful SASL connection.
2817 *
2818 * This sets Connection.authenticated to true on success, which
2819 * starts the processing of user handlers.
2820 *
2821 * Parameters:
2822 * (XMLElement) elem - The matching stanza.
2823 *
2824 * Returns:
2825 * false to remove the handler.
2826 */
2827 _sasl_session_cb: function (elem)
2828 {
2829 if (elem.getAttribute("type") == "result") {
2830 this.authenticated = true;
2831 this._changeConnectStatus(Strophe.Status.CONNECTED, null);
2832 } else if (elem.getAttribute("type") == "error") {
2833 Strophe.info("Session creation failed.");
2834 this._changeConnectStatus(Strophe.Status.AUTHFAIL, null);
2835 return false;
2836 }
2837
2838 return false;
2839 },
2840
2841 /** PrivateFunction: _sasl_failure_cb
2842 * _Private_ handler for SASL authentication failure.
2843 *
2844 * Parameters:
2845 * (XMLElement) elem - The matching stanza.
2846 *
2847 * Returns:
2848 * false to remove the handler.
2849 */
2850 _sasl_failure_cb: function (elem)
2851 {
2852 // delete unneeded handlers
2853 if (this._sasl_success_handler) {
2854 this.deleteHandler(this._sasl_success_handler);
2855 this._sasl_success_handler = null;
2856 }
2857 if (this._sasl_challenge_handler) {
2858 this.deleteHandler(this._sasl_challenge_handler);
2859 this._sasl_challenge_handler = null;
2860 }
2861
2862 this._changeConnectStatus(Strophe.Status.AUTHFAIL, null);
2863 return false;
2864 },
2865
2866 /** PrivateFunction: _auth2_cb
2867 * _Private_ handler to finish legacy authentication.
2868 *
2869 * This handler is called when the result from the jabber:iq:auth
2870 * <iq/> stanza is returned.
2871 *
2872 * Parameters:
2873 * (XMLElement) elem - The stanza that triggered the callback.
2874 *
2875 * Returns:
2876 * false to remove the handler.
2877 */
2878 _auth2_cb: function (elem)
2879 {
2880 if (elem.getAttribute("type") == "result") {
2881 this.authenticated = true;
2882 this._changeConnectStatus(Strophe.Status.CONNECTED, null);
2883 } else if (elem.getAttribute("type") == "error") {
2884 this._changeConnectStatus(Strophe.Status.AUTHFAIL, null);
2885 this.disconnect();
2886 }
2887
2888 return false;
2889 },
2890
2891 /** PrivateFunction: _addSysTimedHandler
2892 * _Private_ function to add a system level timed handler.
2893 *
2894 * This function is used to add a Strophe.TimedHandler for the
2895 * library code. System timed handlers are allowed to run before
2896 * authentication is complete.
2897 *
2898 * Parameters:
2899 * (Integer) period - The period of the handler.
2900 * (Function) handler - The callback function.
2901 */
2902 _addSysTimedHandler: function (period, handler)
2903 {
2904 var thand = new Strophe.TimedHandler(period, handler);
2905 thand.user = false;
2906 this.addTimeds.push(thand);
2907 return thand;
2908 },
2909
2910 /** PrivateFunction: _addSysHandler
2911 * _Private_ function to add a system level stanza handler.
2912 *
2913 * This function is used to add a Strophe.Handler for the
2914 * library code. System stanza handlers are allowed to run before
2915 * authentication is complete.
2916 *
2917 * Parameters:
2918 * (Function) handler - The callback function.
2919 * (String) ns - The namespace to match.
2920 * (String) name - The stanza name to match.
2921 * (String) type - The stanza type attribute to match.
2922 * (String) id - The stanza id attribute to match.
2923 */
2924 _addSysHandler: function (handler, ns, name, type, id)
2925 {
2926 var hand = new Strophe.Handler(handler, ns, name, type, id);
2927 hand.user = false;
2928 this.addHandlers.push(hand);
2929 return hand;
2930 },
2931
2932 /** PrivateFunction: _onDisconnectTimeout
2933 * _Private_ timeout handler for handling non-graceful disconnection.
2934 *
2935 * If the graceful disconnect process does not complete within the
2936 * time allotted, this handler finishes the disconnect anyway.
2937 *
2938 * Returns:
2939 * false to remove the handler.
2940 */
2941 _onDisconnectTimeout: function ()
2942 {
2943 Strophe.info("_onDisconnectTimeout was called");
2944
2945 // cancel all remaining requests and clear the queue
2946 var req;
2947 while (this._requests.length > 0) {
2948 req = this._requests.pop();
2949 req.xhr.abort();
2950 req.abort = true;
2951 }
2952
2953 // actually disconnect
2954 this._doDisconnect();
2955
2956 return false;
2957 },
2958
2959 /** PrivateFunction: _onIdle
2960 * _Private_ handler to process events during idle cycle.
2961 *
2962 * This handler is called every 100ms to fire timed handlers that
2963 * are ready and keep poll requests going.
2964 */
2965 _onIdle: function ()
2966 {
2967 var i, thand, since, newList;
2968
2969 // remove timed handlers that have been scheduled for deletion
2970 while (this.removeTimeds.length > 0) {
2971 thand = this.removeTimeds.pop();
2972 i = this.timedHandlers.indexOf(thand);
2973 if (i >= 0)
2974 this.timedHandlers.splice(i, 1);
2975 }
2976
2977 // add timed handlers scheduled for addition
2978 while (this.addTimeds.length > 0) {
2979 this.timedHandlers.push(this.addTimeds.pop());
2980 }
2981
2982 // call ready timed handlers
2983 var now = new Date().getTime();
2984 newList = [];
2985 for (i = 0; i < this.timedHandlers.length; i++) {
2986 thand = this.timedHandlers[i];
2987 if (this.authenticated || !thand.user) {
2988 since = thand.lastCalled + thand.period;
2989 if (since - now <= 0) {
2990 if (thand.run()) {
2991 newList.push(thand);
2992 }
2993 } else {
2994 newList.push(thand);
2995 }
2996 }
2997 }
2998 this.timedHandlers = newList;
2999
3000 var body, time_elapsed;
3001
3002 // if no requests are in progress, poll
3003 if (this.authenticated && this._requests.length === 0 &&
3004 this._data.length === 0 && !this.disconnecting) {
3005 Strophe.info("no requests during idle cycle, sending " +
3006 "blank request");
3007 this._data.push(null);
3008 }
3009
3010 if (this._requests.length < 2 && this._data.length > 0 &&
3011 !this.paused) {
3012 body = this._buildBody();
3013 for (i = 0; i < this._data.length; i++) {
3014 if (this._data[i] !== null) {
3015 if (this._data[i] === "restart") {
3016 body.attrs({
3017 to: this.domain,
3018 "xml:lang": "en",
3019 "xmpp:restart": "true",
3020 "xmlns:xmpp": Strophe.NS.BOSH
3021 })
3022 } else {
3023 body.cnode(this._data[i]).up();
3024 }
3025 }
3026 }
3027 delete this._data;
3028 this._data = [];
3029 this._requests.push(
3030 new Strophe.Request(body.tree(),
3031 this._onRequestStateChange.bind(this)
3032 .prependArg(this._dataRecv.bind(this)),
3033 body.tree().getAttribute("rid")));
3034 this._processRequest(this._requests.length - 1);
3035 }
3036
3037 if (this._requests.length > 0) {
3038 time_elapsed = this._requests[0].age();
3039 if (this._requests[0].dead !== null) {
3040 if (this._requests[0].timeDead() >
3041 Strophe.SECONDARY_TIMEOUT) {
3042 this._throttledRequestHandler();
3043 }
3044 }
3045
3046 if (time_elapsed > Strophe.TIMEOUT) {
3047 Strophe.warn("Request " +
3048 this._requests[0].id +
3049 " timed out, over " + Strophe.TIMEOUT +
3050 " seconds since last activity");
3051 this._throttledRequestHandler();
3052 }
3053 }
3054
3055 // reactivate the timer
3056 clearTimeout(this._idleTimeout);
3057 this._idleTimeout = setTimeout(this._onIdle.bind(this), 100);
3058 }
3059 };
3060
3061

mercurial