]> git.scottworley.com Git - reliable-chat/blob - webclient/rc.html
URL detection
[reliable-chat] / webclient / rc.html
1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
2 "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
3 <html xmlns="http://www.w3.org/1999/xhtml">
4 <!--
5 reliable-chat - multipath chat
6 Copyright (C) 2012 Scott Worley <sworley@chkno.net>
7 Copyright (C) 2012 Jason Hibbs <skitch@gmail.com>
8
9 This program is free software: you can redistribute it and/or modify
10 it under the terms of the GNU Affero General Public License as
11 published by the Free Software Foundation, either version 3 of the
12 License, or (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU Affero General Public License for more details.
18
19 You should have received a copy of the GNU Affero General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 -->
22 <head>
23 <title>Reliable Chat</title>
24 <style type="text/css"><!--/*--><![CDATA[/*><!--*/
25 html, body {
26 width: 99.9%;
27 height: 100%;
28 margin: 0;
29 padding: 0;
30 background-color: #293134;
31 color: silver;
32 font-family: monospace;
33 }
34 a {
35 color: white;
36 }
37 #container {
38 height: 100%;
39 }
40 #status {
41 width: 100%;
42 text-align: right;
43 background-color: #293134;
44 padding: 5px 5px 5px 0px;
45 }
46 #client {
47 width: 98.5%;
48 padding: 0px 0px 0px 5px;
49 height: 50px;
50 position: fixed;
51 bottom: 0;
52 }
53 #input {
54 width: 100%;
55 background-color: #293134;
56 }
57 #say { width: 100% }
58 #history {
59 padding: 0px 5px 55px 5px;
60 vertical-align: bottom
61 }
62 .servercount {
63 margin-right: 0.5em;
64 font-size: 70%;
65 }
66 .timestamp {
67 margin-right: 0.8em;
68 }
69 .timestamp:hover, .timestamp:hover .servertimestamps {
70 background-color: #556;
71 }
72 .timestamp:hover .servertimestamps {
73 display: block;
74 }
75 .servertimestamps {
76 display: none;
77 position: absolute;
78 left: 3em;
79 z-index: 1;
80 border: 1px solid black;
81 border-radius: 5px;
82 padding-left: 5px;
83 padding-right: 5px;
84 }
85 img { width: 1px; height: 1px; }
86 iframe { display: none }
87 #status span { margin-right: 10px; }
88 #status span.sad {
89 background-color: #f00;
90 color: #fff;
91 border: 1px solid black;
92 border-radius: 5px;
93 padding-left: 5px;
94 padding-right: 5px;
95 }
96 #status span.happy {
97 background-color: #0f0;
98 color: #000;
99 border: 1px solid black;
100 border-radius: 5px;
101 padding-left: 5px;
102 padding-right: 5px;
103 }
104 /*]]>*/--></style>
105 <script type="text/javascript"><!--//--><![CDATA[//><!--
106 var servers = ['chkno.net', 'rc2.chkno.net', 'echto.net', 'the-wes.com', 'vibrantlogic.com'];
107
108 var session = Math.random(); // For outgoing message IDs
109 var since = {}; // server -> time: For fetch?since=
110 var seen = {}; // seen_key -> message
111 var history = []; // List of messages sorted by Time
112 // Messages have these fields:
113 // Time: The timestamp. Median of ServerTimes
114 // ID: Some unique string for deduping
115 // Text: The text of the message
116 // ServerTimes: server -> timestamp
117 // UI: The DOM node for this message in the UI
118
119 function rcnick() {
120 var nick = localStorage.getItem("nick");
121 if (nick) {
122 return nick;
123 }
124 return 'anonymous';
125 }
126
127 function rcsetnick(new_nick) {
128 localStorage.setItem("nick", new_nick);
129 }
130
131 function rcserverbase(server) {
132 // Add the default port if server doesn't contain a port number already
133 if (server.indexOf(":") == -1) {
134 return "http://" + server + ":21059";
135 } else {
136 return "http://" + server;
137 }
138 }
139
140 function rcchangeserverstatus(server, new_status) {
141 var statusbar = document.getElementById("status");
142 var spans = statusbar.getElementsByTagName("span");
143 for (var i in spans) {
144 if (spans[i].firstChild && 'data' in spans[i].firstChild && spans[i].firstChild.data == server) {
145 spans[i].setAttribute("class", new_status);
146 }
147 }
148 }
149
150 function rcformattime(t) {
151 var d = t.getDay();
152 d = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"][d];
153 var h = t.getHours();
154 var m = t.getMinutes();
155 var s = t.getSeconds();
156 function pad(x) {
157 return (x < 10 ? "0" : "") + x;
158 }
159 return d + " " + pad(h) + ":" + pad(m) + ":" + pad(s);
160 }
161
162 function rcaddservertimestamptohover(message, server) {
163 var divs = message.UI.getElementsByTagName("div");
164 for (var i in divs) {
165 if (divs[i].getAttribute && divs[i].getAttribute("class") == "servertimestamps") {
166 var d = document.createElement("div");
167 function pad(x) {
168 return (x < 10 ? "00" : (x < 100 ? "0" : "")) + x;
169 }
170 var text = rcformattime(message.ServerTimes[server]) + "." + pad(message.ServerTimes[server].getUTCMilliseconds()) + " " + server;
171 d.appendChild(document.createTextNode(text));
172 divs[i].appendChild(d);
173 }
174 }
175 }
176
177 function rcmakemessageUI(message) {
178 message.UI = document.createElement("div");
179
180 // Server count
181 var servercount = document.createElement("span");
182 servercount.setAttribute("class", "servercount");
183 servercount.appendChild(document.createTextNode(Object.keys(message.ServerTimes).length));
184 message.UI.appendChild(servercount);
185
186 // Timestamp
187 var timestamp_text = message.Time ? rcformattime(message.Time) : "";
188 var timestamp = document.createElement("span");
189 timestamp.setAttribute("class", "timestamp");
190 timestamp.appendChild(document.createTextNode(timestamp_text));
191 message.UI.appendChild(timestamp);
192
193 // Timestamp hover
194 var timestamp_hover = document.createElement("div");
195 timestamp_hover.setAttribute("class", "servertimestamps");
196 timestamp.appendChild(timestamp_hover);
197 for (var server in message.ServerTimes) {
198 rcaddservertimestamptohover(message, server);
199 }
200
201 // Classify different message types
202 var text_span = document.createElement("span");
203 var type;
204 if (/^\*\*\* /.test(message.Text)) {
205 type = "status";
206 } else if (/^\* /.test(message.Text)) {
207 type = "me";
208 } else {
209 type = "text";
210 }
211 if (Object.keys(message.ServerTimes).length == 0) {
212 type += " self";
213 }
214 text_span.setAttribute("class", type);
215
216 // URL detection
217 var text = message.Text;
218 var URL_re = /\bhttps?:\/\/\S+/;
219 while (URL_re.test(text)) {
220 var match = URL_re.exec(text);
221 var leading_text = text.substr(0, match.index);
222 if (leading_text) {
223 text_span.appendChild(document.createTextNode(leading_text));
224 }
225 var anchor = document.createElement("a");
226 anchor.setAttribute("href", encodeURI(match[0]));
227 anchor.appendChild(document.createTextNode(match[0]));
228 text_span.appendChild(anchor);
229 text = text.substr(match.index + match[0].length);
230 }
231 if (text) {
232 text_span.appendChild(document.createTextNode(text));
233 }
234
235 message.UI.appendChild(text_span);
236 }
237
238 function rcaddmessagetohistory(message) {
239 var message_i;
240 if (message.Time) {
241 for (var i = history.length - 1; ; i--) {
242 if (i < 0 || (history[i].Time && message.Time >= history[i].Time)) {
243 message_i = i+1;
244 history.splice(message_i, 0, message);
245 break;
246 }
247 }
248 } else {
249 history.push(message);
250 message_i = history.length-1;
251 }
252
253 if (!message.UI) {
254 rcmakemessageUI(message);
255 }
256 var h = document.getElementById("history");
257 if (message_i + 1 < history.length) {
258 h.insertBefore(message.UI, history[message_i + 1].UI);
259 } else {
260 h.appendChild(message.UI);
261 }
262 window.scrollTo(0, document.body.scrollHeight);
263 }
264
265 function make_seen_key(id, text) {
266 return id.replace(/@/g, "@@") + "_@_" + text.replace(/@/g, "@@");
267 }
268
269 function rcupdatemessagetime(message) {
270 // Set message.Time to be the median of message.ServerTimes
271 var times = [];
272 for (var i in message.ServerTimes) {
273 times.push(message.ServerTimes[i]);
274 }
275 times.sort();
276 if (times.length % 2) {
277 message.Time = times[(times.length-1)/2];
278 } else {
279 var middle = times.length/2;
280 var difference = times[middle].getTime() - times[middle-1].getTime();
281 message.Time = new Date(times[middle-1].getTime() + difference/2);
282 }
283
284 // This may have broken history's in-sorted-order invariant
285 var hi = history.indexOf(message);
286 if ((history[hi-1] && history[hi-1].Time > message.Time) ||
287 (history[hi+1] && history[hi+1].Time < message.Time)) {
288 history.splice(hi,1);
289 rcaddmessagetohistory(message);
290 }
291
292 // Update the UI
293 var spans = message.UI.getElementsByTagName("span");
294 for (var i in spans) {
295 if (spans[i].getAttribute) {
296 var type = spans[i].getAttribute("class");
297 if (type == "servercount") {
298 spans[i].firstChild.data = Object.keys(message.ServerTimes).length;
299 } else if (type == "timestamp") {
300 spans[i].firstChild.data = rcformattime(message.Time);
301 }
302 }
303 }
304 }
305
306 function rcreceivemessages(server, messages) {
307 for (var i in messages) {
308 var m = messages[i];
309 m.Time = new Date(m.Time);
310 var seen_key = make_seen_key(m.ID, m.Text);
311 if (seen_key in seen) {
312 seen[seen_key].ServerTimes[server] = m.Time;
313 rcupdatemessagetime(seen[seen_key]);
314 rcaddservertimestamptohover(seen[seen_key], server);
315 } else {
316 m.ServerTimes = {};
317 m.ServerTimes[server] = m.Time;
318 seen[seen_key] = m;
319 rcaddmessagetohistory(m);
320 for (var i in servers) {
321 rcchangeserverstatus(servers[i], "sad");
322 }
323 }
324 rcchangeserverstatus(server, "happy");
325 }
326 }
327
328 function rcfetch(server) {
329 var delay = 10000; // TODO: Exponential backoff
330 var xhr = new XMLHttpRequest();
331 xhr.onreadystatechange = function() {
332 if (this.readyState == this.DONE) {
333 if (this.status == 200) {
334 var rtxt = this.responseText;
335 if (rtxt != null) {
336 var messages = JSON.parse(rtxt);
337 if (messages != null) {
338 delay = 40;
339 if (messages.length >= 1 && "Time" in messages[messages.length-1]) {
340 since[server] = messages[messages.length-1].Time;
341 }
342 rcreceivemessages(server, messages);
343 }
344 }
345 }
346 window.setTimeout(rcfetch, delay, server);
347 }
348 }
349 var uri = rcserverbase(server) + "/fetch";
350 if (server in since) {
351 uri += '?since="' + since[server] + '"';
352 }
353 xhr.open("GET", uri);
354 xhr.send();
355 }
356
357 function rcconnect() {
358 for (var i in servers) {
359 rcfetch(servers[i]);
360 // Status bar entry
361 var status_indicator = document.createElement("span");
362 status_indicator.appendChild(document.createTextNode(servers[i]));
363 status_indicator.setAttribute("class", "sad");
364 document.getElementById("status").appendChild(status_indicator);
365 }
366 }
367
368 function rcsend(d, message) {
369 message.ID = new Date().getTime() + "-" + session + "-" + Math.random();
370 seen[make_seen_key(message.ID, message.Text)] = message;
371 var path = "/speak" +
372 "?id=" + encodeURIComponent(message.ID) +
373 "&text=" + encodeURIComponent(message.Text);
374 for (var i in servers) {
375 var uri = rcserverbase(servers[i]) + path;
376 var img = document.createElement("img");
377 img.setAttribute("src", uri);
378 d.appendChild(img);
379 }
380 }
381
382 function rcinput(input) {
383 var message;
384 var re = /^\/([a-z]+) (.*)/
385 var match = re.exec(input);
386 if (match && match[1] == 'me') {
387 message = "* " + rcnick() + " " + match[2];
388 } else if (match && match[1] == 'nick') {
389 message = "*** " + rcnick() + " is now known as " + match[2];
390 rcsetnick(match[2]);
391 } else {
392 message = "<" + rcnick() + "> " + input;
393 }
394
395 var m = {'Text': message, 'ServerTimes': {}};
396 rcaddmessagetohistory(m);
397 rcsend(m.UI, m);
398 }
399
400 function rckeydown(event) {
401 if (event.keyCode == 13) {
402 rcinput(document.input.say.value);
403 document.input.say.value = "";
404
405 }
406 }
407 //--><!]]></script>
408
409 </head>
410
411 <body onload="rcconnect()">
412 <div id="container">
413 <div id="history"></div>
414 <div id="client">
415 <div id="input">
416 <form name="input" onsubmit="return false" autocomplete="off">
417 <input id="say" onkeydown="return rckeydown(event)" autocomplete="off" autofocus="autofocus"></input>
418 </form></div>
419 <div id="status">&nbsp;</div>
420 </div>
421 </div>
422 </body>
423 </html>