]>
Commit | Line | Data |
---|---|---|
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 JH |
25 | html, body { |
26 | width: 99.9%; | |
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 SW |
34 | a { |
35 | color: white; | |
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 { | |
47 | width: 98.5%; | |
48 | padding: 0px 0px 0px 5px; | |
49 | height: 50px; | |
50 | position: fixed; | |
51 | bottom: 0; | |
52 | } | |
53 | #input { | |
54 | width: 100%; | |
b22a2ced | 55 | background-color: #293134; |
70f46223 | 56 | } |
0248a518 | 57 | #say { width: 100% } |
70f46223 JH |
58 | #history { |
59 | padding: 0px 5px 55px 5px; | |
60 | vertical-align: bottom | |
61 | } | |
5afebfc6 SW |
62 | .banner { |
63 | font-size: 85%; | |
64 | text-align: right; | |
65 | } | |
c0aac85a SW |
66 | .servercount { |
67 | margin-right: 0.5em; | |
68 | font-size: 70%; | |
69 | } | |
4b184c5f SW |
70 | .timestamp { |
71 | margin-right: 0.8em; | |
72 | } | |
244a78af SW |
73 | .timestamp:hover, .timestamp:hover .servertimestamps { |
74 | background-color: #556; | |
75 | } | |
76 | .timestamp:hover .servertimestamps { | |
77 | display: block; | |
78 | } | |
79 | .servertimestamps { | |
80 | display: none; | |
81 | position: absolute; | |
82 | left: 3em; | |
83 | z-index: 1; | |
84 | border: 1px solid black; | |
85 | border-radius: 5px; | |
86 | padding-left: 5px; | |
87 | padding-right: 5px; | |
88 | } | |
0248a518 SW |
89 | img { width: 1px; height: 1px; } |
90 | iframe { display: none } | |
70f46223 | 91 | #status span { margin-right: 10px; } |
f09c4ede JH |
92 | #status span.sad { |
93 | background-color: #f00; | |
94 | color: #fff; | |
95 | border: 1px solid black; | |
96 | border-radius: 5px; | |
97 | padding-left: 5px; | |
98 | padding-right: 5px; | |
99 | } | |
100 | #status span.happy { | |
101 | background-color: #0f0; | |
102 | color: #000; | |
103 | border: 1px solid black; | |
104 | border-radius: 5px; | |
105 | padding-left: 5px; | |
106 | padding-right: 5px; | |
107 | } | |
0248a518 SW |
108 | /*]]>*/--></style> |
109 | <script type="text/javascript"><!--//--><![CDATA[//><!-- | |
110 | var servers = ['chkno.net', 'rc2.chkno.net', 'echto.net', 'the-wes.com', 'vibrantlogic.com']; | |
111 | ||
da9ce2ab | 112 | var session = Math.random(); // For outgoing message IDs |
869430fa SW |
113 | var since = {}; // server -> time: For fetch?since= |
114 | var seen = {}; // seen_key -> message | |
7f54ca0a | 115 | var history = []; // List of messages sorted by Time |
5419ea4c | 116 | // Messages have these fields: |
40380bd3 | 117 | // Time: The timestamp. Median of ServerTimes |
5419ea4c SW |
118 | // ID: Some unique string for deduping |
119 | // Text: The text of the message | |
705e26cf | 120 | // ServerTimes: server -> timestamp |
5419ea4c | 121 | // UI: The DOM node for this message in the UI |
0248a518 SW |
122 | |
123 | function rcnick() { | |
124 | var nick = localStorage.getItem("nick"); | |
125 | if (nick) { | |
126 | return nick; | |
127 | } | |
128 | return 'anonymous'; | |
129 | } | |
130 | ||
131 | function rcsetnick(new_nick) { | |
132 | localStorage.setItem("nick", new_nick); | |
133 | } | |
134 | ||
135 | function rcserverbase(server) { | |
136 | // Add the default port if server doesn't contain a port number already | |
137 | if (server.indexOf(":") == -1) { | |
138 | return "http://" + server + ":21059"; | |
139 | } else { | |
140 | return "http://" + server; | |
141 | } | |
142 | } | |
143 | ||
144 | function rcchangeserverstatus(server, new_status) { | |
145 | var statusbar = document.getElementById("status"); | |
146 | var spans = statusbar.getElementsByTagName("span"); | |
147 | for (var i in spans) { | |
148 | if (spans[i].firstChild && 'data' in spans[i].firstChild && spans[i].firstChild.data == server) { | |
149 | spans[i].setAttribute("class", new_status); | |
150 | } | |
151 | } | |
152 | } | |
153 | ||
66b29e97 SW |
154 | function rcpad2(x) { |
155 | return (x < 10 ? "0" : "") + x; | |
156 | } | |
157 | function rcpad3(x) { | |
158 | return (x < 10 ? "00" : (x < 100 ? "0" : "")) + x; | |
159 | } | |
160 | ||
bd1ed9dd SW |
161 | function rcformattime(t) { |
162 | var d = t.getDay(); | |
163 | d = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"][d]; | |
164 | var h = t.getHours(); | |
165 | var m = t.getMinutes(); | |
166 | var s = t.getSeconds(); | |
66b29e97 | 167 | return d + " " + rcpad2(h) + ":" + rcpad2(m) + ":" + rcpad2(s); |
bd1ed9dd SW |
168 | } |
169 | ||
244a78af SW |
170 | function rcaddservertimestamptohover(message, server) { |
171 | var divs = message.UI.getElementsByTagName("div"); | |
66b29e97 | 172 | var t = message.ServerTimes[server]; |
244a78af SW |
173 | for (var i in divs) { |
174 | if (divs[i].getAttribute && divs[i].getAttribute("class") == "servertimestamps") { | |
175 | var d = document.createElement("div"); | |
66b29e97 SW |
176 | var text = t.getFullYear() + "-" + |
177 | rcpad2(t.getMonth()) + "-" + | |
178 | rcpad2(t.getDay()) + " " + | |
179 | rcformattime(t) + "." + | |
180 | rcpad3(t.getMilliseconds()) + " " + | |
181 | server; | |
244a78af SW |
182 | d.appendChild(document.createTextNode(text)); |
183 | divs[i].appendChild(d); | |
184 | } | |
185 | } | |
186 | } | |
187 | ||
bf8f6a39 SW |
188 | function rcmakemessageUI(message) { |
189 | message.UI = document.createElement("div"); | |
190 | ||
191 | // Server count | |
192 | var servercount = document.createElement("span"); | |
193 | servercount.setAttribute("class", "servercount"); | |
194 | servercount.appendChild(document.createTextNode(Object.keys(message.ServerTimes).length)); | |
195 | message.UI.appendChild(servercount); | |
196 | ||
197 | // Timestamp | |
198 | var timestamp_text = message.Time ? rcformattime(message.Time) : ""; | |
199 | var timestamp = document.createElement("span"); | |
200 | timestamp.setAttribute("class", "timestamp"); | |
201 | timestamp.appendChild(document.createTextNode(timestamp_text)); | |
202 | message.UI.appendChild(timestamp); | |
203 | ||
244a78af SW |
204 | // Timestamp hover |
205 | var timestamp_hover = document.createElement("div"); | |
206 | timestamp_hover.setAttribute("class", "servertimestamps"); | |
207 | timestamp.appendChild(timestamp_hover); | |
208 | for (var server in message.ServerTimes) { | |
209 | rcaddservertimestamptohover(message, server); | |
210 | } | |
211 | ||
3a89c414 SW |
212 | // Classify different message types |
213 | var text_span = document.createElement("span"); | |
214 | var type; | |
215 | if (/^\*\*\* /.test(message.Text)) { | |
216 | type = "status"; | |
217 | } else if (/^\* /.test(message.Text)) { | |
218 | type = "me"; | |
219 | } else { | |
220 | type = "text"; | |
221 | } | |
222 | if (Object.keys(message.ServerTimes).length == 0) { | |
223 | type += " self"; | |
224 | } | |
225 | text_span.setAttribute("class", type); | |
e04a2cfd SW |
226 | |
227 | // URL detection | |
228 | var text = message.Text; | |
229 | var URL_re = /\bhttps?:\/\/\S+/; | |
230 | while (URL_re.test(text)) { | |
231 | var match = URL_re.exec(text); | |
232 | var leading_text = text.substr(0, match.index); | |
233 | if (leading_text) { | |
234 | text_span.appendChild(document.createTextNode(leading_text)); | |
235 | } | |
236 | var anchor = document.createElement("a"); | |
606c01e1 | 237 | anchor.setAttribute("rel", "nofollow"); |
e04a2cfd SW |
238 | anchor.setAttribute("href", encodeURI(match[0])); |
239 | anchor.appendChild(document.createTextNode(match[0])); | |
240 | text_span.appendChild(anchor); | |
241 | text = text.substr(match.index + match[0].length); | |
242 | } | |
243 | if (text) { | |
244 | text_span.appendChild(document.createTextNode(text)); | |
245 | } | |
246 | ||
3a89c414 | 247 | message.UI.appendChild(text_span); |
bf8f6a39 SW |
248 | } |
249 | ||
0248a518 | 250 | function rcaddmessagetohistory(message) { |
7f54ca0a SW |
251 | var message_i; |
252 | if (message.Time) { | |
253 | for (var i = history.length - 1; ; i--) { | |
254 | if (i < 0 || (history[i].Time && message.Time >= history[i].Time)) { | |
255 | message_i = i+1; | |
256 | history.splice(message_i, 0, message); | |
257 | break; | |
258 | } | |
259 | } | |
260 | } else { | |
261 | history.push(message); | |
262 | message_i = history.length-1; | |
263 | } | |
264 | ||
95ba71ee | 265 | if (!message.UI) { |
bf8f6a39 | 266 | rcmakemessageUI(message); |
95ba71ee | 267 | } |
0248a518 | 268 | var h = document.getElementById("history"); |
7f54ca0a SW |
269 | if (message_i + 1 < history.length) { |
270 | h.insertBefore(message.UI, history[message_i + 1].UI); | |
271 | } else { | |
272 | h.appendChild(message.UI); | |
273 | } | |
0248a518 | 274 | window.scrollTo(0, document.body.scrollHeight); |
0248a518 SW |
275 | } |
276 | ||
277 | function make_seen_key(id, text) { | |
278 | return id.replace(/@/g, "@@") + "_@_" + text.replace(/@/g, "@@"); | |
279 | } | |
280 | ||
40380bd3 SW |
281 | function rcupdatemessagetime(message) { |
282 | // Set message.Time to be the median of message.ServerTimes | |
283 | var times = []; | |
284 | for (var i in message.ServerTimes) { | |
285 | times.push(message.ServerTimes[i]); | |
286 | } | |
287 | times.sort(); | |
40380bd3 | 288 | if (times.length % 2) { |
ac4bf273 | 289 | message.Time = times[(times.length-1)/2]; |
40380bd3 | 290 | } else { |
ac4bf273 | 291 | var middle = times.length/2; |
40380bd3 SW |
292 | var difference = times[middle].getTime() - times[middle-1].getTime(); |
293 | message.Time = new Date(times[middle-1].getTime() + difference/2); | |
294 | } | |
95ba71ee SW |
295 | |
296 | // This may have broken history's in-sorted-order invariant | |
297 | var hi = history.indexOf(message); | |
298 | if ((history[hi-1] && history[hi-1].Time > message.Time) || | |
299 | (history[hi+1] && history[hi+1].Time < message.Time)) { | |
300 | history.splice(hi,1); | |
301 | rcaddmessagetohistory(message); | |
302 | } | |
5be4c8ec SW |
303 | |
304 | // Update the UI | |
305 | var spans = message.UI.getElementsByTagName("span"); | |
306 | for (var i in spans) { | |
c0aac85a SW |
307 | if (spans[i].getAttribute) { |
308 | var type = spans[i].getAttribute("class"); | |
309 | if (type == "servercount") { | |
310 | spans[i].firstChild.data = Object.keys(message.ServerTimes).length; | |
311 | } else if (type == "timestamp") { | |
312 | spans[i].firstChild.data = rcformattime(message.Time); | |
313 | } | |
5be4c8ec SW |
314 | } |
315 | } | |
40380bd3 SW |
316 | } |
317 | ||
79ced6f1 SW |
318 | function rcreceivemessages(server, messages) { |
319 | for (var i in messages) { | |
ad35b1be | 320 | var m = messages[i]; |
bd1ed9dd | 321 | m.Time = new Date(m.Time); |
ad35b1be | 322 | var seen_key = make_seen_key(m.ID, m.Text); |
705e26cf SW |
323 | if (seen_key in seen) { |
324 | seen[seen_key].ServerTimes[server] = m.Time; | |
40380bd3 | 325 | rcupdatemessagetime(seen[seen_key]); |
244a78af | 326 | rcaddservertimestamptohover(seen[seen_key], server); |
705e26cf | 327 | } else { |
16640c5d SW |
328 | m.ServerTimes = {}; |
329 | m.ServerTimes[server] = m.Time; | |
869430fa | 330 | seen[seen_key] = m; |
ad35b1be | 331 | rcaddmessagetohistory(m); |
79ced6f1 SW |
332 | for (var i in servers) { |
333 | rcchangeserverstatus(servers[i], "sad"); | |
334 | } | |
0248a518 | 335 | } |
79ced6f1 | 336 | rcchangeserverstatus(server, "happy"); |
0248a518 | 337 | } |
0248a518 SW |
338 | } |
339 | ||
79ced6f1 SW |
340 | function rcfetch(server) { |
341 | var delay = 10000; // TODO: Exponential backoff | |
342 | var xhr = new XMLHttpRequest(); | |
343 | xhr.onreadystatechange = function() { | |
344 | if (this.readyState == this.DONE) { | |
345 | if (this.status == 200) { | |
346 | var rtxt = this.responseText; | |
347 | if (rtxt != null) { | |
348 | var messages = JSON.parse(rtxt); | |
349 | if (messages != null) { | |
79ced6f1 SW |
350 | delay = 40; |
351 | if (messages.length >= 1 && "Time" in messages[messages.length-1]) { | |
ad35b1be | 352 | since[server] = messages[messages.length-1].Time; |
79ced6f1 | 353 | } |
92ca5f8a | 354 | rcreceivemessages(server, messages); |
79ced6f1 | 355 | } |
0248a518 SW |
356 | } |
357 | } | |
79ced6f1 | 358 | window.setTimeout(rcfetch, delay, server); |
0248a518 SW |
359 | } |
360 | } | |
79ced6f1 SW |
361 | var uri = rcserverbase(server) + "/fetch"; |
362 | if (server in since) { | |
363 | uri += '?since="' + since[server] + '"'; | |
364 | } | |
365 | xhr.open("GET", uri); | |
366 | xhr.send(); | |
0248a518 SW |
367 | } |
368 | ||
369 | function rcconnect() { | |
0248a518 | 370 | for (var i in servers) { |
79ced6f1 | 371 | rcfetch(servers[i]); |
0248a518 SW |
372 | // Status bar entry |
373 | var status_indicator = document.createElement("span"); | |
374 | status_indicator.appendChild(document.createTextNode(servers[i])); | |
375 | status_indicator.setAttribute("class", "sad"); | |
376 | document.getElementById("status").appendChild(status_indicator); | |
377 | } | |
0248a518 SW |
378 | } |
379 | ||
380 | function rcsend(d, message) { | |
7f54ca0a SW |
381 | message.ID = new Date().getTime() + "-" + session + "-" + Math.random(); |
382 | seen[make_seen_key(message.ID, message.Text)] = message; | |
0248a518 | 383 | var path = "/speak" + |
7f54ca0a SW |
384 | "?id=" + encodeURIComponent(message.ID) + |
385 | "&text=" + encodeURIComponent(message.Text); | |
0248a518 SW |
386 | for (var i in servers) { |
387 | var uri = rcserverbase(servers[i]) + path; | |
388 | var img = document.createElement("img"); | |
389 | img.setAttribute("src", uri); | |
390 | d.appendChild(img); | |
391 | } | |
392 | } | |
393 | ||
5a6c082a SW |
394 | function rcinput(input) { |
395 | var message; | |
396 | var re = /^\/([a-z]+) (.*)/ | |
397 | var match = re.exec(input); | |
398 | if (match && match[1] == 'me') { | |
399 | message = "* " + rcnick() + " " + match[2]; | |
400 | } else if (match && match[1] == 'nick') { | |
401 | message = "*** " + rcnick() + " is now known as " + match[2]; | |
402 | rcsetnick(match[2]); | |
403 | } else { | |
404 | message = "<" + rcnick() + "> " + input; | |
405 | } | |
406 | ||
7f54ca0a | 407 | var m = {'Text': message, 'ServerTimes': {}}; |
869430fa SW |
408 | rcaddmessagetohistory(m); |
409 | rcsend(m.UI, m); | |
5a6c082a SW |
410 | } |
411 | ||
0248a518 SW |
412 | function rckeydown(event) { |
413 | if (event.keyCode == 13) { | |
5a6c082a | 414 | rcinput(document.input.say.value); |
0248a518 SW |
415 | document.input.say.value = ""; |
416 | ||
0248a518 SW |
417 | } |
418 | } | |
419 | //--><!]]></script> | |
420 | ||
827f21bb SW |
421 | </head> |
422 | ||
f0e385c7 | 423 | <body onload="rcconnect()"> |
70f46223 | 424 | <div id="container"> |
5afebfc6 | 425 | <div class="banner">(You are using <a href="https://github.com/chkno/reliable-chat">Reliable Chat</a>)</div> |
70f46223 JH |
426 | <div id="history"></div> |
427 | <div id="client"> | |
428 | <div id="input"> | |
429 | <form name="input" onsubmit="return false" autocomplete="off"> | |
430 | <input id="say" onkeydown="return rckeydown(event)" autocomplete="off" autofocus="autofocus"></input> | |
431 | </form></div> | |
432 | <div id="status"> </div> | |
433 | </div> | |
434 | </div> | |
827f21bb SW |
435 | </body> |
436 | </html> |