Mercurial > eldonilo > lightstring
annotate lightstring.js @ 61:d1ba6f0e2a92
Add PubSub events emitter.
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Wed, 01 Feb 2012 18:09:28 +0100 |
parents | 0e86fca6a596 |
children | b1e75cdbb0ad |
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. |
50
770bb8460df8
Some improvements on namespaces.
Sonny Piers <sonny.piers@gmail.com>
parents:
44
diff
changeset
|
26 * @description http://xmpp.org/xmpp-protocols/protocol-namespaces |
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
|
27 */ |
44
3dfb596cf669
Add a plugin loader.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
43
diff
changeset
|
28 ns: { |
50
770bb8460df8
Some improvements on namespaces.
Sonny Piers <sonny.piers@gmail.com>
parents:
44
diff
changeset
|
29 streams: 'http://etherx.jabber.org/streams', |
770bb8460df8
Some improvements on namespaces.
Sonny Piers <sonny.piers@gmail.com>
parents:
44
diff
changeset
|
30 jabber_client: 'jabber:client' |
13
9aeb0750b9d1
fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents:
12
diff
changeset
|
31 }, |
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
|
32 /** |
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 * @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
|
34 */ |
44
3dfb596cf669
Add a plugin loader.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
43
diff
changeset
|
35 stanzas: { |
13
9aeb0750b9d1
fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents:
12
diff
changeset
|
36 stream: { |
9aeb0750b9d1
fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents:
12
diff
changeset
|
37 open: function(aService) { |
9aeb0750b9d1
fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents:
12
diff
changeset
|
38 //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
|
39 return "<stream:stream to='" + aService + "'" + |
50
770bb8460df8
Some improvements on namespaces.
Sonny Piers <sonny.piers@gmail.com>
parents:
44
diff
changeset
|
40 " xmlns='" + Lightstring.ns['jabber_client'] + "'" + |
55
aaad98b33f86
Fix syntax errors.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
54
diff
changeset
|
41 " xmlns:stream='" + Lightstring.ns['streams'] + "'" + |
21
b7d52bf259e0
Coding style: always use "" for XML strings.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
18
diff
changeset
|
42 " version='1.0'/>"; |
13
9aeb0750b9d1
fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents:
12
diff
changeset
|
43 }, |
9aeb0750b9d1
fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents:
12
diff
changeset
|
44 close: function() { |
21
b7d52bf259e0
Coding style: always use "" for XML strings.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
18
diff
changeset
|
45 return "</stream:stream>"; |
13
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 } |
9aeb0750b9d1
fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents:
12
diff
changeset
|
48 }, |
44
3dfb596cf669
Add a plugin loader.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
43
diff
changeset
|
49 plugins: {}, |
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
|
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 * @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
|
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 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
|
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 * @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
|
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 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
|
58 /** |
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
|
59 * @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
|
60 * @param {String} aString XML string. |
9aeb0750b9d1
fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents:
12
diff
changeset
|
61 * @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
|
62 */ |
29
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
28
diff
changeset
|
63 XML2DOM: function(aString) { |
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
28
diff
changeset
|
64 var DOM = null; |
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
28
diff
changeset
|
65 try { |
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
28
diff
changeset
|
66 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
|
67 } |
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
28
diff
changeset
|
68 catch (e) { |
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
28
diff
changeset
|
69 alert(e); |
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
28
diff
changeset
|
70 } |
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
28
diff
changeset
|
71 finally { |
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
28
diff
changeset
|
72 return DOM; |
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
28
diff
changeset
|
73 }; |
12
9fbd0e3678b5
add comments, jsdoc syntax + move the parser and the serializer to the Lightstring namespace so they don't get recreated at every new Lightstring.Connection
Sonny Piers <sonny.piers@gmail.com>
parents:
9
diff
changeset
|
74 }, |
9fbd0e3678b5
add comments, jsdoc syntax + move the parser and the serializer to the Lightstring namespace so they don't get recreated at every new Lightstring.Connection
Sonny Piers <sonny.piers@gmail.com>
parents:
9
diff
changeset
|
75 /** |
9fbd0e3678b5
add comments, jsdoc syntax + move the parser and the serializer to the Lightstring namespace so they don't get recreated at every new Lightstring.Connection
Sonny Piers <sonny.piers@gmail.com>
parents:
9
diff
changeset
|
76 * @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
|
77 * @param {Object} aString DOM object. |
9aeb0750b9d1
fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents:
12
diff
changeset
|
78 * @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
|
79 */ |
29
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
28
diff
changeset
|
80 DOM2XML: function(aElement) { |
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
28
diff
changeset
|
81 var XML = null; |
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
28
diff
changeset
|
82 try { |
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
28
diff
changeset
|
83 XML = this.serializer.serializeToString(aElement); |
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
28
diff
changeset
|
84 } |
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
28
diff
changeset
|
85 catch (e) { |
57 | 86 //TODO throw error |
29
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
28
diff
changeset
|
87 } |
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
28
diff
changeset
|
88 finally { |
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
28
diff
changeset
|
89 return XML; |
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
28
diff
changeset
|
90 }; |
33
88d24231bf24
Better newId function.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
28
diff
changeset
|
91 }, |
88d24231bf24
Better newId function.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
28
diff
changeset
|
92 /** |
88d24231bf24
Better newId function.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
28
diff
changeset
|
93 * @function Get an unique identifier. |
88d24231bf24
Better newId function.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
28
diff
changeset
|
94 * @param {String} [aString] Prefix to put before the identifier. |
88d24231bf24
Better newId function.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
28
diff
changeset
|
95 * @return {String} Identifier. |
88d24231bf24
Better newId function.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
28
diff
changeset
|
96 */ |
88d24231bf24
Better newId function.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
28
diff
changeset
|
97 newId: (function() { |
88d24231bf24
Better newId function.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
28
diff
changeset
|
98 var id = 1024; |
88d24231bf24
Better newId function.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
28
diff
changeset
|
99 return function(prefix) { |
88d24231bf24
Better newId function.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
28
diff
changeset
|
100 if (typeof prefix === 'string') |
88d24231bf24
Better newId function.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
28
diff
changeset
|
101 return prefix + id++; |
88d24231bf24
Better newId function.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
28
diff
changeset
|
102 return '' + id++; |
88d24231bf24
Better newId function.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
28
diff
changeset
|
103 }; |
34 | 104 })() |
1
96087680669f
Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
105 }; |
96087680669f
Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
106 |
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
|
107 /** |
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 * @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
|
109 * @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
|
110 * @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
|
111 */ |
13
9aeb0750b9d1
fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents:
12
diff
changeset
|
112 Lightstring.Connection = function(aService) { |
9aeb0750b9d1
fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents:
12
diff
changeset
|
113 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
|
114 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
|
115 this.handlers = {}; |
59 | 116 this.callbacks = {}; |
41
2895891f19bb
s/that/this/ in handlers.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
40
diff
changeset
|
117 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
|
118 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
|
119 //SASL/Auth features |
13
9aeb0750b9d1
fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents:
12
diff
changeset
|
120 if (nodes.length > 0) { |
41
2895891f19bb
s/that/this/ in handlers.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
40
diff
changeset
|
121 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
|
122 var mechanisms = {}; |
13
9aeb0750b9d1
fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents:
12
diff
changeset
|
123 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
|
124 mechanisms[nodes[i].textContent] = true; |
13
9aeb0750b9d1
fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents:
12
diff
changeset
|
125 |
9aeb0750b9d1
fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents:
12
diff
changeset
|
126 |
9aeb0750b9d1
fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents:
12
diff
changeset
|
127 //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
|
128 if ('DIGEST-MD5' in mechanisms) |
41
2895891f19bb
s/that/this/ in handlers.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
40
diff
changeset
|
129 this.send( |
21
b7d52bf259e0
Coding style: always use "" for XML strings.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
18
diff
changeset
|
130 "<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
|
131 " mechanism='DIGEST-MD5'/>" |
2 | 132 ); |
13
9aeb0750b9d1
fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents:
12
diff
changeset
|
133 else if ('PLAIN' in mechanisms) { |
2 | 134 var token = btoa( |
41
2895891f19bb
s/that/this/ in handlers.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
40
diff
changeset
|
135 this.jid + |
13
9aeb0750b9d1
fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents:
12
diff
changeset
|
136 '\u0000' + |
41
2895891f19bb
s/that/this/ in handlers.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
40
diff
changeset
|
137 this.jid.node + |
13
9aeb0750b9d1
fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents:
12
diff
changeset
|
138 '\u0000' + |
41
2895891f19bb
s/that/this/ in handlers.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
40
diff
changeset
|
139 this.password |
2 | 140 ); |
41
2895891f19bb
s/that/this/ in handlers.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
40
diff
changeset
|
141 this.send( |
21
b7d52bf259e0
Coding style: always use "" for XML strings.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
18
diff
changeset
|
142 "<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
|
143 " mechanism='PLAIN'>" + token + "</auth>" |
2 | 144 ); |
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
|
145 } |
96087680669f
Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
146 } |
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 //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
|
148 else { |
41
2895891f19bb
s/that/this/ in handlers.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
40
diff
changeset
|
149 this.emit('features', stanza); |
59 | 150 var that = this; |
1
96087680669f
Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
151 //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
|
152 var bind = |
59 | 153 "<iq type='set' id='"+Lightstring.newId('sendiq:')+"' xmlns='jabber:client'>" + |
24
119ceb121908
Bind the correct JID received.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
23
diff
changeset
|
154 "<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
|
155 (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
|
156 "</bind>" + |
119ceb121908
Bind the correct JID received.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
23
diff
changeset
|
157 "</iq>"; |
41
2895891f19bb
s/that/this/ in handlers.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
40
diff
changeset
|
158 this.send( |
24
119ceb121908
Bind the correct JID received.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
23
diff
changeset
|
159 bind, |
59 | 160 //Success |
24
119ceb121908
Bind the correct JID received.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
23
diff
changeset
|
161 function(stanza) { |
119ceb121908
Bind the correct JID received.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
23
diff
changeset
|
162 //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
|
163 this.jid = new Lightstring.JID(stanza.DOM.textContent); |
59 | 164 that.send( |
165 "<iq type='set' id='"+Lightstring.newId('sendiq:')+"' xmlns='jabber:client'>" + | |
24
119ceb121908
Bind the correct JID received.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
23
diff
changeset
|
166 "<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
|
167 "</iq>", |
119ceb121908
Bind the correct JID received.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
23
diff
changeset
|
168 function() { |
59 | 169 that.emit('connected'); |
24
119ceb121908
Bind the correct JID received.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
23
diff
changeset
|
170 } |
119ceb121908
Bind the correct JID received.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
23
diff
changeset
|
171 ); |
59 | 172 }, |
173 //Error | |
174 function(stanza) { | |
175 //TODO: Error? | |
24
119ceb121908
Bind the correct JID received.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
23
diff
changeset
|
176 } |
119ceb121908
Bind the correct JID received.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
23
diff
changeset
|
177 ); |
1
96087680669f
Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
178 } |
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('success', function(stanza) { |
2895891f19bb
s/that/this/ in handlers.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
40
diff
changeset
|
181 this.send( |
2895891f19bb
s/that/this/ in handlers.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
40
diff
changeset
|
182 "<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
|
183 " xmlns='jabber:client'" + |
b7d52bf259e0
Coding style: always use "" for XML strings.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
18
diff
changeset
|
184 " 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
|
185 " version='1.0'/>" |
2 | 186 ); |
1
96087680669f
Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
187 }); |
41
2895891f19bb
s/that/this/ in handlers.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
40
diff
changeset
|
188 this.on('failure', function(stanza) { |
2895891f19bb
s/that/this/ in handlers.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
40
diff
changeset
|
189 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
|
190 }); |
41
2895891f19bb
s/that/this/ in handlers.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
40
diff
changeset
|
191 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
|
192 //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
|
193 |
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 function _quote(str) { |
13
9aeb0750b9d1
fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents:
12
diff
changeset
|
195 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
|
196 }; |
13
9aeb0750b9d1
fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents:
12
diff
changeset
|
197 |
29
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
28
diff
changeset
|
198 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
|
199 |
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
|
200 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
|
201 |
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 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
|
203 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
|
204 var host = null; |
13
9aeb0750b9d1
fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents:
12
diff
changeset
|
205 var nonce = ''; |
9aeb0750b9d1
fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents:
12
diff
changeset
|
206 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
|
207 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
|
208 |
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 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
|
210 matches = challenge.match(attribMatch); |
13
9aeb0750b9d1
fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents:
12
diff
changeset
|
211 challenge = challenge.replace(matches[0], ''); |
9aeb0750b9d1
fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents:
12
diff
changeset
|
212 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
|
213 switch (matches[1]) { |
13
9aeb0750b9d1
fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents:
12
diff
changeset
|
214 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
|
215 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
|
216 break; |
13
9aeb0750b9d1
fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents:
12
diff
changeset
|
217 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
|
218 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
|
219 break; |
13
9aeb0750b9d1
fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents:
12
diff
changeset
|
220 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
|
221 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
|
222 break; |
13
9aeb0750b9d1
fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents:
12
diff
changeset
|
223 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
|
224 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
|
225 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
|
226 } |
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 } |
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 |
41
2895891f19bb
s/that/this/ in handlers.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
40
diff
changeset
|
229 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
|
230 if (host !== null) |
13
9aeb0750b9d1
fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents:
12
diff
changeset
|
231 digest_uri = digest_uri + '/' + host; |
41
2895891f19bb
s/that/this/ in handlers.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
40
diff
changeset
|
232 var A1 = MD5.hash(this.jid.node + |
2895891f19bb
s/that/this/ in handlers.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
40
diff
changeset
|
233 ':' + 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
|
234 ':' + 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
|
235 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
|
236 |
13
9aeb0750b9d1
fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents:
12
diff
changeset
|
237 var responseText = ''; |
41
2895891f19bb
s/that/this/ in handlers.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
40
diff
changeset
|
238 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
|
239 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
|
240 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
|
241 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
|
242 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
|
243 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
|
244 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
|
245 responseText += 'response=' + _quote( |
13
9aeb0750b9d1
fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents:
12
diff
changeset
|
246 MD5.hexdigest(MD5.hexdigest(A1) + ':' + |
9aeb0750b9d1
fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents:
12
diff
changeset
|
247 nonce + ':00000001:' + |
9aeb0750b9d1
fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents:
12
diff
changeset
|
248 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
|
249 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
|
250 responseText += 'charset="utf-8"'; |
41
2895891f19bb
s/that/this/ in handlers.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
40
diff
changeset
|
251 this.send( |
21
b7d52bf259e0
Coding style: always use "" for XML strings.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
18
diff
changeset
|
252 "<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
|
253 btoa(responseText) + |
b7d52bf259e0
Coding style: always use "" for XML strings.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
18
diff
changeset
|
254 "</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
|
255 }); |
96087680669f
Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
256 }; |
17
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
257 Lightstring.Connection.prototype = { |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
258 /** |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
259 * @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
|
260 * @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
|
261 * @param {String} [aPassword] The associated password. |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
262 */ |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
263 connect: function(aJid, aPassword) { |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
264 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
|
265 this.jid = new Lightstring.JID(aJid); |
17
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
266 if (aPassword) |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
267 this.password = aPassword; |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
268 |
22
6a6bb8ded046
Add a JID object, and use it in Lightstring.Connection.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
21
diff
changeset
|
269 if (!this.jid.bare) |
17
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
270 throw 'Lightstring: Connection.jid is undefined.'; |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
271 if (!this.password) |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
272 throw 'Lightstring: Connection.password is undefined.'; |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
273 if (!this.service) |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
274 throw 'Lightstring: Connection.service is undefined.'; |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
275 |
40 | 276 if(typeof(WebSocket) === "function") { |
277 this.socket = new WebSocket(this.service, 'xmpp'); | |
278 } | |
279 else if(typeof(MozWebSocket) === "function") { | |
39
5e32481b8a92
Better WebSocket feature discovering and Opera support
Sonny Piers <sonny.piers@gmail.com>
parents:
37
diff
changeset
|
280 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
|
281 } |
5e32481b8a92
Better WebSocket feature discovering and Opera support
Sonny Piers <sonny.piers@gmail.com>
parents:
37
diff
changeset
|
282 else { |
5e32481b8a92
Better WebSocket feature discovering and Opera support
Sonny Piers <sonny.piers@gmail.com>
parents:
37
diff
changeset
|
283 throw new Error('WebSocket not available.'); |
17
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
284 } |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
285 |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
286 var that = this; |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
287 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
|
288 //TODO: if (this.protocol !== 'xmpp') |
17
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
289 |
44
3dfb596cf669
Add a plugin loader.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
43
diff
changeset
|
290 var stream = Lightstring.stanzas.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
|
291 //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
|
292 that.socket.send(stream); |
29
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
28
diff
changeset
|
293 var stanza = { |
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
28
diff
changeset
|
294 XML: stream |
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
28
diff
changeset
|
295 }; |
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
28
diff
changeset
|
296 that.emit('output', stanza); |
17
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 this.socket.addEventListener('error', function(e) { |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
299 that.emit('error', e.data); |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
300 console.log(e.data); |
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 this.socket.addEventListener('close', function(e) { |
18 | 303 that.emit('disconnected', e.data); |
17
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
304 }); |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
305 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
|
306 var stanza = new Lightstring.Stanza(e.data); |
17
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
307 |
29
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
28
diff
changeset
|
308 //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
|
309 that.emit('input', stanza); |
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
28
diff
changeset
|
310 |
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
28
diff
changeset
|
311 if(!stanza.DOM) |
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
28
diff
changeset
|
312 return; |
57 | 313 |
29
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
28
diff
changeset
|
314 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
|
315 |
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
28
diff
changeset
|
316 if (stanza.DOM.tagName === 'iq') { |
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
28
diff
changeset
|
317 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
|
318 if (payload) |
29
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
28
diff
changeset
|
319 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
|
320 |
136df1708856
Better iq callbacks. (breaks everything)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
42
diff
changeset
|
321 var id = stanza.DOM.getAttributeNS(null, 'id'); |
136df1708856
Better iq callbacks. (breaks everything)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
42
diff
changeset
|
322 if (!(id && id in that.callbacks)) |
136df1708856
Better iq callbacks. (breaks everything)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
42
diff
changeset
|
323 return; |
136df1708856
Better iq callbacks. (breaks everything)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
42
diff
changeset
|
324 |
136df1708856
Better iq callbacks. (breaks everything)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
42
diff
changeset
|
325 var type = stanza.DOM.getAttributeNS(null, 'type'); |
59 | 326 if (type !== 'result' && type !== 'error') |
43
136df1708856
Better iq callbacks. (breaks everything)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
42
diff
changeset
|
327 return; //TODO: emit an warning, perhaps. |
136df1708856
Better iq callbacks. (breaks everything)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
42
diff
changeset
|
328 |
136df1708856
Better iq callbacks. (breaks everything)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
42
diff
changeset
|
329 var callback = that.callbacks[id]; |
59 | 330 if (type === 'result' && callback.success) |
331 callback.success(stanza); | |
43
136df1708856
Better iq callbacks. (breaks everything)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
42
diff
changeset
|
332 else if (type === 'error' && callback.error) |
136df1708856
Better iq callbacks. (breaks everything)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
42
diff
changeset
|
333 callback.error(stanza); |
136df1708856
Better iq callbacks. (breaks everything)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
42
diff
changeset
|
334 |
136df1708856
Better iq callbacks. (breaks everything)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
42
diff
changeset
|
335 delete that.callbacks[id]; |
41
2895891f19bb
s/that/this/ in handlers.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
40
diff
changeset
|
336 |
2895891f19bb
s/that/this/ in handlers.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
40
diff
changeset
|
337 //TODO: really needed? |
2895891f19bb
s/that/this/ in handlers.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
40
diff
changeset
|
338 } else if (stanza.DOM.tagName === 'message') { |
2895891f19bb
s/that/this/ in handlers.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
40
diff
changeset
|
339 var payloads = stanza.DOM.children; |
2895891f19bb
s/that/this/ in handlers.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
40
diff
changeset
|
340 for (var i = 0; i < payloads.length; i++) |
2895891f19bb
s/that/this/ in handlers.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
40
diff
changeset
|
341 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
|
342 } |
17
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
343 }); |
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 /** |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
346 * @function Send a message. |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
347 * @param {String|Object} aStanza The message to send. |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
348 * @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
|
349 */ |
59 | 350 send: function(aStanza, aSuccess, aError) { |
29
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
28
diff
changeset
|
351 if (!(aStanza instanceof Lightstring.Stanza)) |
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
28
diff
changeset
|
352 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
|
353 else |
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
28
diff
changeset
|
354 var stanza = aStanza; |
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
28
diff
changeset
|
355 |
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
28
diff
changeset
|
356 if(!stanza) |
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
28
diff
changeset
|
357 return; |
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
28
diff
changeset
|
358 |
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
28
diff
changeset
|
359 if (stanza.DOM.tagName === 'iq') { |
42
ee874d064650
Check iq sending.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
41
diff
changeset
|
360 var type = stanza.DOM.getAttributeNS(null, 'type'); |
ee874d064650
Check iq sending.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
41
diff
changeset
|
361 if (type !== 'get' || type !== 'set') |
ee874d064650
Check iq sending.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
41
diff
changeset
|
362 ; //TODO: emit an error. |
ee874d064650
Check iq sending.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
41
diff
changeset
|
363 |
59 | 364 var callback = {success: aSuccess, error: aError}; |
43
136df1708856
Better iq callbacks. (breaks everything)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
42
diff
changeset
|
365 |
42
ee874d064650
Check iq sending.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
41
diff
changeset
|
366 var id = stanza.DOM.getAttributeNS(null, 'id'); |
43
136df1708856
Better iq callbacks. (breaks everything)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
42
diff
changeset
|
367 if (!id) |
136df1708856
Better iq callbacks. (breaks everything)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
42
diff
changeset
|
368 ; //TODO: emit an warning. |
136df1708856
Better iq callbacks. (breaks everything)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
42
diff
changeset
|
369 else |
59 | 370 this.callbacks[id] = callback; |
43
136df1708856
Better iq callbacks. (breaks everything)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
42
diff
changeset
|
371 |
59 | 372 } else if (aSuccess || aError) |
42
ee874d064650
Check iq sending.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
41
diff
changeset
|
373 ; //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
|
374 |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
375 |
29
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
28
diff
changeset
|
376 //TODO this.socket.send(stanza.XML); (need some work on Lightstring.Stanza) |
37 | 377 var fixme = Lightstring.DOM2XML(stanza.DOM); |
378 stanza.XML = fixme; | |
379 this.socket.send(fixme); | |
29
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
28
diff
changeset
|
380 this.emit('output', stanza); |
17
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
381 }, |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
382 /** |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
383 * @function Closes the XMPP stream and the socket. |
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 disconnect: function() { |
18 | 386 this.emit('disconnecting'); |
44
3dfb596cf669
Add a plugin loader.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
43
diff
changeset
|
387 var stream = Lightstring.stanzas.stream.close(); |
18 | 388 this.send(stream); |
17
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
389 this.emit('XMLOutput', stream); |
18 | 390 this.socket.close(); |
391 }, | |
44
3dfb596cf669
Add a plugin loader.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
43
diff
changeset
|
392 load: function() { |
3dfb596cf669
Add a plugin loader.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
43
diff
changeset
|
393 for (var i = 0; i < arguments.length; i++) { |
3dfb596cf669
Add a plugin loader.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
43
diff
changeset
|
394 var name = arguments[i]; |
3dfb596cf669
Add a plugin loader.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
43
diff
changeset
|
395 if (!(name in Lightstring.plugins)) |
3dfb596cf669
Add a plugin loader.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
43
diff
changeset
|
396 continue; //TODO: throw an error? |
3dfb596cf669
Add a plugin loader.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
43
diff
changeset
|
397 |
3dfb596cf669
Add a plugin loader.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
43
diff
changeset
|
398 var plugin = Lightstring.plugins[name]; |
3dfb596cf669
Add a plugin loader.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
43
diff
changeset
|
399 |
56
1d4b844586f6
Bind methods so Lightstring.Connection.plugin.method works with the correct context.
Sonny Piers <sonny.piers@gmail.com>
parents:
55
diff
changeset
|
400 //Namespaces |
44
3dfb596cf669
Add a plugin loader.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
43
diff
changeset
|
401 for (var ns in plugin.namespaces) |
3dfb596cf669
Add a plugin loader.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
43
diff
changeset
|
402 Lightstring.ns[ns] = plugin.namespaces[ns]; |
3dfb596cf669
Add a plugin loader.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
43
diff
changeset
|
403 |
56
1d4b844586f6
Bind methods so Lightstring.Connection.plugin.method works with the correct context.
Sonny Piers <sonny.piers@gmail.com>
parents:
55
diff
changeset
|
404 //Stanzas |
54
0b7dd59e264a
Fix Connection.load and add the init method to plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
51
diff
changeset
|
405 Lightstring.stanzas[name] = {}; |
44
3dfb596cf669
Add a plugin loader.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
43
diff
changeset
|
406 for (var stanza in plugin.stanzas) |
51
63c1c8bc8c19
Add plugin namespaces to Lightstring.stanzas and Lightstring.Connection methodes.
Sonny Piers <sonny.piers@gmail.com>
parents:
50
diff
changeset
|
407 Lightstring.stanzas[name][stanza] = plugin.stanzas[stanza]; |
44
3dfb596cf669
Add a plugin loader.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
43
diff
changeset
|
408 |
56
1d4b844586f6
Bind methods so Lightstring.Connection.plugin.method works with the correct context.
Sonny Piers <sonny.piers@gmail.com>
parents:
55
diff
changeset
|
409 //Handlers |
44
3dfb596cf669
Add a plugin loader.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
43
diff
changeset
|
410 for (var handler in plugin.handlers) |
3dfb596cf669
Add a plugin loader.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
43
diff
changeset
|
411 this.on(handler, plugin.handlers[handler]); |
3dfb596cf669
Add a plugin loader.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
43
diff
changeset
|
412 |
56
1d4b844586f6
Bind methods so Lightstring.Connection.plugin.method works with the correct context.
Sonny Piers <sonny.piers@gmail.com>
parents:
55
diff
changeset
|
413 //Methods |
57 | 414 this[name] = {}; |
56
1d4b844586f6
Bind methods so Lightstring.Connection.plugin.method works with the correct context.
Sonny Piers <sonny.piers@gmail.com>
parents:
55
diff
changeset
|
415 for (var method in plugins.methods) |
1d4b844586f6
Bind methods so Lightstring.Connection.plugin.method works with the correct context.
Sonny Piers <sonny.piers@gmail.com>
parents:
55
diff
changeset
|
416 this[name][method].bind(this); |
54
0b7dd59e264a
Fix Connection.load and add the init method to plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
51
diff
changeset
|
417 |
0b7dd59e264a
Fix Connection.load and add the init method to plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
51
diff
changeset
|
418 if (plugin.init) |
0b7dd59e264a
Fix Connection.load and add the init method to plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
51
diff
changeset
|
419 plugin.init(); |
44
3dfb596cf669
Add a plugin loader.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
43
diff
changeset
|
420 } |
3dfb596cf669
Add a plugin loader.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
43
diff
changeset
|
421 }, |
17
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
422 /** |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
423 * @function Emits an event. |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
424 * @param {String} aName The event name. |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
425 * @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
|
426 */ |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
427 emit: function(aName, aData) { |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
428 var handlers = this.handlers[aName]; |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
429 if (!handlers) |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
430 return; |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
431 |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
432 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
|
433 handlers[i].call(this, aData); |
17
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
434 }, |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
435 /** |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
436 * @function Register an event handler. |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
437 * @param {String} aName The event name. |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
438 * @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
|
439 */ |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
440 on: function(aName, callback) { |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
441 if (!this.handlers[aName]) |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
442 this.handlers[aName] = []; |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
443 this.handlers[aName].push(callback); |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
444 } |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
445 }; |