]>
git.scottworley.com Git - reliable-chat/blob - webclient/rc.html
df592d0a8639fc939f419f08468456180cf053c5
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">
5 reliable-chat - multipath chat
6 Copyright (C) 2012 Scott Worley <sworley@chkno.net>
7 Copyright (C) 2012 Jason Hibbs <skitch@gmail.com>
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.
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.
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/>.
23 <title>Reliable Chat
</title>
24 <style type=
"text/css"><!--/*--><![CDATA[/*
><!--*/
30 background-color: #293134;
32 font-family: monospace;
43 background-color: #293134;
44 padding: 5px 5px 5px 0px;
53 background-color: #293134;
57 padding: 0px 5px 30px 5px;
58 vertical-align: bottom;
68 .timestamp:hover, .timestamp:hover .servertimestamps {
69 background-color: #556;
71 .timestamp:hover .servertimestamps {
79 border: 1px solid black;
84 img { width: 1px; height: 1px; }
85 iframe { display: none }
86 #status span { margin-right: 10px; }
88 background-color: #f00;
90 border: 1px solid black;
96 background-color: #0f0;
98 border: 1px solid black;
104 /* BEGIN expando input box trick kindly provided by http://www.alistapart.com/articles/expanding-text-areas-made-elegant/ */
107 border: 1px solid #888;
110 .expandingArea > textarea,
111 .expandingArea > pre {
116 background: transparent;
117 font: 400 13px/16px helvetica, arial, sans-serif;
118 /* Make the text soft-wrap */
119 white-space: pre-wrap;
120 word-wrap: break-word;
122 .expandingArea > textarea {
123 /* The border-box box model is used to allow
124 * padding whilst still keeping the overall width
125 * at exactly that of the containing element.
127 -webkit-box-sizing: border-box;
128 -moz-box-sizing: border-box;
129 -ms-box-sizing: border-box;
130 box-sizing: border-box;
132 /* Hide any scrollbars */
138 /* Remove WebKit user-resize widget */
141 .expandingArea > pre {
143 /* Hide the text; just using it for sizing */
146 /* END expando input box trick kindly provided by http://www.alistapart.com/articles/expanding-text-areas-made-elegant/ */
149 <script type=
"text/javascript"><!--//--><![CDATA[/
/><!--
150 var servers = ['chkno.net', 'rc2.chkno.net', 'reliablechat-chk.rhcloud.com:80', 'intense-basin-3395.herokuapp.com:80', 'echto.net', 'the-wes.com', 'vibrantlogic.com'];
152 var session = Math.random(); // For outgoing message IDs
153 var since = {}; // server -> time: For fetch?since=
154 var seen = {}; // seen_key -> message
155 var history = []; // List of messages sorted by Time
156 // Messages have these fields:
157 // Time: The timestamp. Median of ServerTimes
158 // ID: Some unique string for deduping
159 // Text: The text of the message
160 // ServerTimes: server -> timestamp
161 // UI: The DOM node for this message in the UI
164 var nick = localStorage.getItem("nick");
171 function rcsetnick(new_nick) {
172 localStorage.setItem("nick", new_nick);
175 function rcserverbase(server) {
176 // Add the default port if server doesn't contain a port number already
177 if (server.indexOf(":") == -1) {
178 return "http://" + server + ":21059";
180 return "http://" + server;
184 function rcchangeserverstatus(server, new_status) {
185 var statusbar = document.getElementById("status");
186 var spans = statusbar.getElementsByTagName("span");
187 for (var i in spans) {
188 if (spans[i].firstChild && 'data' in spans[i].firstChild && spans[i].firstChild.data == server) {
189 spans[i].setAttribute("class", new_status);
195 return (x < 10 ? "0" : "") + x;
198 return (x < 10 ? "00" : (x < 100 ? "0" : "")) + x;
201 function rcformattime(t) {
203 d = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"][d];
204 var h = t.getHours();
205 var m = t.getMinutes();
206 var s = t.getSeconds();
207 return d + " " + rcpad2(h) + ":" + rcpad2(m) + ":" + rcpad2(s);
210 function rcaddservertimestamptohover(message, server) {
211 var divs = message.UI.getElementsByTagName("div");
212 var t = message.ServerTimes[server];
213 for (var i in divs) {
214 if (divs[i].getAttribute && divs[i].getAttribute("class") == "servertimestamps") {
215 var d = document.createElement("div");
216 var text = t.getFullYear() + "-" +
217 rcpad2(t.getMonth()) + "-" +
218 rcpad2(t.getDay()) + " " +
219 rcformattime(t) + "." +
220 rcpad3(t.getMilliseconds()) + " " +
222 d.appendChild(document.createTextNode(text));
223 divs[i].appendChild(d);
228 function rcmakemessageUI(message) {
229 message.UI = document.createElement("div");
232 var servercount = document.createElement("span");
233 servercount.setAttribute("class", "servercount");
234 servercount.appendChild(document.createTextNode(Object.keys(message.ServerTimes).length));
235 message.UI.appendChild(servercount);
236 message.UI.appendChild(document.createTextNode(" "));
239 var timestamp_text = message.Time ? rcformattime(message.Time) : "";
240 var timestamp = document.createElement("span");
241 timestamp.setAttribute("class", "timestamp");
242 timestamp.appendChild(document.createTextNode(timestamp_text));
243 message.UI.appendChild(timestamp);
244 message.UI.appendChild(document.createTextNode(" "));
247 var timestamp_hover = document.createElement("div");
248 timestamp_hover.setAttribute("class", "servertimestamps");
249 timestamp.appendChild(timestamp_hover);
250 for (var server in message.ServerTimes) {
251 rcaddservertimestamptohover(message, server);
254 // Classify different message types
255 var text_span = document.createElement("span");
257 if (/^\*\*\* /.test(message.Text)) {
259 } else if (/^\* /.test(message.Text)) {
264 if (Object.keys(message.ServerTimes).length == 0) {
267 text_span.setAttribute("class", type);
270 var text = message.Text;
271 var URL_re = /\bhttps?:\/\/\S+/;
272 while (URL_re.test(text)) {
273 var match = URL_re.exec(text);
274 var leading_text = text.substr(0, match.index);
276 text_span.appendChild(document.createTextNode(leading_text));
278 var anchor = document.createElement("a");
279 anchor.setAttribute("rel", "nofollow");
280 anchor.setAttribute("href", encodeURI(match[0]));
281 anchor.appendChild(document.createTextNode(match[0]));
282 text_span.appendChild(anchor);
283 text = text.substr(match.index + match[0].length);
286 text_span.appendChild(document.createTextNode(text));
289 message.UI.appendChild(text_span);
292 function rcaddmessagetohistory(message) {
295 for (var i = history.length - 1; ; i--) {
296 if (i < 0 || (history[i].Time && message.Time >= history[i].Time)) {
298 history.splice(message_i, 0, message);
303 history.push(message);
304 message_i = history.length-1;
306 if (message_i + 1 < history.length) {
307 rcaddmessagetoUI(message, history[message_i + 1].UI);
309 rcaddmessagetoUI(message, null);
313 function rcaddmessagetoUI(message, before) {
315 rcmakemessageUI(message);
317 var h = document.getElementById("history");
319 h.insertBefore(message.UI, before);
321 h.appendChild(message.UI);
323 window.scrollTo(0, document.body.scrollHeight);
326 function make_seen_key(id, text) {
327 return id.replace(/@/g, "@@") + "_@_" + text.replace(/@/g, "@@");
330 function rcupdatemessagetime(message) {
331 // Set message.Time to be the median of message.ServerTimes
333 for (var i in message.ServerTimes) {
334 times.push(message.ServerTimes[i]);
337 if (times.length % 2) {
338 message.Time = times[(times.length-1)/2];
340 var middle = times.length/2;
341 var difference = times[middle].getTime() - times[middle-1].getTime();
342 message.Time = new Date(times[middle-1].getTime() + difference/2);
345 // This may have broken history's in-sorted-order invariant
346 var hi = history.indexOf(message);
347 if ((history[hi-1] && history[hi-1].Time > message.Time) ||
348 (history[hi+1] && history[hi+1].Time < message.Time)) {
349 history.splice(hi,1);
350 rcaddmessagetohistory(message);
354 var spans = message.UI.getElementsByTagName("span");
355 for (var i in spans) {
356 if (spans[i].getAttribute) {
357 var type = spans[i].getAttribute("class");
358 if (type == "servercount") {
359 spans[i].firstChild.data = Object.keys(message.ServerTimes).length;
360 } else if (type == "timestamp") {
361 spans[i].firstChild.data = rcformattime(message.Time);
367 function rcreceivemessages(server, messages) {
368 for (var i in messages) {
370 m.Time = new Date(m.Time);
371 var seen_key = make_seen_key(m.ID, m.Text);
372 if (seen_key in seen) {
373 seen[seen_key].ServerTimes[server] = m.Time;
374 rcupdatemessagetime(seen[seen_key]);
375 rcaddservertimestamptohover(seen[seen_key], server);
378 m.ServerTimes[server] = m.Time;
380 rcaddmessagetohistory(m);
381 for (var i in servers) {
382 rcchangeserverstatus(servers[i], "sad");
385 rcchangeserverstatus(server, "happy");
389 function rcfetch(server) {
390 var delay = 10000; // TODO: Exponential backoff
391 var xhr = new XMLHttpRequest();
392 xhr.onreadystatechange = function() {
393 if (this.readyState == this.DONE) {
394 if (this.status == 200) {
395 var rtxt = this.responseText;
397 var messages = JSON.parse(rtxt);
398 if (messages != null) {
400 if (messages.length >= 1 && "Time" in messages[messages.length-1]) {
401 since[server] = messages[messages.length-1].Time;
403 rcreceivemessages(server, messages);
407 window.setTimeout(rcfetch, delay, server);
410 var uri = rcserverbase(server) + "/fetch";
411 if (server in since) {
412 uri += '?since="' + since[server] + '"';
414 xhr.open("GET", uri);
418 function rcconnect() {
419 makeExpandingArea(document.getElementById("expando"));
420 for (var i in servers) {
423 var status_indicator = document.createElement("span");
424 status_indicator.appendChild(document.createTextNode(servers[i]));
425 status_indicator.setAttribute("class", "sad");
426 document.getElementById("status").appendChild(status_indicator);
430 function rcsend(d, message) {
431 message.ID = new Date().getTime() + "-" + session + "-" + Math.random();
432 seen[make_seen_key(message.ID, message.Text)] = message;
433 var path = "/speak" +
434 "?id=" + encodeURIComponent(message.ID) +
435 "&text=" + encodeURIComponent(message.Text);
436 for (var i in servers) {
437 var uri = rcserverbase(servers[i]) + path;
438 var img = document.createElement("img");
439 img.setAttribute("src", uri);
444 function rcinput(input) {
446 var re = /^\/([a-z]+) (.*)/
447 var match = re.exec(input);
449 if (match[1] == 'me') {
450 message = "* " + rcnick() + " " + match[2];
451 } else if (match[1] == 'nick') {
452 message = "*** " + rcnick() + " is now known as " + match[2];
455 rcaddmessagetoUI({'Text': '-!- No such command: ' + match[1], 'ServerTimes': {}});
459 message = "<" + rcnick() + "> " + input;
462 var m = {'Text': message, 'ServerTimes': {}};
463 rcaddmessagetohistory(m);
467 function rckeydown(event) {
468 if (event.keyCode == 13) {
469 rcinput(document.input.say.value);
470 document.input.say.value = "";
475 // From http://www.alistapart.com/articles/expanding-text-areas-made-elegant/
476 function makeExpandingArea(container) {
477 var area = container.querySelector('textarea');
478 var span1 = container.querySelector('span');
479 var span2 = document.getElementById('historypad');
480 if (area.addEventListener) {
481 area.addEventListener('input', function() {
482 span1.textContent = area.value;
483 span2.textContent = area.value;
485 span1.textContent = area.value;
486 span2.textContent = area.value;
487 } else if (area.attachEvent) {
489 area.attachEvent('onpropertychange', function() {
490 span1.innerText = area.value;
491 span2.innerText = area.value;
493 span1.innerText = area.value;
494 span2.innerText = area.value;
501 <body onload=
"rcconnect()">
503 <div class=
"banner">(You are using
<a href=
"https://github.com/chkno/reliable-chat">Reliable Chat
</a>)
</div>
504 <div id=
"history"></div>
505 <div class=
"expandingArea" style=
"visibility: hidden">
506 <pre><span id=
"historypad"></span><br></pre>
510 <form name=
"input" onsubmit=
"return false" autocomplete=
"off">
511 <div id=
"expando" class=
"expandingArea">
512 <pre><span></span><br></pre>
513 <textarea id=
"say" onkeydown=
"return rckeydown(event)" autofocus=
"autofocus"></textarea>
516 <div id=
"status"></div>