]> git.scottworley.com Git - reliable-chat/blob - webclient/rc.html
b3109a92ee387b858891d72a9f2fdf10f0641d0b
[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
4 <html xmlns="http://www.w3.org/1999/xhtml">
5 <head>
6 <title>Reliable Chat</title>
7 <style type="text/css"><!--/*--><![CDATA[/*><!--*/
8 html, body {
9 width: 99.9%;
10 height: 100%;
11 margin: 0;
12 padding: 0;
13 background-color: #293134;
14 color: silver;
15 font-family: monospace;
16 }
17 #container {
18 height: 100%;
19 }
20 #status {
21 width: 100%;
22 text-align: right;
23 background-color: #293134;
24 padding: 5px 5px 5px 0px;
25 }
26 #client {
27 width: 98.5%;
28 padding: 0px 0px 0px 5px;
29 height: 50px;
30 position: fixed;
31 bottom: 0;
32 }
33 #input {
34 width: 100%;
35 background-color: #293134;
36 }
37 #say { width: 100% }
38 #history {
39 padding: 0px 5px 55px 5px;
40 vertical-align: bottom
41 }
42 img { width: 1px; height: 1px; }
43 iframe { display: none }
44 #status span { margin-right: 10px; }
45 #status span.sad {
46 background-color: #f00;
47 color: #fff;
48 border: 1px solid black;
49 border-radius: 5px;
50 padding-left: 5px;
51 padding-right: 5px;
52 }
53 #status span.happy {
54 background-color: #0f0;
55 color: #000;
56 border: 1px solid black;
57 border-radius: 5px;
58 padding-left: 5px;
59 padding-right: 5px;
60 }
61 /*]]>*/--></style>
62 <script type="text/javascript"><!--//--><![CDATA[//><!--
63 var servers = ['chkno.net', 'rc2.chkno.net', 'echto.net', 'the-wes.com', 'vibrantlogic.com'];
64
65 var session = Math.random();
66 var since = {};
67 var seen = {};
68
69 function rcnick() {
70 var nick = localStorage.getItem("nick");
71 if (nick) {
72 return nick;
73 }
74 return 'anonymous';
75 }
76
77 function rcsetnick(new_nick) {
78 localStorage.setItem("nick", new_nick);
79 }
80
81 function rcserverbase(server) {
82 // Add the default port if server doesn't contain a port number already
83 if (server.indexOf(":") == -1) {
84 return "http://" + server + ":21059";
85 } else {
86 return "http://" + server;
87 }
88 }
89
90 function rcchangeserverstatus(server, new_status) {
91 var statusbar = document.getElementById("status");
92 var spans = statusbar.getElementsByTagName("span");
93 for (var i in spans) {
94 if (spans[i].firstChild && 'data' in spans[i].firstChild && spans[i].firstChild.data == server) {
95 spans[i].setAttribute("class", new_status);
96 }
97 }
98 }
99
100 function rcaddmessagetohistory(message) {
101 var d = document.createElement("div");
102 d.appendChild(document.createTextNode(message));
103 var h = document.getElementById("history");
104 h.appendChild(d);
105 window.scrollTo(0, document.body.scrollHeight);
106 return d;
107 }
108
109 function make_seen_key(id, text) {
110 return id.replace(/@/g, "@@") + "_@_" + text.replace(/@/g, "@@");
111 }
112
113 function rcreceivemessages(server, messages) {
114 for (var i in messages) {
115 var seen_key = make_seen_key(messages[i]['ID'], messages[i]['Text']);
116 if (!(seen_key in seen)) {
117 seen[seen_key] = true;
118 rcaddmessagetohistory(messages[i]['Text']);
119 for (var i in servers) {
120 rcchangeserverstatus(servers[i], "sad");
121 }
122 }
123 rcchangeserverstatus(server, "happy");
124 }
125 }
126
127 function rcfetch(server) {
128 var delay = 10000; // TODO: Exponential backoff
129 var xhr = new XMLHttpRequest();
130 xhr.onreadystatechange = function() {
131 if (this.readyState == this.DONE) {
132 if (this.status == 200) {
133 var rtxt = this.responseText;
134 if (rtxt != null) {
135 var messages = JSON.parse(rtxt);
136 if (messages != null) {
137 rcreceivemessages(server, messages);
138 window.parent.postMessage(rtxt, "*");
139 delay = 40;
140 if (messages.length >= 1 && "Time" in messages[messages.length-1]) {
141 since[server] = messages[messages.length-1]["Time"];
142 }
143 }
144 }
145 }
146 window.setTimeout(rcfetch, delay, server);
147 }
148 }
149 var uri = rcserverbase(server) + "/fetch";
150 if (server in since) {
151 uri += '?since="' + since[server] + '"';
152 }
153 xhr.open("GET", uri);
154 xhr.send();
155 }
156
157 function rcconnect() {
158 for (var i in servers) {
159 rcfetch(servers[i]);
160 // Status bar entry
161 var status_indicator = document.createElement("span");
162 status_indicator.appendChild(document.createTextNode(servers[i]));
163 status_indicator.setAttribute("class", "sad");
164 document.getElementById("status").appendChild(status_indicator);
165 }
166 if (rcnick() == 'anonymous') {
167 rcaddmessagetohistory("-!- Set your nick with /nick");
168 }
169 }
170
171 function rcsend(d, message) {
172 var id = new Date().getTime() + "-" + session + "-" + Math.random();
173 seen[make_seen_key(id, message)] = true;
174 var path = "/speak" +
175 "?id=" + encodeURIComponent(id) +
176 "&text=" + encodeURIComponent(message);
177 for (var i in servers) {
178 var uri = rcserverbase(servers[i]) + path;
179 var img = document.createElement("img");
180 img.setAttribute("src", uri);
181 d.appendChild(img);
182 }
183 }
184
185 function rckeydown(event) {
186 if (event.keyCode == 13) {
187 var input = document.input.say.value;
188 document.input.say.value = "";
189
190 // Check nick change
191 var message;
192 var re = /^\/nick (.*)/;
193 var match = re.exec(input);
194 if (match) {
195 message = "*** " + rcnick() + " is now known as " + match[1];
196 rcsetnick(match[1]);
197 } else {
198 message = "<" + rcnick() + "> " + input;
199 }
200
201 // /me support
202 var message;
203 var re = /^\/me (.*)/;
204 var match = re.exec(input);
205 var inputme = input.substring(4);
206 if (match) {
207 message = "* " + rcnick() + " " + inputme;
208 } else {
209 message = "<" + rcnick() + "> " + input;
210 }
211
212 // Say the message
213 var d = rcaddmessagetohistory(message);
214 rcsend(d, message);
215
216 // Remind people to set their nick
217 if (rcnick() == 'anonymous') {
218 rcaddmessagetohistory("-!- Set your nick with /nick");
219 }
220 }
221 }
222 //--><!]]></script>
223
224 </head>
225
226 <body onload="rcconnect()">
227 <div id="container">
228 <div id="history"></div>
229 <div id="client">
230 <div id="input">
231 <form name="input" onsubmit="return false" autocomplete="off">
232 <input id="say" onkeydown="return rckeydown(event)" autocomplete="off" autofocus="autofocus"></input>
233 </form></div>
234 <div id="status">&nbsp;</div>
235 </div>
236 </div>
237 </body>
238 </html>