Mercurial > eldonilo > lightstring
annotate lightstring.js @ 100:3e124209821a
move bosh.js and websocket.js to transports/
author | Sonny Piers <sonny.piers@gmail.com> |
---|---|
date | Tue, 12 Jun 2012 19:46:52 +0200 |
parents | f14558915187 |
children | c06ec02217ee |
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 |
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
|
20 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
|
21 /** |
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 * @namespace Holds XMPP namespaces. |
50
770bb8460df8
Some improvements on namespaces.
Sonny Piers <sonny.piers@gmail.com>
parents:
44
diff
changeset
|
23 * @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
|
24 */ |
44
3dfb596cf669
Add a plugin loader.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
43
diff
changeset
|
25 ns: { |
50
770bb8460df8
Some improvements on namespaces.
Sonny Piers <sonny.piers@gmail.com>
parents:
44
diff
changeset
|
26 streams: 'http://etherx.jabber.org/streams', |
72 | 27 jabber_client: 'jabber:client', |
28 xmpp_stanzas: 'urn:ietf:params:xml:ns:xmpp-stanzas' | |
13
9aeb0750b9d1
fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents:
12
diff
changeset
|
29 }, |
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
|
30 /** |
9fbd0e3678b5
add comments, jsdoc syntax + move the parser and the serializer to the Lightstring namespace so they don't get recreated at every new Lightstring.Connection
Sonny Piers <sonny.piers@gmail.com>
parents:
9
diff
changeset
|
31 * @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
|
32 */ |
44
3dfb596cf669
Add a plugin loader.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
43
diff
changeset
|
33 stanzas: { |
13
9aeb0750b9d1
fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents:
12
diff
changeset
|
34 stream: { |
9aeb0750b9d1
fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents:
12
diff
changeset
|
35 open: function(aService) { |
21
b7d52bf259e0
Coding style: always use "" for XML strings.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
18
diff
changeset
|
36 return "<stream:stream to='" + aService + "'" + |
50
770bb8460df8
Some improvements on namespaces.
Sonny Piers <sonny.piers@gmail.com>
parents:
44
diff
changeset
|
37 " xmlns='" + Lightstring.ns['jabber_client'] + "'" + |
55
aaad98b33f86
Fix syntax errors.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
54
diff
changeset
|
38 " xmlns:stream='" + Lightstring.ns['streams'] + "'" + |
92
b211a00efa7f
Various implementation improvements.
Sonny Piers <sonny.piers@gmail.com>
parents:
89
diff
changeset
|
39 " version='1.0'>"; |
13
9aeb0750b9d1
fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents:
12
diff
changeset
|
40 }, |
9aeb0750b9d1
fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents:
12
diff
changeset
|
41 close: function() { |
21
b7d52bf259e0
Coding style: always use "" for XML strings.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
18
diff
changeset
|
42 return "</stream:stream>"; |
13
9aeb0750b9d1
fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents:
12
diff
changeset
|
43 } |
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
|
44 }, |
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
|
45 errors: { |
71
f12c759e4097
Fix service-unavailable handler and errors stanzas.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
68
diff
changeset
|
46 iq: function(from, id, type, error) { |
f12c759e4097
Fix service-unavailable handler and errors stanzas.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
68
diff
changeset
|
47 return "<iq to='" + from + "'" + |
f12c759e4097
Fix service-unavailable handler and errors stanzas.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
68
diff
changeset
|
48 " id='" + id + "'" + |
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
|
49 " 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 "<error type='" + type + "'>" + |
71
f12c759e4097
Fix service-unavailable handler and errors stanzas.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
68
diff
changeset
|
51 "<" + error + " xmlns='" + Lightstring.ns['xmpp_stanzas'] + "'/>" + //TODO: allow text content. |
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
|
52 //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
|
53 "</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
|
54 "</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
|
55 } |
13
9aeb0750b9d1
fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents:
12
diff
changeset
|
56 } |
9aeb0750b9d1
fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents:
12
diff
changeset
|
57 }, |
67
1c8f326fe3ef
Several fixes and comments updated/added.
Sonny Piers <sonny.piers@gmail.com>
parents:
65
diff
changeset
|
58 /** |
1c8f326fe3ef
Several fixes and comments updated/added.
Sonny Piers <sonny.piers@gmail.com>
parents:
65
diff
changeset
|
59 * @namespace Holds Lightstring plugins |
1c8f326fe3ef
Several fixes and comments updated/added.
Sonny Piers <sonny.piers@gmail.com>
parents:
65
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) { |
67
1c8f326fe3ef
Several fixes and comments updated/added.
Sonny Piers <sonny.piers@gmail.com>
parents:
65
diff
changeset
|
81 //TODO: error |
29
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) { |
67
1c8f326fe3ef
Several fixes and comments updated/added.
Sonny Piers <sonny.piers@gmail.com>
parents:
65
diff
changeset
|
98 //TODO: 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 |
99 | 121 * @param {String} [aService] The connection manager URL. |
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
|
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; |
67
1c8f326fe3ef
Several fixes and comments updated/added.
Sonny Piers <sonny.piers@gmail.com>
parents:
65
diff
changeset
|
127 /** |
1c8f326fe3ef
Several fixes and comments updated/added.
Sonny Piers <sonny.piers@gmail.com>
parents:
65
diff
changeset
|
128 * @namespace Holds connection events handlers |
1c8f326fe3ef
Several fixes and comments updated/added.
Sonny Piers <sonny.piers@gmail.com>
parents:
65
diff
changeset
|
129 */ |
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
|
130 this.handlers = {}; |
67
1c8f326fe3ef
Several fixes and comments updated/added.
Sonny Piers <sonny.piers@gmail.com>
parents:
65
diff
changeset
|
131 /** |
1c8f326fe3ef
Several fixes and comments updated/added.
Sonny Piers <sonny.piers@gmail.com>
parents:
65
diff
changeset
|
132 * @namespace Holds connection iq callbacks |
1c8f326fe3ef
Several fixes and comments updated/added.
Sonny Piers <sonny.piers@gmail.com>
parents:
65
diff
changeset
|
133 */ |
59 | 134 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
|
135 }; |
99 | 136 Lightstring.Connection.prototype = new EventEmitter(); |
137 /** | |
138 * @function Create and open a websocket then go though the XMPP authentification process. | |
139 * @param {String} [aJid] The JID (Jabber id) to use. | |
140 * @param {String} [aPassword] The associated password. | |
141 */ | |
142 Lightstring.Connection.prototype.connect = function(aJid, aPassword) { | |
143 this.emit('connecting'); | |
144 this.jid = new Lightstring.JID(aJid); | |
145 if (aPassword) | |
146 this.password = aPassword; | |
97
11e38a9bfe38
multi-transport is now possible
Sonny Piers <sonny.piers@gmail.com>
parents:
92
diff
changeset
|
147 |
99 | 148 if (!this.jid.bare) |
149 return; //TODO: error | |
150 if (!this.service) | |
151 return; //TODO: error | |
63
20da4fb67977
Auth PLAIN as plugin. Several fixes.
Sonny Piers <sonny.piers@gmail.com>
parents:
62
diff
changeset
|
152 |
99 | 153 function getProtocol(aURL) { |
154 var a = document.createElement('a'); | |
155 a.href = aURL; | |
156 return a.protocol.replace(':', ''); | |
157 } | |
158 var protocol = getProtocol(this.service); | |
63
20da4fb67977
Auth PLAIN as plugin. Several fixes.
Sonny Piers <sonny.piers@gmail.com>
parents:
62
diff
changeset
|
159 |
99 | 160 if (protocol.match('http')) |
161 this.connection = new Lightstring.BOSHConnection(this.service); | |
162 else if (protocol.match('ws')) | |
163 this.connection = new Lightstring.WebSocketConnection(this.service); | |
164 | |
165 this.connection.open(); | |
43
136df1708856
Better iq callbacks. (breaks everything)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
42
diff
changeset
|
166 |
99 | 167 var that = this; |
72 | 168 |
99 | 169 this.connection.once('open', function() { |
170 that.emit('open'); | |
171 }); | |
172 this.connection.on('out', function(stanza) { | |
173 that.emit('out', stanza); | |
174 }); | |
175 this.connection.on('in', function(stanza) { | |
176 var stanza = new Lightstring.Stanza(stanza); | |
29
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
28
diff
changeset
|
177 |
99 | 178 //FIXME: node-xmpp-bosh sends a self-closing stream:stream tag; it is wrong! |
179 that.emit('stanza', stanza); | |
180 | |
181 if (!stanza.DOM) | |
29
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
28
diff
changeset
|
182 return; |
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
28
diff
changeset
|
183 |
99 | 184 var name = stanza.DOM.localName; |
42
ee874d064650
Check iq sending.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
41
diff
changeset
|
185 |
99 | 186 //Authentication |
187 //FIXME SASL mechanisms and XMPP features can be both in a stream:features | |
188 if (name === 'features') { | |
189 //SASL mechanisms | |
190 if (stanza.DOM.firstChild.localName === 'mechanisms') { | |
191 stanza.mechanisms = []; | |
192 var nodes = stanza.DOM.getElementsByTagName('mechanism'); | |
193 for (var i = 0; i < nodes.length; i++) | |
194 stanza.mechanisms.push(nodes[i].textContent); | |
195 that.emit('mechanisms', stanza); | |
196 } | |
197 //XMPP features | |
198 else { | |
199 //TODO: stanza.features | |
200 that.emit('features', stanza); | |
201 } | |
202 } | |
203 else if (name === 'challenge') { | |
204 that.emit('challenge', stanza); | |
205 } | |
206 else if (name === 'failure') { | |
207 that.emit('failure', stanza); | |
208 } | |
209 else if (name === 'success') { | |
210 that.emit('success', stanza); | |
211 } | |
212 | |
213 //Iq callbacks | |
214 else if (name === 'iq') { | |
215 var payload = stanza.DOM.firstChild; | |
216 if (payload) | |
217 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
|
218 |
89 | 219 var id = stanza.DOM.getAttribute('id'); |
99 | 220 if (!(id && id in that.callbacks)) |
221 return; | |
222 | |
223 var type = stanza.DOM.getAttribute('type'); | |
224 if (type !== 'result' && type !== 'error') | |
225 return; //TODO: warning | |
226 | |
227 var callback = that.callbacks[id]; | |
228 if (type === 'result' && callback.success) | |
229 callback.success.call(that, stanza); | |
230 else if (type === 'error' && callback.error) | |
231 callback.error.call(that, stanza); | |
232 | |
233 delete that.callbacks[id]; | |
234 } | |
43
136df1708856
Better iq callbacks. (breaks everything)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
42
diff
changeset
|
235 |
99 | 236 else if (name === 'presence' || name === 'message') { |
237 that.emit(name, stanza); | |
78
4d0fc1c9ec88
Call iq-callbacks success/error with the right context.
Sonny Piers <sonny.piers@gmail.com>
parents:
77
diff
changeset
|
238 } |
99 | 239 }); |
240 }; | |
241 /** | |
242 * @function Send a message. | |
243 * @param {String|Object} aStanza The message to send. | |
244 * @param {Function} [aCallback] Executed on answer. (stanza must be iq) | |
245 */ | |
246 Lightstring.Connection.prototype.send = function(aStanza, aSuccess, aError) { | |
247 if (!(aStanza instanceof Lightstring.Stanza)) | |
248 var stanza = new Lightstring.Stanza(aStanza); | |
249 else | |
250 var stanza = aStanza; | |
251 | |
252 if (!stanza) | |
253 return; | |
254 | |
255 if (stanza.DOM.tagName === 'iq') { | |
256 var type = stanza.DOM.getAttribute('type'); | |
257 if (type !== 'get' || type !== 'set') | |
258 ; //TODO: error | |
259 | |
260 var callback = {success: aSuccess, error: aError}; | |
261 | |
262 var id = stanza.DOM.getAttribute('id'); | |
263 if (!id) { | |
264 var id = Lightstring.newId('sendiq:'); | |
265 stanza.DOM.setAttribute('id', id); | |
266 } | |
267 | |
268 this.callbacks[id] = callback; | |
269 | |
270 } | |
271 else if (aSuccess || aError) | |
272 ; //TODO: warning (no callback without iq) | |
17
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
273 |
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
274 |
99 | 275 //FIXME this.socket.send(stanza.XML); (need some work on Lightstring.Stanza) |
276 var fixme = Lightstring.DOM2XML(stanza.DOM); | |
277 stanza.XML = fixme; | |
278 this.connection.send(fixme); | |
279 this.emit('output', stanza); | |
280 }; | |
281 /** | |
282 * @function Closes the XMPP stream and the socket. | |
283 */ | |
284 Lightstring.Connection.prototype.disconnect = function() { | |
285 this.emit('disconnecting'); | |
286 var stream = Lightstring.stanzas.stream.close(); | |
287 this.socket.send(stream); | |
288 this.emit('XMLOutput', stream); | |
289 this.socket.close(); | |
290 }; | |
291 Lightstring.Connection.prototype.load = function() { | |
292 for (var i = 0; i < arguments.length; i++) { | |
293 var name = arguments[i]; | |
294 if (!(name in Lightstring.plugins)) | |
295 continue; //TODO: error | |
54
0b7dd59e264a
Fix Connection.load and add the init method to plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
51
diff
changeset
|
296 |
99 | 297 var plugin = Lightstring.plugins[name]; |
17
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
298 |
99 | 299 //Namespaces |
300 for (var ns in plugin.namespaces) | |
301 Lightstring.ns[ns] = plugin.namespaces[ns]; | |
77
7854e036a572
Fix a problem that made events without data called only once.
Sonny Piers <sonny.piers@gmail.com>
parents:
76
diff
changeset
|
302 |
99 | 303 //Stanzas |
304 Lightstring.stanzas[name] = {}; | |
305 for (var stanza in plugin.stanzas) | |
306 Lightstring.stanzas[name][stanza] = plugin.stanzas[stanza]; | |
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
|
307 |
99 | 308 //Handlers |
309 for (var handler in plugin.handlers) | |
310 this.on(handler, plugin.handlers[handler]); | |
72 | 311 |
99 | 312 //Methods |
313 this[name] = {}; | |
314 for (var method in plugin.methods) | |
315 this[name][method] = plugin.methods[method].bind(this); | |
316 | |
317 if (plugin.init) | |
318 plugin.init.apply(this); | |
17
b7bd814333eb
Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents:
16
diff
changeset
|
319 } |
99 | 320 }; |