]>
git.scottworley.com Git - reliable-chat/blob - webclient/rc.html
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">
5 reliable-chat - multipath chat
6 Copyright (C) 2012 Scott Worley <sworley@chkno.net>
7 Copyright (C) 2012 Jason Hibbs <skitch@gmail.com>
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.
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.
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/>.
23 <title>Reliable Chat
</title>
24 <style type=
"text/css"><!--/*--><![CDATA[/*
><!--*/
30 background-color: #293134;
32 font-family: monospace;
43 background-color: #293134;
44 padding: 5px 5px 5px 0px;
48 padding: 0px 0px 0px 5px;
55 background-color: #293134;
59 padding: 0px 5px 55px 5px;
60 vertical-align: bottom
69 .timestamp:hover, .timestamp:hover .servertimestamps {
70 background-color: #556;
72 .timestamp:hover .servertimestamps {
80 border: 1px solid black;
85 img { width: 1px; height: 1px; }
86 iframe { display: none }
87 #status span { margin-right: 10px; }
89 background-color: #f00;
91 border: 1px solid black;
97 background-color: #0f0;
99 border: 1px solid black;
105 <script type=
"text/javascript"><!--//--><![CDATA[/
/><!--
106 var servers = ['chkno.net', 'rc2.chkno.net', 'echto.net', 'the-wes.com', 'vibrantlogic.com'];
108 var session = Math.random(); // For outgoing message IDs
109 var since = {}; // server -> time: For fetch?since=
110 var seen = {}; // seen_key -> message
111 var history = []; // List of messages sorted by Time
112 // Messages have these fields:
113 // Time: The timestamp. Median of ServerTimes
114 // ID: Some unique string for deduping
115 // Text: The text of the message
116 // ServerTimes: server -> timestamp
117 // UI: The DOM node for this message in the UI
120 var nick = localStorage.getItem("nick");
127 function rcsetnick(new_nick) {
128 localStorage.setItem("nick", new_nick);
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";
136 return "http://" + server;
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);
151 return (x < 10 ? "0" : "") + x;
154 return (x < 10 ? "00" : (x < 100 ? "0" : "")) + x;
157 function rcformattime(t) {
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();
163 return d + " " + rcpad2(h) + ":" + rcpad2(m) + ":" + rcpad2(s);
166 function rcaddservertimestamptohover(message, server) {
167 var divs = message.UI.getElementsByTagName("div");
168 var t = message.ServerTimes[server];
169 for (var i in divs) {
170 if (divs[i].getAttribute && divs[i].getAttribute("class") == "servertimestamps") {
171 var d = document.createElement("div");
172 var text = t.getFullYear() + "-" +
173 rcpad2(t.getMonth()) + "-" +
174 rcpad2(t.getDay()) + " " +
175 rcformattime(t) + "." +
176 rcpad3(t.getMilliseconds()) + " " +
178 d.appendChild(document.createTextNode(text));
179 divs[i].appendChild(d);
184 function rcmakemessageUI(message) {
185 message.UI = document.createElement("div");
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);
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);
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);
208 // Classify different message types
209 var text_span = document.createElement("span");
211 if (/^\*\*\* /.test(message.Text)) {
213 } else if (/^\* /.test(message.Text)) {
218 if (Object.keys(message.ServerTimes).length == 0) {
221 text_span.setAttribute("class", type);
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);
230 text_span.appendChild(document.createTextNode(leading_text));
232 var anchor = document.createElement("a");
233 anchor.setAttribute("href", encodeURI(match[0]));
234 anchor.appendChild(document.createTextNode(match[0]));
235 text_span.appendChild(anchor);
236 text = text.substr(match.index + match[0].length);
239 text_span.appendChild(document.createTextNode(text));
242 message.UI.appendChild(text_span);
245 function rcaddmessagetohistory(message) {
248 for (var i = history.length - 1; ; i--) {
249 if (i < 0 || (history[i].Time && message.Time >= history[i].Time)) {
251 history.splice(message_i, 0, message);
256 history.push(message);
257 message_i = history.length-1;
261 rcmakemessageUI(message);
263 var h = document.getElementById("history");
264 if (message_i + 1 < history.length) {
265 h.insertBefore(message.UI, history[message_i + 1].UI);
267 h.appendChild(message.UI);
269 window.scrollTo(0, document.body.scrollHeight);
272 function make_seen_key(id, text) {
273 return id.replace(/@/g, "@@") + "_@_" + text.replace(/@/g, "@@");
276 function rcupdatemessagetime(message) {
277 // Set message.Time to be the median of message.ServerTimes
279 for (var i in message.ServerTimes) {
280 times.push(message.ServerTimes[i]);
283 if (times.length % 2) {
284 message.Time = times[(times.length-1)/2];
286 var middle = times.length/2;
287 var difference = times[middle].getTime() - times[middle-1].getTime();
288 message.Time = new Date(times[middle-1].getTime() + difference/2);
291 // This may have broken history's in-sorted-order invariant
292 var hi = history.indexOf(message);
293 if ((history[hi-1] && history[hi-1].Time > message.Time) ||
294 (history[hi+1] && history[hi+1].Time < message.Time)) {
295 history.splice(hi,1);
296 rcaddmessagetohistory(message);
300 var spans = message.UI.getElementsByTagName("span");
301 for (var i in spans) {
302 if (spans[i].getAttribute) {
303 var type = spans[i].getAttribute("class");
304 if (type == "servercount") {
305 spans[i].firstChild.data = Object.keys(message.ServerTimes).length;
306 } else if (type == "timestamp") {
307 spans[i].firstChild.data = rcformattime(message.Time);
313 function rcreceivemessages(server, messages) {
314 for (var i in messages) {
316 m.Time = new Date(m.Time);
317 var seen_key = make_seen_key(m.ID, m.Text);
318 if (seen_key in seen) {
319 seen[seen_key].ServerTimes[server] = m.Time;
320 rcupdatemessagetime(seen[seen_key]);
321 rcaddservertimestamptohover(seen[seen_key], server);
324 m.ServerTimes[server] = m.Time;
326 rcaddmessagetohistory(m);
327 for (var i in servers) {
328 rcchangeserverstatus(servers[i], "sad");
331 rcchangeserverstatus(server, "happy");
335 function rcfetch(server) {
336 var delay = 10000; // TODO: Exponential backoff
337 var xhr = new XMLHttpRequest();
338 xhr.onreadystatechange = function() {
339 if (this.readyState == this.DONE) {
340 if (this.status == 200) {
341 var rtxt = this.responseText;
343 var messages = JSON.parse(rtxt);
344 if (messages != null) {
346 if (messages.length >= 1 && "Time" in messages[messages.length-1]) {
347 since[server] = messages[messages.length-1].Time;
349 rcreceivemessages(server, messages);
353 window.setTimeout(rcfetch, delay, server);
356 var uri = rcserverbase(server) + "/fetch";
357 if (server in since) {
358 uri += '?since="' + since[server] + '"';
360 xhr.open("GET", uri);
364 function rcconnect() {
365 for (var i in servers) {
368 var status_indicator = document.createElement("span");
369 status_indicator.appendChild(document.createTextNode(servers[i]));
370 status_indicator.setAttribute("class", "sad");
371 document.getElementById("status").appendChild(status_indicator);
375 function rcsend(d, message) {
376 message.ID = new Date().getTime() + "-" + session + "-" + Math.random();
377 seen[make_seen_key(message.ID, message.Text)] = message;
378 var path = "/speak" +
379 "?id=" + encodeURIComponent(message.ID) +
380 "&text=" + encodeURIComponent(message.Text);
381 for (var i in servers) {
382 var uri = rcserverbase(servers[i]) + path;
383 var img = document.createElement("img");
384 img.setAttribute("src", uri);
389 function rcinput(input) {
391 var re = /^\/([a-z]+) (.*)/
392 var match = re.exec(input);
393 if (match && match[1] == 'me') {
394 message = "* " + rcnick() + " " + match[2];
395 } else if (match && match[1] == 'nick') {
396 message = "*** " + rcnick() + " is now known as " + match[2];
399 message = "<" + rcnick() + "> " + input;
402 var m = {'Text': message, 'ServerTimes': {}};
403 rcaddmessagetohistory(m);
407 function rckeydown(event) {
408 if (event.keyCode == 13) {
409 rcinput(document.input.say.value);
410 document.input.say.value = "";
418 <body onload=
"rcconnect()">
420 <div id=
"history"></div>
423 <form name=
"input" onsubmit=
"return false" autocomplete=
"off">
424 <input id=
"say" onkeydown=
"return rckeydown(event)" autocomplete=
"off" autofocus=
"autofocus"></input>
426 <div id=
"status"> </div>