]>
git.scottworley.com Git - reliable-chat/blob - webclient/rc.html
e4bdb2de89ae70ef7e8e981dd2aed01c709dcf31
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;
54 background-color: #293134;
58 padding: 0px 5px 30px 5px;
59 vertical-align: bottom;
81 .timestamp:hover, .timestamp:hover .servertimestamps {
82 background-color: #556;
84 .timestamp:hover .servertimestamps {
92 border: 1px solid black;
97 img { width: 1px; height: 1px; }
98 iframe { display: none }
99 #status span { margin-right: 10px; }
101 background-color: #f00;
103 border: 1px solid black;
109 background-color: #0f0;
111 border: 1px solid black;
121 /* BEGIN expando input box trick kindly provided by http://www.alistapart.com/articles/expanding-text-areas-made-elegant/ */
124 border: 1px solid #888;
127 .expandingArea > textarea,
128 .expandingArea > pre {
133 background: transparent;
134 font: 400 13px/16px helvetica, arial, sans-serif;
135 /* Make the text soft-wrap */
136 white-space: pre-wrap;
137 word-wrap: break-word;
139 .expandingArea > textarea {
140 /* The border-box box model is used to allow
141 * padding whilst still keeping the overall width
142 * at exactly that of the containing element.
144 -webkit-box-sizing: border-box;
145 -moz-box-sizing: border-box;
146 -ms-box-sizing: border-box;
147 box-sizing: border-box;
149 /* Hide any scrollbars */
155 /* Remove WebKit user-resize widget */
158 .expandingArea > pre {
160 /* Hide the text; just using it for sizing */
163 /* END expando input box trick kindly provided by http://www.alistapart.com/articles/expanding-text-areas-made-elegant/ */
166 <script type=
"text/javascript"><!--//--><![CDATA[/
/><!--
167 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'];
169 var session = Math.random(); // For outgoing message IDs
170 var since = {}; // server -> time: For fetch?since=
171 var seen = {}; // seen_key -> message
172 var history = []; // List of messages sorted by Time
173 // Messages have these fields:
174 // Time: The timestamp. Median of ServerTimes
175 // ID: Some unique string for deduping
176 // Text: The text of the message
177 // ServerTimes: server -> timestamp
178 // UI: The DOM node for this message in the UI
181 return localStorage.getItem("nick");
184 function rcsetnick(new_nick) {
185 localStorage.setItem("nick", new_nick);
188 function rcserverbase(server) {
189 // Add the default port if server doesn't contain a port number already
190 if (server.indexOf(":") == -1) {
191 return "http://" + server + ":21059";
193 return "http://" + server;
197 function rcchangeserverstatus(server, new_status) {
198 var statusbar = document.getElementById("status");
199 var spans = statusbar.getElementsByTagName("span");
200 for (var i in spans) {
201 if (spans[i].firstChild && 'data' in spans[i].firstChild && spans[i].firstChild.data == server) {
202 spans[i].setAttribute("class", new_status);
208 return (x < 10 ? "0" : "") + x;
211 return (x < 10 ? "00" : (x < 100 ? "0" : "")) + x;
214 function rcformattime(t) {
216 d = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"][d];
217 var h = t.getHours();
218 var m = t.getMinutes();
219 var s = t.getSeconds();
220 return d + " " + rcpad2(h) + ":" + rcpad2(m) + ":" + rcpad2(s);
223 function rcaddservertimestamptohover(message, server) {
224 var divs = message.UI.getElementsByTagName("div");
225 var t = message.ServerTimes[server];
226 for (var i in divs) {
227 if (divs[i].getAttribute && divs[i].getAttribute("class") == "servertimestamps") {
228 var d = document.createElement("div");
229 var text = t.getFullYear() + "-" +
230 rcpad2(t.getMonth()) + "-" +
231 rcpad2(t.getDay()) + " " +
232 rcformattime(t) + "." +
233 rcpad3(t.getMilliseconds()) + " " +
235 d.appendChild(document.createTextNode(text));
236 divs[i].appendChild(d);
241 function rcmakemessageUI(message) {
242 message.UI = document.createElement("div");
245 var servercount = document.createElement("span");
246 servercount.setAttribute("class", "servercount");
247 servercount.appendChild(document.createTextNode(Object.keys(message.ServerTimes).length));
248 message.UI.appendChild(servercount);
249 message.UI.appendChild(document.createTextNode(" "));
252 var timestamp_text = message.Time ? rcformattime(message.Time) : "";
253 var timestamp = document.createElement("span");
254 timestamp.setAttribute("class", "timestamp");
255 timestamp.appendChild(document.createTextNode(timestamp_text));
256 message.UI.appendChild(timestamp);
257 message.UI.appendChild(document.createTextNode(" "));
260 var timestamp_hover = document.createElement("div");
261 timestamp_hover.setAttribute("class", "servertimestamps");
262 timestamp.appendChild(timestamp_hover);
263 for (var server in message.ServerTimes) {
264 rcaddservertimestamptohover(message, server);
267 // Classify different message types
268 var text_span = document.createElement("span");
270 if (/^\*\*\* /.test(message.Text)) {
272 } else if (/^\* /.test(message.Text)) {
274 } else if (/^-!- /.test(message.Text)) {
279 if (Object.keys(message.ServerTimes).length == 0) {
282 text_span.setAttribute("class", type);
285 var text = message.Text;
286 var URL_re = /\bhttps?:\/\/\S+/;
287 while (URL_re.test(text)) {
288 var match = URL_re.exec(text);
289 var leading_text = text.substr(0, match.index);
291 text_span.appendChild(document.createTextNode(leading_text));
293 var anchor = document.createElement("a");
294 anchor.setAttribute("rel", "nofollow");
295 anchor.setAttribute("href", encodeURI(match[0]));
296 anchor.appendChild(document.createTextNode(match[0]));
297 text_span.appendChild(anchor);
298 text = text.substr(match.index + match[0].length);
301 text_span.appendChild(document.createTextNode(text));
304 message.UI.appendChild(text_span);
307 function rcaddmessagetohistory(message) {
310 for (var i = history.length - 1; ; i--) {
311 if (i < 0 || (history[i].Time && message.Time >= history[i].Time)) {
313 history.splice(message_i, 0, message);
318 history.push(message);
319 message_i = history.length-1;
321 if (message_i + 1 < history.length) {
322 rcaddmessagetoUI(message, history[message_i + 1].UI);
324 rcaddmessagetoUI(message, null);
328 function rcaddmessagetoUI(message, before) {
330 rcmakemessageUI(message);
332 var h = document.getElementById("history");
334 h.insertBefore(message.UI, before);
336 h.appendChild(message.UI);
338 window.scrollTo(0, document.body.scrollHeight);
341 function make_seen_key(id, text) {
342 return id.replace(/@/g, "@@") + "_@_" + text.replace(/@/g, "@@");
345 function rcupdatemessagetime(message) {
346 // Set message.Time to be the median of message.ServerTimes
348 for (var i in message.ServerTimes) {
349 times.push(message.ServerTimes[i]);
352 if (times.length % 2) {
353 message.Time = times[(times.length-1)/2];
355 var middle = times.length/2;
356 var difference = times[middle].getTime() - times[middle-1].getTime();
357 message.Time = new Date(times[middle-1].getTime() + difference/2);
360 // This may have broken history's in-sorted-order invariant
361 var hi = history.indexOf(message);
362 if ((history[hi-1] && history[hi-1].Time > message.Time) ||
363 (history[hi+1] && history[hi+1].Time < message.Time)) {
364 history.splice(hi,1);
365 rcaddmessagetohistory(message);
369 var spans = message.UI.getElementsByTagName("span");
370 for (var i in spans) {
371 if (spans[i].getAttribute) {
372 var type = spans[i].getAttribute("class");
373 if (type == "servercount") {
374 spans[i].firstChild.data = Object.keys(message.ServerTimes).length;
375 } else if (type == "timestamp") {
376 spans[i].firstChild.data = rcformattime(message.Time);
382 function rcreceivemessages(server, messages) {
383 for (var i in messages) {
385 m.Time = new Date(m.Time);
386 var seen_key = make_seen_key(m.ID, m.Text);
387 if (seen_key in seen) {
388 seen[seen_key].ServerTimes[server] = m.Time;
389 rcupdatemessagetime(seen[seen_key]);
390 rcaddservertimestamptohover(seen[seen_key], server);
393 m.ServerTimes[server] = m.Time;
395 rcaddmessagetohistory(m);
396 for (var i in servers) {
397 rcchangeserverstatus(servers[i], "sad");
400 rcchangeserverstatus(server, "happy");
404 function rcfetch(server) {
405 var delay = 10000; // TODO: Exponential backoff
406 var xhr = new XMLHttpRequest();
407 xhr.onreadystatechange = function() {
408 if (this.readyState == this.DONE) {
409 if (this.status == 200) {
410 var rtxt = this.responseText;
412 var messages = JSON.parse(rtxt);
413 if (messages != null) {
415 if (messages.length >= 1 && "Time" in messages[messages.length-1]) {
416 since[server] = messages[messages.length-1].Time;
418 rcreceivemessages(server, messages);
422 window.setTimeout(rcfetch, delay, server);
425 var uri = rcserverbase(server) + "/fetch";
426 if (server in since) {
427 uri += '?since="' + since[server] + '"';
429 xhr.open("GET", uri);
433 function rcconnect() {
434 makeExpandingArea(document.getElementById("expando"));
435 for (var i in servers) {
438 var status_indicator = document.createElement("span");
439 status_indicator.appendChild(document.createTextNode(servers[i]));
440 status_indicator.setAttribute("class", "sad");
441 document.getElementById("status").appendChild(status_indicator);
447 document.getElementById("client").style.display = 'block';
450 document.getElementById("getnick").style.display = 'block';
454 function rcsend(d, message) {
455 message.ID = new Date().getTime() + "-" + session + "-" + Math.random();
456 seen[make_seen_key(message.ID, message.Text)] = message;
457 var path = "/speak" +
458 "?id=" + encodeURIComponent(message.ID) +
459 "&text=" + encodeURIComponent(message.Text);
460 for (var i in servers) {
461 var xhr = new XMLHttpRequest();
462 xhr.open("POST", rcserverbase(servers[i]) + path);
467 function rcinput(input) {
469 var re = /^\/(\S+)(\s(.*))?/;
470 var match = re.exec(input);
472 var command = match[1];
474 if (command == 'me') {
475 message = "* " + rcnick() + " " + rest;
476 } else if (command == 'nick') {
477 if (rcnick() == rest) {
478 rcaddmessagetoUI({'Text': '-!- Your nick is already ' + rcnick(), 'ServerTimes': {}});
482 message = "*** " + rcnick() + " is now known as " + rest;
485 rcaddmessagetoUI({'Text': '-!- /nick requires an argument', 'ServerTimes': {}});
489 rcaddmessagetoUI({'Text': '-!- No such command: ' + command, 'ServerTimes': {}});
493 message = "<" + rcnick() + "> " + input;
496 var m = {'Text': message, 'ServerTimes': {}};
497 rcaddmessagetohistory(m);
501 function rckeydown(event) {
502 if (event.keyCode == 13) {
503 if (document.input.say.value) {
504 rcinput(document.input.say.value);
506 document.input.say.value = "";
511 function rcsetinitialnick() {
512 if (document.getnickform.initial_nick.value) {
513 rcsetnick(document.getnickform.initial_nick.value);
514 document.getElementById("getnick").style.display = 'none';
515 document.getElementById("client").style.display = 'block';
521 // From http://www.alistapart.com/articles/expanding-text-areas-made-elegant/
522 function makeExpandingArea(container) {
523 var area = container.querySelector('textarea');
524 var span1 = container.querySelector('span');
525 var span2 = document.getElementById('historypad');
526 if (area.addEventListener) {
527 area.addEventListener('input', function() {
528 span1.textContent = area.value;
529 span2.textContent = area.value;
531 span1.textContent = area.value;
532 span2.textContent = area.value;
533 } else if (area.attachEvent) {
535 area.attachEvent('onpropertychange', function() {
536 span1.innerText = area.value;
537 span2.innerText = area.value;
539 span1.innerText = area.value;
540 span2.innerText = area.value;
547 <body onload=
"rcstart()">
549 <div class=
"banner">(You are using
<a href=
"https://github.com/chkno/reliable-chat">Reliable Chat
</a>)
</div>
550 <div id=
"history"></div>
551 <div class=
"expandingArea" style=
"visibility: hidden">
552 <pre><span id=
"historypad"></span><br></pre>
555 <h1>Set your nick
</h1>
556 <form name=
"getnickform" onsubmit=
"return rcsetinitialnick();">
557 <input id=
"initial_nick" type=
"text"></input>
558 <input type=
"submit" value=
"ok"></input>
563 <form name=
"input" onsubmit=
"return false" autocomplete=
"off">
564 <div id=
"expando" class=
"expandingArea">
565 <pre><span></span><br></pre>
566 <textarea id=
"say" onkeydown=
"return rckeydown(event)" autofocus=
"autofocus"></textarea>
569 <div id=
"status"></div>