]> git.scottworley.com Git - reliable-chat/blob - webclient/rc.html
c3284da03b1480583a40cfb4eb93b0d332d22196
[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 img { width: 1px; height: 1px; }
60 iframe { display: none }
61 #status span { margin-right: 10px; }
62 #status span.sad {
63 background-color: #f00;
64 color: #fff;
65 border: 1px solid black;
66 border-radius: 5px;
67 padding-left: 5px;
68 padding-right: 5px;
69 }
70 #status span.happy {
71 background-color: #0f0;
72 color: #000;
73 border: 1px solid black;
74 border-radius: 5px;
75 padding-left: 5px;
76 padding-right: 5px;
77 }
78 /*]]>*/--></style>
79 <script type="text/javascript"><!--//--><![CDATA[//><!--
80 var servers = ['chkno.net', 'rc2.chkno.net', 'echto.net', 'the-wes.com', 'vibrantlogic.com'];
81
82 var session = Math.random(); // For outgoing message IDs
83 var since = {}; // server -> time: For fetch?since=
84 var seen = {}; // seen_key -> message
85 var history = []; // List of messages sorted by Time
86 // Messages have these fields:
87 // Time: The timestamp. Median of ServerTimes
88 // ID: Some unique string for deduping
89 // Text: The text of the message
90 // ServerTimes: server -> timestamp
91 // UI: The DOM node for this message in the UI
92
93 function rcnick() {
94 var nick = localStorage.getItem("nick");
95 if (nick) {
96 return nick;
97 }
98 return 'anonymous';
99 }
100
101 function rcsetnick(new_nick) {
102 localStorage.setItem("nick", new_nick);
103 }
104
105 function rcserverbase(server) {
106 // Add the default port if server doesn't contain a port number already
107 if (server.indexOf(":") == -1) {
108 return "http://" + server + ":21059";
109 } else {
110 return "http://" + server;
111 }
112 }
113
114 function rcchangeserverstatus(server, new_status) {
115 var statusbar = document.getElementById("status");
116 var spans = statusbar.getElementsByTagName("span");
117 for (var i in spans) {
118 if (spans[i].firstChild && 'data' in spans[i].firstChild && spans[i].firstChild.data == server) {
119 spans[i].setAttribute("class", new_status);
120 }
121 }
122 }
123
124 function rcformattime(t) {
125 var d = t.getDay();
126 d = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"][d];
127 var h = t.getHours();
128 var m = t.getMinutes();
129 var s = t.getSeconds();
130 function pad(x) {
131 return (x < 10 ? "0" : "") + x;
132 }
133 return d + " " + pad(h) + ":" + pad(m) + ":" + pad(s);
134 }
135
136 function rcaddmessagetohistory(message) {
137 var message_i;
138 if (message.Time) {
139 for (var i = history.length - 1; ; i--) {
140 if (i < 0 || (history[i].Time && message.Time >= history[i].Time)) {
141 message_i = i+1;
142 history.splice(message_i, 0, message);
143 break;
144 }
145 }
146 } else {
147 history.push(message);
148 message_i = history.length-1;
149 }
150
151 var text = (message.Time ? rcformattime(message.Time) : "") + " " + message.Text;
152 message.UI = document.createElement("div");
153 message.UI.appendChild(document.createTextNode(text));
154 var h = document.getElementById("history");
155 if (message_i + 1 < history.length) {
156 h.insertBefore(message.UI, history[message_i + 1].UI);
157 } else {
158 h.appendChild(message.UI);
159 }
160 window.scrollTo(0, document.body.scrollHeight);
161 }
162
163 function make_seen_key(id, text) {
164 return id.replace(/@/g, "@@") + "_@_" + text.replace(/@/g, "@@");
165 }
166
167 function rcupdatemessagetime(message) {
168 // Set message.Time to be the median of message.ServerTimes
169 var times = [];
170 for (var i in message.ServerTimes) {
171 times.push(message.ServerTimes[i]);
172 }
173 times.sort();
174 var middle = times.length/2;
175 if (times.length % 2) {
176 message.Time = times[middle];
177 } else {
178 var difference = times[middle].getTime() - times[middle-1].getTime();
179 message.Time = new Date(times[middle-1].getTime() + difference/2);
180 }
181 }
182
183 function rcreceivemessages(server, messages) {
184 for (var i in messages) {
185 var m = messages[i];
186 m.Time = new Date(m.Time);
187 var seen_key = make_seen_key(m.ID, m.Text);
188 if (seen_key in seen) {
189 seen[seen_key].ServerTimes[server] = m.Time;
190 rcupdatemessagetime(seen[seen_key]);
191 } else {
192 m.ServerTimes = {};
193 m.ServerTimes[server] = m.Time;
194 seen[seen_key] = m;
195 rcaddmessagetohistory(m);
196 for (var i in servers) {
197 rcchangeserverstatus(servers[i], "sad");
198 }
199 }
200 rcchangeserverstatus(server, "happy");
201 }
202 }
203
204 function rcfetch(server) {
205 var delay = 10000; // TODO: Exponential backoff
206 var xhr = new XMLHttpRequest();
207 xhr.onreadystatechange = function() {
208 if (this.readyState == this.DONE) {
209 if (this.status == 200) {
210 var rtxt = this.responseText;
211 if (rtxt != null) {
212 var messages = JSON.parse(rtxt);
213 if (messages != null) {
214 delay = 40;
215 if (messages.length >= 1 && "Time" in messages[messages.length-1]) {
216 since[server] = messages[messages.length-1].Time;
217 }
218 rcreceivemessages(server, messages);
219 }
220 }
221 }
222 window.setTimeout(rcfetch, delay, server);
223 }
224 }
225 var uri = rcserverbase(server) + "/fetch";
226 if (server in since) {
227 uri += '?since="' + since[server] + '"';
228 }
229 xhr.open("GET", uri);
230 xhr.send();
231 }
232
233 function rcconnect() {
234 for (var i in servers) {
235 rcfetch(servers[i]);
236 // Status bar entry
237 var status_indicator = document.createElement("span");
238 status_indicator.appendChild(document.createTextNode(servers[i]));
239 status_indicator.setAttribute("class", "sad");
240 document.getElementById("status").appendChild(status_indicator);
241 }
242 }
243
244 function rcsend(d, message) {
245 message.ID = new Date().getTime() + "-" + session + "-" + Math.random();
246 seen[make_seen_key(message.ID, message.Text)] = message;
247 var path = "/speak" +
248 "?id=" + encodeURIComponent(message.ID) +
249 "&text=" + encodeURIComponent(message.Text);
250 for (var i in servers) {
251 var uri = rcserverbase(servers[i]) + path;
252 var img = document.createElement("img");
253 img.setAttribute("src", uri);
254 d.appendChild(img);
255 }
256 }
257
258 function rcinput(input) {
259 var message;
260 var re = /^\/([a-z]+) (.*)/
261 var match = re.exec(input);
262 if (match && match[1] == 'me') {
263 message = "* " + rcnick() + " " + match[2];
264 } else if (match && match[1] == 'nick') {
265 message = "*** " + rcnick() + " is now known as " + match[2];
266 rcsetnick(match[2]);
267 } else {
268 message = "<" + rcnick() + "> " + input;
269 }
270
271 var m = {'Text': message, 'ServerTimes': {}};
272 rcaddmessagetohistory(m);
273 rcsend(m.UI, m);
274 }
275
276 function rckeydown(event) {
277 if (event.keyCode == 13) {
278 rcinput(document.input.say.value);
279 document.input.say.value = "";
280
281 }
282 }
283 //--><!]]></script>
284
285 </head>
286
287 <body onload="rcconnect()">
288 <div id="container">
289 <div id="history"></div>
290 <div id="client">
291 <div id="input">
292 <form name="input" onsubmit="return false" autocomplete="off">
293 <input id="say" onkeydown="return rckeydown(event)" autocomplete="off" autofocus="autofocus"></input>
294 </form></div>
295 <div id="status">&nbsp;</div>
296 </div>
297 </div>
298 </body>
299 </html>