annotate lightstring.js @ 13:9aeb0750b9d1

fix an error with the stream builder
author Sonny Piers <sonny.piers@gmail.com>
date Sun, 15 Jan 2012 15:23:51 +0100
parents 9fbd0e3678b5
children 6707f450549e
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 */
13
9aeb0750b9d1 fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents: 12
diff changeset
27 NS: {
9aeb0750b9d1 fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents: 12
diff changeset
28 stream: 'http://etherx.jabber.org/streams',
9aeb0750b9d1 fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents: 12
diff changeset
29 jabberClient: 'jabber:client'
9aeb0750b9d1 fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents: 12
diff changeset
30 },
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
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 * @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
33 */
13
9aeb0750b9d1 fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents: 12
diff changeset
34 stanza: {
9aeb0750b9d1 fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents: 12
diff changeset
35 stream: {
9aeb0750b9d1 fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents: 12
diff changeset
36 open: function(aService) {
9aeb0750b9d1 fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents: 12
diff changeset
37 //FIXME no ending "/" - node-xmpp-bosh bug
9aeb0750b9d1 fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents: 12
diff changeset
38 return "<stream:stream to='" + aService + "'\
9aeb0750b9d1 fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents: 12
diff changeset
39 xmlns='" + Lightstring.NS.jabberClient + "'\
9aeb0750b9d1 fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents: 12
diff changeset
40 xmlns:stream='" + Lightstring.NS.stream + "'\
9aeb0750b9d1 fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents: 12
diff changeset
41 version='1.0'/>";
9aeb0750b9d1 fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents: 12
diff changeset
42 },
9aeb0750b9d1 fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents: 12
diff changeset
43 close: function() {
9aeb0750b9d1 fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents: 12
diff changeset
44 return '</stream:stream>';
9aeb0750b9d1 fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents: 12
diff changeset
45 }
9aeb0750b9d1 fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents: 12
diff changeset
46 }
9aeb0750b9d1 fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents: 12
diff changeset
47 },
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
48 /**
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 * @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
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 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
52 /**
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 * @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
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 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
56 /**
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 * @function Transforms a XML string to a DOM object.
13
9aeb0750b9d1 fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents: 12
diff changeset
58 * @param {String} aString XML string.
9aeb0750b9d1 fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents: 12
diff changeset
59 * @return {Object} Domified XML.
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 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
62 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
63 },
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 /**
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
65 * @function Transforms a DOM object to a XML string.
13
9aeb0750b9d1 fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents: 12
diff changeset
66 * @param {Object} aString DOM object.
9aeb0750b9d1 fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents: 12
diff changeset
67 * @return {String} Stringified DOM.
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
68 */
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
69 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
70 return this.serializer.serializeToString(aElement);
13
9aeb0750b9d1 fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents: 12
diff changeset
71 }
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
72 };
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 * @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
76 * @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
77 * @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
78 */
13
9aeb0750b9d1 fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents: 12
diff changeset
79 Lightstring.Connection = function(aService) {
9aeb0750b9d1 fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents: 12
diff changeset
80 if (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
81 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
82 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
83 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
84 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
85 this.iqid++;
13
9aeb0750b9d1 fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents: 12
diff changeset
86 return 'sendiq:' + this.iqid;
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
87 };
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
88 /**
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
89 * @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
90 * @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
91 * @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
92 */
2
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
93 this.connect = function(aJid, aPassword) {
3
029c12b8f048 various bug fixes and improvements
Sonny Piers <sonny.piers@gmail.com>
parents: 2
diff changeset
94 this.emit('connecting');
13
9aeb0750b9d1 fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents: 12
diff changeset
95 if (aJid)
2
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
96 this.jid = aJid;
13
9aeb0750b9d1 fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents: 12
diff changeset
97 if (this.jid) {
9aeb0750b9d1 fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents: 12
diff changeset
98 this.host = this.jid.split('@')[1];
2
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
99 this.node = this.jid.split('@')[0];
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
100 this.resource = this.jid.split('/')[1];
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
101 }
13
9aeb0750b9d1 fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents: 12
diff changeset
102 if (aPassword)
2
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
103 this.password = aPassword;
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
104
13
9aeb0750b9d1 fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents: 12
diff changeset
105 if (!this.jid)
9aeb0750b9d1 fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents: 12
diff changeset
106 throw 'Lightstring: Connection.jid is undefined.';
9aeb0750b9d1 fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents: 12
diff changeset
107 if (!this.password)
9aeb0750b9d1 fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents: 12
diff changeset
108 throw 'Lightstring: Connection.password is undefined.';
9aeb0750b9d1 fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents: 12
diff changeset
109 if (!this.service)
9aeb0750b9d1 fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents: 12
diff changeset
110 throw 'Lightstring: Connection.service is undefined.';
2
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
111
3
029c12b8f048 various bug fixes and improvements
Sonny Piers <sonny.piers@gmail.com>
parents: 2
diff changeset
112 //"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
113 try {
6
b7a582a2b32c add the sub-protocol (xmpp) negociation
Sonny Piers <sonny.piers@gmail.com>
parents: 3
diff changeset
114 this.socket = new WebSocket(this.service, 'xmpp');
3
029c12b8f048 various bug fixes and improvements
Sonny Piers <sonny.piers@gmail.com>
parents: 2
diff changeset
115 }
13
9aeb0750b9d1 fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents: 12
diff changeset
116 catch (error) {
6
b7a582a2b32c add the sub-protocol (xmpp) negociation
Sonny Piers <sonny.piers@gmail.com>
parents: 3
diff changeset
117 this.socket = new MozWebSocket(this.service, 'xmpp');
3
029c12b8f048 various bug fixes and improvements
Sonny Piers <sonny.piers@gmail.com>
parents: 2
diff changeset
118 }
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
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
120 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
121 this.socket.addEventListener('open', function() {
13
9aeb0750b9d1 fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents: 12
diff changeset
122 if (this.protocol !== 'xmpp')
9aeb0750b9d1 fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents: 12
diff changeset
123 throw 'Lightstring: The server located at '+ that.service + ' is not XMPP aware.';
9aeb0750b9d1 fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents: 12
diff changeset
124
9aeb0750b9d1 fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents: 12
diff changeset
125 var stream = Lightstring.stanza.stream.open(that.host);
9aeb0750b9d1 fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents: 12
diff changeset
126
9aeb0750b9d1 fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents: 12
diff changeset
127 that.socket.send(stream);
3
029c12b8f048 various bug fixes and improvements
Sonny Piers <sonny.piers@gmail.com>
parents: 2
diff changeset
128 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
129 });
2
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
130 this.socket.addEventListener('error', function(e) {
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
131 that.emit('error', e.data);
13
9aeb0750b9d1 fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents: 12
diff changeset
132 console.log(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
133 });
2
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
134 this.socket.addEventListener('close', function(e) {
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
135 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
136 });
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
137 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
138 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
139 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
140 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
141 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
142
13
9aeb0750b9d1 fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents: 12
diff changeset
143 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
144 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
145 });
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
146 };
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
147 /**
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 * @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
149 * @param {String|Object} aStanza The message to send.
13
9aeb0750b9d1 fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents: 12
diff changeset
150 * @param {Function} [aCallback] Executed on answer. (stanza must be iq)
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
151 */
2
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
152 this.send = function(aStanza, aCallback) {
13
9aeb0750b9d1 fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents: 12
diff changeset
153 if (typeof aStanza === 'string') {
2
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
154 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
155 var elm = Lightstring.xml2dom(str);
2
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
156 }
13
9aeb0750b9d1 fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents: 12
diff changeset
157 else if (aStanza instanceof Element) {
2
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
158 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
159 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
160 }
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
161 else {
2
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
162 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
163 }
13
9aeb0750b9d1 fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents: 12
diff changeset
164
9aeb0750b9d1 fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents: 12
diff changeset
165
9aeb0750b9d1 fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents: 12
diff changeset
166 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
167 var id = elm.getAttribute('id');
13
9aeb0750b9d1 fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents: 12
diff changeset
168 if (!id) {
9aeb0750b9d1 fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents: 12
diff changeset
169 elm.setAttribute('id', this.getNewId());
9aeb0750b9d1 fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents: 12
diff changeset
170 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
171 }
13
9aeb0750b9d1 fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents: 12
diff changeset
172 if (aCallback)
2
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
173 this.on(elm.getAttribute('id'), aCallback);
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
174 }
13
9aeb0750b9d1 fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents: 12
diff changeset
175 else if (aCallback) {
2
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
176 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
177 }
13
9aeb0750b9d1 fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents: 12
diff changeset
178
9aeb0750b9d1 fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents: 12
diff changeset
179
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
180 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
181 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
182 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
183 };
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
184 /**
13
9aeb0750b9d1 fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents: 12
diff changeset
185 * @function Closes the XMPP stream and the socket.
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
186 */
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
187 this.disconnect = function() {
2
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
188 this.emit('disconnecting');
13
9aeb0750b9d1 fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents: 12
diff changeset
189 var stream = Lighstring.stanza.stream.close();
9aeb0750b9d1 fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents: 12
diff changeset
190 this.send(stream);
9aeb0750b9d1 fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents: 12
diff changeset
191 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
192 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
193 };
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
194 /**
13
9aeb0750b9d1 fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents: 12
diff changeset
195 * @function Emits an event.
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
196 * @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
197 * @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
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 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
200 var handlers = this.handlers[aName];
13
9aeb0750b9d1 fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents: 12
diff changeset
201 if (!handlers)
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
202 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
203
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
204 //FIXME Better idea than passing the context as argument?
13
9aeb0750b9d1 fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents: 12
diff changeset
205 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
206 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
207
13
9aeb0750b9d1 fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents: 12
diff changeset
208 if (aName.match('sendiq:'))
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 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
210 };
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
211 /**
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 * @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
213 * @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
214 * @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
215 */
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
216 this.on = function(aName, callback) {
13
9aeb0750b9d1 fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents: 12
diff changeset
217 if (!this.handlers[aName])
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
218 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
219 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
220 };
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 //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
222 //~ 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
223 //~ 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
224 //~ 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
225 //~ 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
226 //~ };
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
227 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
228 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
229 //SASL/Auth features
13
9aeb0750b9d1 fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents: 12
diff changeset
230 if (nodes.length > 0) {
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
231 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
232 var mechanisms = {};
13
9aeb0750b9d1 fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents: 12
diff changeset
233 for (var i = 0; i < nodes.length; i++)
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
234 mechanisms[nodes[i].textContent] = true;
13
9aeb0750b9d1 fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents: 12
diff changeset
235
9aeb0750b9d1 fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents: 12
diff changeset
236
9aeb0750b9d1 fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents: 12
diff changeset
237 //FIXME support SCRAM-SHA1 && allow specify method preferences
9aeb0750b9d1 fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents: 12
diff changeset
238 if ('DIGEST-MD5' in mechanisms)
2
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
239 that.send(
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
240 "<auth xmlns='urn:ietf:params:xml:ns:xmpp-sasl'\
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
241 mechanism='DIGEST-MD5'/>"
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
242 );
13
9aeb0750b9d1 fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents: 12
diff changeset
243 else if ('PLAIN' in mechanisms) {
2
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
244 var token = btoa(
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
245 that.jid +
13
9aeb0750b9d1 fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents: 12
diff changeset
246 '\u0000' +
2
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
247 that.jid.node +
13
9aeb0750b9d1 fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents: 12
diff changeset
248 '\u0000' +
2
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
249 that.password
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
250 );
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
251 that.send(
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
252 "<auth xmlns='urn:ietf:params:xml:ns:xmpp-sasl'\
13
9aeb0750b9d1 fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents: 12
diff changeset
253 mechanism='PLAIN'>" + token + '</auth>'
2
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
254 );
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
255 }
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
256 }
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
257 //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
258 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
259 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
260 //Bind http://xmpp.org/rfcs/rfc3920.html#bind
2
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
261 that.send(
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
262 "<iq type='set' xmlns='jabber:client'>\
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
263 <bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'/>\
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
264 </iq>",
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
265 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
266 //Session http://xmpp.org/rfcs/rfc3921.html#session
2
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
267 that.send(
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
268 "<iq type='set' xmlns='jabber:client'>\
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
269 <session xmlns='urn:ietf:params:xml:ns:xmpp-session'/>\
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
270 </iq>",
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
271 function() {
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
272 that.emit('connected');
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
273 }
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
274 );
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
275 });
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 }
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 });
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 this.on('success', function(stanza, that) {
2
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
279 that.send(
13
9aeb0750b9d1 fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents: 12
diff changeset
280 "<stream:stream to='" + that.domain + "'\
2
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
281 xmlns='jabber:client'\
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
282 xmlns:stream='http://etherx.jabber.org/streams'\
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
283 version='1.0' />"
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
284 );
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
285 });
3
029c12b8f048 various bug fixes and improvements
Sonny Piers <sonny.piers@gmail.com>
parents: 2
diff changeset
286 this.on('failure', function(stanza, that) {
029c12b8f048 various bug fixes and improvements
Sonny Piers <sonny.piers@gmail.com>
parents: 2
diff changeset
287 that.emit('conn-error', stanza.firstChild.tagName);
029c12b8f048 various bug fixes and improvements
Sonny Piers <sonny.piers@gmail.com>
parents: 2
diff changeset
288 });
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
289 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
290 //FIXME this is mostly Strophe code
13
9aeb0750b9d1 fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents: 12
diff changeset
291
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
292 function _quote(str) {
13
9aeb0750b9d1 fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents: 12
diff changeset
293 return '"' + str.replace(/\\/g, '\\\\').replace(/"/g, '\\"') + '"';
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
294 };
13
9aeb0750b9d1 fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents: 12
diff changeset
295
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
296 var challenge = atob(stanza.textContent);
13
9aeb0750b9d1 fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents: 12
diff changeset
297
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 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
299
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 var cnonce = MD5.hexdigest(Math.random() * 1234567890);
13
9aeb0750b9d1 fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents: 12
diff changeset
301 var realm = '';
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
302 var host = null;
13
9aeb0750b9d1 fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents: 12
diff changeset
303 var nonce = '';
9aeb0750b9d1 fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents: 12
diff changeset
304 var qop = '';
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
305 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
306
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 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
308 matches = challenge.match(attribMatch);
13
9aeb0750b9d1 fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents: 12
diff changeset
309 challenge = challenge.replace(matches[0], '');
9aeb0750b9d1 fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents: 12
diff changeset
310 matches[2] = matches[2].replace(/^"(.+)"$/, '$1');
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
311 switch (matches[1]) {
13
9aeb0750b9d1 fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents: 12
diff changeset
312 case 'realm':
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
313 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
314 break;
13
9aeb0750b9d1 fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents: 12
diff changeset
315 case 'nonce':
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
316 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
317 break;
13
9aeb0750b9d1 fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents: 12
diff changeset
318 case 'qop':
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
319 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
320 break;
13
9aeb0750b9d1 fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents: 12
diff changeset
321 case 'host':
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
322 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
323 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
324 }
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 }
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
13
9aeb0750b9d1 fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents: 12
diff changeset
327 var digest_uri = 'xmpp/' + that.domain;
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
328 if (host !== null) {
13
9aeb0750b9d1 fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents: 12
diff changeset
329 digest_uri = digest_uri + '/' + host;
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
330 }
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 var A1 = MD5.hash(that.node +
13
9aeb0750b9d1 fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents: 12
diff changeset
332 ':' + realm + ':' + that.password) +
9aeb0750b9d1 fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents: 12
diff changeset
333 ':' + nonce + ':' + cnonce;
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
334 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
335
13
9aeb0750b9d1 fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents: 12
diff changeset
336 var responseText = '';
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
337 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
338 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
339 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
340 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
341 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
342 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
343 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
344 responseText += 'response=' + _quote(
13
9aeb0750b9d1 fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents: 12
diff changeset
345 MD5.hexdigest(MD5.hexdigest(A1) + ':' +
9aeb0750b9d1 fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents: 12
diff changeset
346 nonce + ':00000001:' +
9aeb0750b9d1 fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents: 12
diff changeset
347 cnonce + ':auth:' +
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
348 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
349 responseText += 'charset="utf-8"';
2
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
350 that.send(
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
351 "<response xmlns='urn:ietf:params:xml:ns:xmpp-sasl'>"
13
9aeb0750b9d1 fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents: 12
diff changeset
352 + btoa(responseText) +
9aeb0750b9d1 fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents: 12
diff changeset
353 '</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
354 });
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
355 };