Mercurial > eldonilo > lightstring
annotate lightstring.js @ 33:88d24231bf24
Better newId function.
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Sat, 28 Jan 2012 04:28:25 +0100 |
parents | 630b9579fe4a |
children | 6ce66fba0242 |
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 | 3 /** |
4 Copyright (c) 2011, Sonny Piers <sonny at fastmail dot net> | |
5 | |
6 Permission to use, copy, modify, and/or distribute this software for any | |
7 purpose with or without fee is hereby granted, provided that the above | |
8 copyright notice and this permission notice appear in all copies. | |
9 | |
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | |
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | |
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | |
13 ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | |
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | |
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | |
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | |
17 */ | |
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 */ |
18 | 27 NS: { |
13
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 */ |
18 | 34 stanza: { |
13
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 |
21
b7d52bf259e0
Coding style: always use "" for XML strings.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
18
diff
changeset
|
38 return "<stream:stream to='" + aService + "'" + |
b7d52bf259e0
Coding style: always use "" for XML strings.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
18
diff
changeset
|
39 " xmlns='" + Lightstring.NS.jabberClient + "'" + |
b7d52bf259e0
Coding style: always use "" for XML strings.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
18
diff
changeset
|
40 " xmlns:stream='" + Lightstring.NS.stream + "'" + |
b7d52bf259e0
Coding style: always use "" for XML strings.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
18
diff
changeset
|
41 " version='1.0'/>"; |
13
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() { |
21
b7d52bf259e0
Coding style: always use "" for XML strings.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
18
diff
changeset
|
44 return "</stream:stream>"; |
13
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); |
33
88d24231bf24
Better newId function.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
28
diff
changeset
|
71 }, |
88d24231bf24
Better newId function.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
28
diff
changeset
|
72 /** |
88d24231bf24
Better newId function.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
28
diff
changeset
|
73 * @function Get an unique identifier. |
88d24231bf24
Better newId function.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
28
diff
changeset
|
74 * @param {String} [aString] Prefix to put before the identifier. |
88d24231bf24
Better newId function.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
28
diff
changeset
|
75 * @return {String} Identifier. |
88d24231bf24
Better newId function.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
28
diff
changeset
|
76 */ |
88d24231bf24
Better newId function.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
28
diff
changeset
|
77 newId: (function() { |
88d24231bf24
Better newId function.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
28
diff
changeset
|
78 var id = 1024; |
88d24231bf24
Better newId function.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
28
diff
changeset
|
79 return function(prefix) { |
88d24231bf24
Better newId function.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
28
diff
changeset
|
80 if (typeof prefix === 'string') |
88d24231bf24
Better newId function.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
28
diff
changeset
|
81 return prefix + id++; |
88d24231bf24
Better newId function.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
28
diff
changeset
|
82 return '' + id++; |
88d24231bf24
Better newId function.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
28
diff
changeset
|
83 }; |
88d24231bf24
Better newId function.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
28
diff
changeset
|
84 })(); |
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
|
85 }; |
96087680669f
Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
86 |
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
|
87 /** |
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 * @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
|
89 * @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
|
90 * @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
|
91 */ |
13
9aeb0750b9d1
fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents:
12
diff
changeset
|
92 Lightstring.Connection = function(aService) { |
9aeb0750b9d1
fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents:
12
diff
changeset
|
93 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
|
94 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
|
95 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
|
96 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
|
97 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
|
98 //SASL/Auth features |
13
9aeb0750b9d1
fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents:
12
diff
changeset
|
99 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
|
100 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
|
101 var mechanisms = {}; |
13
9aeb0750b9d1
fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents:
12
diff
changeset
|
102 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
|
103 mechanisms[nodes[i].textContent] = true; |
13
9aeb0750b9d1
fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents:
12
diff
changeset
|
104 |
9aeb0750b9d1
fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents:
12
diff
changeset
|
105 |
9aeb0750b9d1
fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents:
12
diff
changeset
|
106 //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
|
107 if ('DIGEST-MD5' in mechanisms) |
2 | 108 that.send( |
21
b7d52bf259e0
Coding style: always use "" for XML strings.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
18
diff
changeset
|
109 "<auth xmlns='urn:ietf:params:xml:ns:xmpp-sasl'" + |
b7d52bf259e0
Coding style: always use "" for XML strings.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
18
diff
changeset
|
110 " mechanism='DIGEST-MD5'/>" |
2 | 111 ); |
13
9aeb0750b9d1
fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents:
12
diff
changeset
|
112 else if ('PLAIN' in mechanisms) { |
2 | 113 var token = btoa( |
114 that.jid + | |
13
9aeb0750b9d1
fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents:
12
diff
changeset
|
115 '\u0000' + |
2 | 116 that.jid.node + |
13
9aeb0750b9d1
fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents:
12
diff
changeset
|
117 '\u0000' + |
2 | 118 that.password |
119 ); | |
120 that.send( | |
21
b7d52bf259e0
Coding style: always use "" for XML strings.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
18
diff
changeset
|
121 "<auth xmlns='urn:ietf:params:xml:ns:xmpp-sasl'" + |
b7d52bf259e0
Coding style: always use "" for XML strings.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
18
diff
changeset
|
122 " mechanism='PLAIN'>" + token + "</auth>" |
2 | 123 ); |
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
|
124 } |
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 //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
|
127 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
|
128 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
|
129 //Bind http://xmpp.org/rfcs/rfc3920.html#bind |
24
119ceb121908
Bind the correct JID received.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
23
diff
changeset
|
130 var bind = |
21
b7d52bf259e0
Coding style: always use "" for XML strings.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
18
diff
changeset
|
131 "<iq type='set' xmlns='jabber:client'>" + |
24
119ceb121908
Bind the correct JID received.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
23
diff
changeset
|
132 "<bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'>" + |
119ceb121908
Bind the correct JID received.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
23
diff
changeset
|
133 (that.jid.resource? "<resource>" + that.jid.resource + "</resource>": "") + |
119ceb121908
Bind the correct JID received.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
23
diff
changeset
|
134 "</bind>" + |
119ceb121908
Bind the correct JID received.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
23
diff
changeset
|
135 "</iq>"; |
119ceb121908
Bind the correct JID received.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
23
diff
changeset
|
136 that.send( |
119ceb121908
Bind the correct JID received.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
23
diff
changeset
|
137 bind, |
119ceb121908
Bind the correct JID received.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
23
diff
changeset
|
138 function(stanza) { |
119ceb121908
Bind the correct JID received.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
23
diff
changeset
|
139 //Session http://xmpp.org/rfcs/rfc3921.html#session |
119ceb121908
Bind the correct JID received.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
23
diff
changeset
|
140 that.jid = new Lightstring.JID(stanza.textContent); |
119ceb121908
Bind the correct JID received.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
23
diff
changeset
|
141 that.send( |
119ceb121908
Bind the correct JID received.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
23
diff
changeset
|
142 "<iq type='set' xmlns='jabber:client'>" + |
119ceb121908
Bind the correct JID received.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
23
diff
changeset
|
143 "<session xmlns='urn:ietf:params:xml:ns:xmpp-session'/>" + |
119ceb121908
Bind the correct JID received.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
23
diff
changeset
|
144 "</iq>", |
119ceb121908
Bind the correct JID received.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
23
diff
changeset
|
145 function() { |
119ceb121908
Bind the correct JID received.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
23
diff
changeset
|
146 that.emit('connected'); |
119ceb121908
Bind the correct JID received.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
23
diff
changeset
|
147 } |
119ceb121908
Bind the correct JID received.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
23
diff
changeset
|
148 ); |
119ceb121908
Bind the correct JID received.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
23
diff
changeset
|
149 } |
119ceb121908
Bind the correct JID received.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
23
diff
changeset
|
150 ); |
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
|
151 } |
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 this.on('success', function(stanza, that) { |
2 | 154 that.send( |
22
6a6bb8ded046
Add a JID object, and use it in Lightstring.Connection.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
21
diff
changeset
|
155 "<stream:stream to='" + that.jid.domain + "'" + |
21
b7d52bf259e0
Coding style: always use "" for XML strings.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
18
diff
changeset
|
156 " xmlns='jabber:client'" + |
b7d52bf259e0
Coding style: always use "" for XML strings.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
18
diff
changeset
|
157 " xmlns:stream='http://etherx.jabber.org/streams'" + |
b7d52bf259e0
Coding style: always use "" for XML strings.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
18
diff
changeset
|
158 " version='1.0'/>" |
2 | 159 ); |
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 }); |
3
029c12b8f048
various bug fixes and improvements
Sonny Piers <sonny.piers@gmail.com>
parents:
2
diff
changeset
|
161 this.on('failure', function(stanza, that) { |
029c12b8f048
various bug fixes and improvements
Sonny Piers <sonny.piers@gmail.com>
parents:
2
diff
changeset
|
162 that.emit('conn-error', stanza.firstChild.tagName); |
029c12b8f048
various bug fixes and improvements
Sonny Piers <sonny.piers@gmail.com>
parents:
2
diff
changeset
|
163 }); |
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
|
164 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
|
165 //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
|
166 |
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 function _quote(str) { |
13
9aeb0750b9d1
fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents:
12
diff
changeset
|
168 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
|
169 }; |
13
9aeb0750b9d1
fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents:
12
diff
changeset
|
170 |
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 var challenge = atob(stanza.textContent); |
13
9aeb0750b9d1
fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents:
12
diff
changeset
|
172 |
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
|
173 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
|
174 |
96087680669f
Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
175 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
|
176 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
|
177 var host = null; |
13
9aeb0750b9d1
fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents:
12
diff
changeset
|
178 var nonce = ''; |
9aeb0750b9d1
fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents:
12
diff
changeset
|
179 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
|
180 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
|
181 |
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 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
|
183 matches = challenge.match(attribMatch); |
13
9aeb0750b9d1
fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents:
12
diff
changeset
|
184 challenge = challenge.replace(matches[0], ''); |
9aeb0750b9d1
fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents:
12
diff
changeset
|
185 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
|
186 switch (matches[1]) { |
13
9aeb0750b9d1
fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents:
12
diff
changeset
|
187 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
|
188 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
|
189 break; |
13
9aeb0750b9d1
fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents:
12
diff
changeset
|
190 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
|
191 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
|
192 break; |
13
9aeb0750b9d1
fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents:
12
diff
changeset
|
193 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
|
194 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
|
195 break; |
13
9aeb0750b9d1
fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents:
12
diff
changeset
|
196 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
|
197 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
|
198 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
|
199 } |
96087680669f
Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
200 } |
96087680669f
Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
201 |
22
6a6bb8ded046
Add a JID object, and use it in Lightstring.Connection.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
21
diff
changeset
|
202 var digest_uri = 'xmpp/' + that.jid.domain; |
21
b7d52bf259e0
Coding style: always use "" for XML strings.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
18
diff
changeset
|
203 if (host !== null) |
13
9aeb0750b9d1
fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents:
12
diff
changeset
|
204 digest_uri = digest_uri + '/' + host; |
22
6a6bb8ded046
Add a JID object, and use it in Lightstring.Connection.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
21
diff
changeset
|
205 var A1 = MD5.hash(that.jid.node + |
13
9aeb0750b9d1
fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents:
12
diff
changeset
|
206 ':' + realm + ':' + that.password) + |
22
6a6bb8ded046
Add a JID object, and use it in Lightstring.Connection.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
21
diff
changeset
|
207 ':' + 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
|
208 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
|
209 |
13
9aeb0750b9d1
fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents:
12
diff
changeset
|
210 var responseText = ''; |
22
6a6bb8ded046
Add a JID object, and use it in Lightstring.Connection.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
21
diff
changeset
|
211 responseText += 'username=' + _quote(that.jid.node) + ','; |
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
|
212 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
|
213 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
|
214 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
|
215 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
|
216 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
|
217 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
|
218 responseText += 'response=' + _quote( |
13
9aeb0750b9d1
fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents:
12
diff
changeset
|
219 MD5.hexdigest(MD5.hexdigest(A1) + ':' + |
9aeb0750b9d1
fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents:
12
diff
changeset
|
220 nonce + ':00000001:' + |
9aeb0750b9d1
fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents:
12
diff
changeset
|
221 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
|
222 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
|
223 responseText += 'charset="utf-8"'; |
2 | 224 that.send( |
21
b7d52bf259e0
Coding style: always use "" for XML strings.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
18
diff
changeset
|
225 "<response xmlns='urn:ietf:params:xml:ns:xmpp-sasl'>" + |
b7d52bf259e0
Coding style: always use "" for XML strings.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
18
diff
changeset
|
226 btoa(responseText) + |
b7d52bf259e0
Coding style: always use "" for XML strings.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
18
diff
changeset
|
227 "</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
|
228 }); |
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 }; |
17
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
230 Lightstring.Connection.prototype = { |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
231 /** |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
232 * @function Create and open a websocket then go though the XMPP authentification process. |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
233 * @param {String} [aJid] The JID (Jabber id) to use. |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
234 * @param {String} [aPassword] The associated password. |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
235 */ |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
236 connect: function(aJid, aPassword) { |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
237 this.emit('connecting'); |
22
6a6bb8ded046
Add a JID object, and use it in Lightstring.Connection.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
21
diff
changeset
|
238 this.jid = new Lightstring.JID(aJid); |
17
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
239 if (aPassword) |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
240 this.password = aPassword; |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
241 |
22
6a6bb8ded046
Add a JID object, and use it in Lightstring.Connection.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
21
diff
changeset
|
242 if (!this.jid.bare) |
17
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
243 throw 'Lightstring: Connection.jid is undefined.'; |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
244 if (!this.password) |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
245 throw 'Lightstring: Connection.password is undefined.'; |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
246 if (!this.service) |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
247 throw 'Lightstring: Connection.service is undefined.'; |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
248 |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
249 //"Bug 695635 - tracking bug: unprefix WebSockets" https://bugzil.la/695635 |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
250 try { |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
251 this.socket = new WebSocket(this.service, 'xmpp'); |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
252 } |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
253 catch (error) { |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
254 this.socket = new MozWebSocket(this.service, 'xmpp'); |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
255 } |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
256 |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
257 var that = this; |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
258 this.socket.addEventListener('open', function() { |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
259 if (this.protocol !== 'xmpp') |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
260 console.error('Lightstring: The server located at '+ that.service + ' doesn\'t seems to be XMPP aware.'); |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
261 |
22
6a6bb8ded046
Add a JID object, and use it in Lightstring.Connection.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
21
diff
changeset
|
262 var stream = Lightstring.stanza.stream.open(that.jid.domain); |
17
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
263 |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
264 that.socket.send(stream); |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
265 that.emit('XMLOutput', stream); |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
266 }); |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
267 this.socket.addEventListener('error', function(e) { |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
268 that.emit('error', e.data); |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
269 console.log(e.data); |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
270 }); |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
271 this.socket.addEventListener('close', function(e) { |
18 | 272 that.emit('disconnected', e.data); |
17
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
273 }); |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
274 this.socket.addEventListener('message', function(e) { |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
275 that.emit('XMLInput', e.data); |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
276 var elm = Lightstring.xml2dom(e.data); |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
277 that.emit('DOMInput', elm); |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
278 that.emit(elm.tagName, elm); |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
279 |
23
99bf2bdcfd96
Emit an iq/namespace:tag event for each iq containing a payload.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
22
diff
changeset
|
280 if (elm.tagName === 'iq') { |
99bf2bdcfd96
Emit an iq/namespace:tag event for each iq containing a payload.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
22
diff
changeset
|
281 var payload = elm.firstChild; |
99bf2bdcfd96
Emit an iq/namespace:tag event for each iq containing a payload.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
22
diff
changeset
|
282 if (payload) |
99bf2bdcfd96
Emit an iq/namespace:tag event for each iq containing a payload.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
22
diff
changeset
|
283 that.emit('iq/' + payload.namespaceURI + ':' + payload.localName, elm); |
22
6a6bb8ded046
Add a JID object, and use it in Lightstring.Connection.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
21
diff
changeset
|
284 that.emit(elm.getAttribute('id'), elm); //FIXME: possible attack vector. |
23
99bf2bdcfd96
Emit an iq/namespace:tag event for each iq containing a payload.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
22
diff
changeset
|
285 } |
17
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
286 }); |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
287 }, |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
288 /** |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
289 * @function Send a message. |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
290 * @param {String|Object} aStanza The message to send. |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
291 * @param {Function} [aCallback] Executed on answer. (stanza must be iq) |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
292 */ |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
293 send: function(aStanza, aCallback) { |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
294 if (typeof aStanza === 'string') { |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
295 var str = aStanza; |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
296 var elm = Lightstring.xml2dom(str); |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
297 } |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
298 else if (aStanza instanceof Element) { |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
299 var elm = aStanza; |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
300 var str = this.dom2xml(elm); |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
301 } |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
302 else { |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
303 this.emit('error', 'Unsupported data type.'); |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
304 return; |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
305 } |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
306 |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
307 |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
308 if (elm.tagName === 'iq') { |
18 | 309 var id = elm.getAttribute('id'); |
17
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
310 if (!id) { |
33
88d24231bf24
Better newId function.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
28
diff
changeset
|
311 elm.setAttribute('id', Lightstring.newId('sendiq:')); |
17
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
312 str = Lightstring.dom2xml(elm); |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
313 } |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
314 if (aCallback) |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
315 this.on(elm.getAttribute('id'), aCallback); |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
316 } |
21
b7d52bf259e0
Coding style: always use "" for XML strings.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
18
diff
changeset
|
317 else if (aCallback) |
17
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
318 this.emit('warning', 'Callback can\'t be called with non-iq stanza.'); |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
319 |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
320 |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
321 this.socket.send(str); |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
322 this.emit('XMLOutput', str); |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
323 this.emit('DOMOutput', elm); |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
324 }, |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
325 /** |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
326 * @function Closes the XMPP stream and the socket. |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
327 */ |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
328 disconnect: function() { |
18 | 329 this.emit('disconnecting'); |
17
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
330 var stream = Lightstring.stanza.stream.close(); |
18 | 331 this.send(stream); |
17
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
332 this.emit('XMLOutput', stream); |
18 | 333 this.socket.close(); |
334 }, | |
17
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
335 /** |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
336 * @function Emits an event. |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
337 * @param {String} aName The event name. |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
338 * @param {Function|Array|Object} [aData] Data about the event. |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
339 */ |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
340 emit: function(aName, aData) { |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
341 var handlers = this.handlers[aName]; |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
342 if (!handlers) |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
343 return; |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
344 |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
345 //FIXME Better idea than passing the context as argument? |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
346 for (var i = 0; i < handlers.length; i++) |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
347 handlers[i](aData, this); |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
348 |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
349 if (aName.match('sendiq:')) |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
350 delete this.handlers[aName]; |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
351 }, |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
352 /** |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
353 * @function Register an event handler. |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
354 * @param {String} aName The event name. |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
355 * @param {Function} aCallback The callback to call when the event is emitted. |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
356 */ |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
357 on: function(aName, callback) { |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
358 if (!this.handlers[aName]) |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
359 this.handlers[aName] = []; |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
360 this.handlers[aName].push(callback); |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
361 } |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
362 //FIXME do this! |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
363 //~ this.once = function(name, callback) { |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
364 //~ if(!this.handlers[name]) |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
365 //~ this.handlers[name] = []; |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
366 //~ this.handlers[name].push(callback); |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
367 //~ }; |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
368 }; |