]> git.scottworley.com Git - reliable-chat/blob - webclient/rc.html
Dark color scheme
[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 seen = {};
67
68 function rcnick() {
69 var nick = localStorage.getItem("nick");
70 if (nick) {
71 return nick;
72 }
73 return 'anonymous';
74 }
75
76 function rcsetnick(new_nick) {
77 localStorage.setItem("nick", new_nick);
78 }
79
80 function rcserverbase(server) {
81 // Add the default port if server doesn't contain a port number already
82 if (server.indexOf(":") == -1) {
83 return "http://" + server + ":21059";
84 } else {
85 return "http://" + server;
86 }
87 }
88
89 function rcchangeserverstatus(server, new_status) {
90 var statusbar = document.getElementById("status");
91 var spans = statusbar.getElementsByTagName("span");
92 for (var i in spans) {
93 if (spans[i].firstChild && 'data' in spans[i].firstChild && spans[i].firstChild.data == server) {
94 spans[i].setAttribute("class", new_status);
95 }
96 }
97 }
98
99 function rcaddmessagetohistory(message) {
100 var d = document.createElement("div");
101 d.appendChild(document.createTextNode(message));
102 var h = document.getElementById("history");
103 h.appendChild(d);
104 window.scrollTo(0, document.body.scrollHeight);
105 return d;
106 }
107
108 function make_seen_key(id, text) {
109 return id.replace(/@/g, "@@") + "_@_" + text.replace(/@/g, "@@");
110 }
111
112 function receiveMessage(server, time, id, text) {
113 var seen_key = make_seen_key(id, text);
114 if (!(seen_key in seen)) {
115 seen[seen_key] = true;
116 rcaddmessagetohistory(text);
117 for (var i in servers) {
118 rcchangeserverstatus(servers[i], "sad");
119 }
120 }
121 rcchangeserverstatus(server, "happy");
122 }
123
124 function receiveMessageEvent(event)
125 {
126 for (var i in servers) {
127 if (event.origin === rcserverbase(servers[i])) {
128 messages = JSON.parse(event.data);
129 for (var j in messages) {
130 if ('Time' in messages[j] &&
131 'ID' in messages[j] &&
132 'Text' in messages[j]) {
133 receiveMessage(servers[i], messages[j]['Time'], messages[j]['ID'], messages[j]['Text']);
134 }
135 }
136 }
137 }
138 }
139
140 function rcconnect() {
141 window.addEventListener("message", receiveMessageEvent, false);
142 for (var i in servers) {
143 // Create a hidden iframe for same-origin workaround
144 var iframe = document.createElement("iframe");
145 iframe.setAttribute("src", rcserverbase(servers[i]) + "/frame");
146 document.body.insertBefore(iframe, document.body.firstChild);
147 // Status bar entry
148 var status_indicator = document.createElement("span");
149 status_indicator.appendChild(document.createTextNode(servers[i]));
150 status_indicator.setAttribute("class", "sad");
151 document.getElementById("status").appendChild(status_indicator);
152 }
153 if (rcnick() == 'anonymous') {
154 rcaddmessagetohistory("-!- Set your nick with /nick");
155 }
156 }
157
158 function rcsend(d, message) {
159 var id = new Date().getTime() + "-" + session + "-" + Math.random();
160 seen[make_seen_key(id, message)] = true;
161 var path = "/speak" +
162 "?id=" + encodeURIComponent(id) +
163 "&text=" + encodeURIComponent(message);
164 for (var i in servers) {
165 var uri = rcserverbase(servers[i]) + path;
166 var img = document.createElement("img");
167 img.setAttribute("src", uri);
168 d.appendChild(img);
169 }
170 }
171
172 function rckeydown(event) {
173 if (event.keyCode == 13) {
174 var input = document.input.say.value;
175 document.input.say.value = "";
176
177 // Check nick change
178 var message;
179 var re = /^\/nick (.*)/;
180 var match = re.exec(input);
181 if (match) {
182 message = "*** " + rcnick() + " is now known as " + match[1];
183 rcsetnick(match[1]);
184 } else {
185 message = "<" + rcnick() + "> " + input;
186 }
187
188 // /me support
189 var message;
190 var re = /^\/me (.*)/;
191 var match = re.exec(input);
192 var inputme = input.substring(4);
193 if (match) {
194 message = "* " + rcnick() + " " + inputme;
195 } else {
196 message = "<" + rcnick() + "> " + input;
197 }
198
199 // Remind people to set their nick
200 if (rcnick() == 'anonymous') {
201 rcaddmessagetohistory("-!- Set your nick with /nick");
202 }
203
204 // Say the message
205 var d = rcaddmessagetohistory(message);
206 rcsend(d, message);
207 }
208 }
209 //--><!]]></script>
210
211 </head>
212
213 <body onload="rcconnect()">
214 <div id="container">
215 <div id="history"></div>
216 <div id="client">
217 <div id="input">
218 <form name="input" onsubmit="return false" autocomplete="off">
219 <input id="say" onkeydown="return rckeydown(event)" autocomplete="off" autofocus="autofocus"></input>
220 </form></div>
221 <div id="status">&nbsp;</div>
222 </div>
223 </div>
224 </body>
225 </html>