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