]>
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 | } | |
51 | #input { | |
52 | width: 100%; | |
53 | background-color: #293134; | |
54 | } | |
55 | #say { width: 100% } | |
56 | #history { | |
57 | padding: 0px 5px 30px 5px; | |
58 | vertical-align: bottom; | |
59 | } | |
60 | .banner { | |
61 | font-size: 85%; | |
62 | text-align: right; | |
63 | } | |
64 | .status { | |
65 | color: #dd8; | |
66 | } | |
67 | .local.self { | |
68 | color: #d8d; | |
69 | } | |
70 | .self { | |
71 | color: #8d8; | |
72 | } | |
73 | .me { | |
74 | color: #bbd; | |
75 | } | |
76 | .servercount { | |
77 | margin-right: -0.5em; | |
78 | font-size: 70%; | |
79 | } | |
80 | .timestamp:hover, .timestamp:hover .servertimestamps { | |
81 | background-color: #556; | |
82 | } | |
83 | .timestamp:hover .servertimestamps { | |
84 | display: block; | |
85 | } | |
86 | .servertimestamps { | |
87 | display: none; | |
88 | position: absolute; | |
89 | left: 3em; | |
90 | z-index: 1; | |
91 | border: 1px solid black; | |
92 | border-radius: 5px; | |
93 | padding-left: 5px; | |
94 | padding-right: 5px; | |
95 | } | |
96 | img { width: 1px; height: 1px; } | |
97 | iframe { display: none } | |
98 | #status span { margin-right: 10px; } | |
99 | #status span.sad { | |
100 | background-color: #f00; | |
101 | color: #fff; | |
102 | border: 1px solid black; | |
103 | border-radius: 5px; | |
104 | padding-left: 5px; | |
105 | padding-right: 5px; | |
106 | } | |
107 | #status span.happy { | |
108 | background-color: #0f0; | |
109 | color: #000; | |
110 | border: 1px solid black; | |
111 | border-radius: 5px; | |
112 | padding-left: 5px; | |
113 | padding-right: 5px; | |
114 | } | |
115 | ||
116 | /* BEGIN expando input box trick kindly provided by http://www.alistapart.com/articles/expanding-text-areas-made-elegant/ */ | |
117 | .expandingArea { | |
118 | position: relative; | |
119 | border: 1px solid #888; | |
120 | background: silver; | |
121 | } | |
122 | .expandingArea > textarea, | |
123 | .expandingArea > pre { | |
124 | margin: 0; | |
125 | outline: 0; | |
126 | border: 0; | |
127 | padding: 5px; | |
128 | background: transparent; | |
129 | font: 400 13px/16px helvetica, arial, sans-serif; | |
130 | /* Make the text soft-wrap */ | |
131 | white-space: pre-wrap; | |
132 | word-wrap: break-word; | |
133 | } | |
134 | .expandingArea > textarea { | |
135 | /* The border-box box model is used to allow | |
136 | * padding whilst still keeping the overall width | |
137 | * at exactly that of the containing element. | |
138 | */ | |
139 | -webkit-box-sizing: border-box; | |
140 | -moz-box-sizing: border-box; | |
141 | -ms-box-sizing: border-box; | |
142 | box-sizing: border-box; | |
143 | width: 100%; | |
144 | /* Hide any scrollbars */ | |
145 | overflow: hidden; | |
146 | position: absolute; | |
147 | top: 0; | |
148 | left: 0; | |
149 | height: 100%; | |
150 | /* Remove WebKit user-resize widget */ | |
151 | resize: none; | |
152 | } | |
153 | .expandingArea > pre { | |
154 | display: block; | |
155 | /* Hide the text; just using it for sizing */ | |
156 | visibility: hidden; | |
157 | } | |
158 | /* END expando input box trick kindly provided by http://www.alistapart.com/articles/expanding-text-areas-made-elegant/ */ | |
159 | ||
160 | /*]]>*/--></style> | |
161 | <script type="text/javascript"><!--//--><![CDATA[//><!-- | |
162 | 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']; | |
163 | ||
164 | var session = Math.random(); // For outgoing message IDs | |
165 | var since = {}; // server -> time: For fetch?since= | |
166 | var seen = {}; // seen_key -> message | |
167 | var history = []; // List of messages sorted by Time | |
168 | // Messages have these fields: | |
169 | // Time: The timestamp. Median of ServerTimes | |
170 | // ID: Some unique string for deduping | |
171 | // Text: The text of the message | |
172 | // ServerTimes: server -> timestamp | |
173 | // UI: The DOM node for this message in the UI | |
174 | ||
175 | function rcnick() { | |
176 | var nick = localStorage.getItem("nick"); | |
177 | if (nick) { | |
178 | return nick; | |
179 | } | |
180 | return 'anonymous'; | |
181 | } | |
182 | ||
183 | function rcsetnick(new_nick) { | |
184 | localStorage.setItem("nick", new_nick); | |
185 | } | |
186 | ||
187 | function rcserverbase(server) { | |
188 | // Add the default port if server doesn't contain a port number already | |
189 | if (server.indexOf(":") == -1) { | |
190 | return "http://" + server + ":21059"; | |
191 | } else { | |
192 | return "http://" + server; | |
193 | } | |
194 | } | |
195 | ||
196 | function rcchangeserverstatus(server, new_status) { | |
197 | var statusbar = document.getElementById("status"); | |
198 | var spans = statusbar.getElementsByTagName("span"); | |
199 | for (var i in spans) { | |
200 | if (spans[i].firstChild && 'data' in spans[i].firstChild && spans[i].firstChild.data == server) { | |
201 | spans[i].setAttribute("class", new_status); | |
202 | } | |
203 | } | |
204 | } | |
205 | ||
206 | function rcpad2(x) { | |
207 | return (x < 10 ? "0" : "") + x; | |
208 | } | |
209 | function rcpad3(x) { | |
210 | return (x < 10 ? "00" : (x < 100 ? "0" : "")) + x; | |
211 | } | |
212 | ||
213 | function rcformattime(t) { | |
214 | var d = t.getDay(); | |
215 | d = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"][d]; | |
216 | var h = t.getHours(); | |
217 | var m = t.getMinutes(); | |
218 | var s = t.getSeconds(); | |
219 | return d + " " + rcpad2(h) + ":" + rcpad2(m) + ":" + rcpad2(s); | |
220 | } | |
221 | ||
222 | function rcaddservertimestamptohover(message, server) { | |
223 | var divs = message.UI.getElementsByTagName("div"); | |
224 | var t = message.ServerTimes[server]; | |
225 | for (var i in divs) { | |
226 | if (divs[i].getAttribute && divs[i].getAttribute("class") == "servertimestamps") { | |
227 | var d = document.createElement("div"); | |
228 | var text = t.getFullYear() + "-" + | |
229 | rcpad2(t.getMonth()) + "-" + | |
230 | rcpad2(t.getDay()) + " " + | |
231 | rcformattime(t) + "." + | |
232 | rcpad3(t.getMilliseconds()) + " " + | |
233 | server; | |
234 | d.appendChild(document.createTextNode(text)); | |
235 | divs[i].appendChild(d); | |
236 | } | |
237 | } | |
238 | } | |
239 | ||
240 | function rcmakemessageUI(message) { | |
241 | message.UI = document.createElement("div"); | |
242 | ||
243 | // Server count | |
244 | var servercount = document.createElement("span"); | |
245 | servercount.setAttribute("class", "servercount"); | |
246 | servercount.appendChild(document.createTextNode(Object.keys(message.ServerTimes).length)); | |
247 | message.UI.appendChild(servercount); | |
248 | message.UI.appendChild(document.createTextNode(" ")); | |
249 | ||
250 | // Timestamp | |
251 | var timestamp_text = message.Time ? rcformattime(message.Time) : ""; | |
252 | var timestamp = document.createElement("span"); | |
253 | timestamp.setAttribute("class", "timestamp"); | |
254 | timestamp.appendChild(document.createTextNode(timestamp_text)); | |
255 | message.UI.appendChild(timestamp); | |
256 | message.UI.appendChild(document.createTextNode(" ")); | |
257 | ||
258 | // Timestamp hover | |
259 | var timestamp_hover = document.createElement("div"); | |
260 | timestamp_hover.setAttribute("class", "servertimestamps"); | |
261 | timestamp.appendChild(timestamp_hover); | |
262 | for (var server in message.ServerTimes) { | |
263 | rcaddservertimestamptohover(message, server); | |
264 | } | |
265 | ||
266 | // Classify different message types | |
267 | var text_span = document.createElement("span"); | |
268 | var type; | |
269 | if (/^\*\*\* /.test(message.Text)) { | |
270 | type = "status"; | |
271 | } else if (/^\* /.test(message.Text)) { | |
272 | type = "me"; | |
273 | } else if (/^-!- /.test(message.Text)) { | |
274 | type = "local"; | |
275 | } else { | |
276 | type = "text"; | |
277 | } | |
278 | if (Object.keys(message.ServerTimes).length == 0) { | |
279 | type += " self"; | |
280 | } | |
281 | text_span.setAttribute("class", type); | |
282 | ||
283 | // URL detection | |
284 | var text = message.Text; | |
285 | var URL_re = /\bhttps?:\/\/\S+/; | |
286 | while (URL_re.test(text)) { | |
287 | var match = URL_re.exec(text); | |
288 | var leading_text = text.substr(0, match.index); | |
289 | if (leading_text) { | |
290 | text_span.appendChild(document.createTextNode(leading_text)); | |
291 | } | |
292 | var anchor = document.createElement("a"); | |
293 | anchor.setAttribute("rel", "nofollow"); | |
294 | anchor.setAttribute("href", encodeURI(match[0])); | |
295 | anchor.appendChild(document.createTextNode(match[0])); | |
296 | text_span.appendChild(anchor); | |
297 | text = text.substr(match.index + match[0].length); | |
298 | } | |
299 | if (text) { | |
300 | text_span.appendChild(document.createTextNode(text)); | |
301 | } | |
302 | ||
303 | message.UI.appendChild(text_span); | |
304 | } | |
305 | ||
306 | function rcaddmessagetohistory(message) { | |
307 | var message_i; | |
308 | if (message.Time) { | |
309 | for (var i = history.length - 1; ; i--) { | |
310 | if (i < 0 || (history[i].Time && message.Time >= history[i].Time)) { | |
311 | message_i = i+1; | |
312 | history.splice(message_i, 0, message); | |
313 | break; | |
314 | } | |
315 | } | |
316 | } else { | |
317 | history.push(message); | |
318 | message_i = history.length-1; | |
319 | } | |
320 | if (message_i + 1 < history.length) { | |
321 | rcaddmessagetoUI(message, history[message_i + 1].UI); | |
322 | } else { | |
323 | rcaddmessagetoUI(message, null); | |
324 | } | |
325 | } | |
326 | ||
327 | function rcaddmessagetoUI(message, before) { | |
328 | if (!message.UI) { | |
329 | rcmakemessageUI(message); | |
330 | } | |
331 | var h = document.getElementById("history"); | |
332 | if (before) { | |
333 | h.insertBefore(message.UI, before); | |
334 | } else { | |
335 | h.appendChild(message.UI); | |
336 | } | |
337 | window.scrollTo(0, document.body.scrollHeight); | |
338 | } | |
339 | ||
340 | function make_seen_key(id, text) { | |
341 | return id.replace(/@/g, "@@") + "_@_" + text.replace(/@/g, "@@"); | |
342 | } | |
343 | ||
344 | function rcupdatemessagetime(message) { | |
345 | // Set message.Time to be the median of message.ServerTimes | |
346 | var times = []; | |
347 | for (var i in message.ServerTimes) { | |
348 | times.push(message.ServerTimes[i]); | |
349 | } | |
350 | times.sort(); | |
351 | if (times.length % 2) { | |
352 | message.Time = times[(times.length-1)/2]; | |
353 | } else { | |
354 | var middle = times.length/2; | |
355 | var difference = times[middle].getTime() - times[middle-1].getTime(); | |
356 | message.Time = new Date(times[middle-1].getTime() + difference/2); | |
357 | } | |
358 | ||
359 | // This may have broken history's in-sorted-order invariant | |
360 | var hi = history.indexOf(message); | |
361 | if ((history[hi-1] && history[hi-1].Time > message.Time) || | |
362 | (history[hi+1] && history[hi+1].Time < message.Time)) { | |
363 | history.splice(hi,1); | |
364 | rcaddmessagetohistory(message); | |
365 | } | |
366 | ||
367 | // Update the UI | |
368 | var spans = message.UI.getElementsByTagName("span"); | |
369 | for (var i in spans) { | |
370 | if (spans[i].getAttribute) { | |
371 | var type = spans[i].getAttribute("class"); | |
372 | if (type == "servercount") { | |
373 | spans[i].firstChild.data = Object.keys(message.ServerTimes).length; | |
374 | } else if (type == "timestamp") { | |
375 | spans[i].firstChild.data = rcformattime(message.Time); | |
376 | } | |
377 | } | |
378 | } | |
379 | } | |
380 | ||
381 | function rcreceivemessages(server, messages) { | |
382 | for (var i in messages) { | |
383 | var m = messages[i]; | |
384 | m.Time = new Date(m.Time); | |
385 | var seen_key = make_seen_key(m.ID, m.Text); | |
386 | if (seen_key in seen) { | |
387 | seen[seen_key].ServerTimes[server] = m.Time; | |
388 | rcupdatemessagetime(seen[seen_key]); | |
389 | rcaddservertimestamptohover(seen[seen_key], server); | |
390 | } else { | |
391 | m.ServerTimes = {}; | |
392 | m.ServerTimes[server] = m.Time; | |
393 | seen[seen_key] = m; | |
394 | rcaddmessagetohistory(m); | |
395 | for (var i in servers) { | |
396 | rcchangeserverstatus(servers[i], "sad"); | |
397 | } | |
398 | } | |
399 | rcchangeserverstatus(server, "happy"); | |
400 | } | |
401 | } | |
402 | ||
403 | function rcfetch(server) { | |
404 | var delay = 10000; // TODO: Exponential backoff | |
405 | var xhr = new XMLHttpRequest(); | |
406 | xhr.onreadystatechange = function() { | |
407 | if (this.readyState == this.DONE) { | |
408 | if (this.status == 200) { | |
409 | var rtxt = this.responseText; | |
410 | if (rtxt != null) { | |
411 | var messages = JSON.parse(rtxt); | |
412 | if (messages != null) { | |
413 | delay = 40; | |
414 | if (messages.length >= 1 && "Time" in messages[messages.length-1]) { | |
415 | since[server] = messages[messages.length-1].Time; | |
416 | } | |
417 | rcreceivemessages(server, messages); | |
418 | } | |
419 | } | |
420 | } | |
421 | window.setTimeout(rcfetch, delay, server); | |
422 | } | |
423 | } | |
424 | var uri = rcserverbase(server) + "/fetch"; | |
425 | if (server in since) { | |
426 | uri += '?since="' + since[server] + '"'; | |
427 | } | |
428 | xhr.open("GET", uri); | |
429 | xhr.send(); | |
430 | } | |
431 | ||
432 | function rcconnect() { | |
433 | makeExpandingArea(document.getElementById("expando")); | |
434 | for (var i in servers) { | |
435 | rcfetch(servers[i]); | |
436 | // Status bar entry | |
437 | var status_indicator = document.createElement("span"); | |
438 | status_indicator.appendChild(document.createTextNode(servers[i])); | |
439 | status_indicator.setAttribute("class", "sad"); | |
440 | document.getElementById("status").appendChild(status_indicator); | |
441 | } | |
442 | } | |
443 | ||
444 | function rcsend(d, message) { | |
445 | message.ID = new Date().getTime() + "-" + session + "-" + Math.random(); | |
446 | seen[make_seen_key(message.ID, message.Text)] = message; | |
447 | var path = "/speak" + | |
448 | "?id=" + encodeURIComponent(message.ID) + | |
449 | "&text=" + encodeURIComponent(message.Text); | |
450 | for (var i in servers) { | |
451 | var xhr = new XMLHttpRequest(); | |
452 | xhr.open("POST", rcserverbase(servers[i]) + path); | |
453 | xhr.send(); | |
454 | } | |
455 | } | |
456 | ||
457 | function rcinput(input) { | |
458 | var message; | |
459 | var re = /^\/(\S+)(\s(.*))?/; | |
460 | var match = re.exec(input); | |
461 | if (match) { | |
462 | var command = match[1]; | |
463 | var rest = match[3]; | |
464 | if (command == 'me') { | |
465 | message = "* " + rcnick() + " " + rest; | |
466 | } else if (command == 'nick') { | |
467 | if (rcnick() == rest) { | |
468 | rcaddmessagetoUI({'Text': '-!- Your nick is already ' + rcnick(), 'ServerTimes': {}}); | |
469 | return; | |
470 | } | |
471 | if (rest) { | |
472 | message = "*** " + rcnick() + " is now known as " + rest; | |
473 | rcsetnick(rest); | |
474 | } else { | |
475 | rcaddmessagetoUI({'Text': '-!- /nick requires an argument', 'ServerTimes': {}}); | |
476 | return; | |
477 | } | |
478 | } else { | |
479 | rcaddmessagetoUI({'Text': '-!- No such command: ' + command, 'ServerTimes': {}}); | |
480 | return; | |
481 | } | |
482 | } else { | |
483 | message = "<" + rcnick() + "> " + input; | |
484 | } | |
485 | ||
486 | var m = {'Text': message, 'ServerTimes': {}}; | |
487 | rcaddmessagetohistory(m); | |
488 | rcsend(m.UI, m); | |
489 | } | |
490 | ||
491 | function rckeydown(event) { | |
492 | if (event.keyCode == 13) { | |
493 | if (document.input.say.value) { | |
494 | rcinput(document.input.say.value); | |
495 | } | |
496 | document.input.say.value = ""; | |
497 | return false; | |
498 | } | |
499 | } | |
500 | ||
501 | // From http://www.alistapart.com/articles/expanding-text-areas-made-elegant/ | |
502 | function makeExpandingArea(container) { | |
503 | var area = container.querySelector('textarea'); | |
504 | var span1 = container.querySelector('span'); | |
505 | var span2 = document.getElementById('historypad'); | |
506 | if (area.addEventListener) { | |
507 | area.addEventListener('input', function() { | |
508 | span1.textContent = area.value; | |
509 | span2.textContent = area.value; | |
510 | }, false); | |
511 | span1.textContent = area.value; | |
512 | span2.textContent = area.value; | |
513 | } else if (area.attachEvent) { | |
514 | // IE8 compatibility | |
515 | area.attachEvent('onpropertychange', function() { | |
516 | span1.innerText = area.value; | |
517 | span2.innerText = area.value; | |
518 | }); | |
519 | span1.innerText = area.value; | |
520 | span2.innerText = area.value; | |
521 | } | |
522 | } | |
523 | //--><!]]></script> | |
524 | ||
525 | </head> | |
526 | ||
527 | <body onload="rcconnect()"> | |
528 | <div id="container"> | |
529 | <div class="banner">(You are using <a href="https://github.com/chkno/reliable-chat">Reliable Chat</a>)</div> | |
530 | <div id="history"></div> | |
531 | <div class="expandingArea" style="visibility: hidden"> | |
532 | <pre><span id="historypad"></span><br></pre> | |
533 | </div> | |
534 | <div id="client"> | |
535 | <div id="input"> | |
536 | <form name="input" onsubmit="return false" autocomplete="off"> | |
537 | <div id="expando" class="expandingArea"> | |
538 | <pre><span></span><br></pre> | |
539 | <textarea id="say" onkeydown="return rckeydown(event)" autofocus="autofocus"></textarea> | |
540 | </div> | |
541 | </form></div> | |
542 | <div id="status"></div> | |
543 | </div> | |
544 | </div> | |
545 | </body> | |
546 | </html> |