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