annotate lightstring.js @ 109:cd1f57661439 default tip

Fix files permissions.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Wed, 01 Aug 2012 23:18:03 +0200
parents 5cb4733c5189
children
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
108
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
19 (function() {
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
20 define(['./jid', './stanza', './transports/websocket.js', './transports/bosh.js'], function(JID, Stanza, WebSocketTransport, BOSHTransport) {
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
21 Lightstring.JID = JID;
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
22 Lightstring.Stanza = Stanza.stanza;
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
23 Lightstring.IQ = Stanza.iq;
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
24 Lightstring.doc = Stanza.doc;
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
25 Lightstring.Presence = Stanza.presence;
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
26 Lightstring.Message = Stanza.message;
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
27 Lightstring.BOSHTransport = BOSHTransport;
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
28 Lightstring.WebSocketTransport = WebSocketTransport;
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
29 return Lightstring;
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
30 });
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
31
108
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
32 var Lightstring = {
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
33 /*
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
34 * @namespace Holds XMPP namespaces.
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
35 * @description http://xmpp.org/xmpp-protocols/protocol-namespaces
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
36 */
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
37 ns: {
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
38 streams: 'http://etherx.jabber.org/streams',
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
39 jabber_client: 'jabber:client',
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
40 xmpp_stanzas: 'urn:ietf:params:xml:ns:xmpp-stanzas'
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
41 },
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
42 /**
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
43 * @namespace Holds XMPP stanza builders.
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
44 */
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
45 stanzas: {
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
46 stream: {
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
47 open: function(aService) {
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
48 return "<stream:stream to='" + aService + "'" +
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
49 " xmlns='" + Lightstring.ns['jabber_client'] + "'" +
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
50 " xmlns:stream='" + Lightstring.ns['streams'] + "'" +
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
51 " version='1.0'>";
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
52 },
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
53 close: function() {
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
54 return "</stream:stream>";
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
55 }
13
9aeb0750b9d1 fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents: 12
diff changeset
56 },
108
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
57 errors: {
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
58 iq: function(from, id, type, error) {
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
59 return "<iq to='" + from + "'" +
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
60 " id='" + id + "'" +
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
61 " type='error'>" +
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
62 "<error type='" + type + "'>" +
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
63 "<" + error + " xmlns='" + Lightstring.ns['xmpp_stanzas'] + "'/>" + //TODO: allow text content.
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
64 //TODO: allow text and payload.
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
65 "</error>" +
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
66 "</iq>";
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
67 }
13
9aeb0750b9d1 fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents: 12
diff changeset
68 }
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
69 },
108
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
70 /**
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
71 * @namespace Holds Lightstring plugins
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
72 */
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
73 plugins: {},
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
74 /**
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
75 * @private Holds the connections
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
76 */
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
77 connections: [],
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
78 /**
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
79 * @function Returns a new unique identifier.
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
80 * @param {String} [aPrefix] Prefix to put before the identifier.
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
81 * @return {String} Identifier.
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
82 */
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
83 id: function(aPrefix) {
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
84 return (aPrefix || '') + Date.now();
13
9aeb0750b9d1 fix an error with the stream builder
Sonny Piers <sonny.piers@gmail.com>
parents: 12
diff changeset
85 }
108
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
86 };
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
87
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
88 /**
108
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
89 * @constructor Creates a new Lightstring connection
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
90 * @param {String} [aService] The connection manager URL.
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
91 * @memberOf Lightstring
33
88d24231bf24 Better newId function.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 28
diff changeset
92 */
108
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
93 Lightstring.Connection = function(aService) {
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
94 if (aService)
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
95 this.service = aService;
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
96 /**
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
97 * @namespace Holds connection events handlers
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
98 */
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
99 this.handlers = {};
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
100 /**
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
101 * @namespace Holds connection iq callbacks
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
102 */
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
103 this.callbacks = {};
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
104
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
105 Lightstring.connections.push(this);
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
106 };
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
107 Lightstring.Connection.prototype = new EventEmitter();
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
108 Lightstring.Connection.prototype.onTransportLoaded = function() {
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
109 this.transport.open();
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
110
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
111 var that = this;
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
112
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
113 this.transport.once('open', function() {
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
114 that.emit('open');
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
115 });
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
116 this.transport.on('out', function(stanza) {
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
117 setTimeout(function() {
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
118 that.emit('out', stanza);
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
119 }, 0);
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
120 });
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
121 this.transport.on('in', function(stanza) {
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
122 //FIXME: node-xmpp-bosh sends a self-closing stream:stream tag; it is wrong!
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
123 that.emit('stanza', stanza);
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
124
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
125 if (!stanza.el)
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
126 return;
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
127
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
128 var el = stanza.el;
1
96087680669f Delete base64.js since I don't care about IE support for the moment.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
129
108
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
130 //Authentication
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
131 //FIXME SASL mechanisms and XMPP features can be both in a stream:features
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
132 if (el.localName === 'features') {
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
133 var children = el.childNodes;
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
134 for (var i = 0, length = children.length; i < length; i++) {
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
135 //SASL mechanisms
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
136 if(children[i].localName === 'mechanisms') {
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
137 stanza.mechanisms = [];
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
138 var nodes = el.getElementsByTagName('mechanism');
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
139 for (var i = 0; i < nodes.length; i++)
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
140 stanza.mechanisms.push(nodes[i].textContent);
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
141 that.emit('mechanisms', stanza);
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
142 return;
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
143 }
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
144 }
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
145 //XMPP features
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
146 // else {
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
147 //TODO: stanza.features
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
148 that.emit('features', stanza);
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
149 // }
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
150 }
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
151 else if (el.localName === 'challenge') {
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
152 that.emit('challenge', stanza);
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
153 }
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
154 else if (el.localName === 'failure') {
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
155 that.emit('failure', stanza);
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
156 }
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
157 else if (el.localName === 'success') {
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
158 that.emit('success', stanza);
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
159 }
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
160
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
161 //Iq callbacks
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
162 else if (el.localName === 'iq') {
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
163 var payload = el.firstChild;
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
164 if (payload)
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
165 that.emit('iq/' + payload.namespaceURI + ':' + payload.localName, stanza);
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
166
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
167 var id = el.getAttribute('id');
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
168 if (!(id && id in that.callbacks))
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
169 return;
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
170
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
171 var type = el.getAttribute('type');
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
172 if (type !== 'result' && type !== 'error')
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
173 return; //TODO: warning
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
174
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
175 var callback = that.callbacks[id];
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
176 if (type === 'result' && callback.success)
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
177 callback.success.call(that, stanza);
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
178 else if (type === 'error' && callback.error)
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
179 callback.error.call(that, stanza);
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
180
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
181 delete that.callbacks[id];
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
182 }
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
183
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
184 else if (el.localName === 'presence' || el.localName === 'message') {
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
185 that.emit(name, stanza);
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
186 }
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
187 });
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
188 };
67
1c8f326fe3ef Several fixes and comments updated/added.
Sonny Piers <sonny.piers@gmail.com>
parents: 65
diff changeset
189 /**
108
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
190 * @function Create and open a websocket then go though the XMPP authentification process.
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
191 * @param {String} [aJid] The JID (Jabber id) to use.
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
192 * @param {String} [aPassword] The associated password.
67
1c8f326fe3ef Several fixes and comments updated/added.
Sonny Piers <sonny.piers@gmail.com>
parents: 65
diff changeset
193 */
108
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
194 Lightstring.Connection.prototype.connect = function(aJid, aPassword) {
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
195 this.emit('connecting');
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
196 this.jid = new Lightstring.JID(aJid);
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
197 if (aPassword)
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
198 this.password = aPassword;
97
11e38a9bfe38 multi-transport is now possible
Sonny Piers <sonny.piers@gmail.com>
parents: 92
diff changeset
199
108
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
200 if (!this.jid.bare)
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
201 return; //TODO: error
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
202 if (!this.service)
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
203 return; //TODO: error
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
204
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
205 function getProtocol(aURL) {
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
206 var a = document.createElement('a');
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
207 a.href = aURL;
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
208 return a.protocol.replace(':', '');
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
209 }
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
210 var protocol = getProtocol(this.service);
63
20da4fb67977 Auth PLAIN as plugin. Several fixes.
Sonny Piers <sonny.piers@gmail.com>
parents: 62
diff changeset
211
108
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
212 if (protocol.match('http'))
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
213 this.transport = new Lightstring.BOSHTransport(this.service, this.jid);
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
214 else if (protocol.match('ws'))
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
215 this.transport = new Lightstring.WebSocketTransport(this.service, this.jid);
43
136df1708856 Better iq callbacks. (breaks everything)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 42
diff changeset
216
108
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
217 this.onTransportLoaded();
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
218 };
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
219 /**
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
220 * @function Send a message.
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
221 * @param {String|Object} aStanza The message to send.
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
222 * @param {Function} [aCallback] Executed on answer. (stanza must be iq)
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
223 */
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
224 Lightstring.Connection.prototype.send = function(aStanza, aOnSuccess, aOnError) {
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
225 if (!(aStanza instanceof Lightstring.Stanza))
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
226 var stanza = new Lightstring.Stanza(aStanza);
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
227 else
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
228 var stanza = aStanza;
72
5dbf93cef55d Various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 71
diff changeset
229
108
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
230 if (!stanza)
29
1e6d2ca2daae Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents: 28
diff changeset
231 return;
1e6d2ca2daae Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents: 28
diff changeset
232
108
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
233 if (stanza.name === 'iq') {
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
234 var type = stanza.type;
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
235 if (type !== 'get' || type !== 'set')
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
236 ; //TODO: error
42
ee874d064650 Check iq sending.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 41
diff changeset
237
108
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
238 var callback = {success: aOnSuccess, error: aOnError};
99
f14558915187 bosh support
Sonny Piers <sonny.piers@gmail.com>
parents: 97
diff changeset
239
108
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
240 var id = stanza.id;
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
241 if (!id)
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
242 stanza.id = Lightstring.id();
99
f14558915187 bosh support
Sonny Piers <sonny.piers@gmail.com>
parents: 97
diff changeset
243
108
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
244 this.callbacks[stanza.id] = callback;
78
4d0fc1c9ec88 Call iq-callbacks success/error with the right context.
Sonny Piers <sonny.piers@gmail.com>
parents: 77
diff changeset
245 }
108
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
246 else if (aOnSuccess || aOnError)
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
247 ; //TODO: warning (no callback without iq)
99
f14558915187 bosh support
Sonny Piers <sonny.piers@gmail.com>
parents: 97
diff changeset
248
108
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
249 this.transport.send(stanza.toString());
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
250 };
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
251 /**
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
252 * @function Closes the XMPP stream and the socket.
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
253 */
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
254 Lightstring.Connection.prototype.disconnect = function() {
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
255 this.emit('disconnecting');
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
256 var stream = Lightstring.stanzas.stream.close();
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
257 this.transport.send(stream);
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
258 this.emit('out', stream);
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
259 this.transport.close();
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
260 };
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
261 Lightstring.Connection.prototype.load = function() {
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
262 for (var i = 0; i < arguments.length; i++) {
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
263 var name = arguments[i];
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
264 if (!(name in Lightstring.plugins))
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
265 continue; //TODO: error
99
f14558915187 bosh support
Sonny Piers <sonny.piers@gmail.com>
parents: 97
diff changeset
266
108
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
267 var plugin = Lightstring.plugins[name];
17
b7bd814333eb Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents: 16
diff changeset
268
108
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
269 //Namespaces
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
270 for (var ns in plugin.namespaces)
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
271 Lightstring.ns[ns] = plugin.namespaces[ns];
54
0b7dd59e264a Fix Connection.load and add the init method to plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 51
diff changeset
272
108
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
273 //Stanzas
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
274 Lightstring.stanzas[name] = {};
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
275 for (var stanza in plugin.stanzas)
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
276 Lightstring.stanzas[name][stanza] = plugin.stanzas[stanza];
17
b7bd814333eb Move methods to the prototype.
Sonny Piers <sonny.piers@gmail.com>
parents: 16
diff changeset
277
108
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
278 //Handlers
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
279 for (var handler in plugin.handlers)
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
280 this.on(handler, plugin.handlers[handler]);
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
281
108
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
282 //Methods
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
283 this[name] = {};
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
284 for (var method in plugin.methods)
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
285 this[name][method] = plugin.methods[method].bind(this);
72
5dbf93cef55d Various fixes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 71
diff changeset
286
108
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
287 if (plugin.init)
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
288 plugin.init.apply(this);
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
289 }
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
290 };
5cb4733c5189 many api changes
Sonny Piers <sonny@fastmail.net>
parents: 106
diff changeset
291 })();