]>
Commit | Line | Data |
---|---|---|
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: 100%; | |
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: #ddd; | |
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: 100%; | |
48 | position: fixed; | |
49 | bottom: 0; | |
50 | display: none; | |
51 | } | |
52 | #input { | |
53 | width: 100%; | |
54 | background-color: #293134; | |
55 | } | |
56 | #say { width: 100% } | |
57 | #history { | |
58 | padding: 0px 5px 30px 5px; | |
59 | vertical-align: bottom; | |
60 | } | |
61 | .banner { | |
62 | font-size: 85%; | |
63 | text-align: right; | |
64 | } | |
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 | } | |
77 | .servercount { | |
78 | margin-right: -0.5em; | |
79 | font-size: 70%; | |
80 | } | |
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 | } | |
97 | img { width: 1px; height: 1px; } | |
98 | iframe { display: none } | |
99 | #status span { margin-right: 10px; } | |
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 | } | |
116 | #getnick { | |
117 | padding-left: 3em; | |
118 | display: none; | |
119 | } | |
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; | |
125 | background: silver; | |
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 | ||
165 | /*]]>*/--></style> | |
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']; | |
168 | ||
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 hist = []; // 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 | |
179 | ||
180 | function rcnick() { | |
181 | return localStorage.getItem("nick"); | |
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 | ||
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 | ||
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(); | |
220 | return d + " " + rcpad2(h) + ":" + rcpad2(m) + ":" + rcpad2(s); | |
221 | } | |
222 | ||
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()) + " " + | |
234 | server; | |
235 | d.appendChild(document.createTextNode(text)); | |
236 | divs[i].appendChild(d); | |
237 | } | |
238 | } | |
239 | } | |
240 | ||
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); | |
249 | message.UI.appendChild(document.createTextNode(" ")); | |
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); | |
257 | message.UI.appendChild(document.createTextNode(" ")); | |
258 | ||
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 | ||
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"; | |
274 | } else if (/^-!- /.test(message.Text)) { | |
275 | type = "local"; | |
276 | } else { | |
277 | type = "text"; | |
278 | } | |
279 | if (Object.keys(message.ServerTimes).length == 0) { | |
280 | type += " self"; | |
281 | } | |
282 | text_span.setAttribute("class", type); | |
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"); | |
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); | |
299 | } | |
300 | if (text) { | |
301 | text_span.appendChild(document.createTextNode(text)); | |
302 | } | |
303 | ||
304 | message.UI.appendChild(text_span); | |
305 | } | |
306 | ||
307 | function rcaddmessagetohistory(message) { | |
308 | var message_i; | |
309 | if (message.Time) { | |
310 | for (var i = hist.length - 1; ; i--) { | |
311 | if (i < 0 || (hist[i].Time && message.Time >= hist[i].Time)) { | |
312 | message_i = i+1; | |
313 | hist.splice(message_i, 0, message); | |
314 | break; | |
315 | } | |
316 | } | |
317 | } else { | |
318 | hist.push(message); | |
319 | message_i = hist.length-1; | |
320 | } | |
321 | if (message_i + 1 < hist.length) { | |
322 | rcaddmessagetoUI(message, hist[message_i + 1].UI); | |
323 | } else { | |
324 | rcaddmessagetoUI(message, null); | |
325 | } | |
326 | } | |
327 | ||
328 | function rcaddmessagetoUI(message, before) { | |
329 | if (!message.UI) { | |
330 | rcmakemessageUI(message); | |
331 | } | |
332 | var h = document.getElementById("history"); | |
333 | if (before) { | |
334 | h.insertBefore(message.UI, before); | |
335 | } else { | |
336 | h.appendChild(message.UI); | |
337 | } | |
338 | window.scrollTo(0, document.body.scrollHeight); | |
339 | } | |
340 | ||
341 | function make_seen_key(id, text) { | |
342 | return id.replace(/@/g, "@@") + "_@_" + text.replace(/@/g, "@@"); | |
343 | } | |
344 | ||
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(); | |
352 | if (times.length % 2) { | |
353 | message.Time = times[(times.length-1)/2]; | |
354 | } else { | |
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); | |
358 | } | |
359 | ||
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); | |
365 | rcaddmessagetohistory(message); | |
366 | } | |
367 | ||
368 | // Update the UI | |
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); | |
377 | } | |
378 | } | |
379 | } | |
380 | } | |
381 | ||
382 | function rcreceivemessages(server, messages) { | |
383 | for (var i in messages) { | |
384 | var m = messages[i]; | |
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); | |
391 | } else { | |
392 | m.ServerTimes = {}; | |
393 | m.ServerTimes[server] = m.Time; | |
394 | seen[seen_key] = m; | |
395 | rcaddmessagetohistory(m); | |
396 | for (var i in servers) { | |
397 | rcchangeserverstatus(servers[i], "sad"); | |
398 | } | |
399 | } | |
400 | rcchangeserverstatus(server, "happy"); | |
401 | } | |
402 | } | |
403 | ||
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) { | |
414 | delay = 40; | |
415 | if (messages.length >= 1 && "Time" in messages[messages.length-1]) { | |
416 | since[server] = messages[messages.length-1].Time; | |
417 | } | |
418 | rcreceivemessages(server, messages); | |
419 | } | |
420 | } | |
421 | } | |
422 | window.setTimeout(rcfetch, delay, server); | |
423 | } | |
424 | } | |
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(); | |
431 | } | |
432 | ||
433 | function rcconnect() { | |
434 | makeExpandingArea(document.getElementById("expando")); | |
435 | for (var i in servers) { | |
436 | rcfetch(servers[i]); | |
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 | } | |
443 | } | |
444 | ||
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 | ||
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); | |
463 | xhr.send(); | |
464 | } | |
465 | } | |
466 | ||
467 | function rcinput(input) { | |
468 | var message; | |
469 | var re = /^\/(\S+)(\s(.*))?/; | |
470 | var match = re.exec(input); | |
471 | if (match) { | |
472 | var command = match[1]; | |
473 | var rest = match[3]; | |
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': {}}); | |
479 | return; | |
480 | } | |
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 | } | |
488 | } else { | |
489 | rcaddmessagetoUI({'Text': '-!- No such command: ' + command, 'ServerTimes': {}}); | |
490 | return; | |
491 | } | |
492 | } else { | |
493 | message = "<" + rcnick() + "> " + input; | |
494 | } | |
495 | ||
496 | var m = {'Text': message, 'ServerTimes': {}}; | |
497 | rcaddmessagetohistory(m); | |
498 | rcsend(m.UI, m); | |
499 | } | |
500 | ||
501 | function rckeydown(event) { | |
502 | if (event.keyCode == 13) { | |
503 | if (document.input.say.value) { | |
504 | rcinput(document.input.say.value); | |
505 | } | |
506 | document.input.say.value = ""; | |
507 | return false; | |
508 | } | |
509 | } | |
510 | ||
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 | ||
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; | |
541 | } | |
542 | } | |
543 | //--><!]]></script> | |
544 | ||
545 | </head> | |
546 | ||
547 | <body onload="rcstart()"> | |
548 | <div id="container"> | |
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> | |
553 | </div> | |
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> | |
561 | <div id="client"> | |
562 | <div id="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> | |
567 | </div> | |
568 | </form></div> | |
569 | <div id="status"></div> | |
570 | </div> | |
571 | </div> | |
572 | </body> | |
573 | </html> |