Mercurial > eldonilo > barbecue
annotate script2.js @ 8:7b2ca4d5af6d
Add a resend-whole-state button and make it works.
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Thu, 02 Feb 2012 15:45:25 +0100 |
parents | 853dcbe8f06f |
children | d3b37bbb2b88 |
rev | line source |
---|---|
5 | 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 | |
7
853dcbe8f06f
Make barbecue work with latest Lightstring.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
6
diff
changeset
|
28 Lightstring.plugins['jingle'] = { |
853dcbe8f06f
Make barbecue work with latest Lightstring.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
6
diff
changeset
|
29 namespaces: { |
853dcbe8f06f
Make barbecue work with latest Lightstring.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
6
diff
changeset
|
30 jingle: 'urn:xmpp:jingle:1', |
853dcbe8f06f
Make barbecue work with latest Lightstring.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
6
diff
changeset
|
31 jingle_transports_sxe: 'urn:xmpp:jingle:transports:sxe', |
853dcbe8f06f
Make barbecue work with latest Lightstring.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
6
diff
changeset
|
32 jingle_apps_xhtml: 'urn:xmpp:jingle:apps:xhtml' |
5 | 33 } |
34 }; | |
7
853dcbe8f06f
Make barbecue work with latest Lightstring.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
6
diff
changeset
|
35 Lightstring.ns['sxe'] = 'urn:xmpp:sxe:0'; |
5 | 36 |
7
853dcbe8f06f
Make barbecue work with latest Lightstring.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
6
diff
changeset
|
37 conn.on('iq/' + Lightstring.ns['disco#info'] + ':query', function(stanza) { |
5 | 38 if (stanza.DOM.getAttributeNS(null, 'type') !== 'get') |
39 return; | |
40 | |
41 var query = stanza.DOM.firstChild; | |
42 if (query.getAttributeNS(null, 'node')) { | |
43 var response = "<iq to='" + stanza.DOM.getAttributeNS(null, 'from') + "'" + | |
44 " id='" + stanza.DOM.getAttributeNS(null, 'id') + "'" + | |
45 " type='error'/>"; //TODO: precise the error. | |
46 conn.send(response); | |
47 return; | |
48 } | |
49 | |
7
853dcbe8f06f
Make barbecue work with latest Lightstring.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
6
diff
changeset
|
50 var features = [Lightstring.ns['sxe'], Lightstring.ns['jingle_transports_sxe']]; //TODO: put that elsewhere. |
5 | 51 |
52 var response = "<iq to='" + stanza.DOM.getAttributeNS(null, 'from') + "'" + | |
53 " id='" + stanza.DOM.getAttributeNS(null, 'id') + "'" + | |
54 " type='result'>" + | |
7
853dcbe8f06f
Make barbecue work with latest Lightstring.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
6
diff
changeset
|
55 "<query xmlns='" + Lightstring.ns['disco#info'] + "'>" + |
5 | 56 "<identity category='client' type='browser'/>"; |
57 features.forEach(function(f) { | |
58 response += "<feature var='" + f + "'/>"; | |
59 }); | |
60 response += "</query>" + | |
61 "</iq>"; | |
62 | |
63 conn.send(response); | |
64 }); | |
65 | |
66 conn.on('presence', function(stanza) { | |
67 var from = new Lightstring.JID(stanza.DOM.getAttributeNS(null, 'from')); | |
68 if (!from.equals(conn.jid)) { | |
69 var type = stanza.DOM.getAttributeNS(null, 'type'); | |
70 if (!type) | |
7
853dcbe8f06f
Make barbecue work with latest Lightstring.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
6
diff
changeset
|
71 conn.disco.info(from, undefined, function(stanza) { |
853dcbe8f06f
Make barbecue work with latest Lightstring.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
6
diff
changeset
|
72 roster[from.full] = {identities: stanza.identities, features: stanza.features}; |
853dcbe8f06f
Make barbecue work with latest Lightstring.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
6
diff
changeset
|
73 }, function(stanza) { |
853dcbe8f06f
Make barbecue work with latest Lightstring.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
6
diff
changeset
|
74 console.log(from + ' doesn’t handle disco#info.'); |
853dcbe8f06f
Make barbecue work with latest Lightstring.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
6
diff
changeset
|
75 roster[from.full] = {}; |
5 | 76 }); |
77 else if (type === 'unavailable') | |
78 delete roster[from.full]; | |
79 } | |
80 }); | |
81 | |
82 var host = function(sid, name) { | |
83 if (!sid) | |
84 sid = String(Math.random()); | |
85 | |
86 documents[sid] = new Document(conn.jid.full, name, conn.jid.full, 'document'); | |
87 | |
88 for (var jid in roster) { | |
89 var contact = roster[jid]; | |
7
853dcbe8f06f
Make barbecue work with latest Lightstring.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
6
diff
changeset
|
90 if ((contact.features.indexOf(Lightstring.ns['sxe']) !== -1) && (contact.features.indexOf(Lightstring.ns['jingle_transports_sxe']) !== -1)) |
5 | 91 initiate(jid, sid); |
92 } | |
93 } | |
94 | |
95 var initiate = function(jid, sid) { | |
96 var initiate = "<iq to='" + jid + "' type='set'>" + | |
7
853dcbe8f06f
Make barbecue work with latest Lightstring.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
6
diff
changeset
|
97 "<jingle xmlns='" + Lightstring.ns.jingle.main + "'" + |
5 | 98 " action='session-initiate'" + |
99 " initiator='" + documents[sid].initiator + "'" + | |
100 " sid='" + sid + "'>" + | |
101 "<content creator='initiator' name='" + documents[sid].name + "'>" + | |
7
853dcbe8f06f
Make barbecue work with latest Lightstring.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
6
diff
changeset
|
102 "<description xmlns='" + Lightstring.ns['jingle_apps_xhtml'] + "'/>" + |
853dcbe8f06f
Make barbecue work with latest Lightstring.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
6
diff
changeset
|
103 "<transport xmlns='" + Lightstring.ns['jingle_transports_sxe'] + "'>" + |
5 | 104 "<host>" + documents[sid].host + "</host>" + |
105 "</transport>" + | |
106 "</content>" + | |
107 "</jingle>" + | |
108 "</iq>"; | |
109 conn.send(initiate); | |
110 }; | |
111 | |
112 var accept = function(sid) { | |
6
24aa8dccb170
Make XMPP actually work.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
5
diff
changeset
|
113 var doc = documents[sid]; |
24aa8dccb170
Make XMPP actually work.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
5
diff
changeset
|
114 var accept = "<iq to='" + doc.initiator + "' type='set'>" + |
7
853dcbe8f06f
Make barbecue work with latest Lightstring.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
6
diff
changeset
|
115 "<jingle xmlns='" + Lightstring.ns.jingle.main + "'" + |
5 | 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 | 118 " sid='" + sid + "'>" + |
119 "<content creator='initiator' name='" + documents[sid].name + "'>" + | |
7
853dcbe8f06f
Make barbecue work with latest Lightstring.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
6
diff
changeset
|
120 "<description xmlns='" + Lightstring.ns['jingle_apps_xhtml'] + "'/>" + |
853dcbe8f06f
Make barbecue work with latest Lightstring.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
6
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 | 123 "</transport>" + |
124 "</content>" + | |
125 "</jingle>" + | |
126 "</iq>"; | |
127 conn.send(accept, function(stanza) { | |
128 if (stanza.DOM.getAttributeNS(null, 'type') === 'result') | |
129 connect(sid); | |
130 else | |
131 terminate('TODO'); //XXX | |
132 }); | |
133 }; | |
134 | |
135 var terminate = function(sid, reason) { | |
136 if (!(sid in documents)) | |
137 return console.log('BIG WARNING!!!'); | |
138 | |
139 var terminate = "<iq to='" + documents[sid].initiator + "' type='set'>" + | |
7
853dcbe8f06f
Make barbecue work with latest Lightstring.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
6
diff
changeset
|
140 "<jingle xmlns='" + Lightstring.ns.jingle.main + "'" + |
5 | 141 " action='session-terminate'" + |
142 " host='" + documents[sid].host + "'" + | |
143 " initiator='" + documents[sid].initiator + "'" + | |
144 " sid='" + sid + "'>" + | |
145 "<reason>" + | |
146 "<" + reason + "/>" + | |
147 "</reason>" + | |
148 "</jingle>" + | |
149 "</iq>"; | |
150 delete documents[sid]; | |
151 conn.send(terminate); | |
152 }; | |
153 | |
7
853dcbe8f06f
Make barbecue work with latest Lightstring.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
6
diff
changeset
|
154 conn.on('iq/' + Lightstring.ns.jingle + ':jingle', function(stanza) { |
5 | 155 conn.send("<iq to='" + stanza.DOM.getAttributeNS(null, 'from') + "'" + |
156 " id='" + stanza.DOM.getAttributeNS(null, 'id') + "'" + | |
157 " type='result'/>"); | |
158 | |
159 var jingle = stanza.DOM.firstChild; | |
160 var action = jingle.getAttributeNS(null, 'action'); | |
161 var initiator = jingle.getAttributeNS(null, 'initiator'); | |
162 var sid = jingle.getAttributeNS(null, 'sid'); | |
163 | |
164 var payload = jingle.firstChild; //TODO: what if they are multiple? | |
165 | |
166 //TODO: verify that the payload is really what we want. | |
167 //TODO: use the reason even with content as payload. | |
168 if (action === 'session-initiate') { | |
169 var content = payload; | |
170 var creator = content.getAttributeNS(null, 'creator'); | |
171 var name = content.getAttributeNS(null, 'name'); | |
172 | |
173 if (documents[sid]) | |
174 return terminate(sid, 'alternative-session'); //TODO: The XEP: “and wishes to use that [previous] session instead” | |
175 | |
176 var description = content.getElementsByTagName('description')[0]; //TODO: supporte multiple applications. | |
7
853dcbe8f06f
Make barbecue work with latest Lightstring.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
6
diff
changeset
|
177 if (description.namespaceURI !== Lightstring.ns['jingle_apps_xhtml']) |
5 | 178 return terminate(sid, 'unsupported-applications'); |
179 | |
180 var transport = content.getElementsByTagName('transport')[0]; //TODO: supporte multiple transports. | |
7
853dcbe8f06f
Make barbecue work with latest Lightstring.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
6
diff
changeset
|
181 if (transport.namespaceURI !== Lightstring.ns['jingle_transports_sxe']) |
5 | 182 return terminate(sid, 'unsupported-transports'); |
183 | |
184 var host = transport.textContent; //TODO: verify the presence of the host element. | |
185 | |
186 documents[sid] = new Document(initiator, name, host, 'document'); | |
187 | |
188 if (confirm('Do you accept?')) | |
189 accept(sid); | |
190 else | |
191 terminate(sid, 'decline'); | |
192 | |
193 } else if (action === 'session-accept') { | |
194 var content = payload; | |
195 var creator = content.getAttributeNS(null, 'creator'); | |
196 var name = content.getAttributeNS(null, 'name'); | |
197 | |
198 alert('Accepted! \\o/'); | |
199 | |
200 var description = content.getElementsByTagName('description')[0]; //TODO: supporte multiple applications. | |
7
853dcbe8f06f
Make barbecue work with latest Lightstring.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
6
diff
changeset
|
201 if (description.namespaceURI !== Lightstring.ns['jingle_apps_xhtml']) |
5 | 202 return terminate(sid, 'unsupported-applications'); |
203 | |
204 var transport = content.getElementsByTagName('transport')[0]; //TODO: supporte multiple transports. | |
7
853dcbe8f06f
Make barbecue work with latest Lightstring.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
6
diff
changeset
|
205 if (transport.namespaceURI !== Lightstring.ns['jingle_transports_sxe']) |
5 | 206 return terminate(sid, 'unsupported-transports'); |
207 | |
208 var host = transport.textContent; //TODO: verify the presence of the host element. | |
209 | |
210 var doc = documents[sid]; | |
211 if (doc.initiator !== initiator || | |
212 doc.name !== name || | |
213 doc.host !== host) | |
214 return terminate(sid, 'unknown-error'); //XXX | |
215 | |
216 } else if (action === 'session-terminate') { | |
217 var reason = payload; | |
218 | |
219 //TODO: verify they are what the should be. | |
220 var element = reason.firstChild; | |
221 var text = element.nextSibling; | |
222 | |
223 alert('Terminated, reason: ' + element.localName + (text? ' (' + text.textContent + ')': '')); | |
224 | |
225 delete documents[sid]; | |
226 } | |
227 }); | |
228 | |
229 var connect = function(sid) { | |
8
7b2ca4d5af6d
Add a resend-whole-state button and make it works.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
230 var doc = documents[sid]; |
7b2ca4d5af6d
Add a resend-whole-state button and make it works.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
231 var host = doc.host; |
5 | 232 var identities = roster[host].identities; |
233 var type = 'chat'; | |
234 identities.forEach(function(identity) { | |
235 if (identity.category === 'conference') | |
236 type = 'groupchat'; | |
237 }); | |
238 var message = "<message to='" + host + "'" + | |
239 " type='" + type + "'>" + | |
7
853dcbe8f06f
Make barbecue work with latest Lightstring.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
6
diff
changeset
|
240 "<sxe xmlns='" + Lightstring.ns['sxe'] + "'" + |
5 | 241 " id='" + Lightstring.newId('sxe') + "'" + |
242 " session='" + sid + "'>" + | |
243 "<connect/>" + | |
244 "</sxe>" + | |
245 "</message>"; | |
246 conn.send(message); | |
8
7b2ca4d5af6d
Add a resend-whole-state button and make it works.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
247 doc.participants[conn.jid.full] = type; |
7b2ca4d5af6d
Add a resend-whole-state button and make it works.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
248 }; |
7b2ca4d5af6d
Add a resend-whole-state button and make it works.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
249 |
7b2ca4d5af6d
Add a resend-whole-state button and make it works.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
250 var offerState = function(to, type, sid) { |
7b2ca4d5af6d
Add a resend-whole-state button and make it works.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
251 var message = "<message to='" + to + "'" + |
7b2ca4d5af6d
Add a resend-whole-state button and make it works.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
252 " type='" + type + "'>" + |
7b2ca4d5af6d
Add a resend-whole-state button and make it works.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
253 "<sxe xmlns='" + Lightstring.ns['sxe'] + "'" + |
7b2ca4d5af6d
Add a resend-whole-state button and make it works.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
254 " id='" + Lightstring.newId('sxe') + "'" + |
7b2ca4d5af6d
Add a resend-whole-state button and make it works.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
255 " session='" + sid + "'>" + |
7b2ca4d5af6d
Add a resend-whole-state button and make it works.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
256 "<state-offer>" + |
7b2ca4d5af6d
Add a resend-whole-state button and make it works.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
257 "<description xmlns='" + Lightstring.ns['jingle_apps_xhtml'] + "'/>" + |
7b2ca4d5af6d
Add a resend-whole-state button and make it works.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
258 "</state-offer>" + |
7b2ca4d5af6d
Add a resend-whole-state button and make it works.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
259 "</sxe>" + |
7b2ca4d5af6d
Add a resend-whole-state button and make it works.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
260 "</message>"; |
7b2ca4d5af6d
Add a resend-whole-state button and make it works.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
261 conn.send(message); |
7b2ca4d5af6d
Add a resend-whole-state button and make it works.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
262 }; |
7b2ca4d5af6d
Add a resend-whole-state button and make it works.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
263 |
7b2ca4d5af6d
Add a resend-whole-state button and make it works.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
264 var sendState = function(sid) { |
7b2ca4d5af6d
Add a resend-whole-state button and make it works.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
265 var doc = documents[sid]; |
7b2ca4d5af6d
Add a resend-whole-state button and make it works.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
266 var initialState = doc.createState([]).map(function(element) { |
7b2ca4d5af6d
Add a resend-whole-state button and make it works.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
267 return Lightstring.DOM2XML(element); |
7b2ca4d5af6d
Add a resend-whole-state button and make it works.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
268 }).join(''); |
7b2ca4d5af6d
Add a resend-whole-state button and make it works.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
269 if (conn.jid.full in doc.participants) { // participant |
7b2ca4d5af6d
Add a resend-whole-state button and make it works.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
270 var type = 'chat'; //XXX |
7b2ca4d5af6d
Add a resend-whole-state button and make it works.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
271 var message = "<message to='" + doc.host + "'" + |
7b2ca4d5af6d
Add a resend-whole-state button and make it works.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
272 " type='" + type + "'>" + |
7b2ca4d5af6d
Add a resend-whole-state button and make it works.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
273 "<sxe xmlns='" + Lightstring.ns['sxe'] + "'" + |
7b2ca4d5af6d
Add a resend-whole-state button and make it works.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
274 " id='" + Lightstring.newId('sxe') + "'" + |
7b2ca4d5af6d
Add a resend-whole-state button and make it works.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
275 " session='" + sid + "'>" + |
7b2ca4d5af6d
Add a resend-whole-state button and make it works.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
276 "<state>" + |
7b2ca4d5af6d
Add a resend-whole-state button and make it works.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
277 "<document-begin prolog='" + doc.prolog + "'/>" + |
7b2ca4d5af6d
Add a resend-whole-state button and make it works.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
278 initialState + |
7b2ca4d5af6d
Add a resend-whole-state button and make it works.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
279 "<document-end last-sender='' last-id=''/>" + |
7b2ca4d5af6d
Add a resend-whole-state button and make it works.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
280 "</state>" + |
7b2ca4d5af6d
Add a resend-whole-state button and make it works.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
281 "</sxe>" + |
7b2ca4d5af6d
Add a resend-whole-state button and make it works.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
282 "</message>"; |
7b2ca4d5af6d
Add a resend-whole-state button and make it works.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
283 conn.send(message); |
7b2ca4d5af6d
Add a resend-whole-state button and make it works.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
284 } else { // host |
7b2ca4d5af6d
Add a resend-whole-state button and make it works.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
285 for (var jid in doc.participants) { |
7b2ca4d5af6d
Add a resend-whole-state button and make it works.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
286 var type = doc.participants[jid]; |
7b2ca4d5af6d
Add a resend-whole-state button and make it works.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
287 var message = "<message to='" + jid + "'" + |
7b2ca4d5af6d
Add a resend-whole-state button and make it works.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
288 " type='" + type + "'>" + |
7b2ca4d5af6d
Add a resend-whole-state button and make it works.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
289 "<sxe xmlns='" + Lightstring.ns['sxe'] + "'" + |
7b2ca4d5af6d
Add a resend-whole-state button and make it works.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
290 " id='" + Lightstring.newId('sxe') + "'" + |
7b2ca4d5af6d
Add a resend-whole-state button and make it works.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
291 " session='" + sid + "'>" + |
7b2ca4d5af6d
Add a resend-whole-state button and make it works.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
292 "<state>" + |
7b2ca4d5af6d
Add a resend-whole-state button and make it works.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
293 "<document-begin prolog='" + doc.prolog + "'/>" + |
7b2ca4d5af6d
Add a resend-whole-state button and make it works.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
294 initialState + |
7b2ca4d5af6d
Add a resend-whole-state button and make it works.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
295 "<document-end last-sender='' last-id=''/>" + |
7b2ca4d5af6d
Add a resend-whole-state button and make it works.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
296 "</state>" + |
7b2ca4d5af6d
Add a resend-whole-state button and make it works.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
297 "</sxe>" + |
7b2ca4d5af6d
Add a resend-whole-state button and make it works.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
298 "</message>"; |
7b2ca4d5af6d
Add a resend-whole-state button and make it works.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
299 conn.send(message); |
7b2ca4d5af6d
Add a resend-whole-state button and make it works.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
300 } |
7b2ca4d5af6d
Add a resend-whole-state button and make it works.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
301 } |
5 | 302 }; |
303 | |
7
853dcbe8f06f
Make barbecue work with latest Lightstring.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
6
diff
changeset
|
304 conn.on('message/' + Lightstring.ns['sxe'] + ':sxe', function(stanza) { |
5 | 305 var from = stanza.DOM.getAttributeNS(null, 'from'); |
306 var type = 'chat'; //TODO: always? | |
307 var sxe = stanza.DOM.firstChild; //TODO: there can be multiple payloads. | |
308 var id = sxe.getAttributeNS(null, 'id'); | |
309 var sid = sxe.getAttributeNS(null, 'session'); | |
310 | |
311 var doc = documents[sid]; | |
312 if (!doc) { | |
313 conn.send("<message to='" + from + "' type='error'/>"); //XXX | |
314 return; | |
315 } | |
316 | |
317 var payload = sxe.firstChild; //TODO: really? | |
318 switch (payload.localName) { | |
319 case 'connect': | |
8
7b2ca4d5af6d
Add a resend-whole-state button and make it works.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
320 offerState(from, type, sid); |
7b2ca4d5af6d
Add a resend-whole-state button and make it works.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
321 doc.participants[from] = type; |
5 | 322 break; |
323 case 'state-offer': | |
324 var description = payload.firstChild; | |
7
853dcbe8f06f
Make barbecue work with latest Lightstring.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
6
diff
changeset
|
325 if (description.namespaceURI !== Lightstring.ns['jingle_apps_xhtml']) |
5 | 326 return terminate(sid, 'unsupported-applications'); |
327 | |
8
7b2ca4d5af6d
Add a resend-whole-state button and make it works.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
328 var accept = /*false; |
5 | 329 if (from === doc.host || from === doc.initiator) |
8
7b2ca4d5af6d
Add a resend-whole-state button and make it works.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
330 accept =*/ true; |
5 | 331 |
332 //TODO: refuse if proposed multiple times. | |
333 | |
334 var message = "<message to='" + from + "'" + | |
335 " type='" + type + "'>" + | |
7
853dcbe8f06f
Make barbecue work with latest Lightstring.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
6
diff
changeset
|
336 "<sxe xmlns='" + Lightstring.ns['sxe'] + "'" + |
5 | 337 " id='" + Lightstring.newId('sxe') + "'" + |
338 " session='" + sid + "'>" + | |
339 "<" + (accept? 'accept': 'refuse') + "-state/>" + | |
340 "</sxe>" + | |
341 "</message>"; | |
342 conn.send(message); | |
343 break; | |
344 case 'accept-state': | |
8
7b2ca4d5af6d
Add a resend-whole-state button and make it works.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
345 sendState(sid); |
5 | 346 break; |
347 case 'refuse-state': | |
348 terminate(sid, 'decline'); | |
349 break; | |
350 case 'state': | |
351 var elements = payload.children; | |
8
7b2ca4d5af6d
Add a resend-whole-state button and make it works.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
352 doc.empty(); |
5 | 353 doc.processState(from, elements); |
354 break; | |
355 default: | |
356 var elements = sxe.children; | |
357 doc.processState(from, elements); | |
358 } | |
359 }); | |
360 | |
361 (function() { | |
362 var events = ['connected', 'conn-error', 'connecting', 'disconnected', 'disconnecting']; | |
363 var state = document.getElementById('state'); | |
364 events.forEach(function(e) { | |
365 conn.on(e, function() { | |
366 state.innerHTML = e; | |
7
853dcbe8f06f
Make barbecue work with latest Lightstring.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
6
diff
changeset
|
367 return false; |
5 | 368 }); |
369 }); | |
370 })(); | |
371 | |
372 conn.on('error', function(a) { | |
373 alert(a); | |
374 }); | |
375 | |
376 document.getElementById('host').addEventListener('click', function() { | |
377 host(Lightstring.newId('sess'), 'My First XHTML Document'); | |
378 }, false); | |
379 | |
8
7b2ca4d5af6d
Add a resend-whole-state button and make it works.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
380 document.getElementById('resend-state').addEventListener('click', function() { |
7b2ca4d5af6d
Add a resend-whole-state button and make it works.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
381 for (var sid in documents) |
7b2ca4d5af6d
Add a resend-whole-state button and make it works.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
382 sendState(sid); |
7b2ca4d5af6d
Add a resend-whole-state button and make it works.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
383 }, false); |
7b2ca4d5af6d
Add a resend-whole-state button and make it works.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
384 |
7
853dcbe8f06f
Make barbecue work with latest Lightstring.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
6
diff
changeset
|
385 conn.on('output', function(stanza) { |
853dcbe8f06f
Make barbecue work with latest Lightstring.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
6
diff
changeset
|
386 console.log('out:', stanza.DOM || stanza.XML); |
853dcbe8f06f
Make barbecue work with latest Lightstring.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
6
diff
changeset
|
387 }); |
853dcbe8f06f
Make barbecue work with latest Lightstring.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
6
diff
changeset
|
388 conn.on('input', function(stanza) { |
853dcbe8f06f
Make barbecue work with latest Lightstring.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
6
diff
changeset
|
389 console.log('in:', stanza.DOM || stanza.XML); |
5 | 390 }); |
391 | |
7
853dcbe8f06f
Make barbecue work with latest Lightstring.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
6
diff
changeset
|
392 conn.on('connected', function() { |
853dcbe8f06f
Make barbecue work with latest Lightstring.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
6
diff
changeset
|
393 conn.presence.send(conn); |
853dcbe8f06f
Make barbecue work with latest Lightstring.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
6
diff
changeset
|
394 return true; |
853dcbe8f06f
Make barbecue work with latest Lightstring.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
6
diff
changeset
|
395 }); |
5 | 396 |
7
853dcbe8f06f
Make barbecue work with latest Lightstring.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
6
diff
changeset
|
397 conn.load('DIGEST-MD5'); |
853dcbe8f06f
Make barbecue work with latest Lightstring.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
6
diff
changeset
|
398 conn.load('presence'); |
853dcbe8f06f
Make barbecue work with latest Lightstring.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
6
diff
changeset
|
399 conn.load('disco'); |
853dcbe8f06f
Make barbecue work with latest Lightstring.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
6
diff
changeset
|
400 conn.load('jingle'); |
853dcbe8f06f
Make barbecue work with latest Lightstring.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
6
diff
changeset
|
401 conn.disco.addFeatures(Lightstring.ns['jingle_transports_sxe'], Lightstring.ns['sxe']); |
853dcbe8f06f
Make barbecue work with latest Lightstring.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
6
diff
changeset
|
402 conn.connect(JID + '/' + prompt('Resource?'), PASSWORD); |