]> git.scottworley.com Git - reliable-chat/blob - webclient/rc.js
29b762ef655c8a2b87f2b0b72a0eff31fe646bde
[reliable-chat] / webclient / rc.js
1 var servers = ['chkno.net', 'rc2.chkno.net', 'echto.net', 'the-wes.com', 'vibrantlogic.com'];
2
3 var session = Math.random();
4 var seen = {};
5
6 function rcnick() {
7 var nick = localStorage.getItem("nick");
8 if (nick) {
9 return nick;
10 }
11 return 'anonymous';
12 }
13
14 function rcsetnick(new_nick) {
15 localStorage.setItem("nick", new_nick);
16 }
17
18 function rcserverbase(server) {
19 // Add the default port if server doesn't contain a port number already
20 if (server.indexOf(":") == -1) {
21 return "http://" + server + ":21059";
22 } else {
23 return "http://" + server;
24 }
25 }
26
27 function rcchangeserverstatus(server, new_status) {
28 var statusbar = document.getElementById("status");
29 var spans = statusbar.getElementsByTagName("span");
30 for (var i in spans) {
31 if (spans[i].firstChild && 'data' in spans[i].firstChild && spans[i].firstChild.data == server) {
32 spans[i].setAttribute("class", new_status);
33 }
34 }
35 }
36
37 function rcaddmessagetohistory(message) {
38 var d = document.createElement("div");
39 d.appendChild(document.createTextNode(message));
40 var h = document.getElementById("history");
41 h.appendChild(d);
42 window.scrollTo(0, document.body.scrollHeight);
43 return d;
44 }
45
46 function make_seen_key(id, text) {
47 return id.replace(/@/g, "@@") + "_@_" + text.replace(/@/g, "@@");
48 }
49
50 function receiveMessage(server, time, id, text) {
51 var seen_key = make_seen_key(id, text);
52 if (!(seen_key in seen)) {
53 seen[seen_key] = true;
54 rcaddmessagetohistory(text);
55 for (var i in servers) {
56 rcchangeserverstatus(servers[i], "sad");
57 }
58 }
59 rcchangeserverstatus(server, "happy");
60 }
61
62 function receiveMessageEvent(event)
63 {
64 for (var i in servers) {
65 if (event.origin === rcserverbase(servers[i])) {
66 messages = JSON.parse(event.data);
67 for (var j in messages) {
68 if ('Time' in messages[j] &&
69 'ID' in messages[j] &&
70 'Text' in messages[j]) {
71 receiveMessage(servers[i], messages[j]['Time'], messages[j]['ID'], messages[j]['Text']);
72 }
73 }
74 }
75 }
76 }
77
78 function rcconnect() {
79 window.addEventListener("message", receiveMessageEvent, false);
80 for (var i in servers) {
81 // Create a hidden iframe for same-origin workaround
82 var iframe = document.createElement("iframe");
83 iframe.setAttribute("src", rcserverbase(servers[i]) + "/frame");
84 document.body.insertBefore(iframe, document.body.firstChild);
85 // Status bar entry
86 var status_indicator = document.createElement("span");
87 status_indicator.appendChild(document.createTextNode(servers[i]));
88 status_indicator.setAttribute("class", "sad");
89 document.getElementById("status").appendChild(status_indicator);
90 }
91 if (rcnick() == 'anonymous') {
92 rcaddmessagetohistory("-!- Set your nick with /nick");
93 }
94 }
95
96 function rcsend(d, message) {
97 var id = new Date().getTime() + "-" + session + "-" + Math.random();
98 seen[make_seen_key(id, message)] = true;
99 var path = "/speak" +
100 "?id=" + encodeURIComponent(id) +
101 "&text=" + encodeURIComponent(message);
102 for (var i in servers) {
103 var uri = rcserverbase(servers[i]) + path;
104 var img = document.createElement("img");
105 img.setAttribute("src", uri);
106 d.appendChild(img);
107 }
108 }
109
110 function rckeydown(event) {
111 if (event.keyCode == 13) {
112 var input = document.input.say.value;
113 document.input.say.value = "";
114
115 // Check nick change
116 var message;
117 var re = /^\/nick (.*)/;
118 var match = re.exec(input);
119 if (match) {
120 message = "*** " + rcnick() + " is now known as " + match[1];
121 rcsetnick(match[1]);
122 } else {
123 message = "<" + rcnick() + "> " + input;
124 }
125
126 // Remind people to set their nick
127 if (rcnick() == 'anonymous') {
128 rcaddmessagetohistory("-!- Set your nick with /nick");
129 }
130
131 // Say the message
132 var d = rcaddmessagetohistory(message);
133 rcsend(d, message);
134 }
135 }