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