annotate script2.js @ 6:24aa8dccb170

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