]> git.scottworley.com Git - reliable-chat/blame_incremental - webclient/rc.html
Remove useless postMessage call
[reliable-chat] / webclient / rc.html
... / ...
CommitLineData
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();
83 var since = {};
84 var seen = {};
85
86 function rcnick() {
87 var nick = localStorage.getItem("nick");
88 if (nick) {
89 return nick;
90 }
91 return 'anonymous';
92 }
93
94 function rcsetnick(new_nick) {
95 localStorage.setItem("nick", new_nick);
96 }
97
98 function rcserverbase(server) {
99 // Add the default port if server doesn't contain a port number already
100 if (server.indexOf(":") == -1) {
101 return "http://" + server + ":21059";
102 } else {
103 return "http://" + server;
104 }
105 }
106
107 function rcchangeserverstatus(server, new_status) {
108 var statusbar = document.getElementById("status");
109 var spans = statusbar.getElementsByTagName("span");
110 for (var i in spans) {
111 if (spans[i].firstChild && 'data' in spans[i].firstChild && spans[i].firstChild.data == server) {
112 spans[i].setAttribute("class", new_status);
113 }
114 }
115 }
116
117 function rcaddmessagetohistory(message) {
118 var d = document.createElement("div");
119 d.appendChild(document.createTextNode(message));
120 var h = document.getElementById("history");
121 h.appendChild(d);
122 window.scrollTo(0, document.body.scrollHeight);
123 return d;
124 }
125
126 function make_seen_key(id, text) {
127 return id.replace(/@/g, "@@") + "_@_" + text.replace(/@/g, "@@");
128 }
129
130 function rcreceivemessages(server, messages) {
131 for (var i in messages) {
132 var seen_key = make_seen_key(messages[i]['ID'], messages[i]['Text']);
133 if (!(seen_key in seen)) {
134 seen[seen_key] = true;
135 rcaddmessagetohistory(messages[i]['Text']);
136 for (var i in servers) {
137 rcchangeserverstatus(servers[i], "sad");
138 }
139 }
140 rcchangeserverstatus(server, "happy");
141 }
142 }
143
144 function rcfetch(server) {
145 var delay = 10000; // TODO: Exponential backoff
146 var xhr = new XMLHttpRequest();
147 xhr.onreadystatechange = function() {
148 if (this.readyState == this.DONE) {
149 if (this.status == 200) {
150 var rtxt = this.responseText;
151 if (rtxt != null) {
152 var messages = JSON.parse(rtxt);
153 if (messages != null) {
154 rcreceivemessages(server, messages);
155 delay = 40;
156 if (messages.length >= 1 && "Time" in messages[messages.length-1]) {
157 since[server] = messages[messages.length-1]["Time"];
158 }
159 }
160 }
161 }
162 window.setTimeout(rcfetch, delay, server);
163 }
164 }
165 var uri = rcserverbase(server) + "/fetch";
166 if (server in since) {
167 uri += '?since="' + since[server] + '"';
168 }
169 xhr.open("GET", uri);
170 xhr.send();
171 }
172
173 function rcconnect() {
174 for (var i in servers) {
175 rcfetch(servers[i]);
176 // Status bar entry
177 var status_indicator = document.createElement("span");
178 status_indicator.appendChild(document.createTextNode(servers[i]));
179 status_indicator.setAttribute("class", "sad");
180 document.getElementById("status").appendChild(status_indicator);
181 }
182 if (rcnick() == 'anonymous') {
183 rcaddmessagetohistory("-!- Set your nick with /nick");
184 }
185 }
186
187 function rcsend(d, message) {
188 var id = new Date().getTime() + "-" + session + "-" + Math.random();
189 seen[make_seen_key(id, message)] = true;
190 var path = "/speak" +
191 "?id=" + encodeURIComponent(id) +
192 "&text=" + encodeURIComponent(message);
193 for (var i in servers) {
194 var uri = rcserverbase(servers[i]) + path;
195 var img = document.createElement("img");
196 img.setAttribute("src", uri);
197 d.appendChild(img);
198 }
199 }
200
201 function rckeydown(event) {
202 if (event.keyCode == 13) {
203 var input = document.input.say.value;
204 document.input.say.value = "";
205
206 // Check nick change
207 var message;
208 var re = /^\/nick (.*)/;
209 var match = re.exec(input);
210 if (match) {
211 message = "*** " + rcnick() + " is now known as " + match[1];
212 rcsetnick(match[1]);
213 } else {
214 message = "<" + rcnick() + "> " + input;
215 }
216
217 // /me support
218 var message;
219 var re = /^\/me (.*)/;
220 var match = re.exec(input);
221 var inputme = input.substring(4);
222 if (match) {
223 message = "* " + rcnick() + " " + inputme;
224 } else {
225 message = "<" + rcnick() + "> " + input;
226 }
227
228 // Say the message
229 var d = rcaddmessagetohistory(message);
230 rcsend(d, message);
231
232 // Remind people to set their nick
233 if (rcnick() == 'anonymous') {
234 rcaddmessagetohistory("-!- Set your nick with /nick");
235 }
236 }
237 }
238 //--><!]]></script>
239
240</head>
241
242<body onload="rcconnect()">
243 <div id="container">
244 <div id="history"></div>
245 <div id="client">
246 <div id="input">
247 <form name="input" onsubmit="return false" autocomplete="off">
248 <input id="say" onkeydown="return rckeydown(event)" autocomplete="off" autofocus="autofocus"></input>
249 </form></div>
250 <div id="status">&nbsp;</div>
251 </div>
252 </div>
253</body>
254</html>