comparison script2.js @ 5:03ef53b969bd

Add XMPP support.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Tue, 31 Jan 2012 01:09:41 +0100
parents
children 24aa8dccb170
comparison
equal deleted inserted replaced
4:95c6fa5f4715 5:03ef53b969bd
1 'use strict';
2
3 /** Copyright (c) 2012 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to
7 * deal in the Software without restriction, including without limitation the
8 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
9 * sell copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 var conn = new Lightstring.Connection(SERVICE);
25 var roster = {};
26 var documents = {};
27
28 Lightstring.NS.sxe = 'urn:xmpp:sxe:0';
29 Lightstring.NS.jingle = {
30 main: 'urn:xmpp:jingle:1',
31 transports: {
32 sxe: 'urn:xmpp:jingle:transports:sxe'
33 },
34 apps: {
35 xhtml: 'urn:xmpp:jingle:apps:xhtml'
36 }
37 };
38
39 conn.on('iq/' + Lightstring.NS['disco#info'] + ':query', function(stanza) {
40 if (stanza.DOM.getAttributeNS(null, 'type') !== 'get')
41 return;
42
43 var query = stanza.DOM.firstChild;
44 if (query.getAttributeNS(null, 'node')) {
45 var response = "<iq to='" + stanza.DOM.getAttributeNS(null, 'from') + "'" +
46 " id='" + stanza.DOM.getAttributeNS(null, 'id') + "'" +
47 " type='error'/>"; //TODO: precise the error.
48 conn.send(response);
49 return;
50 }
51
52 var features = [Lightstring.NS.sxe, Lightstring.NS.jingle.transports.sxe]; //TODO: put that elsewhere.
53
54 var response = "<iq to='" + stanza.DOM.getAttributeNS(null, 'from') + "'" +
55 " id='" + stanza.DOM.getAttributeNS(null, 'id') + "'" +
56 " type='result'>" +
57 "<query xmlns='" + Lightstring.NS['disco#info'] + "'>" +
58 "<identity category='client' type='browser'/>";
59 features.forEach(function(f) {
60 response += "<feature var='" + f + "'/>";
61 });
62 response += "</query>" +
63 "</iq>";
64
65 conn.send(response);
66 });
67
68 conn.on('presence', function(stanza) {
69 var from = new Lightstring.JID(stanza.DOM.getAttributeNS(null, 'from'));
70 if (!from.equals(conn.jid)) {
71 var type = stanza.DOM.getAttributeNS(null, 'type');
72 if (!type)
73 Lightstring.discoInfo(conn, from, undefined, function(aData) {
74 roster[from.full] = aData;
75 });
76 else if (type === 'unavailable')
77 delete roster[from.full];
78 }
79 });
80
81 var host = function(sid, name) {
82 if (!sid)
83 sid = String(Math.random());
84
85 documents[sid] = new Document(conn.jid.full, name, conn.jid.full, 'document');
86
87 for (var jid in roster) {
88 var contact = roster[jid];
89 if ((contact.features.indexOf(Lightstring.NS.sxe) !== -1) && (contact.features.indexOf(Lightstring.NS.jingle.transports.sxe) !== -1))
90 initiate(jid, sid);
91 }
92 }
93
94 var initiate = function(jid, sid) {
95 var initiate = "<iq to='" + jid + "' type='set'>" +
96 "<jingle xmlns='" + Lightstring.NS.jingle.main + "'" +
97 " action='session-initiate'" +
98 " initiator='" + documents[sid].initiator + "'" +
99 " sid='" + sid + "'>" +
100 "<content creator='initiator' name='" + documents[sid].name + "'>" +
101 "<description xmlns='" + Lightstring.NS.jingle.apps.xhtml + "'/>" +
102 "<transport xmlns='" + Lightstring.NS.jingle.transports.sxe + "'>" +
103 "<host>" + documents[sid].host + "</host>" +
104 "</transport>" +
105 "</content>" +
106 "</jingle>" +
107 "</iq>";
108 conn.send(initiate);
109 };
110
111 var accept = function(sid) {
112 var accept = "<iq to='" + documents[sid].initiator + "' type='set'>" +
113 "<jingle xmlns='" + Lightstring.NS.jingle.main + "'" +
114 " action='session-accept'" +
115 " initiator='" + documents[sid].initiator + "'" +
116 " sid='" + sid + "'>" +
117 "<content creator='initiator' name='" + documents[sid].name + "'>" +
118 "<description xmlns='" + Lightstring.NS.jingle.apps.xhtml + "'/>" +
119 "<transport xmlns='" + Lightstring.NS.jingle.transports.sxe + "'>" +
120 "<host>" + documents[sid].host + "</host>" +
121 "</transport>" +
122 "</content>" +
123 "</jingle>" +
124 "</iq>";
125 conn.send(accept, function(stanza) {
126 if (stanza.DOM.getAttributeNS(null, 'type') === 'result')
127 connect(sid);
128 else
129 terminate('TODO'); //XXX
130 });
131 };
132
133 var terminate = function(sid, reason) {
134 if (!(sid in documents))
135 return console.log('BIG WARNING!!!');
136
137 var terminate = "<iq to='" + documents[sid].initiator + "' type='set'>" +
138 "<jingle xmlns='" + Lightstring.NS.jingle.main + "'" +
139 " action='session-terminate'" +
140 " host='" + documents[sid].host + "'" +
141 " initiator='" + documents[sid].initiator + "'" +
142 " sid='" + sid + "'>" +
143 "<reason>" +
144 "<" + reason + "/>" +
145 "</reason>" +
146 "</jingle>" +
147 "</iq>";
148 delete documents[sid];
149 conn.send(terminate);
150 };
151
152 conn.on('iq/' + Lightstring.NS.jingle.main + ':jingle', function(stanza) {
153 conn.send("<iq to='" + stanza.DOM.getAttributeNS(null, 'from') + "'" +
154 " id='" + stanza.DOM.getAttributeNS(null, 'id') + "'" +
155 " type='result'/>");
156
157 var jingle = stanza.DOM.firstChild;
158 var action = jingle.getAttributeNS(null, 'action');
159 var initiator = jingle.getAttributeNS(null, 'initiator');
160 var sid = jingle.getAttributeNS(null, 'sid');
161
162 var payload = jingle.firstChild; //TODO: what if they are multiple?
163
164 //TODO: verify that the payload is really what we want.
165 //TODO: use the reason even with content as payload.
166 if (action === 'session-initiate') {
167 var content = payload;
168 var creator = content.getAttributeNS(null, 'creator');
169 var name = content.getAttributeNS(null, 'name');
170
171 if (documents[sid])
172 return terminate(sid, 'alternative-session'); //TODO: The XEP: “and wishes to use that [previous] session instead”
173
174 var description = content.getElementsByTagName('description')[0]; //TODO: supporte multiple applications.
175 if (description.namespaceURI !== Lightstring.NS.jingle.apps.xhtml)
176 return terminate(sid, 'unsupported-applications');
177
178 var transport = content.getElementsByTagName('transport')[0]; //TODO: supporte multiple transports.
179 if (transport.namespaceURI !== Lightstring.NS.jingle.transports.sxe)
180 return terminate(sid, 'unsupported-transports');
181
182 var host = transport.textContent; //TODO: verify the presence of the host element.
183
184 documents[sid] = new Document(initiator, name, host, 'document');
185
186 if (confirm('Do you accept?'))
187 accept(sid);
188 else
189 terminate(sid, 'decline');
190
191 } else if (action === 'session-accept') {
192 var content = payload;
193 var creator = content.getAttributeNS(null, 'creator');
194 var name = content.getAttributeNS(null, 'name');
195
196 alert('Accepted! \\o/');
197
198 var description = content.getElementsByTagName('description')[0]; //TODO: supporte multiple applications.
199 if (description.namespaceURI !== Lightstring.NS.jingle.apps.xhtml)
200 return terminate(sid, 'unsupported-applications');
201
202 var transport = content.getElementsByTagName('transport')[0]; //TODO: supporte multiple transports.
203 if (transport.namespaceURI !== Lightstring.NS.jingle.transports.sxe)
204 return terminate(sid, 'unsupported-transports');
205
206 var host = transport.textContent; //TODO: verify the presence of the host element.
207
208 var doc = documents[sid];
209 if (doc.initiator !== initiator ||
210 doc.name !== name ||
211 doc.host !== host)
212 return terminate(sid, 'unknown-error'); //XXX
213
214 } else if (action === 'session-terminate') {
215 var reason = payload;
216
217 //TODO: verify they are what the should be.
218 var element = reason.firstChild;
219 var text = element.nextSibling;
220
221 alert('Terminated, reason: ' + element.localName + (text? ' (' + text.textContent + ')': ''));
222
223 delete documents[sid];
224 }
225 });
226
227 var connect = function(sid) {
228 var host = documents[sid].host;
229 var identities = roster[host].identities;
230 var type = 'chat';
231 identities.forEach(function(identity) {
232 if (identity.category === 'conference')
233 type = 'groupchat';
234 });
235 var message = "<message to='" + host + "'" +
236 " type='" + type + "'>" +
237 "<sxe xmlns='" + Lightstring.NS.sxe + "'" +
238 " id='" + Lightstring.newId('sxe') + "'" +
239 " session='" + sid + "'>" +
240 "<connect/>" +
241 "</sxe>" +
242 "</message>";
243 conn.send(message);
244 };
245
246 conn.on('message/' + Lightstring.NS.sxe + ':sxe', function(stanza) {
247 var from = stanza.DOM.getAttributeNS(null, 'from');
248 var type = 'chat'; //TODO: always?
249 var sxe = stanza.DOM.firstChild; //TODO: there can be multiple payloads.
250 var id = sxe.getAttributeNS(null, 'id');
251 var sid = sxe.getAttributeNS(null, 'session');
252
253 var doc = documents[sid];
254 if (!doc) {
255 conn.send("<message to='" + from + "' type='error'/>"); //XXX
256 return;
257 }
258
259 var payload = sxe.firstChild; //TODO: really?
260 switch (payload.localName) {
261 case 'connect':
262 var message = "<message to='" + from + "'" +
263 " type='" + type + "'>" +
264 "<sxe xmlns='" + Lightstring.NS.sxe + "'" +
265 " id='" + Lightstring.newId('sxe') + "'" +
266 " session='" + sid + "'>" +
267 "<state-offer>" +
268 "<description xmlns='" + Lightstring.NS.jingle.apps.xhtml + "'/>" +
269 "</state-offer>" +
270 "</sxe>" +
271 "</message>";
272 conn.send(message);
273 break;
274 case 'state-offer':
275 var description = payload.firstChild;
276 if (description.namespaceURI !== Lightstring.NS.jingle.apps.xhtml)
277 return terminate(sid, 'unsupported-applications');
278
279 var accept = false;
280 if (from === doc.host || from === doc.initiator)
281 accept = true;
282
283 //TODO: refuse if proposed multiple times.
284
285 var message = "<message to='" + from + "'" +
286 " type='" + type + "'>" +
287 "<sxe xmlns='" + Lightstring.NS.sxe + "'" +
288 " id='" + Lightstring.newId('sxe') + "'" +
289 " session='" + sid + "'>" +
290 "<" + (accept? 'accept': 'refuse') + "-state/>" +
291 "</sxe>" +
292 "</message>";
293 conn.send(message);
294 break;
295 case 'accept-state':
296 var message = "<message to='" + from + "'" +
297 " type='" + type + "'>" +
298 "<sxe xmlns='" + Lightstring.NS.sxe + "'" +
299 " id='" + Lightstring.newId('sxe') + "'" +
300 " session='" + sid + "'>" +
301 "<state>" +
302 "<document-begin prolog='" + doc.prolog + "'/>" +
303 //TODO: support non-empty documents.
304 "<document-end last-sender='' last-id=''/>" +
305 "</state>" +
306 "</sxe>" +
307 "</message>";
308 conn.send(message);
309 break;
310 case 'refuse-state':
311 terminate(sid, 'decline');
312 break;
313 case 'state':
314 var elements = payload.children;
315 doc.processState(from, elements);
316 break;
317 default:
318 var elements = sxe.children;
319 doc.processState(from, elements);
320 }
321 });
322
323 (function() {
324 var events = ['connected', 'conn-error', 'connecting', 'disconnected', 'disconnecting'];
325 var state = document.getElementById('state');
326 events.forEach(function(e) {
327 conn.on(e, function() {
328 state.innerHTML = e;
329 });
330 });
331 })();
332
333 conn.on('error', function(a) {
334 alert(a);
335 });
336
337 document.getElementById('host').addEventListener('click', function() {
338 host(Lightstring.newId('sess'), 'My First XHTML Document');
339 }, false);
340
341 conn.on('connected', function() {
342 conn.on('output', function(stanza) {
343 console.log('out:', stanza.DOM);
344 });
345 conn.on('input', function(stanza) {
346 console.log('in:', stanza.DOM);
347 });
348
349 Lightstring.presence(conn);
350 });
351
352 conn.connect(JID + '/' + prompt('Resource?'), PASSWORD);
353
354 register_feature_not_implemented.call(conn);