annotate lightstring.js @ 106:c06ec02217ee

many changes
author Sonny Piers <sonny@fastmail.net>
date Tue, 26 Jun 2012 12:02:14 +0200
parents f14558915187
children 5cb4733c5189
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) {
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 */
106
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents: 99
diff changeset
75 parse: function(aString) {
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents: 99
diff changeset
76 var el = null;
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents: 99
diff changeset
77 //FIXME webkit doesn't throw an error when the parsing fails
29
1e6d2ca2daae Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents: 28
diff changeset
78 try {
106
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents: 99
diff changeset
79 el = this.parser.parseFromString(aString, 'text/xml').documentElement;
29
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 {
106
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents: 99
diff changeset
85 return el;
29
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 */
106
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents: 99
diff changeset
93 serialize: function(aElement) {
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents: 99
diff changeset
94 var string = null;
29
1e6d2ca2daae Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents: 28
diff changeset
95 try {
106
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents: 99
diff changeset
96 string = this.serializer.serializeToString(aElement);
29
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 {
106
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents: 99
diff changeset
102 return string;
29
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
99
f14558915187 bosh support
Sonny Piers <sonny.piers@gmail.com>
parents: 97
diff changeset
122 * @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
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) {
106
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents: 99
diff changeset
126 var that = this;
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents: 99
diff changeset
127 window.addEventListener('message', function(e) {
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents: 99
diff changeset
128 that.send(e.data.send)
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents: 99
diff changeset
129 });
13
9aeb0750b9d1 fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents: 12
diff changeset
130 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
131 this.service = aService;
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 events handlers
1c8f326fe3ef Several fixes and comments updated/added.
Sonny Piers <sonny.piers@gmail.com>
parents: 65
diff changeset
134 */
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 this.handlers = {};
67
1c8f326fe3ef Several fixes and comments updated/added.
Sonny Piers <sonny.piers@gmail.com>
parents: 65
diff changeset
136 /**
1c8f326fe3ef Several fixes and comments updated/added.
Sonny Piers <sonny.piers@gmail.com>
parents: 65
diff changeset
137 * @namespace Holds connection iq callbacks
1c8f326fe3ef Several fixes and comments updated/added.
Sonny Piers <sonny.piers@gmail.com>
parents: 65
diff changeset
138 */
59
bbcc6bbdac73 Make it works :-)
Sonny Piers <sonny.piers@gmail.com>
parents: 57
diff changeset
139 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
140 };
99
f14558915187 bosh support
Sonny Piers <sonny.piers@gmail.com>
parents: 97
diff changeset
141 Lightstring.Connection.prototype = new EventEmitter();
f14558915187 bosh support
Sonny Piers <sonny.piers@gmail.com>
parents: 97
diff changeset
142 /**
f14558915187 bosh support
Sonny Piers <sonny.piers@gmail.com>
parents: 97
diff changeset
143 * @function Create and open a websocket then go though the XMPP authentification process.
f14558915187 bosh support
Sonny Piers <sonny.piers@gmail.com>
parents: 97
diff changeset
144 * @param {String} [aJid] The JID (Jabber id) to use.
f14558915187 bosh support
Sonny Piers <sonny.piers@gmail.com>
parents: 97
diff changeset
145 * @param {String} [aPassword] The associated password.
f14558915187 bosh support
Sonny Piers <sonny.piers@gmail.com>
parents: 97
diff changeset
146 */
f14558915187 bosh support
Sonny Piers <sonny.piers@gmail.com>
parents: 97
diff changeset
147 Lightstring.Connection.prototype.connect = function(aJid, aPassword) {
f14558915187 bosh support
Sonny Piers <sonny.piers@gmail.com>
parents: 97
diff changeset
148 this.emit('connecting');
f14558915187 bosh support
Sonny Piers <sonny.piers@gmail.com>
parents: 97
diff changeset
149 this.jid = new Lightstring.JID(aJid);
f14558915187 bosh support
Sonny Piers <sonny.piers@gmail.com>
parents: 97
diff changeset
150 if (aPassword)
f14558915187 bosh support
Sonny Piers <sonny.piers@gmail.com>
parents: 97
diff changeset
151 this.password = aPassword;
97
11e38a9bfe38 multi-transport is now possible
Sonny Piers <sonny.piers@gmail.com>
parents: 92
diff changeset
152
99
f14558915187 bosh support
Sonny Piers <sonny.piers@gmail.com>
parents: 97
diff changeset
153 if (!this.jid.bare)
f14558915187 bosh support
Sonny Piers <sonny.piers@gmail.com>
parents: 97
diff changeset
154 return; //TODO: error
f14558915187 bosh support
Sonny Piers <sonny.piers@gmail.com>
parents: 97
diff changeset
155 if (!this.service)
f14558915187 bosh support
Sonny Piers <sonny.piers@gmail.com>
parents: 97
diff changeset
156 return; //TODO: error
63
20da4fb67977 Auth PLAIN as plugin. Several fixes.
Sonny Piers <sonny.piers@gmail.com>
parents: 62
diff changeset
157
99
f14558915187 bosh support
Sonny Piers <sonny.piers@gmail.com>
parents: 97
diff changeset
158 function getProtocol(aURL) {
f14558915187 bosh support
Sonny Piers <sonny.piers@gmail.com>
parents: 97
diff changeset
159 var a = document.createElement('a');
f14558915187 bosh support
Sonny Piers <sonny.piers@gmail.com>
parents: 97
diff changeset
160 a.href = aURL;
f14558915187 bosh support
Sonny Piers <sonny.piers@gmail.com>
parents: 97
diff changeset
161 return a.protocol.replace(':', '');
f14558915187 bosh support
Sonny Piers <sonny.piers@gmail.com>
parents: 97
diff changeset
162 }
f14558915187 bosh support
Sonny Piers <sonny.piers@gmail.com>
parents: 97
diff changeset
163 var protocol = getProtocol(this.service);
63
20da4fb67977 Auth PLAIN as plugin. Several fixes.
Sonny Piers <sonny.piers@gmail.com>
parents: 62
diff changeset
164
99
f14558915187 bosh support
Sonny Piers <sonny.piers@gmail.com>
parents: 97
diff changeset
165 if (protocol.match('http'))
106
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents: 99
diff changeset
166 this.connection = new Lightstring.BOSHConnection(this.service, this.jid);
99
f14558915187 bosh support
Sonny Piers <sonny.piers@gmail.com>
parents: 97
diff changeset
167 else if (protocol.match('ws'))
106
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents: 99
diff changeset
168 this.connection = new Lightstring.WebSocketConnection(this.service, this.jid);
99
f14558915187 bosh support
Sonny Piers <sonny.piers@gmail.com>
parents: 97
diff changeset
169
f14558915187 bosh support
Sonny Piers <sonny.piers@gmail.com>
parents: 97
diff changeset
170 this.connection.open();
43
136df1708856 Better iq callbacks. (breaks everything)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 42
diff changeset
171
99
f14558915187 bosh support
Sonny Piers <sonny.piers@gmail.com>
parents: 97
diff changeset
172 var that = this;
72
5dbf93cef55d Various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 71
diff changeset
173
99
f14558915187 bosh support
Sonny Piers <sonny.piers@gmail.com>
parents: 97
diff changeset
174 this.connection.once('open', function() {
f14558915187 bosh support
Sonny Piers <sonny.piers@gmail.com>
parents: 97
diff changeset
175 that.emit('open');
f14558915187 bosh support
Sonny Piers <sonny.piers@gmail.com>
parents: 97
diff changeset
176 });
f14558915187 bosh support
Sonny Piers <sonny.piers@gmail.com>
parents: 97
diff changeset
177 this.connection.on('out', function(stanza) {
f14558915187 bosh support
Sonny Piers <sonny.piers@gmail.com>
parents: 97
diff changeset
178 that.emit('out', stanza);
f14558915187 bosh support
Sonny Piers <sonny.piers@gmail.com>
parents: 97
diff changeset
179 });
f14558915187 bosh support
Sonny Piers <sonny.piers@gmail.com>
parents: 97
diff changeset
180 this.connection.on('in', function(stanza) {
f14558915187 bosh support
Sonny Piers <sonny.piers@gmail.com>
parents: 97
diff changeset
181 //FIXME: node-xmpp-bosh sends a self-closing stream:stream tag; it is wrong!
f14558915187 bosh support
Sonny Piers <sonny.piers@gmail.com>
parents: 97
diff changeset
182 that.emit('stanza', stanza);
f14558915187 bosh support
Sonny Piers <sonny.piers@gmail.com>
parents: 97
diff changeset
183
106
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents: 99
diff changeset
184 if (!stanza.el)
29
1e6d2ca2daae Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents: 28
diff changeset
185 return;
1e6d2ca2daae Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents: 28
diff changeset
186
106
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents: 99
diff changeset
187 var el = stanza.el;
42
ee874d064650 Check iq sending.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 41
diff changeset
188
99
f14558915187 bosh support
Sonny Piers <sonny.piers@gmail.com>
parents: 97
diff changeset
189 //Authentication
f14558915187 bosh support
Sonny Piers <sonny.piers@gmail.com>
parents: 97
diff changeset
190 //FIXME SASL mechanisms and XMPP features can be both in a stream:features
106
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents: 99
diff changeset
191 if (el.localName === 'features') {
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents: 99
diff changeset
192 var children = el.childNodes;
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents: 99
diff changeset
193 for (var i = 0, length = children.length; i < length; i++) {
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents: 99
diff changeset
194 //SASL mechanisms
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents: 99
diff changeset
195 if(children[i].localName === 'mechanisms') {
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents: 99
diff changeset
196 stanza.mechanisms = [];
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents: 99
diff changeset
197 var nodes = el.getElementsByTagName('mechanism');
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents: 99
diff changeset
198 for (var i = 0; i < nodes.length; i++)
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents: 99
diff changeset
199 stanza.mechanisms.push(nodes[i].textContent);
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents: 99
diff changeset
200 that.emit('mechanisms', stanza);
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents: 99
diff changeset
201 return;
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents: 99
diff changeset
202 }
99
f14558915187 bosh support
Sonny Piers <sonny.piers@gmail.com>
parents: 97
diff changeset
203 }
f14558915187 bosh support
Sonny Piers <sonny.piers@gmail.com>
parents: 97
diff changeset
204 //XMPP features
106
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents: 99
diff changeset
205 // else {
99
f14558915187 bosh support
Sonny Piers <sonny.piers@gmail.com>
parents: 97
diff changeset
206 //TODO: stanza.features
f14558915187 bosh support
Sonny Piers <sonny.piers@gmail.com>
parents: 97
diff changeset
207 that.emit('features', stanza);
106
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents: 99
diff changeset
208 // }
99
f14558915187 bosh support
Sonny Piers <sonny.piers@gmail.com>
parents: 97
diff changeset
209 }
106
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents: 99
diff changeset
210 else if (el.localName === 'challenge') {
99
f14558915187 bosh support
Sonny Piers <sonny.piers@gmail.com>
parents: 97
diff changeset
211 that.emit('challenge', stanza);
f14558915187 bosh support
Sonny Piers <sonny.piers@gmail.com>
parents: 97
diff changeset
212 }
106
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents: 99
diff changeset
213 else if (el.localName === 'failure') {
99
f14558915187 bosh support
Sonny Piers <sonny.piers@gmail.com>
parents: 97
diff changeset
214 that.emit('failure', stanza);
f14558915187 bosh support
Sonny Piers <sonny.piers@gmail.com>
parents: 97
diff changeset
215 }
106
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents: 99
diff changeset
216 else if (el.localName === 'success') {
99
f14558915187 bosh support
Sonny Piers <sonny.piers@gmail.com>
parents: 97
diff changeset
217 that.emit('success', stanza);
f14558915187 bosh support
Sonny Piers <sonny.piers@gmail.com>
parents: 97
diff changeset
218 }
f14558915187 bosh support
Sonny Piers <sonny.piers@gmail.com>
parents: 97
diff changeset
219
f14558915187 bosh support
Sonny Piers <sonny.piers@gmail.com>
parents: 97
diff changeset
220 //Iq callbacks
106
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents: 99
diff changeset
221 else if (el.localName === 'iq') {
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents: 99
diff changeset
222 var payload = el.firstChild;
99
f14558915187 bosh support
Sonny Piers <sonny.piers@gmail.com>
parents: 97
diff changeset
223 if (payload)
f14558915187 bosh support
Sonny Piers <sonny.piers@gmail.com>
parents: 97
diff changeset
224 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
225
106
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents: 99
diff changeset
226 var id = el.getAttribute('id');
99
f14558915187 bosh support
Sonny Piers <sonny.piers@gmail.com>
parents: 97
diff changeset
227 if (!(id && id in that.callbacks))
f14558915187 bosh support
Sonny Piers <sonny.piers@gmail.com>
parents: 97
diff changeset
228 return;
f14558915187 bosh support
Sonny Piers <sonny.piers@gmail.com>
parents: 97
diff changeset
229
106
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents: 99
diff changeset
230 var type = el.getAttribute('type');
99
f14558915187 bosh support
Sonny Piers <sonny.piers@gmail.com>
parents: 97
diff changeset
231 if (type !== 'result' && type !== 'error')
f14558915187 bosh support
Sonny Piers <sonny.piers@gmail.com>
parents: 97
diff changeset
232 return; //TODO: warning
f14558915187 bosh support
Sonny Piers <sonny.piers@gmail.com>
parents: 97
diff changeset
233
f14558915187 bosh support
Sonny Piers <sonny.piers@gmail.com>
parents: 97
diff changeset
234 var callback = that.callbacks[id];
f14558915187 bosh support
Sonny Piers <sonny.piers@gmail.com>
parents: 97
diff changeset
235 if (type === 'result' && callback.success)
f14558915187 bosh support
Sonny Piers <sonny.piers@gmail.com>
parents: 97
diff changeset
236 callback.success.call(that, stanza);
f14558915187 bosh support
Sonny Piers <sonny.piers@gmail.com>
parents: 97
diff changeset
237 else if (type === 'error' && callback.error)
f14558915187 bosh support
Sonny Piers <sonny.piers@gmail.com>
parents: 97
diff changeset
238 callback.error.call(that, stanza);
f14558915187 bosh support
Sonny Piers <sonny.piers@gmail.com>
parents: 97
diff changeset
239
f14558915187 bosh support
Sonny Piers <sonny.piers@gmail.com>
parents: 97
diff changeset
240 delete that.callbacks[id];
f14558915187 bosh support
Sonny Piers <sonny.piers@gmail.com>
parents: 97
diff changeset
241 }
43
136df1708856 Better iq callbacks. (breaks everything)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 42
diff changeset
242
106
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents: 99
diff changeset
243 else if (el.localName === 'presence' || el.localName === 'message') {
99
f14558915187 bosh support
Sonny Piers <sonny.piers@gmail.com>
parents: 97
diff changeset
244 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
245 }
99
f14558915187 bosh support
Sonny Piers <sonny.piers@gmail.com>
parents: 97
diff changeset
246 });
f14558915187 bosh support
Sonny Piers <sonny.piers@gmail.com>
parents: 97
diff changeset
247 };
f14558915187 bosh support
Sonny Piers <sonny.piers@gmail.com>
parents: 97
diff changeset
248 /**
f14558915187 bosh support
Sonny Piers <sonny.piers@gmail.com>
parents: 97
diff changeset
249 * @function Send a message.
f14558915187 bosh support
Sonny Piers <sonny.piers@gmail.com>
parents: 97
diff changeset
250 * @param {String|Object} aStanza The message to send.
f14558915187 bosh support
Sonny Piers <sonny.piers@gmail.com>
parents: 97
diff changeset
251 * @param {Function} [aCallback] Executed on answer. (stanza must be iq)
f14558915187 bosh support
Sonny Piers <sonny.piers@gmail.com>
parents: 97
diff changeset
252 */
f14558915187 bosh support
Sonny Piers <sonny.piers@gmail.com>
parents: 97
diff changeset
253 Lightstring.Connection.prototype.send = function(aStanza, aSuccess, aError) {
f14558915187 bosh support
Sonny Piers <sonny.piers@gmail.com>
parents: 97
diff changeset
254 if (!(aStanza instanceof Lightstring.Stanza))
f14558915187 bosh support
Sonny Piers <sonny.piers@gmail.com>
parents: 97
diff changeset
255 var stanza = new Lightstring.Stanza(aStanza);
f14558915187 bosh support
Sonny Piers <sonny.piers@gmail.com>
parents: 97
diff changeset
256 else
f14558915187 bosh support
Sonny Piers <sonny.piers@gmail.com>
parents: 97
diff changeset
257 var stanza = aStanza;
f14558915187 bosh support
Sonny Piers <sonny.piers@gmail.com>
parents: 97
diff changeset
258
f14558915187 bosh support
Sonny Piers <sonny.piers@gmail.com>
parents: 97
diff changeset
259 if (!stanza)
f14558915187 bosh support
Sonny Piers <sonny.piers@gmail.com>
parents: 97
diff changeset
260 return;
f14558915187 bosh support
Sonny Piers <sonny.piers@gmail.com>
parents: 97
diff changeset
261
106
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents: 99
diff changeset
262 if (stanza.el.tagName === 'iq') {
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents: 99
diff changeset
263 var type = stanza.el.getAttribute('type');
99
f14558915187 bosh support
Sonny Piers <sonny.piers@gmail.com>
parents: 97
diff changeset
264 if (type !== 'get' || type !== 'set')
f14558915187 bosh support
Sonny Piers <sonny.piers@gmail.com>
parents: 97
diff changeset
265 ; //TODO: error
f14558915187 bosh support
Sonny Piers <sonny.piers@gmail.com>
parents: 97
diff changeset
266
f14558915187 bosh support
Sonny Piers <sonny.piers@gmail.com>
parents: 97
diff changeset
267 var callback = {success: aSuccess, error: aError};
f14558915187 bosh support
Sonny Piers <sonny.piers@gmail.com>
parents: 97
diff changeset
268
106
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents: 99
diff changeset
269 var id = stanza.el.getAttribute('id');
99
f14558915187 bosh support
Sonny Piers <sonny.piers@gmail.com>
parents: 97
diff changeset
270 if (!id) {
f14558915187 bosh support
Sonny Piers <sonny.piers@gmail.com>
parents: 97
diff changeset
271 var id = Lightstring.newId('sendiq:');
106
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents: 99
diff changeset
272 stanza.el.setAttribute('id', id);
99
f14558915187 bosh support
Sonny Piers <sonny.piers@gmail.com>
parents: 97
diff changeset
273 }
f14558915187 bosh support
Sonny Piers <sonny.piers@gmail.com>
parents: 97
diff changeset
274
f14558915187 bosh support
Sonny Piers <sonny.piers@gmail.com>
parents: 97
diff changeset
275 this.callbacks[id] = callback;
f14558915187 bosh support
Sonny Piers <sonny.piers@gmail.com>
parents: 97
diff changeset
276
f14558915187 bosh support
Sonny Piers <sonny.piers@gmail.com>
parents: 97
diff changeset
277 }
f14558915187 bosh support
Sonny Piers <sonny.piers@gmail.com>
parents: 97
diff changeset
278 else if (aSuccess || aError)
f14558915187 bosh support
Sonny Piers <sonny.piers@gmail.com>
parents: 97
diff changeset
279 ; //TODO: warning (no callback without iq)
17
b7bd814333eb Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents: 16
diff changeset
280
106
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents: 99
diff changeset
281 this.connection.send(stanza.toString());
99
f14558915187 bosh support
Sonny Piers <sonny.piers@gmail.com>
parents: 97
diff changeset
282 };
f14558915187 bosh support
Sonny Piers <sonny.piers@gmail.com>
parents: 97
diff changeset
283 /**
f14558915187 bosh support
Sonny Piers <sonny.piers@gmail.com>
parents: 97
diff changeset
284 * @function Closes the XMPP stream and the socket.
f14558915187 bosh support
Sonny Piers <sonny.piers@gmail.com>
parents: 97
diff changeset
285 */
f14558915187 bosh support
Sonny Piers <sonny.piers@gmail.com>
parents: 97
diff changeset
286 Lightstring.Connection.prototype.disconnect = function() {
f14558915187 bosh support
Sonny Piers <sonny.piers@gmail.com>
parents: 97
diff changeset
287 this.emit('disconnecting');
f14558915187 bosh support
Sonny Piers <sonny.piers@gmail.com>
parents: 97
diff changeset
288 var stream = Lightstring.stanzas.stream.close();
f14558915187 bosh support
Sonny Piers <sonny.piers@gmail.com>
parents: 97
diff changeset
289 this.socket.send(stream);
106
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents: 99
diff changeset
290 this.emit('out', stream);
99
f14558915187 bosh support
Sonny Piers <sonny.piers@gmail.com>
parents: 97
diff changeset
291 this.socket.close();
f14558915187 bosh support
Sonny Piers <sonny.piers@gmail.com>
parents: 97
diff changeset
292 };
f14558915187 bosh support
Sonny Piers <sonny.piers@gmail.com>
parents: 97
diff changeset
293 Lightstring.Connection.prototype.load = function() {
f14558915187 bosh support
Sonny Piers <sonny.piers@gmail.com>
parents: 97
diff changeset
294 for (var i = 0; i < arguments.length; i++) {
f14558915187 bosh support
Sonny Piers <sonny.piers@gmail.com>
parents: 97
diff changeset
295 var name = arguments[i];
f14558915187 bosh support
Sonny Piers <sonny.piers@gmail.com>
parents: 97
diff changeset
296 if (!(name in Lightstring.plugins))
f14558915187 bosh support
Sonny Piers <sonny.piers@gmail.com>
parents: 97
diff changeset
297 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
298
99
f14558915187 bosh support
Sonny Piers <sonny.piers@gmail.com>
parents: 97
diff changeset
299 var plugin = Lightstring.plugins[name];
17
b7bd814333eb Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents: 16
diff changeset
300
99
f14558915187 bosh support
Sonny Piers <sonny.piers@gmail.com>
parents: 97
diff changeset
301 //Namespaces
f14558915187 bosh support
Sonny Piers <sonny.piers@gmail.com>
parents: 97
diff changeset
302 for (var ns in plugin.namespaces)
f14558915187 bosh support
Sonny Piers <sonny.piers@gmail.com>
parents: 97
diff changeset
303 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
304
99
f14558915187 bosh support
Sonny Piers <sonny.piers@gmail.com>
parents: 97
diff changeset
305 //Stanzas
f14558915187 bosh support
Sonny Piers <sonny.piers@gmail.com>
parents: 97
diff changeset
306 Lightstring.stanzas[name] = {};
f14558915187 bosh support
Sonny Piers <sonny.piers@gmail.com>
parents: 97
diff changeset
307 for (var stanza in plugin.stanzas)
f14558915187 bosh support
Sonny Piers <sonny.piers@gmail.com>
parents: 97
diff changeset
308 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
309
99
f14558915187 bosh support
Sonny Piers <sonny.piers@gmail.com>
parents: 97
diff changeset
310 //Handlers
f14558915187 bosh support
Sonny Piers <sonny.piers@gmail.com>
parents: 97
diff changeset
311 for (var handler in plugin.handlers)
f14558915187 bosh support
Sonny Piers <sonny.piers@gmail.com>
parents: 97
diff changeset
312 this.on(handler, plugin.handlers[handler]);
72
5dbf93cef55d Various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 71
diff changeset
313
99
f14558915187 bosh support
Sonny Piers <sonny.piers@gmail.com>
parents: 97
diff changeset
314 //Methods
f14558915187 bosh support
Sonny Piers <sonny.piers@gmail.com>
parents: 97
diff changeset
315 this[name] = {};
f14558915187 bosh support
Sonny Piers <sonny.piers@gmail.com>
parents: 97
diff changeset
316 for (var method in plugin.methods)
f14558915187 bosh support
Sonny Piers <sonny.piers@gmail.com>
parents: 97
diff changeset
317 this[name][method] = plugin.methods[method].bind(this);
f14558915187 bosh support
Sonny Piers <sonny.piers@gmail.com>
parents: 97
diff changeset
318
f14558915187 bosh support
Sonny Piers <sonny.piers@gmail.com>
parents: 97
diff changeset
319 if (plugin.init)
f14558915187 bosh support
Sonny Piers <sonny.piers@gmail.com>
parents: 97
diff changeset
320 plugin.init.apply(this);
17
b7bd814333eb Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents: 16
diff changeset
321 }
99
f14558915187 bosh support
Sonny Piers <sonny.piers@gmail.com>
parents: 97
diff changeset
322 };