Mercurial > eldonilo > barbecue
annotate script2.js @ 9:d3b37bbb2b88 default tip master
Support multiple participants.
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Thu, 02 Feb 2012 16:37:28 +0100 |
parents | 7b2ca4d5af6d |
children |
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 |
9
d3b37bbb2b88
Support multiple participants.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
8
diff
changeset
|
264 var sendState = function(sid, from) { |
8
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) { |
9
d3b37bbb2b88
Support multiple participants.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
8
diff
changeset
|
286 if (from == jid) |
d3b37bbb2b88
Support multiple participants.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
8
diff
changeset
|
287 continue; |
8
7b2ca4d5af6d
Add a resend-whole-state button and make it works.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
288 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
|
289 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
|
290 " type='" + type + "'>" + |
7b2ca4d5af6d
Add a resend-whole-state button and make it works.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
291 "<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
|
292 " 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
|
293 " session='" + sid + "'>" + |
7b2ca4d5af6d
Add a resend-whole-state button and make it works.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
294 "<state>" + |
7b2ca4d5af6d
Add a resend-whole-state button and make it works.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
295 "<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
|
296 initialState + |
7b2ca4d5af6d
Add a resend-whole-state button and make it works.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
297 "<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
|
298 "</state>" + |
7b2ca4d5af6d
Add a resend-whole-state button and make it works.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
299 "</sxe>" + |
7b2ca4d5af6d
Add a resend-whole-state button and make it works.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
300 "</message>"; |
7b2ca4d5af6d
Add a resend-whole-state button and make it works.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
301 conn.send(message); |
7b2ca4d5af6d
Add a resend-whole-state button and make it works.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
302 } |
7b2ca4d5af6d
Add a resend-whole-state button and make it works.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
303 } |
5 | 304 }; |
305 | |
7
853dcbe8f06f
Make barbecue work with latest Lightstring.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
6
diff
changeset
|
306 conn.on('message/' + Lightstring.ns['sxe'] + ':sxe', function(stanza) { |
5 | 307 var from = stanza.DOM.getAttributeNS(null, 'from'); |
308 var type = 'chat'; //TODO: always? | |
309 var sxe = stanza.DOM.firstChild; //TODO: there can be multiple payloads. | |
310 var id = sxe.getAttributeNS(null, 'id'); | |
311 var sid = sxe.getAttributeNS(null, 'session'); | |
312 | |
313 var doc = documents[sid]; | |
314 if (!doc) { | |
315 conn.send("<message to='" + from + "' type='error'/>"); //XXX | |
316 return; | |
317 } | |
318 | |
319 var payload = sxe.firstChild; //TODO: really? | |
320 switch (payload.localName) { | |
321 case 'connect': | |
8
7b2ca4d5af6d
Add a resend-whole-state button and make it works.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
322 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
|
323 doc.participants[from] = type; |
5 | 324 break; |
325 case 'state-offer': | |
326 var description = payload.firstChild; | |
7
853dcbe8f06f
Make barbecue work with latest Lightstring.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
6
diff
changeset
|
327 if (description.namespaceURI !== Lightstring.ns['jingle_apps_xhtml']) |
5 | 328 return terminate(sid, 'unsupported-applications'); |
329 | |
8
7b2ca4d5af6d
Add a resend-whole-state button and make it works.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
330 var accept = /*false; |
5 | 331 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
|
332 accept =*/ true; |
5 | 333 |
334 //TODO: refuse if proposed multiple times. | |
335 | |
336 var message = "<message to='" + from + "'" + | |
337 " type='" + type + "'>" + | |
7
853dcbe8f06f
Make barbecue work with latest Lightstring.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
6
diff
changeset
|
338 "<sxe xmlns='" + Lightstring.ns['sxe'] + "'" + |
5 | 339 " id='" + Lightstring.newId('sxe') + "'" + |
340 " session='" + sid + "'>" + | |
341 "<" + (accept? 'accept': 'refuse') + "-state/>" + | |
342 "</sxe>" + | |
343 "</message>"; | |
344 conn.send(message); | |
345 break; | |
346 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
|
347 sendState(sid); |
5 | 348 break; |
349 case 'refuse-state': | |
350 terminate(sid, 'decline'); | |
351 break; | |
352 case 'state': | |
353 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
|
354 doc.empty(); |
5 | 355 doc.processState(from, elements); |
9
d3b37bbb2b88
Support multiple participants.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
8
diff
changeset
|
356 if (doc.host === conn.jid.full) |
d3b37bbb2b88
Support multiple participants.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
8
diff
changeset
|
357 sendState(sid, from); |
5 | 358 break; |
359 default: | |
360 var elements = sxe.children; | |
361 doc.processState(from, elements); | |
362 } | |
363 }); | |
364 | |
365 (function() { | |
366 var events = ['connected', 'conn-error', 'connecting', 'disconnected', 'disconnecting']; | |
367 var state = document.getElementById('state'); | |
368 events.forEach(function(e) { | |
369 conn.on(e, function() { | |
370 state.innerHTML = e; | |
7
853dcbe8f06f
Make barbecue work with latest Lightstring.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
6
diff
changeset
|
371 return false; |
5 | 372 }); |
373 }); | |
374 })(); | |
375 | |
376 conn.on('error', function(a) { | |
377 alert(a); | |
378 }); | |
379 | |
380 document.getElementById('host').addEventListener('click', function() { | |
381 host(Lightstring.newId('sess'), 'My First XHTML Document'); | |
382 }, false); | |
383 | |
8
7b2ca4d5af6d
Add a resend-whole-state button and make it works.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
384 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
|
385 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
|
386 sendState(sid); |
7b2ca4d5af6d
Add a resend-whole-state button and make it works.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
387 }, false); |
7b2ca4d5af6d
Add a resend-whole-state button and make it works.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
388 |
7
853dcbe8f06f
Make barbecue work with latest Lightstring.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
6
diff
changeset
|
389 conn.on('output', function(stanza) { |
853dcbe8f06f
Make barbecue work with latest Lightstring.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
6
diff
changeset
|
390 console.log('out:', stanza.DOM || stanza.XML); |
853dcbe8f06f
Make barbecue work with latest Lightstring.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
6
diff
changeset
|
391 }); |
853dcbe8f06f
Make barbecue work with latest Lightstring.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
6
diff
changeset
|
392 conn.on('input', function(stanza) { |
853dcbe8f06f
Make barbecue work with latest Lightstring.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
6
diff
changeset
|
393 console.log('in:', stanza.DOM || stanza.XML); |
5 | 394 }); |
395 | |
7
853dcbe8f06f
Make barbecue work with latest Lightstring.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
6
diff
changeset
|
396 conn.on('connected', function() { |
853dcbe8f06f
Make barbecue work with latest Lightstring.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
6
diff
changeset
|
397 conn.presence.send(conn); |
853dcbe8f06f
Make barbecue work with latest Lightstring.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
6
diff
changeset
|
398 return true; |
853dcbe8f06f
Make barbecue work with latest Lightstring.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
6
diff
changeset
|
399 }); |
5 | 400 |
7
853dcbe8f06f
Make barbecue work with latest Lightstring.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
6
diff
changeset
|
401 conn.load('DIGEST-MD5'); |
853dcbe8f06f
Make barbecue work with latest Lightstring.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
6
diff
changeset
|
402 conn.load('presence'); |
853dcbe8f06f
Make barbecue work with latest Lightstring.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
6
diff
changeset
|
403 conn.load('disco'); |
853dcbe8f06f
Make barbecue work with latest Lightstring.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
6
diff
changeset
|
404 conn.load('jingle'); |
853dcbe8f06f
Make barbecue work with latest Lightstring.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
6
diff
changeset
|
405 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
|
406 conn.connect(JID + '/' + prompt('Resource?'), PASSWORD); |