]> git.scottworley.com Git - reliable-chat/blame - webclient/rc.html
Documentation: What's in a message
[reliable-chat] / webclient / rc.html
CommitLineData
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 }
0248a518
SW
59 img { width: 1px; height: 1px; }
60 iframe { display: none }
70f46223 61 #status span { margin-right: 10px; }
f09c4ede
JH
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 }
0248a518
SW
78 /*]]>*/--></style>
79 <script type="text/javascript"><!--//--><![CDATA[//><!--
80 var servers = ['chkno.net', 'rc2.chkno.net', 'echto.net', 'the-wes.com', 'vibrantlogic.com'];
81
da9ce2ab 82 var session = Math.random(); // For outgoing message IDs
869430fa
SW
83 var since = {}; // server -> time: For fetch?since=
84 var seen = {}; // seen_key -> message
5419ea4c
SW
85 // Messages have these fields:
86 // Time: The server timestamp of the first copy to arrive
87 // ID: Some unique string for deduping
88 // Text: The text of the message
89 // UI: The DOM node for this message in the UI
0248a518
SW
90
91 function rcnick() {
92 var nick = localStorage.getItem("nick");
93 if (nick) {
94 return nick;
95 }
96 return 'anonymous';
97 }
98
99 function rcsetnick(new_nick) {
100 localStorage.setItem("nick", new_nick);
101 }
102
103 function rcserverbase(server) {
104 // Add the default port if server doesn't contain a port number already
105 if (server.indexOf(":") == -1) {
106 return "http://" + server + ":21059";
107 } else {
108 return "http://" + server;
109 }
110 }
111
112 function rcchangeserverstatus(server, new_status) {
113 var statusbar = document.getElementById("status");
114 var spans = statusbar.getElementsByTagName("span");
115 for (var i in spans) {
116 if (spans[i].firstChild && 'data' in spans[i].firstChild && spans[i].firstChild.data == server) {
117 spans[i].setAttribute("class", new_status);
118 }
119 }
120 }
121
bd1ed9dd
SW
122 function rcformattime(t) {
123 var d = t.getDay();
124 d = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"][d];
125 var h = t.getHours();
126 var m = t.getMinutes();
127 var s = t.getSeconds();
128 function pad(x) {
129 return (x < 10 ? "0" : "") + x;
130 }
131 return d + " " + pad(h) + ":" + pad(m) + ":" + pad(s);
132 }
133
0248a518 134 function rcaddmessagetohistory(message) {
869430fa 135 message.UI = document.createElement("div");
bd1ed9dd 136 var text = (message.Time ? rcformattime(message.Time) : "") + " " + message.Text;
869430fa 137 message.UI.appendChild(document.createTextNode(text));
0248a518 138 var h = document.getElementById("history");
869430fa 139 h.appendChild(message.UI);
0248a518 140 window.scrollTo(0, document.body.scrollHeight);
0248a518
SW
141 }
142
143 function make_seen_key(id, text) {
144 return id.replace(/@/g, "@@") + "_@_" + text.replace(/@/g, "@@");
145 }
146
79ced6f1
SW
147 function rcreceivemessages(server, messages) {
148 for (var i in messages) {
ad35b1be 149 var m = messages[i];
bd1ed9dd 150 m.Time = new Date(m.Time);
ad35b1be 151 var seen_key = make_seen_key(m.ID, m.Text);
79ced6f1 152 if (!(seen_key in seen)) {
869430fa 153 seen[seen_key] = m;
ad35b1be 154 rcaddmessagetohistory(m);
79ced6f1
SW
155 for (var i in servers) {
156 rcchangeserverstatus(servers[i], "sad");
157 }
0248a518 158 }
79ced6f1 159 rcchangeserverstatus(server, "happy");
0248a518 160 }
0248a518
SW
161 }
162
79ced6f1
SW
163 function rcfetch(server) {
164 var delay = 10000; // TODO: Exponential backoff
165 var xhr = new XMLHttpRequest();
166 xhr.onreadystatechange = function() {
167 if (this.readyState == this.DONE) {
168 if (this.status == 200) {
169 var rtxt = this.responseText;
170 if (rtxt != null) {
171 var messages = JSON.parse(rtxt);
172 if (messages != null) {
79ced6f1
SW
173 delay = 40;
174 if (messages.length >= 1 && "Time" in messages[messages.length-1]) {
ad35b1be 175 since[server] = messages[messages.length-1].Time;
79ced6f1 176 }
92ca5f8a 177 rcreceivemessages(server, messages);
79ced6f1 178 }
0248a518
SW
179 }
180 }
79ced6f1 181 window.setTimeout(rcfetch, delay, server);
0248a518
SW
182 }
183 }
79ced6f1
SW
184 var uri = rcserverbase(server) + "/fetch";
185 if (server in since) {
186 uri += '?since="' + since[server] + '"';
187 }
188 xhr.open("GET", uri);
189 xhr.send();
0248a518
SW
190 }
191
192 function rcconnect() {
0248a518 193 for (var i in servers) {
79ced6f1 194 rcfetch(servers[i]);
0248a518
SW
195 // Status bar entry
196 var status_indicator = document.createElement("span");
197 status_indicator.appendChild(document.createTextNode(servers[i]));
198 status_indicator.setAttribute("class", "sad");
199 document.getElementById("status").appendChild(status_indicator);
200 }
0248a518
SW
201 }
202
203 function rcsend(d, message) {
204 var id = new Date().getTime() + "-" + session + "-" + Math.random();
869430fa 205 seen[make_seen_key(id, message)] = message;
0248a518
SW
206 var path = "/speak" +
207 "?id=" + encodeURIComponent(id) +
208 "&text=" + encodeURIComponent(message);
209 for (var i in servers) {
210 var uri = rcserverbase(servers[i]) + path;
211 var img = document.createElement("img");
212 img.setAttribute("src", uri);
213 d.appendChild(img);
214 }
215 }
216
5a6c082a
SW
217 function rcinput(input) {
218 var message;
219 var re = /^\/([a-z]+) (.*)/
220 var match = re.exec(input);
221 if (match && match[1] == 'me') {
222 message = "* " + rcnick() + " " + match[2];
223 } else if (match && match[1] == 'nick') {
224 message = "*** " + rcnick() + " is now known as " + match[2];
225 rcsetnick(match[2]);
226 } else {
227 message = "<" + rcnick() + "> " + input;
228 }
229
869430fa
SW
230 var m = {'Text': message};
231 rcaddmessagetohistory(m);
232 rcsend(m.UI, m);
5a6c082a
SW
233 }
234
0248a518
SW
235 function rckeydown(event) {
236 if (event.keyCode == 13) {
5a6c082a 237 rcinput(document.input.say.value);
0248a518
SW
238 document.input.say.value = "";
239
0248a518
SW
240 }
241 }
242 //--><!]]></script>
243
827f21bb
SW
244</head>
245
f0e385c7 246<body onload="rcconnect()">
70f46223
JH
247 <div id="container">
248 <div id="history"></div>
249 <div id="client">
250 <div id="input">
251 <form name="input" onsubmit="return false" autocomplete="off">
252 <input id="say" onkeydown="return rckeydown(event)" autocomplete="off" autofocus="autofocus"></input>
253 </form></div>
254 <div id="status">&nbsp;</div>
255 </div>
256 </div>
827f21bb
SW
257</body>
258</html>