comparison blog.js @ 0:f62b5c395a48

Initial commit.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Sat, 04 Jun 2011 05:02:47 +0200
parents
children 82905edac9d8
comparison
equal deleted inserted replaced
-1:000000000000 0:f62b5c395a48
1 //'use strict';
2
3 const BOSH_SERVICE = 'http://linkmauve.fr/http-bind/';
4 var conn = null;
5 var jid = 'blog@linkmauve.fr'; // FIXME: Strophe should accept anonymous connections.
6 var password = 'blog';
7 var service = 'psgxs.linkmauve.fr';
8 var node = 'blog';
9 var re = true;
10
11 var params = (function() {
12 var vars = {};
13 var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split(';');
14
15 for(var i = 0; i < hashes.length; i++) {
16 var s = hashes[i].indexOf('=');
17 var key = hashes[i].substring(0, s);
18 var value = hashes[i].substring(s+1);
19 vars[key] = value;
20 }
21
22 return vars;
23 })();
24
25 if (params.jid)
26 service = params.jid;
27
28 if (params.node)
29 node = params.node;
30
31 var logs = document.getElementById('log');
32 if (params.debug)
33 logs.parentNode.hidden = false;
34
35 var received = {};
36 var messages = document.getElementById('messages');
37
38 function log(msg, error) {
39 var p = document.createElementNS(ns.xhtml, 'p');
40 p.appendChild(document.createTextNode(msg));
41
42 if (!error)
43 p.setAttributeNS(null, 'class', 'error');
44
45 logs.appendChild(p);
46 }
47
48 function rawInput(data) {
49 log('RECV: ' + data, 1);
50 }
51
52 function rawOutput(data) {
53 log('SENT: ' + data, 1);
54 }
55
56 var html = function(name, id) {
57 return received[name][id].html;
58 }
59
60 var date = function(name, id) {
61 return received[name][id].date;
62 }
63
64 var updateMessage = function(name, id) {
65 var divs = messages.getElementsByTagNameNS(ns.xhtml, 'div');
66 var container = null;
67
68 for (var i in divs) {
69 var div = divs[i]
70 if (typeof div != 'object')
71 continue;
72
73 if (div.getAttributeNS(ns.idq, 'jid') === name) {
74 container = div;
75 break;
76 }
77 }
78
79 if (!container) {
80 var container = document.createElementNS(ns.xhtml, 'div');
81 container.setAttributeNS(ns.idq, 'jid', name);
82 messages.appendChild(container);
83 }
84
85 var articles = container.getElementsByTagNameNS(ns.xhtml, 'article');
86 for (var i in articles) {
87 var article = articles[i];
88 if (typeof article != 'object')
89 continue;
90
91 if (article.getAttributeNS(ns.idq, 'id') === id) {
92 container.replaceChild(html(name, id), article);
93 return;
94 }
95 }
96
97 var article = html(name, id);
98
99 if (!container.firstChild)
100 container.appendChild(article);
101 else {
102 var d = date(name, id);
103 var toInsert;
104 for (var i in articles) {
105 var a = articles[i];
106 if (typeof a != 'object')
107 continue;
108
109 var ad = new Date();
110 ad.set8601(a.getAttributeNS(ns.idq, 'date'));
111
112 if (ad < d) {
113 toInsert = a;
114 break;
115 }
116 }
117
118 if (toInsert)
119 container.insertBefore(article, toInsert);
120 else
121 container.appendChild(article);
122 }
123 }
124
125 var convert = function(id, xml) {
126 var ns = xml['@xmlns'];
127 if (ns in parsers)
128 return new parsers[ns](id, xml);
129 return new parsers[''](id, xml);
130 };
131
132 var parsePubSubEvent = function(stanza) {
133 var e = {};
134
135 e.service = stanza.getAttribute('from');
136
137 var pubsub = stanza.getChild('event', ns.pse);
138 if (!pubsub) {
139 pubsub = stanza.getChild('pubsub', ns.ps);
140 if (!pubsub)
141 return;
142 }
143 e.ns = pubsub.getAttribute('xmlns');
144
145 var items = pubsub.getChild('items', e.ns);
146 if (!items)
147 return;
148
149 e.node = items.getAttribute('node');
150 items = items.getChildren('item', e.ns);
151 if (!items)
152 return;
153
154 e.name = e.service + '/' + e.node;
155
156 e.items = {};
157 for (var i in items) {
158 var item = items[i];
159 if (!item.getAttribute)
160 continue;
161
162 var pl = item.getChild();
163 if (!pl)
164 continue;
165
166 var id = item.getAttribute('id');
167
168 e.items[id] = pl;
169 }
170
171 return e;
172 }
173
174 var onMessages = function(stanza) {
175 conn.addHandler(onMessages, null, 'message', null, null, null);
176
177 log('message');
178 stanza = xml2json(stanza);
179 var e = parsePubSubEvent(stanza);
180
181 if (!received[e.name])
182 received[e.name] = {};
183
184 for (var id in e.items) {
185 received[e.name][id] = convert(id, e.items[id]);
186 updateMessage(e.name, id);
187 }
188 }
189
190 var onInfo = function(stanza) {
191 log('info');
192 /*var query = stanza.getElementsByTagNameNS(ns.info, 'query')[0];
193 var x = query.getElementsByTagNameNS(ns.data, 'x')[0];
194 var form = forms.parse(x);*/
195 }
196
197 var onSubscribed = function(stanza) {
198 log('subscribed');
199 var type = stanza.getAttribute('type');
200 if (type !== 'result') {
201 messages.innerHTML = 'Error, impossible to retrieve messages.';
202 conn.disconnect();
203 }
204 }
205
206 function onConnect(status) {
207 if (status == Strophe.Status.CONNECTING) {
208 log('Strophe is connecting.');
209 } else if (status == Strophe.Status.CONNFAIL) {
210 log('Strophe failed to connect.');
211 } else if (status == Strophe.Status.AUTHENTICATING) {
212 log('Strophe is authenticating.');
213 } else if (status == Strophe.Status.DISCONNECTING) {
214 log('Strophe is disconnecting.');
215 } else if (status == Strophe.Status.DISCONNECTED) {
216 log('Strophe is disconnected.');
217 if (re)
218 conn.connect(jid, password, onConnect);
219 } else if (status == Strophe.Status.CONNECTED) {
220 log('Strophe is connected.');
221 conn.addHandler(onMessages, null, 'message', null, null, null);
222 conn.send($pres().tree());
223 conn.pubsub.subscribe(jid, service, node, undefined, onMessages, onSubscribed);
224 if (params.no === 'server') {
225 conn.pubsub.items(jid, service, node, onMessages);
226 conn.pubsub.info(jid, service, node, onInfo);
227 }
228 } else
229 log('Strophe is '+status+'.');
230 }
231
232 window.addEventListener('load', function () {
233 conn = new Strophe.Connection(BOSH_SERVICE);
234 conn.rawInput = rawInput;
235 conn.rawOutput = rawOutput;
236 conn.connect(jid, password, onConnect);
237 }, false);
238
239 window.addEventListener('unload', function (e) {
240 re = false;
241 conn.disconnect();
242 e.preventDefault();
243 }, false);