]> git.scottworley.com Git - reliable-chat/blame - webclient/rc.html
Rename "history" to "hist"
[reliable-chat] / webclient / rc.html
CommitLineData
827f21bb
SW
1<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
2 "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
827f21bb 3<html xmlns="http://www.w3.org/1999/xhtml">
520c21fd
SW
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-->
827f21bb
SW
22<head>
23 <title>Reliable Chat</title>
0248a518 24 <style type="text/css"><!--/*--><![CDATA[/*><!--*/
70f46223 25 html, body {
19724ebd 26 width: 100%;
70f46223
JH
27 height: 100%;
28 margin: 0;
29 padding: 0;
b22a2ced
JH
30 background-color: #293134;
31 color: silver;
70f46223
JH
32 font-family: monospace;
33 }
e04a2cfd 34 a {
63b1551b 35 color: #ddd;
e04a2cfd 36 }
70f46223
JH
37 #container {
38 height: 100%;
39 }
40 #status {
41 width: 100%;
638866e9 42 text-align: right;
b22a2ced 43 background-color: #293134;
70f46223
JH
44 padding: 5px 5px 5px 0px;
45 }
46 #client {
19724ebd 47 width: 100%;
70f46223
JH
48 position: fixed;
49 bottom: 0;
72f02cfd 50 display: none;
70f46223
JH
51 }
52 #input {
53 width: 100%;
b22a2ced 54 background-color: #293134;
70f46223 55 }
0248a518 56 #say { width: 100% }
70f46223 57 #history {
19724ebd
SW
58 padding: 0px 5px 30px 5px;
59 vertical-align: bottom;
70f46223 60 }
5afebfc6
SW
61 .banner {
62 font-size: 85%;
63 text-align: right;
64 }
7310ffee
SW
65 .status {
66 color: #dd8;
67 }
68 .local.self {
69 color: #d8d;
70 }
71 .self {
72 color: #8d8;
73 }
74 .me {
75 color: #bbd;
76 }
c0aac85a 77 .servercount {
33fdb071 78 margin-right: -0.5em;
c0aac85a
SW
79 font-size: 70%;
80 }
244a78af
SW
81 .timestamp:hover, .timestamp:hover .servertimestamps {
82 background-color: #556;
83 }
84 .timestamp:hover .servertimestamps {
85 display: block;
86 }
87 .servertimestamps {
88 display: none;
89 position: absolute;
90 left: 3em;
91 z-index: 1;
92 border: 1px solid black;
93 border-radius: 5px;
94 padding-left: 5px;
95 padding-right: 5px;
96 }
0248a518
SW
97 img { width: 1px; height: 1px; }
98 iframe { display: none }
70f46223 99 #status span { margin-right: 10px; }
f09c4ede
JH
100 #status span.sad {
101 background-color: #f00;
102 color: #fff;
103 border: 1px solid black;
104 border-radius: 5px;
105 padding-left: 5px;
106 padding-right: 5px;
107 }
108 #status span.happy {
109 background-color: #0f0;
110 color: #000;
111 border: 1px solid black;
112 border-radius: 5px;
113 padding-left: 5px;
114 padding-right: 5px;
115 }
72f02cfd
SW
116 #getnick {
117 padding-left: 3em;
118 display: none;
119 }
19724ebd
SW
120
121 /* BEGIN expando input box trick kindly provided by http://www.alistapart.com/articles/expanding-text-areas-made-elegant/ */
122 .expandingArea {
123 position: relative;
124 border: 1px solid #888;
63b1551b 125 background: silver;
19724ebd
SW
126 }
127 .expandingArea > textarea,
128 .expandingArea > pre {
129 margin: 0;
130 outline: 0;
131 border: 0;
132 padding: 5px;
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;
138 }
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.
143 */
144 -webkit-box-sizing: border-box;
145 -moz-box-sizing: border-box;
146 -ms-box-sizing: border-box;
147 box-sizing: border-box;
148 width: 100%;
149 /* Hide any scrollbars */
150 overflow: hidden;
151 position: absolute;
152 top: 0;
153 left: 0;
154 height: 100%;
155 /* Remove WebKit user-resize widget */
156 resize: none;
157 }
158 .expandingArea > pre {
159 display: block;
160 /* Hide the text; just using it for sizing */
161 visibility: hidden;
162 }
163 /* END expando input box trick kindly provided by http://www.alistapart.com/articles/expanding-text-areas-made-elegant/ */
164
0248a518
SW
165 /*]]>*/--></style>
166 <script type="text/javascript"><!--//--><![CDATA[//><!--
b669fb70 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'];
0248a518 168
da9ce2ab 169 var session = Math.random(); // For outgoing message IDs
869430fa
SW
170 var since = {}; // server -> time: For fetch?since=
171 var seen = {}; // seen_key -> message
f9e5ee82 172 var hist = []; // List of messages sorted by Time
5419ea4c 173 // Messages have these fields:
40380bd3 174 // Time: The timestamp. Median of ServerTimes
5419ea4c
SW
175 // ID: Some unique string for deduping
176 // Text: The text of the message
705e26cf 177 // ServerTimes: server -> timestamp
5419ea4c 178 // UI: The DOM node for this message in the UI
0248a518
SW
179
180 function rcnick() {
72f02cfd 181 return localStorage.getItem("nick");
0248a518
SW
182 }
183
184 function rcsetnick(new_nick) {
185 localStorage.setItem("nick", new_nick);
186 }
187
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";
192 } else {
193 return "http://" + server;
194 }
195 }
196
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);
203 }
204 }
205 }
206
66b29e97
SW
207 function rcpad2(x) {
208 return (x < 10 ? "0" : "") + x;
209 }
210 function rcpad3(x) {
211 return (x < 10 ? "00" : (x < 100 ? "0" : "")) + x;
212 }
213
bd1ed9dd
SW
214 function rcformattime(t) {
215 var d = t.getDay();
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();
66b29e97 220 return d + " " + rcpad2(h) + ":" + rcpad2(m) + ":" + rcpad2(s);
bd1ed9dd
SW
221 }
222
244a78af
SW
223 function rcaddservertimestamptohover(message, server) {
224 var divs = message.UI.getElementsByTagName("div");
66b29e97 225 var t = message.ServerTimes[server];
244a78af
SW
226 for (var i in divs) {
227 if (divs[i].getAttribute && divs[i].getAttribute("class") == "servertimestamps") {
228 var d = document.createElement("div");
66b29e97
SW
229 var text = t.getFullYear() + "-" +
230 rcpad2(t.getMonth()) + "-" +
231 rcpad2(t.getDay()) + " " +
232 rcformattime(t) + "." +
233 rcpad3(t.getMilliseconds()) + " " +
234 server;
244a78af
SW
235 d.appendChild(document.createTextNode(text));
236 divs[i].appendChild(d);
237 }
238 }
239 }
240
bf8f6a39
SW
241 function rcmakemessageUI(message) {
242 message.UI = document.createElement("div");
243
244 // Server count
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);
33fdb071 249 message.UI.appendChild(document.createTextNode(" "));
bf8f6a39
SW
250
251 // Timestamp
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);
33fdb071 257 message.UI.appendChild(document.createTextNode(" "));
bf8f6a39 258
244a78af
SW
259 // Timestamp hover
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);
265 }
266
3a89c414
SW
267 // Classify different message types
268 var text_span = document.createElement("span");
269 var type;
270 if (/^\*\*\* /.test(message.Text)) {
271 type = "status";
272 } else if (/^\* /.test(message.Text)) {
273 type = "me";
c301e690
SW
274 } else if (/^-!- /.test(message.Text)) {
275 type = "local";
3a89c414
SW
276 } else {
277 type = "text";
278 }
279 if (Object.keys(message.ServerTimes).length == 0) {
280 type += " self";
281 }
282 text_span.setAttribute("class", type);
e04a2cfd
SW
283
284 // URL detection
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);
290 if (leading_text) {
291 text_span.appendChild(document.createTextNode(leading_text));
292 }
293 var anchor = document.createElement("a");
606c01e1 294 anchor.setAttribute("rel", "nofollow");
e04a2cfd
SW
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);
299 }
300 if (text) {
301 text_span.appendChild(document.createTextNode(text));
302 }
303
3a89c414 304 message.UI.appendChild(text_span);
bf8f6a39
SW
305 }
306
0248a518 307 function rcaddmessagetohistory(message) {
7f54ca0a
SW
308 var message_i;
309 if (message.Time) {
f9e5ee82
SW
310 for (var i = hist.length - 1; ; i--) {
311 if (i < 0 || (hist[i].Time && message.Time >= hist[i].Time)) {
7f54ca0a 312 message_i = i+1;
f9e5ee82 313 hist.splice(message_i, 0, message);
7f54ca0a
SW
314 break;
315 }
316 }
317 } else {
f9e5ee82
SW
318 hist.push(message);
319 message_i = hist.length-1;
7f54ca0a 320 }
f9e5ee82
SW
321 if (message_i + 1 < hist.length) {
322 rcaddmessagetoUI(message, hist[message_i + 1].UI);
16f7340b
SW
323 } else {
324 rcaddmessagetoUI(message, null);
325 }
326 }
7f54ca0a 327
16f7340b 328 function rcaddmessagetoUI(message, before) {
95ba71ee 329 if (!message.UI) {
bf8f6a39 330 rcmakemessageUI(message);
95ba71ee 331 }
0248a518 332 var h = document.getElementById("history");
16f7340b
SW
333 if (before) {
334 h.insertBefore(message.UI, before);
7f54ca0a
SW
335 } else {
336 h.appendChild(message.UI);
337 }
0248a518 338 window.scrollTo(0, document.body.scrollHeight);
0248a518
SW
339 }
340
341 function make_seen_key(id, text) {
342 return id.replace(/@/g, "@@") + "_@_" + text.replace(/@/g, "@@");
343 }
344
40380bd3
SW
345 function rcupdatemessagetime(message) {
346 // Set message.Time to be the median of message.ServerTimes
347 var times = [];
348 for (var i in message.ServerTimes) {
349 times.push(message.ServerTimes[i]);
350 }
351 times.sort();
40380bd3 352 if (times.length % 2) {
ac4bf273 353 message.Time = times[(times.length-1)/2];
40380bd3 354 } else {
ac4bf273 355 var middle = times.length/2;
40380bd3
SW
356 var difference = times[middle].getTime() - times[middle-1].getTime();
357 message.Time = new Date(times[middle-1].getTime() + difference/2);
358 }
95ba71ee 359
f9e5ee82
SW
360 // This may have broken hist's in-sorted-order invariant
361 var hi = hist.indexOf(message);
362 if ((hist[hi-1] && hist[hi-1].Time > message.Time) ||
363 (hist[hi+1] && hist[hi+1].Time < message.Time)) {
364 hist.splice(hi,1);
95ba71ee
SW
365 rcaddmessagetohistory(message);
366 }
5be4c8ec
SW
367
368 // Update the UI
369 var spans = message.UI.getElementsByTagName("span");
370 for (var i in spans) {
c0aac85a
SW
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);
377 }
5be4c8ec
SW
378 }
379 }
40380bd3
SW
380 }
381
79ced6f1
SW
382 function rcreceivemessages(server, messages) {
383 for (var i in messages) {
ad35b1be 384 var m = messages[i];
bd1ed9dd 385 m.Time = new Date(m.Time);
ad35b1be 386 var seen_key = make_seen_key(m.ID, m.Text);
705e26cf
SW
387 if (seen_key in seen) {
388 seen[seen_key].ServerTimes[server] = m.Time;
40380bd3 389 rcupdatemessagetime(seen[seen_key]);
244a78af 390 rcaddservertimestamptohover(seen[seen_key], server);
705e26cf 391 } else {
16640c5d
SW
392 m.ServerTimes = {};
393 m.ServerTimes[server] = m.Time;
869430fa 394 seen[seen_key] = m;
ad35b1be 395 rcaddmessagetohistory(m);
79ced6f1
SW
396 for (var i in servers) {
397 rcchangeserverstatus(servers[i], "sad");
398 }
0248a518 399 }
79ced6f1 400 rcchangeserverstatus(server, "happy");
0248a518 401 }
0248a518
SW
402 }
403
79ced6f1
SW
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;
411 if (rtxt != null) {
412 var messages = JSON.parse(rtxt);
413 if (messages != null) {
79ced6f1
SW
414 delay = 40;
415 if (messages.length >= 1 && "Time" in messages[messages.length-1]) {
ad35b1be 416 since[server] = messages[messages.length-1].Time;
79ced6f1 417 }
92ca5f8a 418 rcreceivemessages(server, messages);
79ced6f1 419 }
0248a518
SW
420 }
421 }
79ced6f1 422 window.setTimeout(rcfetch, delay, server);
0248a518
SW
423 }
424 }
79ced6f1
SW
425 var uri = rcserverbase(server) + "/fetch";
426 if (server in since) {
427 uri += '?since="' + since[server] + '"';
428 }
429 xhr.open("GET", uri);
430 xhr.send();
0248a518
SW
431 }
432
433 function rcconnect() {
19724ebd 434 makeExpandingArea(document.getElementById("expando"));
0248a518 435 for (var i in servers) {
79ced6f1 436 rcfetch(servers[i]);
0248a518
SW
437 // Status bar entry
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);
442 }
0248a518
SW
443 }
444
72f02cfd
SW
445 function rcstart() {
446 if (rcnick()) {
447 document.getElementById("client").style.display = 'block';
448 rcconnect();
449 } else {
450 document.getElementById("getnick").style.display = 'block';
451 }
452 }
453
0248a518 454 function rcsend(d, message) {
7f54ca0a
SW
455 message.ID = new Date().getTime() + "-" + session + "-" + Math.random();
456 seen[make_seen_key(message.ID, message.Text)] = message;
0248a518 457 var path = "/speak" +
7f54ca0a
SW
458 "?id=" + encodeURIComponent(message.ID) +
459 "&text=" + encodeURIComponent(message.Text);
0248a518 460 for (var i in servers) {
9878d03b
SW
461 var xhr = new XMLHttpRequest();
462 xhr.open("POST", rcserverbase(servers[i]) + path);
463 xhr.send();
0248a518
SW
464 }
465 }
466
5a6c082a
SW
467 function rcinput(input) {
468 var message;
219b6a2b 469 var re = /^\/(\S+)(\s(.*))?/;
5a6c082a 470 var match = re.exec(input);
16f7340b 471 if (match) {
112df9d1
SW
472 var command = match[1];
473 var rest = match[3];
474 if (command == 'me') {
475 message = "* " + rcnick() + " " + rest;
476 } else if (command == 'nick') {
219b6a2b
SW
477 if (rcnick() == rest) {
478 rcaddmessagetoUI({'Text': '-!- Your nick is already ' + rcnick(), 'ServerTimes': {}});
479 return;
480 }
a7a2c8b6
SW
481 if (rest) {
482 message = "*** " + rcnick() + " is now known as " + rest;
483 rcsetnick(rest);
484 } else {
485 rcaddmessagetoUI({'Text': '-!- /nick requires an argument', 'ServerTimes': {}});
486 return;
487 }
16f7340b 488 } else {
112df9d1 489 rcaddmessagetoUI({'Text': '-!- No such command: ' + command, 'ServerTimes': {}});
16f7340b
SW
490 return;
491 }
5a6c082a
SW
492 } else {
493 message = "<" + rcnick() + "> " + input;
494 }
495
7f54ca0a 496 var m = {'Text': message, 'ServerTimes': {}};
869430fa
SW
497 rcaddmessagetohistory(m);
498 rcsend(m.UI, m);
5a6c082a
SW
499 }
500
0248a518
SW
501 function rckeydown(event) {
502 if (event.keyCode == 13) {
cf44a976
SW
503 if (document.input.say.value) {
504 rcinput(document.input.say.value);
505 }
0248a518 506 document.input.say.value = "";
19724ebd
SW
507 return false;
508 }
509 }
510
72f02cfd
SW
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';
516 rcconnect();
517 }
518 return false;
519 }
520
19724ebd
SW
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;
530 }, false);
531 span1.textContent = area.value;
532 span2.textContent = area.value;
533 } else if (area.attachEvent) {
534 // IE8 compatibility
535 area.attachEvent('onpropertychange', function() {
536 span1.innerText = area.value;
537 span2.innerText = area.value;
538 });
539 span1.innerText = area.value;
540 span2.innerText = area.value;
0248a518
SW
541 }
542 }
543 //--><!]]></script>
544
827f21bb
SW
545</head>
546
72f02cfd 547<body onload="rcstart()">
70f46223 548 <div id="container">
5afebfc6 549 <div class="banner">(You are using <a href="https://github.com/chkno/reliable-chat">Reliable Chat</a>)</div>
70f46223 550 <div id="history"></div>
19724ebd
SW
551 <div class="expandingArea" style="visibility: hidden">
552 <pre><span id="historypad"></span><br></pre>
553 </div>
72f02cfd
SW
554 <div id="getnick">
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>
559 </form>
560 </div>
70f46223
JH
561 <div id="client">
562 <div id="input">
563 <form name="input" onsubmit="return false" autocomplete="off">
19724ebd
SW
564 <div id="expando" class="expandingArea">
565 <pre><span></span><br></pre>
566 <textarea id="say" onkeydown="return rckeydown(event)" autofocus="autofocus"></textarea>
567 </div>
70f46223 568 </form></div>
19724ebd 569 <div id="status"></div>
70f46223
JH
570 </div>
571 </div>
827f21bb
SW
572</body>
573</html>