annotate lightstring.js @ 88:595be4a8584f

fix the roster plugin
author Sonny Piers <sonny.piers@gmail.com>
date Thu, 23 Feb 2012 17:07:22 +0100
parents d9804e206393
children 55548845b177
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
3 /**
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
4 Copyright (c) 2011, Sonny Piers <sonny at fastmail dot net>
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
5
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
6 Permission to use, copy, modify, and/or distribute this software for any
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
7 purpose with or without fee is hereby granted, provided that the above
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
8 copyright notice and this permission notice appear in all copies.
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
9
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
13 ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
17 */
f31a75c3b6c8 code cleaning
Sonny Piers <sonny.piers@gmail.com>
parents: 1
diff changeset
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
5dbf93cef55d Various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 71
diff changeset
27 jabber_client: 'jabber:client',
5dbf93cef55d Various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 71
diff changeset
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) {
67
1c8f326fe3ef Several fixes and comments updated/added.
Sonny Piers <sonny.piers@gmail.com>
parents: 65
diff changeset
36 //WORKAROUND: 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
37 return "<stream:stream to='" + aService + "'" +
50
770bb8460df8 Some improvements on namespaces.
Sonny Piers <sonny.piers@gmail.com>
parents: 44
diff changeset
38 " xmlns='" + Lightstring.ns['jabber_client'] + "'" +
55
aaad98b33f86 Fix syntax errors.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 54
diff changeset
39 " xmlns:stream='" + Lightstring.ns['streams'] + "'" +
21
b7d52bf259e0 Coding style: always use "" for XML strings.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 18
diff changeset
40 " version='1.0'/>";
13
9aeb0750b9d1 fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents: 12
diff changeset
41 },
9aeb0750b9d1 fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents: 12
diff changeset
42 close: function() {
21
b7d52bf259e0 Coding style: always use "" for XML strings.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 18
diff changeset
43 return "</stream:stream>";
13
9aeb0750b9d1 fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents: 12
diff changeset
44 }
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
45 },
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
46 errors: {
71
f12c759e4097 Fix service-unavailable handler and errors stanzas.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 68
diff changeset
47 iq: function(from, id, type, error) {
f12c759e4097 Fix service-unavailable handler and errors stanzas.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 68
diff changeset
48 return "<iq to='" + from + "'" +
f12c759e4097 Fix service-unavailable handler and errors stanzas.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 68
diff changeset
49 " 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
50 " 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
51 "<error type='" + type + "'>" +
71
f12c759e4097 Fix service-unavailable handler and errors stanzas.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 68
diff changeset
52 "<" + 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
53 //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
54 "</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
55 "</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
56 }
13
9aeb0750b9d1 fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents: 12
diff changeset
57 }
9aeb0750b9d1 fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents: 12
diff changeset
58 },
67
1c8f326fe3ef Several fixes and comments updated/added.
Sonny Piers <sonny.piers@gmail.com>
parents: 65
diff changeset
59 /**
1c8f326fe3ef Several fixes and comments updated/added.
Sonny Piers <sonny.piers@gmail.com>
parents: 65
diff changeset
60 * @namespace Holds Lightstring plugins
1c8f326fe3ef Several fixes and comments updated/added.
Sonny Piers <sonny.piers@gmail.com>
parents: 65
diff changeset
61 */
44
3dfb596cf669 Add a plugin loader.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 43
diff changeset
62 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
63 /**
9fbd0e3678b5 add comments, jsdoc syntax + move the parser and the serializer to the Lightstring namespace so they don't get recreated at every new Lightstring.Connection
Sonny Piers <sonny.piers@gmail.com>
parents: 9
diff changeset
64 * @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
65 */
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 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
67 /**
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 * @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
69 */
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 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
71 /**
9fbd0e3678b5 add comments, jsdoc syntax + move the parser and the serializer to the Lightstring namespace so they don't get recreated at every new Lightstring.Connection
Sonny Piers <sonny.piers@gmail.com>
parents: 9
diff changeset
72 * @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
73 * @param {String} aString XML string.
9aeb0750b9d1 fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents: 12
diff changeset
74 * @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
75 */
29
1e6d2ca2daae Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents: 28
diff changeset
76 XML2DOM: function(aString) {
1e6d2ca2daae Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents: 28
diff changeset
77 var DOM = null;
1e6d2ca2daae Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents: 28
diff changeset
78 try {
1e6d2ca2daae Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents: 28
diff changeset
79 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
80 }
1e6d2ca2daae Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents: 28
diff changeset
81 catch (e) {
67
1c8f326fe3ef Several fixes and comments updated/added.
Sonny Piers <sonny.piers@gmail.com>
parents: 65
diff changeset
82 //TODO: error
29
1e6d2ca2daae Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents: 28
diff changeset
83 }
1e6d2ca2daae Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents: 28
diff changeset
84 finally {
1e6d2ca2daae Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents: 28
diff changeset
85 return DOM;
1e6d2ca2daae Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents: 28
diff changeset
86 };
12
9fbd0e3678b5 add comments, jsdoc syntax + move the parser and the serializer to the Lightstring namespace so they don't get recreated at every new Lightstring.Connection
Sonny Piers <sonny.piers@gmail.com>
parents: 9
diff changeset
87 },
9fbd0e3678b5 add comments, jsdoc syntax + move the parser and the serializer to the Lightstring namespace so they don't get recreated at every new Lightstring.Connection
Sonny Piers <sonny.piers@gmail.com>
parents: 9
diff changeset
88 /**
9fbd0e3678b5 add comments, jsdoc syntax + move the parser and the serializer to the Lightstring namespace so they don't get recreated at every new Lightstring.Connection
Sonny Piers <sonny.piers@gmail.com>
parents: 9
diff changeset
89 * @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
90 * @param {Object} aString DOM object.
9aeb0750b9d1 fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents: 12
diff changeset
91 * @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
92 */
29
1e6d2ca2daae Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents: 28
diff changeset
93 DOM2XML: function(aElement) {
1e6d2ca2daae Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents: 28
diff changeset
94 var XML = null;
1e6d2ca2daae Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents: 28
diff changeset
95 try {
1e6d2ca2daae Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents: 28
diff changeset
96 XML = this.serializer.serializeToString(aElement);
1e6d2ca2daae Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents: 28
diff changeset
97 }
1e6d2ca2daae Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents: 28
diff changeset
98 catch (e) {
67
1c8f326fe3ef Several fixes and comments updated/added.
Sonny Piers <sonny.piers@gmail.com>
parents: 65
diff changeset
99 //TODO: error
29
1e6d2ca2daae Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents: 28
diff changeset
100 }
1e6d2ca2daae Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents: 28
diff changeset
101 finally {
1e6d2ca2daae Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents: 28
diff changeset
102 return XML;
1e6d2ca2daae Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents: 28
diff changeset
103 };
33
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 /**
88d24231bf24 Better newId function.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 28
diff changeset
106 * @function Get an unique identifier.
88d24231bf24 Better newId function.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 28
diff changeset
107 * @param {String} [aString] Prefix to put before the identifier.
88d24231bf24 Better newId function.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 28
diff changeset
108 * @return {String} Identifier.
88d24231bf24 Better newId function.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 28
diff changeset
109 */
88d24231bf24 Better newId function.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 28
diff changeset
110 newId: (function() {
88d24231bf24 Better newId function.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 28
diff changeset
111 var id = 1024;
88d24231bf24 Better newId function.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 28
diff changeset
112 return function(prefix) {
88d24231bf24 Better newId function.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 28
diff changeset
113 if (typeof prefix === 'string')
88d24231bf24 Better newId function.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 28
diff changeset
114 return prefix + id++;
88d24231bf24 Better newId function.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 28
diff changeset
115 return '' + id++;
88d24231bf24 Better newId function.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 28
diff changeset
116 };
34
6ce66fba0242 Merging.
Sonny Piers <sonny.piers@gmail.com>
parents: 29 33
diff changeset
117 })()
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
118 };
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
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
120 /**
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 * @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
122 * @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
123 * @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
124 */
13
9aeb0750b9d1 fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents: 12
diff changeset
125 Lightstring.Connection = function(aService) {
9aeb0750b9d1 fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents: 12
diff changeset
126 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
127 this.service = aService;
67
1c8f326fe3ef Several fixes and comments updated/added.
Sonny Piers <sonny.piers@gmail.com>
parents: 65
diff changeset
128 /**
1c8f326fe3ef Several fixes and comments updated/added.
Sonny Piers <sonny.piers@gmail.com>
parents: 65
diff changeset
129 * @namespace Holds connection events handlers
1c8f326fe3ef Several fixes and comments updated/added.
Sonny Piers <sonny.piers@gmail.com>
parents: 65
diff changeset
130 */
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
131 this.handlers = {};
67
1c8f326fe3ef Several fixes and comments updated/added.
Sonny Piers <sonny.piers@gmail.com>
parents: 65
diff changeset
132 /**
1c8f326fe3ef Several fixes and comments updated/added.
Sonny Piers <sonny.piers@gmail.com>
parents: 65
diff changeset
133 * @namespace Holds connection iq callbacks
1c8f326fe3ef Several fixes and comments updated/added.
Sonny Piers <sonny.piers@gmail.com>
parents: 65
diff changeset
134 */
59
bbcc6bbdac73 Make it works :-)
Sonny Piers <sonny.piers@gmail.com>
parents: 57
diff changeset
135 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
136 };
17
b7bd814333eb Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents: 16
diff changeset
137 Lightstring.Connection.prototype = {
b7bd814333eb Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents: 16
diff changeset
138 /**
b7bd814333eb Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents: 16
diff changeset
139 * @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
140 * @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
141 * @param {String} [aPassword] The associated password.
b7bd814333eb Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents: 16
diff changeset
142 */
b7bd814333eb Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents: 16
diff changeset
143 connect: function(aJid, aPassword) {
b7bd814333eb Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents: 16
diff changeset
144 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
145 this.jid = new Lightstring.JID(aJid);
17
b7bd814333eb Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents: 16
diff changeset
146 if (aPassword)
b7bd814333eb Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents: 16
diff changeset
147 this.password = aPassword;
b7bd814333eb Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents: 16
diff changeset
148
22
6a6bb8ded046 Add a JID object, and use it in Lightstring.Connection.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 21
diff changeset
149 if (!this.jid.bare)
67
1c8f326fe3ef Several fixes and comments updated/added.
Sonny Piers <sonny.piers@gmail.com>
parents: 65
diff changeset
150 return; //TODO: error
17
b7bd814333eb Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents: 16
diff changeset
151 if (!this.service)
67
1c8f326fe3ef Several fixes and comments updated/added.
Sonny Piers <sonny.piers@gmail.com>
parents: 65
diff changeset
152 return; //TODO: error
17
b7bd814333eb Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents: 16
diff changeset
153
85
d9804e206393 Fix Safari/iOS support.
Sonny Piers <sonny.piers@gmail.com>
parents: 79
diff changeset
154 // Standard
d9804e206393 Fix Safari/iOS support.
Sonny Piers <sonny.piers@gmail.com>
parents: 79
diff changeset
155 if (typeof(WebSocket) === 'function')
40
e02bfcb78428 priority to WebSocket
Sonny Piers <sonny.piers@gmail.com>
parents: 39
diff changeset
156 this.socket = new WebSocket(this.service, 'xmpp');
85
d9804e206393 Fix Safari/iOS support.
Sonny Piers <sonny.piers@gmail.com>
parents: 79
diff changeset
157 // Safari
d9804e206393 Fix Safari/iOS support.
Sonny Piers <sonny.piers@gmail.com>
parents: 79
diff changeset
158 else if (typeof(WebSocket) === 'object')
d9804e206393 Fix Safari/iOS support.
Sonny Piers <sonny.piers@gmail.com>
parents: 79
diff changeset
159 this.socket = new WebSocket(this.service);
d9804e206393 Fix Safari/iOS support.
Sonny Piers <sonny.piers@gmail.com>
parents: 79
diff changeset
160 // Old Gecko
d9804e206393 Fix Safari/iOS support.
Sonny Piers <sonny.piers@gmail.com>
parents: 79
diff changeset
161 else if (typeof(MozWebSocket) === 'function') {
39
5e32481b8a92 Better WebSocket feature discovering and Opera support
Sonny Piers <sonny.piers@gmail.com>
parents: 37
diff changeset
162 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
163 }
85
d9804e206393 Fix Safari/iOS support.
Sonny Piers <sonny.piers@gmail.com>
parents: 79
diff changeset
164 // No known WebSocket support
39
5e32481b8a92 Better WebSocket feature discovering and Opera support
Sonny Piers <sonny.piers@gmail.com>
parents: 37
diff changeset
165 else {
67
1c8f326fe3ef Several fixes and comments updated/added.
Sonny Piers <sonny.piers@gmail.com>
parents: 65
diff changeset
166 return; //TODO: error
17
b7bd814333eb Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents: 16
diff changeset
167 }
b7bd814333eb Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents: 16
diff changeset
168
67
1c8f326fe3ef Several fixes and comments updated/added.
Sonny Piers <sonny.piers@gmail.com>
parents: 65
diff changeset
169 var Conn = this;
17
b7bd814333eb Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents: 16
diff changeset
170 this.socket.addEventListener('open', function() {
85
d9804e206393 Fix Safari/iOS support.
Sonny Piers <sonny.piers@gmail.com>
parents: 79
diff changeset
171 //FIXME: Opera/Safari WebSocket implementation doesn't support sub-protocol mechanism.
d9804e206393 Fix Safari/iOS support.
Sonny Piers <sonny.piers@gmail.com>
parents: 79
diff changeset
172 //if (this.protocol !== 'xmpp')
d9804e206393 Fix Safari/iOS support.
Sonny Piers <sonny.piers@gmail.com>
parents: 79
diff changeset
173 //return; //TODO: error
17
b7bd814333eb Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents: 16
diff changeset
174
67
1c8f326fe3ef Several fixes and comments updated/added.
Sonny Piers <sonny.piers@gmail.com>
parents: 65
diff changeset
175 var stream = Lightstring.stanzas.stream.open(Conn.jid.domain);
1c8f326fe3ef Several fixes and comments updated/added.
Sonny Piers <sonny.piers@gmail.com>
parents: 65
diff changeset
176 //FIXME: Use Lightstring.Connection.send (problem with parsing steam);
1c8f326fe3ef Several fixes and comments updated/added.
Sonny Piers <sonny.piers@gmail.com>
parents: 65
diff changeset
177 Conn.socket.send(stream);
29
1e6d2ca2daae Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents: 28
diff changeset
178 var stanza = {
1e6d2ca2daae Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents: 28
diff changeset
179 XML: stream
1e6d2ca2daae Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents: 28
diff changeset
180 };
67
1c8f326fe3ef Several fixes and comments updated/added.
Sonny Piers <sonny.piers@gmail.com>
parents: 65
diff changeset
181 Conn.emit('output', stanza);
17
b7bd814333eb Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents: 16
diff changeset
182 });
b7bd814333eb Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents: 16
diff changeset
183 this.socket.addEventListener('error', function(e) {
67
1c8f326fe3ef Several fixes and comments updated/added.
Sonny Piers <sonny.piers@gmail.com>
parents: 65
diff changeset
184 Conn.emit('disconnecting', e.data);
1c8f326fe3ef Several fixes and comments updated/added.
Sonny Piers <sonny.piers@gmail.com>
parents: 65
diff changeset
185 //TODO: error
17
b7bd814333eb Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents: 16
diff changeset
186 });
b7bd814333eb Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents: 16
diff changeset
187 this.socket.addEventListener('close', function(e) {
67
1c8f326fe3ef Several fixes and comments updated/added.
Sonny Piers <sonny.piers@gmail.com>
parents: 65
diff changeset
188 Conn.emit('disconnected', e.data);
17
b7bd814333eb Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents: 16
diff changeset
189 });
b7bd814333eb Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents: 16
diff changeset
190 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
191 var stanza = new Lightstring.Stanza(e.data);
17
b7bd814333eb Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents: 16
diff changeset
192
67
1c8f326fe3ef Several fixes and comments updated/added.
Sonny Piers <sonny.piers@gmail.com>
parents: 65
diff changeset
193 //FIXME: node-xmpp-bosh sends a self-closing stream:stream tag; it is wrong!
1c8f326fe3ef Several fixes and comments updated/added.
Sonny Piers <sonny.piers@gmail.com>
parents: 65
diff changeset
194 Conn.emit('input', stanza);
63
20da4fb67977 Auth PLAIN as plugin. Several fixes.
Sonny Piers <sonny.piers@gmail.com>
parents: 62
diff changeset
195
72
5dbf93cef55d Various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 71
diff changeset
196 if (!stanza.DOM)
29
1e6d2ca2daae Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents: 28
diff changeset
197 return;
57
91f18fdc0e2c Some clean up.
Sonny Piers <sonny.piers@gmail.com>
parents: 56
diff changeset
198
67
1c8f326fe3ef Several fixes and comments updated/added.
Sonny Piers <sonny.piers@gmail.com>
parents: 65
diff changeset
199 var name = stanza.DOM.localName;
63
20da4fb67977 Auth PLAIN as plugin. Several fixes.
Sonny Piers <sonny.piers@gmail.com>
parents: 62
diff changeset
200
67
1c8f326fe3ef Several fixes and comments updated/added.
Sonny Piers <sonny.piers@gmail.com>
parents: 65
diff changeset
201 //Authentication
65
2e8fbf3bce7f ANONYMOUS auth plugin.
Sonny Piers <sonny.piers@gmail.com>
parents: 64
diff changeset
202 //FIXME SASL mechanisms and XMPP features can be both in a stream:features
63
20da4fb67977 Auth PLAIN as plugin. Several fixes.
Sonny Piers <sonny.piers@gmail.com>
parents: 62
diff changeset
203 if (name === 'features') {
20da4fb67977 Auth PLAIN as plugin. Several fixes.
Sonny Piers <sonny.piers@gmail.com>
parents: 62
diff changeset
204 //SASL mechanisms
20da4fb67977 Auth PLAIN as plugin. Several fixes.
Sonny Piers <sonny.piers@gmail.com>
parents: 62
diff changeset
205 if (stanza.DOM.firstChild.localName === 'mechanisms') {
20da4fb67977 Auth PLAIN as plugin. Several fixes.
Sonny Piers <sonny.piers@gmail.com>
parents: 62
diff changeset
206 stanza.mechanisms = [];
20da4fb67977 Auth PLAIN as plugin. Several fixes.
Sonny Piers <sonny.piers@gmail.com>
parents: 62
diff changeset
207 var nodes = stanza.DOM.querySelectorAll('mechanism');
20da4fb67977 Auth PLAIN as plugin. Several fixes.
Sonny Piers <sonny.piers@gmail.com>
parents: 62
diff changeset
208 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
209 stanza.mechanisms.push(nodes[i].textContent);
67
1c8f326fe3ef Several fixes and comments updated/added.
Sonny Piers <sonny.piers@gmail.com>
parents: 65
diff changeset
210 Conn.emit('mechanisms', stanza);
63
20da4fb67977 Auth PLAIN as plugin. Several fixes.
Sonny Piers <sonny.piers@gmail.com>
parents: 62
diff changeset
211 }
20da4fb67977 Auth PLAIN as plugin. Several fixes.
Sonny Piers <sonny.piers@gmail.com>
parents: 62
diff changeset
212 //XMPP features
65
2e8fbf3bce7f ANONYMOUS auth plugin.
Sonny Piers <sonny.piers@gmail.com>
parents: 64
diff changeset
213 else {
63
20da4fb67977 Auth PLAIN as plugin. Several fixes.
Sonny Piers <sonny.piers@gmail.com>
parents: 62
diff changeset
214 //TODO: stanza.features
67
1c8f326fe3ef Several fixes and comments updated/added.
Sonny Piers <sonny.piers@gmail.com>
parents: 65
diff changeset
215 Conn.emit('features', stanza);
63
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 }
20da4fb67977 Auth PLAIN as plugin. Several fixes.
Sonny Piers <sonny.piers@gmail.com>
parents: 62
diff changeset
218 else if (name === 'challenge') {
67
1c8f326fe3ef Several fixes and comments updated/added.
Sonny Piers <sonny.piers@gmail.com>
parents: 65
diff changeset
219 Conn.emit('challenge', stanza);
63
20da4fb67977 Auth PLAIN as plugin. Several fixes.
Sonny Piers <sonny.piers@gmail.com>
parents: 62
diff changeset
220 }
64
d9f5ae0b6d98 Support for DIGEST-MD5 authentication. (plugin)
Sonny Piers <sonny.piers@gmail.com>
parents: 63
diff changeset
221 else if (name === 'failure') {
67
1c8f326fe3ef Several fixes and comments updated/added.
Sonny Piers <sonny.piers@gmail.com>
parents: 65
diff changeset
222 Conn.emit('failure', stanza);
64
d9f5ae0b6d98 Support for DIGEST-MD5 authentication. (plugin)
Sonny Piers <sonny.piers@gmail.com>
parents: 63
diff changeset
223 }
63
20da4fb67977 Auth PLAIN as plugin. Several fixes.
Sonny Piers <sonny.piers@gmail.com>
parents: 62
diff changeset
224 else if (name === 'success') {
67
1c8f326fe3ef Several fixes and comments updated/added.
Sonny Piers <sonny.piers@gmail.com>
parents: 65
diff changeset
225 Conn.emit('success', stanza);
63
20da4fb67977 Auth PLAIN as plugin. Several fixes.
Sonny Piers <sonny.piers@gmail.com>
parents: 62
diff changeset
226 }
20da4fb67977 Auth PLAIN as plugin. Several fixes.
Sonny Piers <sonny.piers@gmail.com>
parents: 62
diff changeset
227
20da4fb67977 Auth PLAIN as plugin. Several fixes.
Sonny Piers <sonny.piers@gmail.com>
parents: 62
diff changeset
228 //Iq callbacks
20da4fb67977 Auth PLAIN as plugin. Several fixes.
Sonny Piers <sonny.piers@gmail.com>
parents: 62
diff changeset
229 else if (name === 'iq') {
29
1e6d2ca2daae Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents: 28
diff changeset
230 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
231 if (payload)
67
1c8f326fe3ef Several fixes and comments updated/added.
Sonny Piers <sonny.piers@gmail.com>
parents: 65
diff changeset
232 Conn.emit('iq/' + payload.namespaceURI + ':' + payload.localName, stanza);
43
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 id = stanza.DOM.getAttributeNS(null, 'id');
67
1c8f326fe3ef Several fixes and comments updated/added.
Sonny Piers <sonny.piers@gmail.com>
parents: 65
diff changeset
235 if (!(id && id in Conn.callbacks))
43
136df1708856 Better iq callbacks. (breaks everything)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 42
diff changeset
236 return;
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 type = stanza.DOM.getAttributeNS(null, 'type');
59
bbcc6bbdac73 Make it works :-)
Sonny Piers <sonny.piers@gmail.com>
parents: 57
diff changeset
239 if (type !== 'result' && type !== 'error')
67
1c8f326fe3ef Several fixes and comments updated/added.
Sonny Piers <sonny.piers@gmail.com>
parents: 65
diff changeset
240 return; //TODO: warning
43
136df1708856 Better iq callbacks. (breaks everything)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 42
diff changeset
241
67
1c8f326fe3ef Several fixes and comments updated/added.
Sonny Piers <sonny.piers@gmail.com>
parents: 65
diff changeset
242 var callback = Conn.callbacks[id];
59
bbcc6bbdac73 Make it works :-)
Sonny Piers <sonny.piers@gmail.com>
parents: 57
diff changeset
243 if (type === 'result' && callback.success)
78
4d0fc1c9ec88 Call iq-callbacks success/error with the right context.
Sonny Piers <sonny.piers@gmail.com>
parents: 77
diff changeset
244 callback.success.call(Conn, stanza);
43
136df1708856 Better iq callbacks. (breaks everything)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 42
diff changeset
245 else if (type === 'error' && callback.error)
78
4d0fc1c9ec88 Call iq-callbacks success/error with the right context.
Sonny Piers <sonny.piers@gmail.com>
parents: 77
diff changeset
246 callback.error.call(Conn, stanza);
43
136df1708856 Better iq callbacks. (breaks everything)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 42
diff changeset
247
67
1c8f326fe3ef Several fixes and comments updated/added.
Sonny Piers <sonny.piers@gmail.com>
parents: 65
diff changeset
248 delete Conn.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
249 }
72
5dbf93cef55d Various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 71
diff changeset
250
5dbf93cef55d Various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 71
diff changeset
251 else if (name === 'presence' || name === 'message') {
5dbf93cef55d Various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 71
diff changeset
252 Conn.emit(name, stanza);
5dbf93cef55d Various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 71
diff changeset
253 }
17
b7bd814333eb Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents: 16
diff changeset
254 });
b7bd814333eb Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents: 16
diff changeset
255 },
b7bd814333eb Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents: 16
diff changeset
256 /**
b7bd814333eb Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents: 16
diff changeset
257 * @function Send a message.
b7bd814333eb Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents: 16
diff changeset
258 * @param {String|Object} aStanza The message to send.
b7bd814333eb Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents: 16
diff changeset
259 * @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
260 */
59
bbcc6bbdac73 Make it works :-)
Sonny Piers <sonny.piers@gmail.com>
parents: 57
diff changeset
261 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
262 if (!(aStanza instanceof Lightstring.Stanza))
1e6d2ca2daae Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents: 28
diff changeset
263 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
264 else
1e6d2ca2daae Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents: 28
diff changeset
265 var stanza = aStanza;
1e6d2ca2daae Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents: 28
diff changeset
266
72
5dbf93cef55d Various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 71
diff changeset
267 if (!stanza)
29
1e6d2ca2daae Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents: 28
diff changeset
268 return;
1e6d2ca2daae Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents: 28
diff changeset
269
1e6d2ca2daae Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents: 28
diff changeset
270 if (stanza.DOM.tagName === 'iq') {
42
ee874d064650 Check iq sending.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 41
diff changeset
271 var type = stanza.DOM.getAttributeNS(null, 'type');
ee874d064650 Check iq sending.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 41
diff changeset
272 if (type !== 'get' || type !== 'set')
67
1c8f326fe3ef Several fixes and comments updated/added.
Sonny Piers <sonny.piers@gmail.com>
parents: 65
diff changeset
273 ; //TODO: error
42
ee874d064650 Check iq sending.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 41
diff changeset
274
59
bbcc6bbdac73 Make it works :-)
Sonny Piers <sonny.piers@gmail.com>
parents: 57
diff changeset
275 var callback = {success: aSuccess, error: aError};
43
136df1708856 Better iq callbacks. (breaks everything)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 42
diff changeset
276
42
ee874d064650 Check iq sending.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 41
diff changeset
277 var id = stanza.DOM.getAttributeNS(null, 'id');
79
2290321f5c8b Always having an id on out-iq.
Sonny Piers <sonny.piers@gmail.com>
parents: 78
diff changeset
278 if (!id) {
2290321f5c8b Always having an id on out-iq.
Sonny Piers <sonny.piers@gmail.com>
parents: 78
diff changeset
279 var id = Lightstring.newId('sendiq:');
2290321f5c8b Always having an id on out-iq.
Sonny Piers <sonny.piers@gmail.com>
parents: 78
diff changeset
280 stanza.DOM.setAttributeNS(null, 'id', id);
2290321f5c8b Always having an id on out-iq.
Sonny Piers <sonny.piers@gmail.com>
parents: 78
diff changeset
281 }
43
136df1708856 Better iq callbacks. (breaks everything)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 42
diff changeset
282
78
4d0fc1c9ec88 Call iq-callbacks success/error with the right context.
Sonny Piers <sonny.piers@gmail.com>
parents: 77
diff changeset
283 this.callbacks[id] = callback;
4d0fc1c9ec88 Call iq-callbacks success/error with the right context.
Sonny Piers <sonny.piers@gmail.com>
parents: 77
diff changeset
284
4d0fc1c9ec88 Call iq-callbacks success/error with the right context.
Sonny Piers <sonny.piers@gmail.com>
parents: 77
diff changeset
285 }
4d0fc1c9ec88 Call iq-callbacks success/error with the right context.
Sonny Piers <sonny.piers@gmail.com>
parents: 77
diff changeset
286 else if (aSuccess || aError)
67
1c8f326fe3ef Several fixes and comments updated/added.
Sonny Piers <sonny.piers@gmail.com>
parents: 65
diff changeset
287 ; //TODO: warning (no callback without iq)
17
b7bd814333eb Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents: 16
diff changeset
288
b7bd814333eb Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents: 16
diff changeset
289
67
1c8f326fe3ef Several fixes and comments updated/added.
Sonny Piers <sonny.piers@gmail.com>
parents: 65
diff changeset
290 //FIXME this.socket.send(stanza.XML); (need some work on Lightstring.Stanza)
37
6773e5bc2ca0 several fixes
Sonny Piers <sonny.piers@gmail.com>
parents: 34
diff changeset
291 var fixme = Lightstring.DOM2XML(stanza.DOM);
6773e5bc2ca0 several fixes
Sonny Piers <sonny.piers@gmail.com>
parents: 34
diff changeset
292 stanza.XML = fixme;
6773e5bc2ca0 several fixes
Sonny Piers <sonny.piers@gmail.com>
parents: 34
diff changeset
293 this.socket.send(fixme);
29
1e6d2ca2daae Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents: 28
diff changeset
294 this.emit('output', stanza);
17
b7bd814333eb Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents: 16
diff changeset
295 },
b7bd814333eb Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents: 16
diff changeset
296 /**
b7bd814333eb Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents: 16
diff changeset
297 * @function Closes the XMPP stream and the socket.
b7bd814333eb Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents: 16
diff changeset
298 */
b7bd814333eb Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents: 16
diff changeset
299 disconnect: function() {
18
48bc65a0e0f1 add pubsub config
Sonny Piers <sonny.piers@gmail.com>
parents: 17
diff changeset
300 this.emit('disconnecting');
44
3dfb596cf669 Add a plugin loader.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 43
diff changeset
301 var stream = Lightstring.stanzas.stream.close();
18
48bc65a0e0f1 add pubsub config
Sonny Piers <sonny.piers@gmail.com>
parents: 17
diff changeset
302 this.send(stream);
17
b7bd814333eb Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents: 16
diff changeset
303 this.emit('XMLOutput', stream);
18
48bc65a0e0f1 add pubsub config
Sonny Piers <sonny.piers@gmail.com>
parents: 17
diff changeset
304 this.socket.close();
48bc65a0e0f1 add pubsub config
Sonny Piers <sonny.piers@gmail.com>
parents: 17
diff changeset
305 },
44
3dfb596cf669 Add a plugin loader.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 43
diff changeset
306 load: function() {
3dfb596cf669 Add a plugin loader.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 43
diff changeset
307 for (var i = 0; i < arguments.length; i++) {
3dfb596cf669 Add a plugin loader.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 43
diff changeset
308 var name = arguments[i];
3dfb596cf669 Add a plugin loader.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 43
diff changeset
309 if (!(name in Lightstring.plugins))
67
1c8f326fe3ef Several fixes and comments updated/added.
Sonny Piers <sonny.piers@gmail.com>
parents: 65
diff changeset
310 continue; //TODO: error
44
3dfb596cf669 Add a plugin loader.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 43
diff changeset
311
3dfb596cf669 Add a plugin loader.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 43
diff changeset
312 var plugin = Lightstring.plugins[name];
3dfb596cf669 Add a plugin loader.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 43
diff changeset
313
56
1d4b844586f6 Bind methods so Lightstring.Connection.plugin.method works with the correct context.
Sonny Piers <sonny.piers@gmail.com>
parents: 55
diff changeset
314 //Namespaces
44
3dfb596cf669 Add a plugin loader.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 43
diff changeset
315 for (var ns in plugin.namespaces)
3dfb596cf669 Add a plugin loader.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 43
diff changeset
316 Lightstring.ns[ns] = plugin.namespaces[ns];
3dfb596cf669 Add a plugin loader.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 43
diff changeset
317
56
1d4b844586f6 Bind methods so Lightstring.Connection.plugin.method works with the correct context.
Sonny Piers <sonny.piers@gmail.com>
parents: 55
diff changeset
318 //Stanzas
54
0b7dd59e264a Fix Connection.load and add the init method to plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 51
diff changeset
319 Lightstring.stanzas[name] = {};
44
3dfb596cf669 Add a plugin loader.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 43
diff changeset
320 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
321 Lightstring.stanzas[name][stanza] = plugin.stanzas[stanza];
44
3dfb596cf669 Add a plugin loader.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 43
diff changeset
322
56
1d4b844586f6 Bind methods so Lightstring.Connection.plugin.method works with the correct context.
Sonny Piers <sonny.piers@gmail.com>
parents: 55
diff changeset
323 //Handlers
44
3dfb596cf669 Add a plugin loader.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 43
diff changeset
324 for (var handler in plugin.handlers)
3dfb596cf669 Add a plugin loader.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 43
diff changeset
325 this.on(handler, plugin.handlers[handler]);
3dfb596cf669 Add a plugin loader.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 43
diff changeset
326
56
1d4b844586f6 Bind methods so Lightstring.Connection.plugin.method works with the correct context.
Sonny Piers <sonny.piers@gmail.com>
parents: 55
diff changeset
327 //Methods
57
91f18fdc0e2c Some clean up.
Sonny Piers <sonny.piers@gmail.com>
parents: 56
diff changeset
328 this[name] = {};
63
20da4fb67977 Auth PLAIN as plugin. Several fixes.
Sonny Piers <sonny.piers@gmail.com>
parents: 62
diff changeset
329 for (var method in plugin.methods)
72
5dbf93cef55d Various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 71
diff changeset
330 this[name][method] = plugin.methods[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
331
0b7dd59e264a Fix Connection.load and add the init method to plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 51
diff changeset
332 if (plugin.init)
76
0b8e7ca19f83 Plugins init functions are now called with Lightstring.Connection scope.
Sonny Piers <sonny.piers@gmail.com>
parents: 72
diff changeset
333 plugin.init.apply(this);
44
3dfb596cf669 Add a plugin loader.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 43
diff changeset
334 }
3dfb596cf669 Add a plugin loader.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 43
diff changeset
335 },
17
b7bd814333eb Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents: 16
diff changeset
336 /**
b7bd814333eb Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents: 16
diff changeset
337 * @function Emits an event.
b7bd814333eb Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents: 16
diff changeset
338 * @param {String} aName The event name.
b7bd814333eb Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents: 16
diff changeset
339 * @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
340 */
b7bd814333eb Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents: 16
diff changeset
341 emit: function(aName, aData) {
b7bd814333eb Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents: 16
diff changeset
342 var handlers = this.handlers[aName];
b7bd814333eb Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents: 16
diff changeset
343 if (!handlers)
b7bd814333eb Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents: 16
diff changeset
344 return;
b7bd814333eb Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents: 16
diff changeset
345
77
7854e036a572 Fix a problem that made events without data called only once.
Sonny Piers <sonny.piers@gmail.com>
parents: 76
diff changeset
346 //No data events
7854e036a572 Fix a problem that made events without data called only once.
Sonny Piers <sonny.piers@gmail.com>
parents: 76
diff changeset
347 if(!aData) {
7854e036a572 Fix a problem that made events without data called only once.
Sonny Piers <sonny.piers@gmail.com>
parents: 76
diff changeset
348 for (var i = 0; i < handlers.length; i++)
7854e036a572 Fix a problem that made events without data called only once.
Sonny Piers <sonny.piers@gmail.com>
parents: 76
diff changeset
349 handlers[i].call(this, aData);
7854e036a572 Fix a problem that made events without data called only once.
Sonny Piers <sonny.piers@gmail.com>
parents: 76
diff changeset
350
7854e036a572 Fix a problem that made events without data called only once.
Sonny Piers <sonny.piers@gmail.com>
parents: 76
diff changeset
351 return;
7854e036a572 Fix a problem that made events without data called only once.
Sonny Piers <sonny.piers@gmail.com>
parents: 76
diff changeset
352 }
7854e036a572 Fix a problem that made events without data called only once.
Sonny Piers <sonny.piers@gmail.com>
parents: 76
diff changeset
353
7854e036a572 Fix a problem that made events without data called only once.
Sonny Piers <sonny.piers@gmail.com>
parents: 76
diff changeset
354 //Non-iq events
63
20da4fb67977 Auth PLAIN as plugin. Several fixes.
Sonny Piers <sonny.piers@gmail.com>
parents: 62
diff changeset
355 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
356 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
357 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
358
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
359 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
360 }
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
361
77
7854e036a572 Fix a problem that made events without data called only once.
Sonny Piers <sonny.piers@gmail.com>
parents: 76
diff changeset
362 //Iq events
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
363 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
364 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
365 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
366 if (typeof ret !== 'boolean')
67
1c8f326fe3ef Several fixes and comments updated/added.
Sonny Piers <sonny.piers@gmail.com>
parents: 65
diff changeset
367 return; //TODO: error
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
368
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
369 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
370 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
371 }
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
372
71
f12c759e4097 Fix service-unavailable handler and errors stanzas.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 68
diff changeset
373 if (aData && aData.DOM) {
72
5dbf93cef55d Various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 71
diff changeset
374 var type = aData.DOM.getAttributeNS(null, 'type');
5dbf93cef55d Various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 71
diff changeset
375 if (type !== 'get' && type !== 'set')
5dbf93cef55d Various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 71
diff changeset
376 return;
5dbf93cef55d Various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 71
diff changeset
377
71
f12c759e4097 Fix service-unavailable handler and errors stanzas.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 68
diff changeset
378 var from = aData.DOM.getAttributeNS(null, 'from');
f12c759e4097 Fix service-unavailable handler and errors stanzas.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 68
diff changeset
379 var id = aData.DOM.getAttributeNS(null, 'id');
f12c759e4097 Fix service-unavailable handler and errors stanzas.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 68
diff changeset
380 this.send(Lightstring.stanzas.errors.iq(from, id, 'cancel', 'service-unavailable'));
f12c759e4097 Fix service-unavailable handler and errors stanzas.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 68
diff changeset
381 }
17
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 /**
b7bd814333eb Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents: 16
diff changeset
384 * @function Register an event handler.
b7bd814333eb Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents: 16
diff changeset
385 * @param {String} aName The event name.
b7bd814333eb Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents: 16
diff changeset
386 * @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
387 */
b7bd814333eb Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents: 16
diff changeset
388 on: function(aName, callback) {
b7bd814333eb Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents: 16
diff changeset
389 if (!this.handlers[aName])
b7bd814333eb Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents: 16
diff changeset
390 this.handlers[aName] = [];
b7bd814333eb Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents: 16
diff changeset
391 this.handlers[aName].push(callback);
b7bd814333eb Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents: 16
diff changeset
392 }
b7bd814333eb Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents: 16
diff changeset
393 };