annotate lightstring.js @ 12:9fbd0e3678b5

add comments, jsdoc syntax + move the parser and the serializer to the Lightstring namespace so they don't get recreated at every new Lightstring.Connection
author Sonny Piers <sonny.piers@gmail.com>
date Sun, 15 Jan 2012 03:47:28 +0100
parents 24bc40461b2c
children 9aeb0750b9d1
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
1 'use strict';
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
2
2
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
3 /**
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
4 Copyright (c) 2011, Sonny Piers <sonny at fastmail dot net>
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
5
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
6 Permission to use, copy, modify, and/or distribute this software for any
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
7 purpose with or without fee is hereby granted, provided that the above
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
8 copyright notice and this permission notice appear in all copies.
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
9
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
13 ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
17 */
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
18
12
9fbd0e3678b5 add comments, jsdoc syntax + move the parser and the serializer to the Lightstring namespace so they don't get recreated at every new Lightstring.Connection
Sonny Piers <sonny.piers@gmail.com>
parents: 9
diff changeset
19
9fbd0e3678b5 add comments, jsdoc syntax + move the parser and the serializer to the Lightstring namespace so they don't get recreated at every new Lightstring.Connection
Sonny Piers <sonny.piers@gmail.com>
parents: 9
diff changeset
20 /**
9fbd0e3678b5 add comments, jsdoc syntax + move the parser and the serializer to the Lightstring namespace so they don't get recreated at every new Lightstring.Connection
Sonny Piers <sonny.piers@gmail.com>
parents: 9
diff changeset
21 * @namespace No code from lightstring should be callable outside this namespace/scope.
9fbd0e3678b5 add comments, jsdoc syntax + move the parser and the serializer to the Lightstring namespace so they don't get recreated at every new Lightstring.Connection
Sonny Piers <sonny.piers@gmail.com>
parents: 9
diff changeset
22 */
1
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
23 var Lightstring = {
12
9fbd0e3678b5 add comments, jsdoc syntax + move the parser and the serializer to the Lightstring namespace so they don't get recreated at every new Lightstring.Connection
Sonny Piers <sonny.piers@gmail.com>
parents: 9
diff changeset
24 /**
9fbd0e3678b5 add comments, jsdoc syntax + move the parser and the serializer to the Lightstring namespace so they don't get recreated at every new Lightstring.Connection
Sonny Piers <sonny.piers@gmail.com>
parents: 9
diff changeset
25 * @namespace Holds XMPP namespaces.
9fbd0e3678b5 add comments, jsdoc syntax + move the parser and the serializer to the Lightstring namespace so they don't get recreated at every new Lightstring.Connection
Sonny Piers <sonny.piers@gmail.com>
parents: 9
diff changeset
26 *
9fbd0e3678b5 add comments, jsdoc syntax + move the parser and the serializer to the Lightstring namespace so they don't get recreated at every new Lightstring.Connection
Sonny Piers <sonny.piers@gmail.com>
parents: 9
diff changeset
27 */
1
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
28 NS: {},
12
9fbd0e3678b5 add comments, jsdoc syntax + move the parser and the serializer to the Lightstring namespace so they don't get recreated at every new Lightstring.Connection
Sonny Piers <sonny.piers@gmail.com>
parents: 9
diff changeset
29 /**
9fbd0e3678b5 add comments, jsdoc syntax + move the parser and the serializer to the Lightstring namespace so they don't get recreated at every new Lightstring.Connection
Sonny Piers <sonny.piers@gmail.com>
parents: 9
diff changeset
30 * @namespace Holds XMPP stanza builders.
9fbd0e3678b5 add comments, jsdoc syntax + move the parser and the serializer to the Lightstring namespace so they don't get recreated at every new Lightstring.Connection
Sonny Piers <sonny.piers@gmail.com>
parents: 9
diff changeset
31 *
9fbd0e3678b5 add comments, jsdoc syntax + move the parser and the serializer to the Lightstring namespace so they don't get recreated at every new Lightstring.Connection
Sonny Piers <sonny.piers@gmail.com>
parents: 9
diff changeset
32 */
1
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
33 stanza: {},
12
9fbd0e3678b5 add comments, jsdoc syntax + move the parser and the serializer to the Lightstring namespace so they don't get recreated at every new Lightstring.Connection
Sonny Piers <sonny.piers@gmail.com>
parents: 9
diff changeset
34 /**
9fbd0e3678b5 add comments, jsdoc syntax + move the parser and the serializer to the Lightstring namespace so they don't get recreated at every new Lightstring.Connection
Sonny Piers <sonny.piers@gmail.com>
parents: 9
diff changeset
35 * @private
9fbd0e3678b5 add comments, jsdoc syntax + move the parser and the serializer to the Lightstring namespace so they don't get recreated at every new Lightstring.Connection
Sonny Piers <sonny.piers@gmail.com>
parents: 9
diff changeset
36 */
9fbd0e3678b5 add comments, jsdoc syntax + move the parser and the serializer to the Lightstring namespace so they don't get recreated at every new Lightstring.Connection
Sonny Piers <sonny.piers@gmail.com>
parents: 9
diff changeset
37 parser: new DOMParser(),
9fbd0e3678b5 add comments, jsdoc syntax + move the parser and the serializer to the Lightstring namespace so they don't get recreated at every new Lightstring.Connection
Sonny Piers <sonny.piers@gmail.com>
parents: 9
diff changeset
38 /**
9fbd0e3678b5 add comments, jsdoc syntax + move the parser and the serializer to the Lightstring namespace so they don't get recreated at every new Lightstring.Connection
Sonny Piers <sonny.piers@gmail.com>
parents: 9
diff changeset
39 * @private
9fbd0e3678b5 add comments, jsdoc syntax + move the parser and the serializer to the Lightstring namespace so they don't get recreated at every new Lightstring.Connection
Sonny Piers <sonny.piers@gmail.com>
parents: 9
diff changeset
40 */
9fbd0e3678b5 add comments, jsdoc syntax + move the parser and the serializer to the Lightstring namespace so they don't get recreated at every new Lightstring.Connection
Sonny Piers <sonny.piers@gmail.com>
parents: 9
diff changeset
41 serializer: new XMLSerializer(),
9fbd0e3678b5 add comments, jsdoc syntax + move the parser and the serializer to the Lightstring namespace so they don't get recreated at every new Lightstring.Connection
Sonny Piers <sonny.piers@gmail.com>
parents: 9
diff changeset
42 /**
9fbd0e3678b5 add comments, jsdoc syntax + move the parser and the serializer to the Lightstring namespace so they don't get recreated at every new Lightstring.Connection
Sonny Piers <sonny.piers@gmail.com>
parents: 9
diff changeset
43 * @function Transforms a XML string to a DOM object.
9fbd0e3678b5 add comments, jsdoc syntax + move the parser and the serializer to the Lightstring namespace so they don't get recreated at every new Lightstring.Connection
Sonny Piers <sonny.piers@gmail.com>
parents: 9
diff changeset
44 * @param {String} aString XML string
9fbd0e3678b5 add comments, jsdoc syntax + move the parser and the serializer to the Lightstring namespace so they don't get recreated at every new Lightstring.Connection
Sonny Piers <sonny.piers@gmail.com>
parents: 9
diff changeset
45 * @returns {Object} Domified XML.
9fbd0e3678b5 add comments, jsdoc syntax + move the parser and the serializer to the Lightstring namespace so they don't get recreated at every new Lightstring.Connection
Sonny Piers <sonny.piers@gmail.com>
parents: 9
diff changeset
46 */
9fbd0e3678b5 add comments, jsdoc syntax + move the parser and the serializer to the Lightstring namespace so they don't get recreated at every new Lightstring.Connection
Sonny Piers <sonny.piers@gmail.com>
parents: 9
diff changeset
47 xml2dom: function(aString) {
9fbd0e3678b5 add comments, jsdoc syntax + move the parser and the serializer to the Lightstring namespace so they don't get recreated at every new Lightstring.Connection
Sonny Piers <sonny.piers@gmail.com>
parents: 9
diff changeset
48 return this.parser.parseFromString(aString, 'text/xml').documentElement;
9fbd0e3678b5 add comments, jsdoc syntax + move the parser and the serializer to the Lightstring namespace so they don't get recreated at every new Lightstring.Connection
Sonny Piers <sonny.piers@gmail.com>
parents: 9
diff changeset
49 },
9fbd0e3678b5 add comments, jsdoc syntax + move the parser and the serializer to the Lightstring namespace so they don't get recreated at every new Lightstring.Connection
Sonny Piers <sonny.piers@gmail.com>
parents: 9
diff changeset
50 /**
9fbd0e3678b5 add comments, jsdoc syntax + move the parser and the serializer to the Lightstring namespace so they don't get recreated at every new Lightstring.Connection
Sonny Piers <sonny.piers@gmail.com>
parents: 9
diff changeset
51 * @function Transforms a DOM object to a XML string.
9fbd0e3678b5 add comments, jsdoc syntax + move the parser and the serializer to the Lightstring namespace so they don't get recreated at every new Lightstring.Connection
Sonny Piers <sonny.piers@gmail.com>
parents: 9
diff changeset
52 * @param {Object} aString DOM object
9fbd0e3678b5 add comments, jsdoc syntax + move the parser and the serializer to the Lightstring namespace so they don't get recreated at every new Lightstring.Connection
Sonny Piers <sonny.piers@gmail.com>
parents: 9
diff changeset
53 * @returns {String} Stringified DOM.
9fbd0e3678b5 add comments, jsdoc syntax + move the parser and the serializer to the Lightstring namespace so they don't get recreated at every new Lightstring.Connection
Sonny Piers <sonny.piers@gmail.com>
parents: 9
diff changeset
54 */
9fbd0e3678b5 add comments, jsdoc syntax + move the parser and the serializer to the Lightstring namespace so they don't get recreated at every new Lightstring.Connection
Sonny Piers <sonny.piers@gmail.com>
parents: 9
diff changeset
55 dom2xml: function(aElement) {
9fbd0e3678b5 add comments, jsdoc syntax + move the parser and the serializer to the Lightstring namespace so they don't get recreated at every new Lightstring.Connection
Sonny Piers <sonny.piers@gmail.com>
parents: 9
diff changeset
56 return this.serializer.serializeToString(aElement);
9fbd0e3678b5 add comments, jsdoc syntax + move the parser and the serializer to the Lightstring namespace so they don't get recreated at every new Lightstring.Connection
Sonny Piers <sonny.piers@gmail.com>
parents: 9
diff changeset
57 },
1
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
58 };
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
59
12
9fbd0e3678b5 add comments, jsdoc syntax + move the parser and the serializer to the Lightstring namespace so they don't get recreated at every new Lightstring.Connection
Sonny Piers <sonny.piers@gmail.com>
parents: 9
diff changeset
60 /**
9fbd0e3678b5 add comments, jsdoc syntax + move the parser and the serializer to the Lightstring namespace so they don't get recreated at every new Lightstring.Connection
Sonny Piers <sonny.piers@gmail.com>
parents: 9
diff changeset
61 * @constructor Creates a new Lightstring connection
9fbd0e3678b5 add comments, jsdoc syntax + move the parser and the serializer to the Lightstring namespace so they don't get recreated at every new Lightstring.Connection
Sonny Piers <sonny.piers@gmail.com>
parents: 9
diff changeset
62 * @param {String} [aService] The Websocket service URL.
9fbd0e3678b5 add comments, jsdoc syntax + move the parser and the serializer to the Lightstring namespace so they don't get recreated at every new Lightstring.Connection
Sonny Piers <sonny.piers@gmail.com>
parents: 9
diff changeset
63 * @memberOf Lightstring
9fbd0e3678b5 add comments, jsdoc syntax + move the parser and the serializer to the Lightstring namespace so they don't get recreated at every new Lightstring.Connection
Sonny Piers <sonny.piers@gmail.com>
parents: 9
diff changeset
64 */
2
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
65 Lightstring.Connection = function (aService) {
12
9fbd0e3678b5 add comments, jsdoc syntax + move the parser and the serializer to the Lightstring namespace so they don't get recreated at every new Lightstring.Connection
Sonny Piers <sonny.piers@gmail.com>
parents: 9
diff changeset
66 if(aService)
9fbd0e3678b5 add comments, jsdoc syntax + move the parser and the serializer to the Lightstring namespace so they don't get recreated at every new Lightstring.Connection
Sonny Piers <sonny.piers@gmail.com>
parents: 9
diff changeset
67 this.service = aService;
1
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
68 this.handlers = {};
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
69 this.iqid = 1024;
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
70 this.getNewId = function() {
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
71 this.iqid++;
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
72 return 'sendiq:'+this.iqid;
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
73 };
12
9fbd0e3678b5 add comments, jsdoc syntax + move the parser and the serializer to the Lightstring namespace so they don't get recreated at every new Lightstring.Connection
Sonny Piers <sonny.piers@gmail.com>
parents: 9
diff changeset
74 /**
9fbd0e3678b5 add comments, jsdoc syntax + move the parser and the serializer to the Lightstring namespace so they don't get recreated at every new Lightstring.Connection
Sonny Piers <sonny.piers@gmail.com>
parents: 9
diff changeset
75 * @function Create and open a websocket then go though the XMPP authentification process.
9fbd0e3678b5 add comments, jsdoc syntax + move the parser and the serializer to the Lightstring namespace so they don't get recreated at every new Lightstring.Connection
Sonny Piers <sonny.piers@gmail.com>
parents: 9
diff changeset
76 * @param {String} [aJid] The JID (Jabber id) to use.
9fbd0e3678b5 add comments, jsdoc syntax + move the parser and the serializer to the Lightstring namespace so they don't get recreated at every new Lightstring.Connection
Sonny Piers <sonny.piers@gmail.com>
parents: 9
diff changeset
77 * @param {String} [aPassword] The associated password.
9fbd0e3678b5 add comments, jsdoc syntax + move the parser and the serializer to the Lightstring namespace so they don't get recreated at every new Lightstring.Connection
Sonny Piers <sonny.piers@gmail.com>
parents: 9
diff changeset
78 */
2
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
79 this.connect = function(aJid, aPassword) {
3
029c12b8f048 various bug fixes and improvements
Sonny Piers <sonny.piers@gmail.com>
parents: 2
diff changeset
80 this.emit('connecting');
2
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
81 if(aJid)
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
82 this.jid = aJid;
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
83 if(this.jid) {
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
84 this.domain = this.jid.split('@')[1];
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
85 this.node = this.jid.split('@')[0];
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
86 this.resource = this.jid.split('/')[1];
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
87 }
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
88 if(aPassword)
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
89 this.password = aPassword;
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
90
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
91 if(!this.jid)
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
92 throw "Lightstring: Connection.jid is undefined.";
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
93 if(!this.password)
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
94 throw "Lightstring: Connection.password is undefined.";
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
95 if(!this.service)
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
96 throw "Lightstring: Connection.service is undefined.";
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
97
3
029c12b8f048 various bug fixes and improvements
Sonny Piers <sonny.piers@gmail.com>
parents: 2
diff changeset
98 //"Bug 695635 - tracking bug: unprefix WebSockets" https://bugzil.la/695635
029c12b8f048 various bug fixes and improvements
Sonny Piers <sonny.piers@gmail.com>
parents: 2
diff changeset
99 try {
6
b7a582a2b32c add the sub-protocol (xmpp) negociation
Sonny Piers <sonny.piers@gmail.com>
parents: 3
diff changeset
100 this.socket = new WebSocket(this.service, 'xmpp');
3
029c12b8f048 various bug fixes and improvements
Sonny Piers <sonny.piers@gmail.com>
parents: 2
diff changeset
101 }
029c12b8f048 various bug fixes and improvements
Sonny Piers <sonny.piers@gmail.com>
parents: 2
diff changeset
102 catch(error) {
6
b7a582a2b32c add the sub-protocol (xmpp) negociation
Sonny Piers <sonny.piers@gmail.com>
parents: 3
diff changeset
103 this.socket = new MozWebSocket(this.service, 'xmpp');
3
029c12b8f048 various bug fixes and improvements
Sonny Piers <sonny.piers@gmail.com>
parents: 2
diff changeset
104 }
1
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
105
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
106 var that = this;
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
107 this.socket.addEventListener('open', function() {
6
b7a582a2b32c add the sub-protocol (xmpp) negociation
Sonny Piers <sonny.piers@gmail.com>
parents: 3
diff changeset
108 if(this.protocol !== 'xmpp')
b7a582a2b32c add the sub-protocol (xmpp) negociation
Sonny Piers <sonny.piers@gmail.com>
parents: 3
diff changeset
109 throw "Lightstring: The server located at "+that.service+" is not XMPP aware.";
12
9fbd0e3678b5 add comments, jsdoc syntax + move the parser and the serializer to the Lightstring namespace so they don't get recreated at every new Lightstring.Connection
Sonny Piers <sonny.piers@gmail.com>
parents: 9
diff changeset
110 //FIXME no ending "/" - node-xmpp-bosh bug
3
029c12b8f048 various bug fixes and improvements
Sonny Piers <sonny.piers@gmail.com>
parents: 2
diff changeset
111 var stream =
2
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
112 "<stream:stream to='"+that.domain+"'\
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
113 xmlns='jabber:client'\
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
114 xmlns:stream='http://etherx.jabber.org/streams'\
3
029c12b8f048 various bug fixes and improvements
Sonny Piers <sonny.piers@gmail.com>
parents: 2
diff changeset
115 version='1.0'/>";
12
9fbd0e3678b5 add comments, jsdoc syntax + move the parser and the serializer to the Lightstring namespace so they don't get recreated at every new Lightstring.Connection
Sonny Piers <sonny.piers@gmail.com>
parents: 9
diff changeset
116
3
029c12b8f048 various bug fixes and improvements
Sonny Piers <sonny.piers@gmail.com>
parents: 2
diff changeset
117 that.socket.send(stream)
029c12b8f048 various bug fixes and improvements
Sonny Piers <sonny.piers@gmail.com>
parents: 2
diff changeset
118 that.emit('XMLOutput', stream);
1
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
119 });
2
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
120 this.socket.addEventListener('error', function(e) {
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
121 that.emit('error', e.data);
1
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
122 });
2
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
123 this.socket.addEventListener('close', function(e) {
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
124 that.emit('disconnected', e.data);
1
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
125 });
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
126 this.socket.addEventListener('message', function(e) {
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
127 that.emit('XMLInput', e.data);
12
9fbd0e3678b5 add comments, jsdoc syntax + move the parser and the serializer to the Lightstring namespace so they don't get recreated at every new Lightstring.Connection
Sonny Piers <sonny.piers@gmail.com>
parents: 9
diff changeset
128 var elm = Lightstring.xml2dom(e.data);
1
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
129 that.emit('DOMInput', elm);
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
130 that.emit(elm.tagName, elm);
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
131
2
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
132 if(elm.tagName === 'iq')
1
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
133 that.emit(elm.getAttribute('id'), elm);
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
134 });
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
135 };
12
9fbd0e3678b5 add comments, jsdoc syntax + move the parser and the serializer to the Lightstring namespace so they don't get recreated at every new Lightstring.Connection
Sonny Piers <sonny.piers@gmail.com>
parents: 9
diff changeset
136 /**
9fbd0e3678b5 add comments, jsdoc syntax + move the parser and the serializer to the Lightstring namespace so they don't get recreated at every new Lightstring.Connection
Sonny Piers <sonny.piers@gmail.com>
parents: 9
diff changeset
137 * @function Send a message.
9fbd0e3678b5 add comments, jsdoc syntax + move the parser and the serializer to the Lightstring namespace so they don't get recreated at every new Lightstring.Connection
Sonny Piers <sonny.piers@gmail.com>
parents: 9
diff changeset
138 * @param {String|Object} aStanza The message to send.
9fbd0e3678b5 add comments, jsdoc syntax + move the parser and the serializer to the Lightstring namespace so they don't get recreated at every new Lightstring.Connection
Sonny Piers <sonny.piers@gmail.com>
parents: 9
diff changeset
139 * @param {Function} [aCallback] A callback that will be called when the answer will answer.
9fbd0e3678b5 add comments, jsdoc syntax + move the parser and the serializer to the Lightstring namespace so they don't get recreated at every new Lightstring.Connection
Sonny Piers <sonny.piers@gmail.com>
parents: 9
diff changeset
140 */
2
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
141 this.send = function(aStanza, aCallback) {
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
142 if(typeof aStanza === 'string') {
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
143 var str = aStanza;
12
9fbd0e3678b5 add comments, jsdoc syntax + move the parser and the serializer to the Lightstring namespace so they don't get recreated at every new Lightstring.Connection
Sonny Piers <sonny.piers@gmail.com>
parents: 9
diff changeset
144 var elm = Lightstring.xml2dom(str);
2
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
145 }
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
146 else if(aStanza instanceof Element) {
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
147 var elm = aStanza;
12
9fbd0e3678b5 add comments, jsdoc syntax + move the parser and the serializer to the Lightstring namespace so they don't get recreated at every new Lightstring.Connection
Sonny Piers <sonny.piers@gmail.com>
parents: 9
diff changeset
148 var str = this.dom2xml(elm);
1
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
149 }
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
150 else {
2
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
151 that.emit('error', 'Unsupported data type.');
1
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
152 }
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
153
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
154
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
155 if(elm.tagName === 'iq') {
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
156 var id = elm.getAttribute('id');
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
157 if(!id) {
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
158 elm.setAttribute('id', this.getNewId())
12
9fbd0e3678b5 add comments, jsdoc syntax + move the parser and the serializer to the Lightstring namespace so they don't get recreated at every new Lightstring.Connection
Sonny Piers <sonny.piers@gmail.com>
parents: 9
diff changeset
159 str = Lightstring.dom2xml(elm)
1
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
160 }
2
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
161 if(aCallback)
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
162 this.on(elm.getAttribute('id'), aCallback);
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
163 }
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
164 else if(aCallback) {
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
165 that.emit('warning', 'Callback can\'t be called with non-iq stanza.');
1
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
166 }
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
167
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
168
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
169 this.socket.send(str);
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
170 this.emit('XMLOutput', str);
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
171 this.emit('DOMOutput', elm);
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
172 };
12
9fbd0e3678b5 add comments, jsdoc syntax + move the parser and the serializer to the Lightstring namespace so they don't get recreated at every new Lightstring.Connection
Sonny Piers <sonny.piers@gmail.com>
parents: 9
diff changeset
173 /**
9fbd0e3678b5 add comments, jsdoc syntax + move the parser and the serializer to the Lightstring namespace so they don't get recreated at every new Lightstring.Connection
Sonny Piers <sonny.piers@gmail.com>
parents: 9
diff changeset
174 * @function Close the XMPP stream and the socket.
9fbd0e3678b5 add comments, jsdoc syntax + move the parser and the serializer to the Lightstring namespace so they don't get recreated at every new Lightstring.Connection
Sonny Piers <sonny.piers@gmail.com>
parents: 9
diff changeset
175 */
1
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
176 this.disconnect = function() {
2
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
177 this.emit('disconnecting');
1
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
178 this.send('</stream:stream>');
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
179 this.socket.close();
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
180 };
12
9fbd0e3678b5 add comments, jsdoc syntax + move the parser and the serializer to the Lightstring namespace so they don't get recreated at every new Lightstring.Connection
Sonny Piers <sonny.piers@gmail.com>
parents: 9
diff changeset
181 /**
9fbd0e3678b5 add comments, jsdoc syntax + move the parser and the serializer to the Lightstring namespace so they don't get recreated at every new Lightstring.Connection
Sonny Piers <sonny.piers@gmail.com>
parents: 9
diff changeset
182 * @function Emit an event.
9fbd0e3678b5 add comments, jsdoc syntax + move the parser and the serializer to the Lightstring namespace so they don't get recreated at every new Lightstring.Connection
Sonny Piers <sonny.piers@gmail.com>
parents: 9
diff changeset
183 * @param {String} aName The event name.
9fbd0e3678b5 add comments, jsdoc syntax + move the parser and the serializer to the Lightstring namespace so they don't get recreated at every new Lightstring.Connection
Sonny Piers <sonny.piers@gmail.com>
parents: 9
diff changeset
184 * @param {Function|Array|Object} [aData] Data about the event.
9fbd0e3678b5 add comments, jsdoc syntax + move the parser and the serializer to the Lightstring namespace so they don't get recreated at every new Lightstring.Connection
Sonny Piers <sonny.piers@gmail.com>
parents: 9
diff changeset
185 */
9fbd0e3678b5 add comments, jsdoc syntax + move the parser and the serializer to the Lightstring namespace so they don't get recreated at every new Lightstring.Connection
Sonny Piers <sonny.piers@gmail.com>
parents: 9
diff changeset
186 this.emit = function(aName, aData) {
9fbd0e3678b5 add comments, jsdoc syntax + move the parser and the serializer to the Lightstring namespace so they don't get recreated at every new Lightstring.Connection
Sonny Piers <sonny.piers@gmail.com>
parents: 9
diff changeset
187 var handlers = this.handlers[aName];
1
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
188 if(!handlers)
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
189 return;
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
190
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
191 //FIXME Better idea than passing the context as argument?
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
192 for(var i=0; i<handlers.length; i++)
12
9fbd0e3678b5 add comments, jsdoc syntax + move the parser and the serializer to the Lightstring namespace so they don't get recreated at every new Lightstring.Connection
Sonny Piers <sonny.piers@gmail.com>
parents: 9
diff changeset
193 handlers[i](aData, this);
1
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
194
12
9fbd0e3678b5 add comments, jsdoc syntax + move the parser and the serializer to the Lightstring namespace so they don't get recreated at every new Lightstring.Connection
Sonny Piers <sonny.piers@gmail.com>
parents: 9
diff changeset
195 if(aName.match('sendiq:'))
9fbd0e3678b5 add comments, jsdoc syntax + move the parser and the serializer to the Lightstring namespace so they don't get recreated at every new Lightstring.Connection
Sonny Piers <sonny.piers@gmail.com>
parents: 9
diff changeset
196 delete this.handlers[aName];
1
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
197 };
12
9fbd0e3678b5 add comments, jsdoc syntax + move the parser and the serializer to the Lightstring namespace so they don't get recreated at every new Lightstring.Connection
Sonny Piers <sonny.piers@gmail.com>
parents: 9
diff changeset
198 /**
9fbd0e3678b5 add comments, jsdoc syntax + move the parser and the serializer to the Lightstring namespace so they don't get recreated at every new Lightstring.Connection
Sonny Piers <sonny.piers@gmail.com>
parents: 9
diff changeset
199 * @function Register an event handler.
9fbd0e3678b5 add comments, jsdoc syntax + move the parser and the serializer to the Lightstring namespace so they don't get recreated at every new Lightstring.Connection
Sonny Piers <sonny.piers@gmail.com>
parents: 9
diff changeset
200 * @param {String} aName The event name.
9fbd0e3678b5 add comments, jsdoc syntax + move the parser and the serializer to the Lightstring namespace so they don't get recreated at every new Lightstring.Connection
Sonny Piers <sonny.piers@gmail.com>
parents: 9
diff changeset
201 * @param {Function} aCallback The callback to call when the event is emitted.
9fbd0e3678b5 add comments, jsdoc syntax + move the parser and the serializer to the Lightstring namespace so they don't get recreated at every new Lightstring.Connection
Sonny Piers <sonny.piers@gmail.com>
parents: 9
diff changeset
202 */
9fbd0e3678b5 add comments, jsdoc syntax + move the parser and the serializer to the Lightstring namespace so they don't get recreated at every new Lightstring.Connection
Sonny Piers <sonny.piers@gmail.com>
parents: 9
diff changeset
203 this.on = function(aName, callback) {
9fbd0e3678b5 add comments, jsdoc syntax + move the parser and the serializer to the Lightstring namespace so they don't get recreated at every new Lightstring.Connection
Sonny Piers <sonny.piers@gmail.com>
parents: 9
diff changeset
204 if(!this.handlers[aName])
9fbd0e3678b5 add comments, jsdoc syntax + move the parser and the serializer to the Lightstring namespace so they don't get recreated at every new Lightstring.Connection
Sonny Piers <sonny.piers@gmail.com>
parents: 9
diff changeset
205 this.handlers[aName] = [];
9fbd0e3678b5 add comments, jsdoc syntax + move the parser and the serializer to the Lightstring namespace so they don't get recreated at every new Lightstring.Connection
Sonny Piers <sonny.piers@gmail.com>
parents: 9
diff changeset
206 this.handlers[aName].push(callback);
1
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
207 };
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
208 //FIXME do this!
12
9fbd0e3678b5 add comments, jsdoc syntax + move the parser and the serializer to the Lightstring namespace so they don't get recreated at every new Lightstring.Connection
Sonny Piers <sonny.piers@gmail.com>
parents: 9
diff changeset
209 //~ this.once = function(name, callback) {
9fbd0e3678b5 add comments, jsdoc syntax + move the parser and the serializer to the Lightstring namespace so they don't get recreated at every new Lightstring.Connection
Sonny Piers <sonny.piers@gmail.com>
parents: 9
diff changeset
210 //~ if(!this.handlers[name])
9fbd0e3678b5 add comments, jsdoc syntax + move the parser and the serializer to the Lightstring namespace so they don't get recreated at every new Lightstring.Connection
Sonny Piers <sonny.piers@gmail.com>
parents: 9
diff changeset
211 //~ this.handlers[name] = [];
9fbd0e3678b5 add comments, jsdoc syntax + move the parser and the serializer to the Lightstring namespace so they don't get recreated at every new Lightstring.Connection
Sonny Piers <sonny.piers@gmail.com>
parents: 9
diff changeset
212 //~ this.handlers[name].push(callback);
9fbd0e3678b5 add comments, jsdoc syntax + move the parser and the serializer to the Lightstring namespace so they don't get recreated at every new Lightstring.Connection
Sonny Piers <sonny.piers@gmail.com>
parents: 9
diff changeset
213 //~ };
1
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
214 this.on('stream:features', function(stanza, that) {
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
215 var nodes = stanza.querySelectorAll('mechanism');
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
216 //SASL/Auth features
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
217 if(nodes.length > 0) {
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
218 that.emit('mechanisms', stanza);
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
219 var mechanisms = {};
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
220 for(var i=0; i<nodes.length; i++)
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
221 mechanisms[nodes[i].textContent] = true;
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
222
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
223
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
224 //FIXME support SCRAM-SHA1 && allow specify method preferences
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
225 if('DIGEST-MD5' in mechanisms)
2
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
226 that.send(
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
227 "<auth xmlns='urn:ietf:params:xml:ns:xmpp-sasl'\
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
228 mechanism='DIGEST-MD5'/>"
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
229 );
1
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
230 else if('PLAIN' in mechanisms) {
2
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
231 var token = btoa(
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
232 that.jid +
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
233 "\u0000" +
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
234 that.jid.node +
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
235 "\u0000" +
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
236 that.password
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
237 );
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
238 that.send(
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
239 "<auth xmlns='urn:ietf:params:xml:ns:xmpp-sasl'\
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
240 mechanism='PLAIN'>"+token+"</auth>"
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
241 );
1
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
242 }
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
243 }
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
244 //XMPP features
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
245 else {
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
246 that.emit('features', stanza);
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
247 //Bind http://xmpp.org/rfcs/rfc3920.html#bind
2
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
248 that.send(
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
249 "<iq type='set' xmlns='jabber:client'>\
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
250 <bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'/>\
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
251 </iq>",
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
252 function() {
1
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
253 //Session http://xmpp.org/rfcs/rfc3921.html#session
2
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
254 that.send(
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
255 "<iq type='set' xmlns='jabber:client'>\
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
256 <session xmlns='urn:ietf:params:xml:ns:xmpp-session'/>\
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
257 </iq>",
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
258 function() {
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
259 that.emit('connected');
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
260 }
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
261 );
1
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
262 });
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
263 }
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
264 });
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
265 this.on('success', function(stanza, that) {
2
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
266 that.send(
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
267 "<stream:stream to='"+that.domain+"'\
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
268 xmlns='jabber:client'\
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
269 xmlns:stream='http://etherx.jabber.org/streams'\
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
270 version='1.0' />"
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
271 );
1
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
272 });
3
029c12b8f048 various bug fixes and improvements
Sonny Piers <sonny.piers@gmail.com>
parents: 2
diff changeset
273 this.on('failure', function(stanza, that) {
029c12b8f048 various bug fixes and improvements
Sonny Piers <sonny.piers@gmail.com>
parents: 2
diff changeset
274 that.emit('conn-error', stanza.firstChild.tagName);
029c12b8f048 various bug fixes and improvements
Sonny Piers <sonny.piers@gmail.com>
parents: 2
diff changeset
275 });
1
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
276 this.on('challenge', function(stanza, that) {
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
277 //FIXME this is mostly Strophe code
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
278
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
279 function _quote(str) {
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
280 return '"' + str.replace(/\\/g, "\\\\").replace(/"/g, '\\"') + '"';
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
281 };
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
282
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
283 var challenge = atob(stanza.textContent);
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
284
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
285 var attribMatch = /([a-z]+)=("[^"]+"|[^,"]+)(?:,|$)/;
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
286
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
287 var cnonce = MD5.hexdigest(Math.random() * 1234567890);
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
288 var realm = "";
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
289 var host = null;
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
290 var nonce = "";
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
291 var qop = "";
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
292 var matches;
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
293
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
294 while (challenge.match(attribMatch)) {
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
295 matches = challenge.match(attribMatch);
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
296 challenge = challenge.replace(matches[0], "");
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
297 matches[2] = matches[2].replace(/^"(.+)"$/, "$1");
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
298 switch (matches[1]) {
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
299 case "realm":
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
300 realm = matches[2];
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
301 break;
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
302 case "nonce":
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
303 nonce = matches[2];
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
304 break;
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
305 case "qop":
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
306 qop = matches[2];
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
307 break;
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
308 case "host":
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
309 host = matches[2];
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
310 break;
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
311 }
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
312 }
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
313
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
314 var digest_uri = "xmpp/" + that.domain;
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
315 if (host !== null) {
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
316 digest_uri = digest_uri + "/" + host;
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
317 }
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
318 var A1 = MD5.hash(that.node +
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
319 ":" + realm + ":" + that.password) +
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
320 ":" + nonce + ":" + cnonce;
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
321 var A2 = 'AUTHENTICATE:' + digest_uri;
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
322
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
323 var responseText = "";
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
324 responseText += 'username=' + _quote(that.node) + ',';
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
325 responseText += 'realm=' + _quote(realm) + ',';
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
326 responseText += 'nonce=' + _quote(nonce) + ',';
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
327 responseText += 'cnonce=' + _quote(cnonce) + ',';
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
328 responseText += 'nc="00000001",';
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
329 responseText += 'qop="auth",';
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
330 responseText += 'digest-uri=' + _quote(digest_uri) + ',';
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
331 responseText += 'response=' + _quote(
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
332 MD5.hexdigest(MD5.hexdigest(A1) + ":" +
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
333 nonce + ":00000001:" +
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
334 cnonce + ":auth:" +
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
335 MD5.hexdigest(A2))) + ',';
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
336 responseText += 'charset="utf-8"';
2
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
337 that.send(
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
338 "<response xmlns='urn:ietf:params:xml:ns:xmpp-sasl'>"
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
339 +btoa(responseText)+
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
340 "</response>");
1
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
341 });
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
342 };