Mercurial > eldonilo > lightstring
annotate lightstring.js @ 64:d9f5ae0b6d98
Support for DIGEST-MD5 authentication. (plugin)
author | Sonny Piers <sonny.piers@gmail.com> |
---|---|
date | Wed, 01 Feb 2012 19:47:49 +0100 |
parents | 20da4fb67977 |
children | 2e8fbf3bce7f |
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 } |
62
b1e75cdbb0ad
Don’t allow more than one iq handler to respond, and respond with an service-unavailable when not handled.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
60
diff
changeset
|
47 }, |
b1e75cdbb0ad
Don’t allow more than one iq handler to respond, and respond with an service-unavailable when not handled.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
60
diff
changeset
|
48 errors: { |
b1e75cdbb0ad
Don’t allow more than one iq handler to respond, and respond with an service-unavailable when not handled.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
60
diff
changeset
|
49 iq: function(type, error) { |
b1e75cdbb0ad
Don’t allow more than one iq handler to respond, and respond with an service-unavailable when not handled.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
60
diff
changeset
|
50 return "<iq to='" + stanza.DOM.getAttributeNS(null, 'from') + "'" + |
b1e75cdbb0ad
Don’t allow more than one iq handler to respond, and respond with an service-unavailable when not handled.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
60
diff
changeset
|
51 " id='" + stanza.DOM.getAttributeNS(null, 'id') + "'" + |
b1e75cdbb0ad
Don’t allow more than one iq handler to respond, and respond with an service-unavailable when not handled.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
60
diff
changeset
|
52 " type='error'>" + |
b1e75cdbb0ad
Don’t allow more than one iq handler to respond, and respond with an service-unavailable when not handled.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
60
diff
changeset
|
53 "<error type='" + type + "'>" + |
b1e75cdbb0ad
Don’t allow more than one iq handler to respond, and respond with an service-unavailable when not handled.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
60
diff
changeset
|
54 "<" + error + " xmlns='" + Lightstring.namespaces['xmpp_stanzas'] + "'/>" + //TODO: allow text content. |
b1e75cdbb0ad
Don’t allow more than one iq handler to respond, and respond with an service-unavailable when not handled.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
60
diff
changeset
|
55 //TODO: allow text and payload. |
b1e75cdbb0ad
Don’t allow more than one iq handler to respond, and respond with an service-unavailable when not handled.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
60
diff
changeset
|
56 "</error>" + |
b1e75cdbb0ad
Don’t allow more than one iq handler to respond, and respond with an service-unavailable when not handled.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
60
diff
changeset
|
57 "</iq>"; |
b1e75cdbb0ad
Don’t allow more than one iq handler to respond, and respond with an service-unavailable when not handled.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
60
diff
changeset
|
58 } |
13
9aeb0750b9d1
fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents:
12
diff
changeset
|
59 } |
9aeb0750b9d1
fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents:
12
diff
changeset
|
60 }, |
44
3dfb596cf669
Add a plugin loader.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
43
diff
changeset
|
61 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
|
62 /** |
9fbd0e3678b5
add comments, jsdoc syntax + move the parser and the serializer to the Lightstring namespace so they don't get recreated at every new Lightstring.Connection
Sonny Piers <sonny.piers@gmail.com>
parents:
9
diff
changeset
|
63 * @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
|
64 */ |
9fbd0e3678b5
add comments, jsdoc syntax + move the parser and the serializer to the Lightstring namespace so they don't get recreated at every new Lightstring.Connection
Sonny Piers <sonny.piers@gmail.com>
parents:
9
diff
changeset
|
65 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
|
66 /** |
9fbd0e3678b5
add comments, jsdoc syntax + move the parser and the serializer to the Lightstring namespace so they don't get recreated at every new Lightstring.Connection
Sonny Piers <sonny.piers@gmail.com>
parents:
9
diff
changeset
|
67 * @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
|
68 */ |
9fbd0e3678b5
add comments, jsdoc syntax + move the parser and the serializer to the Lightstring namespace so they don't get recreated at every new Lightstring.Connection
Sonny Piers <sonny.piers@gmail.com>
parents:
9
diff
changeset
|
69 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
|
70 /** |
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
|
71 * @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
|
72 * @param {String} aString XML string. |
9aeb0750b9d1
fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents:
12
diff
changeset
|
73 * @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
|
74 */ |
29
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
28
diff
changeset
|
75 XML2DOM: function(aString) { |
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
28
diff
changeset
|
76 var DOM = null; |
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
28
diff
changeset
|
77 try { |
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
28
diff
changeset
|
78 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
|
79 } |
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
28
diff
changeset
|
80 catch (e) { |
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
28
diff
changeset
|
81 alert(e); |
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 finally { |
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
28
diff
changeset
|
84 return DOM; |
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
28
diff
changeset
|
85 }; |
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
|
86 }, |
9fbd0e3678b5
add comments, jsdoc syntax + move the parser and the serializer to the Lightstring namespace so they don't get recreated at every new Lightstring.Connection
Sonny Piers <sonny.piers@gmail.com>
parents:
9
diff
changeset
|
87 /** |
9fbd0e3678b5
add comments, jsdoc syntax + move the parser and the serializer to the Lightstring namespace so they don't get recreated at every new Lightstring.Connection
Sonny Piers <sonny.piers@gmail.com>
parents:
9
diff
changeset
|
88 * @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
|
89 * @param {Object} aString DOM object. |
9aeb0750b9d1
fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents:
12
diff
changeset
|
90 * @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
|
91 */ |
29
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
28
diff
changeset
|
92 DOM2XML: function(aElement) { |
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
28
diff
changeset
|
93 var XML = null; |
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
28
diff
changeset
|
94 try { |
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
28
diff
changeset
|
95 XML = this.serializer.serializeToString(aElement); |
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
28
diff
changeset
|
96 } |
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
28
diff
changeset
|
97 catch (e) { |
57 | 98 //TODO throw error |
29
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
28
diff
changeset
|
99 } |
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
28
diff
changeset
|
100 finally { |
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
28
diff
changeset
|
101 return XML; |
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
28
diff
changeset
|
102 }; |
33
88d24231bf24
Better newId function.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
28
diff
changeset
|
103 }, |
88d24231bf24
Better newId function.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
28
diff
changeset
|
104 /** |
88d24231bf24
Better newId function.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
28
diff
changeset
|
105 * @function Get an unique identifier. |
88d24231bf24
Better newId function.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
28
diff
changeset
|
106 * @param {String} [aString] Prefix to put before the identifier. |
88d24231bf24
Better newId function.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
28
diff
changeset
|
107 * @return {String} Identifier. |
88d24231bf24
Better newId function.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
28
diff
changeset
|
108 */ |
88d24231bf24
Better newId function.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
28
diff
changeset
|
109 newId: (function() { |
88d24231bf24
Better newId function.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
28
diff
changeset
|
110 var id = 1024; |
88d24231bf24
Better newId function.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
28
diff
changeset
|
111 return function(prefix) { |
88d24231bf24
Better newId function.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
28
diff
changeset
|
112 if (typeof prefix === 'string') |
88d24231bf24
Better newId function.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
28
diff
changeset
|
113 return prefix + id++; |
88d24231bf24
Better newId function.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
28
diff
changeset
|
114 return '' + id++; |
88d24231bf24
Better newId function.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
28
diff
changeset
|
115 }; |
34 | 116 })() |
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
|
117 }; |
96087680669f
Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
118 |
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
|
119 /** |
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
|
120 * @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
|
121 * @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
|
122 * @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
|
123 */ |
13
9aeb0750b9d1
fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents:
12
diff
changeset
|
124 Lightstring.Connection = function(aService) { |
9aeb0750b9d1
fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents:
12
diff
changeset
|
125 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
|
126 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
|
127 this.handlers = {}; |
59 | 128 this.callbacks = {}; |
1
96087680669f
Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
129 }; |
17
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
130 Lightstring.Connection.prototype = { |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
131 /** |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
132 * @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
|
133 * @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
|
134 * @param {String} [aPassword] The associated password. |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
135 */ |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
136 connect: function(aJid, aPassword) { |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
137 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
|
138 this.jid = new Lightstring.JID(aJid); |
17
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
139 if (aPassword) |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
140 this.password = aPassword; |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
141 |
22
6a6bb8ded046
Add a JID object, and use it in Lightstring.Connection.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
21
diff
changeset
|
142 if (!this.jid.bare) |
17
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
143 throw 'Lightstring: Connection.jid is undefined.'; |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
144 if (!this.password) |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
145 throw 'Lightstring: Connection.password is undefined.'; |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
146 if (!this.service) |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
147 throw 'Lightstring: Connection.service is undefined.'; |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
148 |
40 | 149 if(typeof(WebSocket) === "function") { |
150 this.socket = new WebSocket(this.service, 'xmpp'); | |
151 } | |
152 else if(typeof(MozWebSocket) === "function") { | |
39
5e32481b8a92
Better WebSocket feature discovering and Opera support
Sonny Piers <sonny.piers@gmail.com>
parents:
37
diff
changeset
|
153 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
|
154 } |
5e32481b8a92
Better WebSocket feature discovering and Opera support
Sonny Piers <sonny.piers@gmail.com>
parents:
37
diff
changeset
|
155 else { |
5e32481b8a92
Better WebSocket feature discovering and Opera support
Sonny Piers <sonny.piers@gmail.com>
parents:
37
diff
changeset
|
156 throw new Error('WebSocket not available.'); |
17
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
157 } |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
158 |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
159 var that = this; |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
160 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
|
161 //TODO: if (this.protocol !== 'xmpp') |
17
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
162 |
44
3dfb596cf669
Add a plugin loader.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
43
diff
changeset
|
163 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
|
164 //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
|
165 that.socket.send(stream); |
29
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
28
diff
changeset
|
166 var stanza = { |
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
28
diff
changeset
|
167 XML: stream |
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
28
diff
changeset
|
168 }; |
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
28
diff
changeset
|
169 that.emit('output', stanza); |
17
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
170 }); |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
171 this.socket.addEventListener('error', function(e) { |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
172 that.emit('error', e.data); |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
173 console.log(e.data); |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
174 }); |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
175 this.socket.addEventListener('close', function(e) { |
18 | 176 that.emit('disconnected', e.data); |
17
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
177 }); |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
178 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
|
179 var stanza = new Lightstring.Stanza(e.data); |
17
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
180 |
29
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
28
diff
changeset
|
181 //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
|
182 that.emit('input', stanza); |
63
20da4fb67977
Auth PLAIN as plugin. Several fixes.
Sonny Piers <sonny.piers@gmail.com>
parents:
62
diff
changeset
|
183 |
29
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
28
diff
changeset
|
184 if(!stanza.DOM) |
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
28
diff
changeset
|
185 return; |
57 | 186 |
63
20da4fb67977
Auth PLAIN as plugin. Several fixes.
Sonny Piers <sonny.piers@gmail.com>
parents:
62
diff
changeset
|
187 |
20da4fb67977
Auth PLAIN as plugin. Several fixes.
Sonny Piers <sonny.piers@gmail.com>
parents:
62
diff
changeset
|
188 var name = stanza.DOM.localName; |
20da4fb67977
Auth PLAIN as plugin. Several fixes.
Sonny Piers <sonny.piers@gmail.com>
parents:
62
diff
changeset
|
189 if (name === 'features') { |
20da4fb67977
Auth PLAIN as plugin. Several fixes.
Sonny Piers <sonny.piers@gmail.com>
parents:
62
diff
changeset
|
190 //SASL mechanisms |
20da4fb67977
Auth PLAIN as plugin. Several fixes.
Sonny Piers <sonny.piers@gmail.com>
parents:
62
diff
changeset
|
191 if (stanza.DOM.firstChild.localName === 'mechanisms') { |
20da4fb67977
Auth PLAIN as plugin. Several fixes.
Sonny Piers <sonny.piers@gmail.com>
parents:
62
diff
changeset
|
192 stanza.mechanisms = []; |
20da4fb67977
Auth PLAIN as plugin. Several fixes.
Sonny Piers <sonny.piers@gmail.com>
parents:
62
diff
changeset
|
193 var nodes = stanza.DOM.querySelectorAll('mechanism'); |
20da4fb67977
Auth PLAIN as plugin. Several fixes.
Sonny Piers <sonny.piers@gmail.com>
parents:
62
diff
changeset
|
194 for (var i = 0; i < nodes.length; i++) |
20da4fb67977
Auth PLAIN as plugin. Several fixes.
Sonny Piers <sonny.piers@gmail.com>
parents:
62
diff
changeset
|
195 stanza.mechanisms.push(nodes[i].textContent); |
20da4fb67977
Auth PLAIN as plugin. Several fixes.
Sonny Piers <sonny.piers@gmail.com>
parents:
62
diff
changeset
|
196 that.emit('mechanisms', stanza); |
20da4fb67977
Auth PLAIN as plugin. Several fixes.
Sonny Piers <sonny.piers@gmail.com>
parents:
62
diff
changeset
|
197 } |
20da4fb67977
Auth PLAIN as plugin. Several fixes.
Sonny Piers <sonny.piers@gmail.com>
parents:
62
diff
changeset
|
198 //XMPP features |
20da4fb67977
Auth PLAIN as plugin. Several fixes.
Sonny Piers <sonny.piers@gmail.com>
parents:
62
diff
changeset
|
199 else if (stanza.DOM.firstChild.localName === 'c') { |
20da4fb67977
Auth PLAIN as plugin. Several fixes.
Sonny Piers <sonny.piers@gmail.com>
parents:
62
diff
changeset
|
200 //TODO: stanza.features |
20da4fb67977
Auth PLAIN as plugin. Several fixes.
Sonny Piers <sonny.piers@gmail.com>
parents:
62
diff
changeset
|
201 that.emit('features', stanza); |
20da4fb67977
Auth PLAIN as plugin. Several fixes.
Sonny Piers <sonny.piers@gmail.com>
parents:
62
diff
changeset
|
202 } |
20da4fb67977
Auth PLAIN as plugin. Several fixes.
Sonny Piers <sonny.piers@gmail.com>
parents:
62
diff
changeset
|
203 } |
20da4fb67977
Auth PLAIN as plugin. Several fixes.
Sonny Piers <sonny.piers@gmail.com>
parents:
62
diff
changeset
|
204 else if (name === 'challenge') { |
64
d9f5ae0b6d98
Support for DIGEST-MD5 authentication. (plugin)
Sonny Piers <sonny.piers@gmail.com>
parents:
63
diff
changeset
|
205 that.emit('challenge', stanza); |
63
20da4fb67977
Auth PLAIN as plugin. Several fixes.
Sonny Piers <sonny.piers@gmail.com>
parents:
62
diff
changeset
|
206 } |
20da4fb67977
Auth PLAIN as plugin. Several fixes.
Sonny Piers <sonny.piers@gmail.com>
parents:
62
diff
changeset
|
207 else if (name === 'response') { |
20da4fb67977
Auth PLAIN as plugin. Several fixes.
Sonny Piers <sonny.piers@gmail.com>
parents:
62
diff
changeset
|
208 |
20da4fb67977
Auth PLAIN as plugin. Several fixes.
Sonny Piers <sonny.piers@gmail.com>
parents:
62
diff
changeset
|
209 |
20da4fb67977
Auth PLAIN as plugin. Several fixes.
Sonny Piers <sonny.piers@gmail.com>
parents:
62
diff
changeset
|
210 } |
64
d9f5ae0b6d98
Support for DIGEST-MD5 authentication. (plugin)
Sonny Piers <sonny.piers@gmail.com>
parents:
63
diff
changeset
|
211 else if (name === 'failure') { |
d9f5ae0b6d98
Support for DIGEST-MD5 authentication. (plugin)
Sonny Piers <sonny.piers@gmail.com>
parents:
63
diff
changeset
|
212 that.emit('failure', stanza); |
d9f5ae0b6d98
Support for DIGEST-MD5 authentication. (plugin)
Sonny Piers <sonny.piers@gmail.com>
parents:
63
diff
changeset
|
213 } |
63
20da4fb67977
Auth PLAIN as plugin. Several fixes.
Sonny Piers <sonny.piers@gmail.com>
parents:
62
diff
changeset
|
214 else if (name === 'success') { |
20da4fb67977
Auth PLAIN as plugin. Several fixes.
Sonny Piers <sonny.piers@gmail.com>
parents:
62
diff
changeset
|
215 that.emit('success', stanza); |
20da4fb67977
Auth PLAIN as plugin. Several fixes.
Sonny Piers <sonny.piers@gmail.com>
parents:
62
diff
changeset
|
216 } |
20da4fb67977
Auth PLAIN as plugin. Several fixes.
Sonny Piers <sonny.piers@gmail.com>
parents:
62
diff
changeset
|
217 else if(name === 'stream') { |
20da4fb67977
Auth PLAIN as plugin. Several fixes.
Sonny Piers <sonny.piers@gmail.com>
parents:
62
diff
changeset
|
218 |
20da4fb67977
Auth PLAIN as plugin. Several fixes.
Sonny Piers <sonny.piers@gmail.com>
parents:
62
diff
changeset
|
219 |
20da4fb67977
Auth PLAIN as plugin. Several fixes.
Sonny Piers <sonny.piers@gmail.com>
parents:
62
diff
changeset
|
220 } |
20da4fb67977
Auth PLAIN as plugin. Several fixes.
Sonny Piers <sonny.piers@gmail.com>
parents:
62
diff
changeset
|
221 |
20da4fb67977
Auth PLAIN as plugin. Several fixes.
Sonny Piers <sonny.piers@gmail.com>
parents:
62
diff
changeset
|
222 |
20da4fb67977
Auth PLAIN as plugin. Several fixes.
Sonny Piers <sonny.piers@gmail.com>
parents:
62
diff
changeset
|
223 |
20da4fb67977
Auth PLAIN as plugin. Several fixes.
Sonny Piers <sonny.piers@gmail.com>
parents:
62
diff
changeset
|
224 //Iq callbacks |
20da4fb67977
Auth PLAIN as plugin. Several fixes.
Sonny Piers <sonny.piers@gmail.com>
parents:
62
diff
changeset
|
225 else if (name === 'iq') { |
29
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
28
diff
changeset
|
226 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
|
227 if (payload) |
29
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
28
diff
changeset
|
228 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
|
229 |
136df1708856
Better iq callbacks. (breaks everything)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
42
diff
changeset
|
230 var id = stanza.DOM.getAttributeNS(null, 'id'); |
136df1708856
Better iq callbacks. (breaks everything)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
42
diff
changeset
|
231 if (!(id && id in that.callbacks)) |
136df1708856
Better iq callbacks. (breaks everything)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
42
diff
changeset
|
232 return; |
136df1708856
Better iq callbacks. (breaks everything)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
42
diff
changeset
|
233 |
136df1708856
Better iq callbacks. (breaks everything)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
42
diff
changeset
|
234 var type = stanza.DOM.getAttributeNS(null, 'type'); |
59 | 235 if (type !== 'result' && type !== 'error') |
43
136df1708856
Better iq callbacks. (breaks everything)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
42
diff
changeset
|
236 return; //TODO: emit an warning, perhaps. |
136df1708856
Better iq callbacks. (breaks everything)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
42
diff
changeset
|
237 |
136df1708856
Better iq callbacks. (breaks everything)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
42
diff
changeset
|
238 var callback = that.callbacks[id]; |
59 | 239 if (type === 'result' && callback.success) |
240 callback.success(stanza); | |
43
136df1708856
Better iq callbacks. (breaks everything)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
42
diff
changeset
|
241 else if (type === 'error' && callback.error) |
136df1708856
Better iq callbacks. (breaks everything)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
42
diff
changeset
|
242 callback.error(stanza); |
136df1708856
Better iq callbacks. (breaks everything)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
42
diff
changeset
|
243 |
136df1708856
Better iq callbacks. (breaks everything)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
42
diff
changeset
|
244 delete that.callbacks[id]; |
23
99bf2bdcfd96
Emit an iq/namespace:tag event for each iq containing a payload.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
22
diff
changeset
|
245 } |
17
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
246 }); |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
247 }, |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
248 /** |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
249 * @function Send a message. |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
250 * @param {String|Object} aStanza The message to send. |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
251 * @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
|
252 */ |
59 | 253 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
|
254 if (!(aStanza instanceof Lightstring.Stanza)) |
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
28
diff
changeset
|
255 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
|
256 else |
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
28
diff
changeset
|
257 var stanza = aStanza; |
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
28
diff
changeset
|
258 |
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
28
diff
changeset
|
259 if(!stanza) |
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
28
diff
changeset
|
260 return; |
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
28
diff
changeset
|
261 |
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
28
diff
changeset
|
262 if (stanza.DOM.tagName === 'iq') { |
42
ee874d064650
Check iq sending.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
41
diff
changeset
|
263 var type = stanza.DOM.getAttributeNS(null, 'type'); |
ee874d064650
Check iq sending.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
41
diff
changeset
|
264 if (type !== 'get' || type !== 'set') |
ee874d064650
Check iq sending.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
41
diff
changeset
|
265 ; //TODO: emit an error. |
ee874d064650
Check iq sending.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
41
diff
changeset
|
266 |
59 | 267 var callback = {success: aSuccess, error: aError}; |
43
136df1708856
Better iq callbacks. (breaks everything)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
42
diff
changeset
|
268 |
42
ee874d064650
Check iq sending.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
41
diff
changeset
|
269 var id = stanza.DOM.getAttributeNS(null, 'id'); |
43
136df1708856
Better iq callbacks. (breaks everything)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
42
diff
changeset
|
270 if (!id) |
136df1708856
Better iq callbacks. (breaks everything)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
42
diff
changeset
|
271 ; //TODO: emit an warning. |
136df1708856
Better iq callbacks. (breaks everything)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
42
diff
changeset
|
272 else |
59 | 273 this.callbacks[id] = callback; |
43
136df1708856
Better iq callbacks. (breaks everything)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
42
diff
changeset
|
274 |
59 | 275 } else if (aSuccess || aError) |
42
ee874d064650
Check iq sending.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
41
diff
changeset
|
276 ; //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
|
277 |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
278 |
29
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
28
diff
changeset
|
279 //TODO this.socket.send(stanza.XML); (need some work on Lightstring.Stanza) |
37 | 280 var fixme = Lightstring.DOM2XML(stanza.DOM); |
281 stanza.XML = fixme; | |
282 this.socket.send(fixme); | |
29
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
28
diff
changeset
|
283 this.emit('output', stanza); |
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 * @function Closes the XMPP stream and the socket. |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
287 */ |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
288 disconnect: function() { |
18 | 289 this.emit('disconnecting'); |
44
3dfb596cf669
Add a plugin loader.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
43
diff
changeset
|
290 var stream = Lightstring.stanzas.stream.close(); |
18 | 291 this.send(stream); |
17
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
292 this.emit('XMLOutput', stream); |
18 | 293 this.socket.close(); |
294 }, | |
44
3dfb596cf669
Add a plugin loader.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
43
diff
changeset
|
295 load: function() { |
3dfb596cf669
Add a plugin loader.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
43
diff
changeset
|
296 for (var i = 0; i < arguments.length; i++) { |
3dfb596cf669
Add a plugin loader.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
43
diff
changeset
|
297 var name = arguments[i]; |
3dfb596cf669
Add a plugin loader.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
43
diff
changeset
|
298 if (!(name in Lightstring.plugins)) |
3dfb596cf669
Add a plugin loader.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
43
diff
changeset
|
299 continue; //TODO: throw an error? |
3dfb596cf669
Add a plugin loader.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
43
diff
changeset
|
300 |
3dfb596cf669
Add a plugin loader.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
43
diff
changeset
|
301 var plugin = Lightstring.plugins[name]; |
3dfb596cf669
Add a plugin loader.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
43
diff
changeset
|
302 |
56
1d4b844586f6
Bind methods so Lightstring.Connection.plugin.method works with the correct context.
Sonny Piers <sonny.piers@gmail.com>
parents:
55
diff
changeset
|
303 //Namespaces |
44
3dfb596cf669
Add a plugin loader.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
43
diff
changeset
|
304 for (var ns in plugin.namespaces) |
3dfb596cf669
Add a plugin loader.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
43
diff
changeset
|
305 Lightstring.ns[ns] = plugin.namespaces[ns]; |
3dfb596cf669
Add a plugin loader.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
43
diff
changeset
|
306 |
56
1d4b844586f6
Bind methods so Lightstring.Connection.plugin.method works with the correct context.
Sonny Piers <sonny.piers@gmail.com>
parents:
55
diff
changeset
|
307 //Stanzas |
54
0b7dd59e264a
Fix Connection.load and add the init method to plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
51
diff
changeset
|
308 Lightstring.stanzas[name] = {}; |
44
3dfb596cf669
Add a plugin loader.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
43
diff
changeset
|
309 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
|
310 Lightstring.stanzas[name][stanza] = plugin.stanzas[stanza]; |
44
3dfb596cf669
Add a plugin loader.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
43
diff
changeset
|
311 |
56
1d4b844586f6
Bind methods so Lightstring.Connection.plugin.method works with the correct context.
Sonny Piers <sonny.piers@gmail.com>
parents:
55
diff
changeset
|
312 //Handlers |
44
3dfb596cf669
Add a plugin loader.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
43
diff
changeset
|
313 for (var handler in plugin.handlers) |
3dfb596cf669
Add a plugin loader.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
43
diff
changeset
|
314 this.on(handler, plugin.handlers[handler]); |
3dfb596cf669
Add a plugin loader.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
43
diff
changeset
|
315 |
56
1d4b844586f6
Bind methods so Lightstring.Connection.plugin.method works with the correct context.
Sonny Piers <sonny.piers@gmail.com>
parents:
55
diff
changeset
|
316 //Methods |
57 | 317 this[name] = {}; |
63
20da4fb67977
Auth PLAIN as plugin. Several fixes.
Sonny Piers <sonny.piers@gmail.com>
parents:
62
diff
changeset
|
318 for (var method in plugin.methods) |
56
1d4b844586f6
Bind methods so Lightstring.Connection.plugin.method works with the correct context.
Sonny Piers <sonny.piers@gmail.com>
parents:
55
diff
changeset
|
319 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
|
320 |
0b7dd59e264a
Fix Connection.load and add the init method to plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
51
diff
changeset
|
321 if (plugin.init) |
0b7dd59e264a
Fix Connection.load and add the init method to plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
51
diff
changeset
|
322 plugin.init(); |
44
3dfb596cf669
Add a plugin loader.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
43
diff
changeset
|
323 } |
3dfb596cf669
Add a plugin loader.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
43
diff
changeset
|
324 }, |
17
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
325 /** |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
326 * @function Emits an event. |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
327 * @param {String} aName The event name. |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
328 * @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
|
329 */ |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
330 emit: function(aName, aData) { |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
331 var handlers = this.handlers[aName]; |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
332 if (!handlers) |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
333 return; |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
334 |
63
20da4fb67977
Auth PLAIN as plugin. Several fixes.
Sonny Piers <sonny.piers@gmail.com>
parents:
62
diff
changeset
|
335 if (aData && aData.DOM && aData.DOM.localName !== 'iq') { |
62
b1e75cdbb0ad
Don’t allow more than one iq handler to respond, and respond with an service-unavailable when not handled.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
60
diff
changeset
|
336 for (var i = 0; i < handlers.length; i++) |
b1e75cdbb0ad
Don’t allow more than one iq handler to respond, and respond with an service-unavailable when not handled.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
60
diff
changeset
|
337 handlers[i].call(this, aData); |
b1e75cdbb0ad
Don’t allow more than one iq handler to respond, and respond with an service-unavailable when not handled.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
60
diff
changeset
|
338 |
b1e75cdbb0ad
Don’t allow more than one iq handler to respond, and respond with an service-unavailable when not handled.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
60
diff
changeset
|
339 return; |
b1e75cdbb0ad
Don’t allow more than one iq handler to respond, and respond with an service-unavailable when not handled.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
60
diff
changeset
|
340 } |
b1e75cdbb0ad
Don’t allow more than one iq handler to respond, and respond with an service-unavailable when not handled.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
60
diff
changeset
|
341 |
b1e75cdbb0ad
Don’t allow more than one iq handler to respond, and respond with an service-unavailable when not handled.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
60
diff
changeset
|
342 var ret; |
b1e75cdbb0ad
Don’t allow more than one iq handler to respond, and respond with an service-unavailable when not handled.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
60
diff
changeset
|
343 for (var i = 0; i < handlers.length; i++) { |
b1e75cdbb0ad
Don’t allow more than one iq handler to respond, and respond with an service-unavailable when not handled.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
60
diff
changeset
|
344 ret = handlers[i].call(this, aData); |
b1e75cdbb0ad
Don’t allow more than one iq handler to respond, and respond with an service-unavailable when not handled.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
60
diff
changeset
|
345 if (typeof ret !== 'boolean') |
b1e75cdbb0ad
Don’t allow more than one iq handler to respond, and respond with an service-unavailable when not handled.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
60
diff
changeset
|
346 return; //TODO: emit a big error! |
b1e75cdbb0ad
Don’t allow more than one iq handler to respond, and respond with an service-unavailable when not handled.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
60
diff
changeset
|
347 |
b1e75cdbb0ad
Don’t allow more than one iq handler to respond, and respond with an service-unavailable when not handled.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
60
diff
changeset
|
348 if (ret) |
b1e75cdbb0ad
Don’t allow more than one iq handler to respond, and respond with an service-unavailable when not handled.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
60
diff
changeset
|
349 return; |
b1e75cdbb0ad
Don’t allow more than one iq handler to respond, and respond with an service-unavailable when not handled.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
60
diff
changeset
|
350 } |
b1e75cdbb0ad
Don’t allow more than one iq handler to respond, and respond with an service-unavailable when not handled.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
60
diff
changeset
|
351 |
b1e75cdbb0ad
Don’t allow more than one iq handler to respond, and respond with an service-unavailable when not handled.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
60
diff
changeset
|
352 conn.send(Lightstring.stanzas.errors.iq('cancel', 'service-unavailable')); |
17
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
353 }, |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
354 /** |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
355 * @function Register an event handler. |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
356 * @param {String} aName The event name. |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
357 * @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
|
358 */ |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
359 on: function(aName, callback) { |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
360 if (!this.handlers[aName]) |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
361 this.handlers[aName] = []; |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
362 this.handlers[aName].push(callback); |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
363 } |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
364 }; |