Mercurial > eldonilo > lightstring
annotate lightstring.js @ 43:136df1708856
Better iq callbacks. (breaks everything)
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Tue, 31 Jan 2012 18:12:19 +0100 |
parents | ee874d064650 |
children | 3dfb596cf669 |
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 */ |
29
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
28
diff
changeset
|
61 XML2DOM: function(aString) { |
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
28
diff
changeset
|
62 var DOM = null; |
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
28
diff
changeset
|
63 try { |
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
28
diff
changeset
|
64 DOM = this.parser.parseFromString(aString, 'text/xml').documentElement; |
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
28
diff
changeset
|
65 } |
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
28
diff
changeset
|
66 catch (e) { |
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
28
diff
changeset
|
67 alert(e); |
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
28
diff
changeset
|
68 } |
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
28
diff
changeset
|
69 finally { |
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
28
diff
changeset
|
70 return DOM; |
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
28
diff
changeset
|
71 }; |
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
|
72 }, |
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
|
73 /** |
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 * @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
|
75 * @param {Object} aString DOM object. |
9aeb0750b9d1
fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents:
12
diff
changeset
|
76 * @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
|
77 */ |
29
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
28
diff
changeset
|
78 DOM2XML: function(aElement) { |
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
28
diff
changeset
|
79 var XML = null; |
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
28
diff
changeset
|
80 try { |
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
28
diff
changeset
|
81 XML = this.serializer.serializeToString(aElement); |
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
28
diff
changeset
|
82 } |
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
28
diff
changeset
|
83 catch (e) { |
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
28
diff
changeset
|
84 alert(e); |
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
28
diff
changeset
|
85 } |
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
28
diff
changeset
|
86 finally { |
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
28
diff
changeset
|
87 return XML; |
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
28
diff
changeset
|
88 }; |
33
88d24231bf24
Better newId function.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
28
diff
changeset
|
89 }, |
88d24231bf24
Better newId function.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
28
diff
changeset
|
90 /** |
88d24231bf24
Better newId function.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
28
diff
changeset
|
91 * @function Get an unique identifier. |
88d24231bf24
Better newId function.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
28
diff
changeset
|
92 * @param {String} [aString] Prefix to put before the identifier. |
88d24231bf24
Better newId function.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
28
diff
changeset
|
93 * @return {String} Identifier. |
88d24231bf24
Better newId function.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
28
diff
changeset
|
94 */ |
88d24231bf24
Better newId function.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
28
diff
changeset
|
95 newId: (function() { |
88d24231bf24
Better newId function.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
28
diff
changeset
|
96 var id = 1024; |
88d24231bf24
Better newId function.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
28
diff
changeset
|
97 return function(prefix) { |
88d24231bf24
Better newId function.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
28
diff
changeset
|
98 if (typeof prefix === 'string') |
88d24231bf24
Better newId function.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
28
diff
changeset
|
99 return prefix + id++; |
88d24231bf24
Better newId function.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
28
diff
changeset
|
100 return '' + id++; |
88d24231bf24
Better newId function.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
28
diff
changeset
|
101 }; |
34 | 102 })() |
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 }; |
96087680669f
Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
104 |
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
|
105 /** |
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
|
106 * @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
|
107 * @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
|
108 * @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
|
109 */ |
13
9aeb0750b9d1
fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents:
12
diff
changeset
|
110 Lightstring.Connection = function(aService) { |
9aeb0750b9d1
fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents:
12
diff
changeset
|
111 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
|
112 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
|
113 this.handlers = {}; |
41
2895891f19bb
s/that/this/ in handlers.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
40
diff
changeset
|
114 this.on('stream:features', function(stanza) { |
29
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
28
diff
changeset
|
115 var nodes = stanza.DOM.querySelectorAll('mechanism'); |
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
|
116 //SASL/Auth features |
13
9aeb0750b9d1
fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents:
12
diff
changeset
|
117 if (nodes.length > 0) { |
41
2895891f19bb
s/that/this/ in handlers.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
40
diff
changeset
|
118 this.emit('mechanisms', 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
|
119 var mechanisms = {}; |
13
9aeb0750b9d1
fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents:
12
diff
changeset
|
120 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
|
121 mechanisms[nodes[i].textContent] = true; |
13
9aeb0750b9d1
fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents:
12
diff
changeset
|
122 |
9aeb0750b9d1
fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents:
12
diff
changeset
|
123 |
9aeb0750b9d1
fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents:
12
diff
changeset
|
124 //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
|
125 if ('DIGEST-MD5' in mechanisms) |
41
2895891f19bb
s/that/this/ in handlers.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
40
diff
changeset
|
126 this.send( |
21
b7d52bf259e0
Coding style: always use "" for XML strings.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
18
diff
changeset
|
127 "<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
|
128 " mechanism='DIGEST-MD5'/>" |
2 | 129 ); |
13
9aeb0750b9d1
fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents:
12
diff
changeset
|
130 else if ('PLAIN' in mechanisms) { |
2 | 131 var token = btoa( |
41
2895891f19bb
s/that/this/ in handlers.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
40
diff
changeset
|
132 this.jid + |
13
9aeb0750b9d1
fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents:
12
diff
changeset
|
133 '\u0000' + |
41
2895891f19bb
s/that/this/ in handlers.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
40
diff
changeset
|
134 this.jid.node + |
13
9aeb0750b9d1
fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents:
12
diff
changeset
|
135 '\u0000' + |
41
2895891f19bb
s/that/this/ in handlers.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
40
diff
changeset
|
136 this.password |
2 | 137 ); |
41
2895891f19bb
s/that/this/ in handlers.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
40
diff
changeset
|
138 this.send( |
21
b7d52bf259e0
Coding style: always use "" for XML strings.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
18
diff
changeset
|
139 "<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
|
140 " mechanism='PLAIN'>" + token + "</auth>" |
2 | 141 ); |
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
|
142 } |
96087680669f
Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
143 } |
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 //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
|
145 else { |
41
2895891f19bb
s/that/this/ in handlers.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
40
diff
changeset
|
146 this.emit('features', 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
|
147 //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
|
148 var bind = |
21
b7d52bf259e0
Coding style: always use "" for XML strings.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
18
diff
changeset
|
149 "<iq type='set' xmlns='jabber:client'>" + |
24
119ceb121908
Bind the correct JID received.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
23
diff
changeset
|
150 "<bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'>" + |
41
2895891f19bb
s/that/this/ in handlers.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
40
diff
changeset
|
151 (this.jid.resource? "<resource>" + this.jid.resource + "</resource>": "") + |
24
119ceb121908
Bind the correct JID received.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
23
diff
changeset
|
152 "</bind>" + |
119ceb121908
Bind the correct JID received.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
23
diff
changeset
|
153 "</iq>"; |
29
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
28
diff
changeset
|
154 |
41
2895891f19bb
s/that/this/ in handlers.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
40
diff
changeset
|
155 this.send( |
24
119ceb121908
Bind the correct JID received.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
23
diff
changeset
|
156 bind, |
119ceb121908
Bind the correct JID received.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
23
diff
changeset
|
157 function(stanza) { |
119ceb121908
Bind the correct JID received.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
23
diff
changeset
|
158 //Session http://xmpp.org/rfcs/rfc3921.html#session |
41
2895891f19bb
s/that/this/ in handlers.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
40
diff
changeset
|
159 this.jid = new Lightstring.JID(stanza.DOM.textContent); |
2895891f19bb
s/that/this/ in handlers.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
40
diff
changeset
|
160 this.send( |
24
119ceb121908
Bind the correct JID received.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
23
diff
changeset
|
161 "<iq type='set' xmlns='jabber:client'>" + |
119ceb121908
Bind the correct JID received.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
23
diff
changeset
|
162 "<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
|
163 "</iq>", |
119ceb121908
Bind the correct JID received.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
23
diff
changeset
|
164 function() { |
41
2895891f19bb
s/that/this/ in handlers.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
40
diff
changeset
|
165 this.emit('connected'); |
24
119ceb121908
Bind the correct JID received.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
23
diff
changeset
|
166 } |
119ceb121908
Bind the correct JID received.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
23
diff
changeset
|
167 ); |
119ceb121908
Bind the correct JID received.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
23
diff
changeset
|
168 } |
119ceb121908
Bind the correct JID received.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
23
diff
changeset
|
169 ); |
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
|
170 } |
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 }); |
41
2895891f19bb
s/that/this/ in handlers.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
40
diff
changeset
|
172 this.on('success', function(stanza) { |
2895891f19bb
s/that/this/ in handlers.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
40
diff
changeset
|
173 this.send( |
2895891f19bb
s/that/this/ in handlers.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
40
diff
changeset
|
174 "<stream:stream to='" + this.jid.domain + "'" + |
21
b7d52bf259e0
Coding style: always use "" for XML strings.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
18
diff
changeset
|
175 " xmlns='jabber:client'" + |
b7d52bf259e0
Coding style: always use "" for XML strings.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
18
diff
changeset
|
176 " 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
|
177 " version='1.0'/>" |
2 | 178 ); |
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
|
179 }); |
41
2895891f19bb
s/that/this/ in handlers.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
40
diff
changeset
|
180 this.on('failure', function(stanza) { |
2895891f19bb
s/that/this/ in handlers.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
40
diff
changeset
|
181 this.emit('conn-error', stanza.DOM.firstChild.tagName); |
3
029c12b8f048
various bug fixes and improvements
Sonny Piers <sonny.piers@gmail.com>
parents:
2
diff
changeset
|
182 }); |
41
2895891f19bb
s/that/this/ in handlers.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
40
diff
changeset
|
183 this.on('challenge', function(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
|
184 //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
|
185 |
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 function _quote(str) { |
13
9aeb0750b9d1
fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents:
12
diff
changeset
|
187 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
|
188 }; |
13
9aeb0750b9d1
fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents:
12
diff
changeset
|
189 |
29
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
28
diff
changeset
|
190 var challenge = atob(stanza.DOM.textContent); |
13
9aeb0750b9d1
fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents:
12
diff
changeset
|
191 |
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 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
|
193 |
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 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
|
195 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
|
196 var host = null; |
13
9aeb0750b9d1
fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents:
12
diff
changeset
|
197 var nonce = ''; |
9aeb0750b9d1
fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents:
12
diff
changeset
|
198 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
|
199 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
|
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 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
|
202 matches = challenge.match(attribMatch); |
13
9aeb0750b9d1
fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents:
12
diff
changeset
|
203 challenge = challenge.replace(matches[0], ''); |
9aeb0750b9d1
fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents:
12
diff
changeset
|
204 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
|
205 switch (matches[1]) { |
13
9aeb0750b9d1
fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents:
12
diff
changeset
|
206 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
|
207 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
|
208 break; |
13
9aeb0750b9d1
fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents:
12
diff
changeset
|
209 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
|
210 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
|
211 break; |
13
9aeb0750b9d1
fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents:
12
diff
changeset
|
212 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
|
213 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
|
214 break; |
13
9aeb0750b9d1
fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents:
12
diff
changeset
|
215 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
|
216 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
|
217 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
|
218 } |
96087680669f
Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
219 } |
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 |
41
2895891f19bb
s/that/this/ in handlers.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
40
diff
changeset
|
221 var digest_uri = 'xmpp/' + this.jid.domain; |
21
b7d52bf259e0
Coding style: always use "" for XML strings.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
18
diff
changeset
|
222 if (host !== null) |
13
9aeb0750b9d1
fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents:
12
diff
changeset
|
223 digest_uri = digest_uri + '/' + host; |
41
2895891f19bb
s/that/this/ in handlers.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
40
diff
changeset
|
224 var A1 = MD5.hash(this.jid.node + |
2895891f19bb
s/that/this/ in handlers.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
40
diff
changeset
|
225 ':' + realm + ':' + this.password) + |
22
6a6bb8ded046
Add a JID object, and use it in Lightstring.Connection.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
21
diff
changeset
|
226 ':' + 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
|
227 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
|
228 |
13
9aeb0750b9d1
fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents:
12
diff
changeset
|
229 var responseText = ''; |
41
2895891f19bb
s/that/this/ in handlers.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
40
diff
changeset
|
230 responseText += 'username=' + _quote(this.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
|
231 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
|
232 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
|
233 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
|
234 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
|
235 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
|
236 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
|
237 responseText += 'response=' + _quote( |
13
9aeb0750b9d1
fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents:
12
diff
changeset
|
238 MD5.hexdigest(MD5.hexdigest(A1) + ':' + |
9aeb0750b9d1
fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents:
12
diff
changeset
|
239 nonce + ':00000001:' + |
9aeb0750b9d1
fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents:
12
diff
changeset
|
240 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
|
241 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
|
242 responseText += 'charset="utf-8"'; |
41
2895891f19bb
s/that/this/ in handlers.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
40
diff
changeset
|
243 this.send( |
21
b7d52bf259e0
Coding style: always use "" for XML strings.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
18
diff
changeset
|
244 "<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
|
245 btoa(responseText) + |
b7d52bf259e0
Coding style: always use "" for XML strings.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
18
diff
changeset
|
246 "</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
|
247 }); |
96087680669f
Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
248 }; |
17
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
249 Lightstring.Connection.prototype = { |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
250 /** |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
251 * @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
|
252 * @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
|
253 * @param {String} [aPassword] The associated password. |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
254 */ |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
255 connect: function(aJid, aPassword) { |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
256 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
|
257 this.jid = new Lightstring.JID(aJid); |
17
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
258 if (aPassword) |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
259 this.password = aPassword; |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
260 |
22
6a6bb8ded046
Add a JID object, and use it in Lightstring.Connection.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
21
diff
changeset
|
261 if (!this.jid.bare) |
17
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
262 throw 'Lightstring: Connection.jid is undefined.'; |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
263 if (!this.password) |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
264 throw 'Lightstring: Connection.password is undefined.'; |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
265 if (!this.service) |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
266 throw 'Lightstring: Connection.service is undefined.'; |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
267 |
40 | 268 if(typeof(WebSocket) === "function") { |
269 this.socket = new WebSocket(this.service, 'xmpp'); | |
270 } | |
271 else if(typeof(MozWebSocket) === "function") { | |
39
5e32481b8a92
Better WebSocket feature discovering and Opera support
Sonny Piers <sonny.piers@gmail.com>
parents:
37
diff
changeset
|
272 this.socket = new MozWebSocket(this.service, 'xmpp'); |
5e32481b8a92
Better WebSocket feature discovering and Opera support
Sonny Piers <sonny.piers@gmail.com>
parents:
37
diff
changeset
|
273 } |
5e32481b8a92
Better WebSocket feature discovering and Opera support
Sonny Piers <sonny.piers@gmail.com>
parents:
37
diff
changeset
|
274 else { |
5e32481b8a92
Better WebSocket feature discovering and Opera support
Sonny Piers <sonny.piers@gmail.com>
parents:
37
diff
changeset
|
275 throw new Error('WebSocket not available.'); |
17
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
276 } |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
277 |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
278 var that = this; |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
279 this.socket.addEventListener('open', function() { |
29
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
28
diff
changeset
|
280 //TODO: if (this.protocol !== 'xmpp') |
17
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
281 |
22
6a6bb8ded046
Add a JID object, and use it in Lightstring.Connection.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
21
diff
changeset
|
282 var stream = Lightstring.stanza.stream.open(that.jid.domain); |
29
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
28
diff
changeset
|
283 //TODO: Use Lightstring.Connection.send (problem with parsing steam); |
17
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
284 that.socket.send(stream); |
29
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
28
diff
changeset
|
285 var stanza = { |
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
28
diff
changeset
|
286 XML: stream |
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
28
diff
changeset
|
287 }; |
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
28
diff
changeset
|
288 that.emit('output', stanza); |
17
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
289 }); |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
290 this.socket.addEventListener('error', function(e) { |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
291 that.emit('error', e.data); |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
292 console.log(e.data); |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
293 }); |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
294 this.socket.addEventListener('close', function(e) { |
18 | 295 that.emit('disconnected', e.data); |
17
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
296 }); |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
297 this.socket.addEventListener('message', function(e) { |
29
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
28
diff
changeset
|
298 var stanza = new Lightstring.Stanza(e.data); |
17
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
299 |
29
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
28
diff
changeset
|
300 //TODO node-xmpp-bosh sends a self-closing stream:stream tag; it is wrong! |
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
28
diff
changeset
|
301 that.emit('input', stanza); |
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
28
diff
changeset
|
302 |
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
28
diff
changeset
|
303 if(!stanza.DOM) |
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
28
diff
changeset
|
304 return; |
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
28
diff
changeset
|
305 |
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
28
diff
changeset
|
306 that.emit(stanza.DOM.tagName, stanza); |
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
28
diff
changeset
|
307 |
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
28
diff
changeset
|
308 if (stanza.DOM.tagName === 'iq') { |
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
28
diff
changeset
|
309 var payload = stanza.DOM.firstChild; |
23
99bf2bdcfd96
Emit an iq/namespace:tag event for each iq containing a payload.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
22
diff
changeset
|
310 if (payload) |
29
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
28
diff
changeset
|
311 that.emit('iq/' + payload.namespaceURI + ':' + payload.localName, stanza); |
43
136df1708856
Better iq callbacks. (breaks everything)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
42
diff
changeset
|
312 |
136df1708856
Better iq callbacks. (breaks everything)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
42
diff
changeset
|
313 var id = stanza.DOM.getAttributeNS(null, 'id'); |
136df1708856
Better iq callbacks. (breaks everything)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
42
diff
changeset
|
314 if (!(id && id in that.callbacks)) |
136df1708856
Better iq callbacks. (breaks everything)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
42
diff
changeset
|
315 return; |
136df1708856
Better iq callbacks. (breaks everything)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
42
diff
changeset
|
316 |
136df1708856
Better iq callbacks. (breaks everything)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
42
diff
changeset
|
317 var type = stanza.DOM.getAttributeNS(null, 'type'); |
136df1708856
Better iq callbacks. (breaks everything)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
42
diff
changeset
|
318 if (type !== 'result' || type !== 'error') |
136df1708856
Better iq callbacks. (breaks everything)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
42
diff
changeset
|
319 return; //TODO: emit an warning, perhaps. |
136df1708856
Better iq callbacks. (breaks everything)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
42
diff
changeset
|
320 |
136df1708856
Better iq callbacks. (breaks everything)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
42
diff
changeset
|
321 var callback = that.callbacks[id]; |
136df1708856
Better iq callbacks. (breaks everything)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
42
diff
changeset
|
322 if (type === 'result' && callback.result) |
136df1708856
Better iq callbacks. (breaks everything)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
42
diff
changeset
|
323 callback.result(stanza); |
136df1708856
Better iq callbacks. (breaks everything)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
42
diff
changeset
|
324 else if (type === 'error' && callback.error) |
136df1708856
Better iq callbacks. (breaks everything)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
42
diff
changeset
|
325 callback.error(stanza); |
136df1708856
Better iq callbacks. (breaks everything)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
42
diff
changeset
|
326 |
136df1708856
Better iq callbacks. (breaks everything)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
42
diff
changeset
|
327 delete that.callbacks[id]; |
41
2895891f19bb
s/that/this/ in handlers.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
40
diff
changeset
|
328 |
2895891f19bb
s/that/this/ in handlers.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
40
diff
changeset
|
329 //TODO: really needed? |
2895891f19bb
s/that/this/ in handlers.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
40
diff
changeset
|
330 } else if (stanza.DOM.tagName === 'message') { |
2895891f19bb
s/that/this/ in handlers.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
40
diff
changeset
|
331 var payloads = stanza.DOM.children; |
2895891f19bb
s/that/this/ in handlers.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
40
diff
changeset
|
332 for (var i = 0; i < payloads.length; i++) |
2895891f19bb
s/that/this/ in handlers.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
40
diff
changeset
|
333 that.emit('message/' + payloads[i].namespaceURI + ':' + payloads[i].localName, stanza); |
23
99bf2bdcfd96
Emit an iq/namespace:tag event for each iq containing a payload.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
22
diff
changeset
|
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 }, |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
337 /** |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
338 * @function Send a message. |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
339 * @param {String|Object} aStanza The message to send. |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
340 * @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
|
341 */ |
43
136df1708856
Better iq callbacks. (breaks everything)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
42
diff
changeset
|
342 send: function(aStanza, aResult, aError) { |
29
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
28
diff
changeset
|
343 if (!(aStanza instanceof Lightstring.Stanza)) |
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
28
diff
changeset
|
344 var stanza = new Lightstring.Stanza(aStanza); |
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
28
diff
changeset
|
345 else |
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
28
diff
changeset
|
346 var stanza = aStanza; |
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
28
diff
changeset
|
347 |
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
28
diff
changeset
|
348 if(!stanza) |
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
28
diff
changeset
|
349 return; |
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
28
diff
changeset
|
350 |
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
28
diff
changeset
|
351 if (stanza.DOM.tagName === 'iq') { |
42
ee874d064650
Check iq sending.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
41
diff
changeset
|
352 var type = stanza.DOM.getAttributeNS(null, 'type'); |
ee874d064650
Check iq sending.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
41
diff
changeset
|
353 if (type !== 'get' || type !== 'set') |
ee874d064650
Check iq sending.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
41
diff
changeset
|
354 ; //TODO: emit an error. |
ee874d064650
Check iq sending.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
41
diff
changeset
|
355 |
43
136df1708856
Better iq callbacks. (breaks everything)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
42
diff
changeset
|
356 var callback = {result: aResult, error: aError}; |
136df1708856
Better iq callbacks. (breaks everything)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
42
diff
changeset
|
357 |
42
ee874d064650
Check iq sending.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
41
diff
changeset
|
358 var id = stanza.DOM.getAttributeNS(null, 'id'); |
43
136df1708856
Better iq callbacks. (breaks everything)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
42
diff
changeset
|
359 if (!id) |
136df1708856
Better iq callbacks. (breaks everything)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
42
diff
changeset
|
360 ; //TODO: emit an warning. |
136df1708856
Better iq callbacks. (breaks everything)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
42
diff
changeset
|
361 else |
136df1708856
Better iq callbacks. (breaks everything)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
42
diff
changeset
|
362 this.callback[id] = callback; |
136df1708856
Better iq callbacks. (breaks everything)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
42
diff
changeset
|
363 |
136df1708856
Better iq callbacks. (breaks everything)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
42
diff
changeset
|
364 } else if (aResult || aError) |
42
ee874d064650
Check iq sending.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
41
diff
changeset
|
365 ; //TODO: callback can’t be called with non-iq stanza. |
17
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
366 |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
367 |
29
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
28
diff
changeset
|
368 //TODO this.socket.send(stanza.XML); (need some work on Lightstring.Stanza) |
37 | 369 var fixme = Lightstring.DOM2XML(stanza.DOM); |
370 stanza.XML = fixme; | |
371 this.socket.send(fixme); | |
29
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
28
diff
changeset
|
372 this.emit('output', stanza); |
17
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
373 }, |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
374 /** |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
375 * @function Closes the XMPP stream and the socket. |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
376 */ |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
377 disconnect: function() { |
18 | 378 this.emit('disconnecting'); |
17
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
379 var stream = Lightstring.stanza.stream.close(); |
18 | 380 this.send(stream); |
17
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
381 this.emit('XMLOutput', stream); |
18 | 382 this.socket.close(); |
383 }, | |
17
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
384 /** |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
385 * @function Emits an event. |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
386 * @param {String} aName The event name. |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
387 * @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
|
388 */ |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
389 emit: function(aName, aData) { |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
390 var handlers = this.handlers[aName]; |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
391 if (!handlers) |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
392 return; |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
393 |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
394 for (var i = 0; i < handlers.length; i++) |
41
2895891f19bb
s/that/this/ in handlers.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
40
diff
changeset
|
395 handlers[i].call(this, aData); |
17
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
396 }, |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
397 /** |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
398 * @function Register an event handler. |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
399 * @param {String} aName The event name. |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
400 * @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
|
401 */ |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
402 on: function(aName, callback) { |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
403 if (!this.handlers[aName]) |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
404 this.handlers[aName] = []; |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
405 this.handlers[aName].push(callback); |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
406 } |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
407 }; |