]> git.scottworley.com Git - reliable-chat/blob - webclient/rc.html
9bbed4ef3a6dc4e25e273cd57af18fccfd13602a
[reliable-chat] / 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">
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 img { width: 1px; height: 1px; }
67 iframe { display: none }
68 #status span { margin-right: 10px; }
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 }
85 /*]]>*/--></style>
86 <script type="text/javascript"><!--//--><![CDATA[//><!--
87 var servers = ['chkno.net', 'rc2.chkno.net', 'echto.net', 'the-wes.com', 'vibrantlogic.com'];
88
89 var session = Math.random(); // For outgoing message IDs
90 var since = {}; // server -> time: For fetch?since=
91 var seen = {}; // seen_key -> message
92 var history = []; // List of messages sorted by Time
93 // Messages have these fields:
94 // Time: The timestamp. Median of ServerTimes
95 // ID: Some unique string for deduping
96 // Text: The text of the message
97 // ServerTimes: server -> timestamp
98 // UI: The DOM node for this message in the UI
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
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
143 function rcaddmessagetohistory(message) {
144 var message_i;
145 if (message.Time) {
146 for (var i = history.length - 1; ; i--) {
147 if (i < 0 || (history[i].Time && message.Time >= history[i].Time)) {
148 message_i = i+1;
149 history.splice(message_i, 0, message);
150 break;
151 }
152 }
153 } else {
154 history.push(message);
155 message_i = history.length-1;
156 }
157
158 if (!message.UI) {
159 message.UI = document.createElement("div");
160
161 // Server count
162 var servercount = document.createElement("span");
163 servercount.setAttribute("class", "servercount");
164 servercount.appendChild(document.createTextNode(Object.keys(message.ServerTimes).length));
165 message.UI.appendChild(servercount);
166
167 // Timestamp
168 var timestamp_text = message.Time ? rcformattime(message.Time) : "";
169 var timestamp = document.createElement("span");
170 timestamp.setAttribute("class", "timestamp");
171 timestamp.appendChild(document.createTextNode(timestamp_text));
172 message.UI.appendChild(timestamp);
173
174 message.UI.appendChild(document.createTextNode(message.Text));
175 }
176 var h = document.getElementById("history");
177 if (message_i + 1 < history.length) {
178 h.insertBefore(message.UI, history[message_i + 1].UI);
179 } else {
180 h.appendChild(message.UI);
181 }
182 window.scrollTo(0, document.body.scrollHeight);
183 }
184
185 function make_seen_key(id, text) {
186 return id.replace(/@/g, "@@") + "_@_" + text.replace(/@/g, "@@");
187 }
188
189 function rcupdatemessagetime(message) {
190 // Set message.Time to be the median of message.ServerTimes
191 var times = [];
192 for (var i in message.ServerTimes) {
193 times.push(message.ServerTimes[i]);
194 }
195 times.sort();
196 if (times.length % 2) {
197 message.Time = times[(times.length-1)/2];
198 } else {
199 var middle = times.length/2;
200 var difference = times[middle].getTime() - times[middle-1].getTime();
201 message.Time = new Date(times[middle-1].getTime() + difference/2);
202 }
203
204 // This may have broken history's in-sorted-order invariant
205 var hi = history.indexOf(message);
206 if ((history[hi-1] && history[hi-1].Time > message.Time) ||
207 (history[hi+1] && history[hi+1].Time < message.Time)) {
208 history.splice(hi,1);
209 rcaddmessagetohistory(message);
210 }
211
212 // Update the UI
213 var spans = message.UI.getElementsByTagName("span");
214 for (var i in spans) {
215 if (spans[i].getAttribute) {
216 var type = spans[i].getAttribute("class");
217 if (type == "servercount") {
218 spans[i].firstChild.data = Object.keys(message.ServerTimes).length;
219 } else if (type == "timestamp") {
220 spans[i].firstChild.data = rcformattime(message.Time);
221 }
222 }
223 }
224 }
225
226 function rcreceivemessages(server, messages) {
227 for (var i in messages) {
228 var m = messages[i];
229 m.Time = new Date(m.Time);
230 var seen_key = make_seen_key(m.ID, m.Text);
231 if (seen_key in seen) {
232 seen[seen_key].ServerTimes[server] = m.Time;
233 rcupdatemessagetime(seen[seen_key]);
234 } else {
235 m.ServerTimes = {};
236 m.ServerTimes[server] = m.Time;
237 seen[seen_key] = m;
238 rcaddmessagetohistory(m);
239 for (var i in servers) {
240 rcchangeserverstatus(servers[i], "sad");
241 }
242 }
243 rcchangeserverstatus(server, "happy");
244 }
245 }
246
247 function rcfetch(server) {
248 var delay = 10000; // TODO: Exponential backoff
249 var xhr = new XMLHttpRequest();
250 xhr.onreadystatechange = function() {
251 if (this.readyState == this.DONE) {
252 if (this.status == 200) {
253 var rtxt = this.responseText;
254 if (rtxt != null) {
255 var messages = JSON.parse(rtxt);
256 if (messages != null) {
257 delay = 40;
258 if (messages.length >= 1 && "Time" in messages[messages.length-1]) {
259 since[server] = messages[messages.length-1].Time;
260 }
261 rcreceivemessages(server, messages);
262 }
263 }
264 }
265 window.setTimeout(rcfetch, delay, server);
266 }
267 }
268 var uri = rcserverbase(server) + "/fetch";
269 if (server in since) {
270 uri += '?since="' + since[server] + '"';
271 }
272 xhr.open("GET", uri);
273 xhr.send();
274 }
275
276 function rcconnect() {
277 for (var i in servers) {
278 rcfetch(servers[i]);
279 // Status bar entry
280 var status_indicator = document.createElement("span");
281 status_indicator.appendChild(document.createTextNode(servers[i]));
282 status_indicator.setAttribute("class", "sad");
283 document.getElementById("status").appendChild(status_indicator);
284 }
285 }
286
287 function rcsend(d, message) {
288 message.ID = new Date().getTime() + "-" + session + "-" + Math.random();
289 seen[make_seen_key(message.ID, message.Text)] = message;
290 var path = "/speak" +
291 "?id=" + encodeURIComponent(message.ID) +
292 "&text=" + encodeURIComponent(message.Text);
293 for (var i in servers) {
294 var uri = rcserverbase(servers[i]) + path;
295 var img = document.createElement("img");
296 img.setAttribute("src", uri);
297 d.appendChild(img);
298 }
299 }
300
301 function rcinput(input) {
302 var message;
303 var re = /^\/([a-z]+) (.*)/
304 var match = re.exec(input);
305 if (match && match[1] == 'me') {
306 message = "* " + rcnick() + " " + match[2];
307 } else if (match && match[1] == 'nick') {
308 message = "*** " + rcnick() + " is now known as " + match[2];
309 rcsetnick(match[2]);
310 } else {
311 message = "<" + rcnick() + "> " + input;
312 }
313
314 var m = {'Text': message, 'ServerTimes': {}};
315 rcaddmessagetohistory(m);
316 rcsend(m.UI, m);
317 }
318
319 function rckeydown(event) {
320 if (event.keyCode == 13) {
321 rcinput(document.input.say.value);
322 document.input.say.value = "";
323
324 }
325 }
326 //--><!]]></script>
327
328 </head>
329
330 <body onload="rcconnect()">
331 <div id="container">
332 <div id="history"></div>
333 <div id="client">
334 <div id="input">
335 <form name="input" onsubmit="return false" autocomplete="off">
336 <input id="say" onkeydown="return rckeydown(event)" autocomplete="off" autofocus="autofocus"></input>
337 </form></div>
338 <div id="status">&nbsp;</div>
339 </div>
340 </div>
341 </body>
342 </html>