]> git.scottworley.com Git - reliable-chat/blobdiff - webclient/rc.js
Implement nicknames
[reliable-chat] / webclient / rc.js
index 6dc527e724ee1dcb4389a65f0eb7b098634267a5..3231f4c34b607ff42aaa55a2a8e1734b68ce825c 100644 (file)
@@ -1,8 +1,20 @@
-var servers = ['chkno.net', 'localhost'];
+var servers = ['chkno.net', 'rc2.chkno.net', 'echto.net', 'the-wes.com', 'vibrantlogic.com'];
 
 var session = Math.random();
 var seen = {};
 
+function rcnick() {
+       var nick = localStorage.getItem("nick");
+       if (nick) {
+               return nick;
+       }
+       return 'anonymous';
+}
+
+function rcsetnick(new_nick) {
+       localStorage.setItem("nick", new_nick);
+}
+
 function rcserverbase(server) {
        // Add the default port if server doesn't contain a port number already
        if (server.indexOf(":") == -1) {
@@ -12,6 +24,16 @@ function rcserverbase(server) {
        }
 }
 
+function rcchangeserverstatus(server, new_status) {
+       var statusbar = document.getElementById("status");
+       var spans = statusbar.getElementsByTagName("span");
+       for (i in spans) {
+               if (spans[i].firstChild && 'data' in spans[i].firstChild && spans[i].firstChild.data == server) {
+                       spans[i].setAttribute("class", new_status);
+               }
+       }
+}
+
 function rcaddmessagetohistory(message) {
        var d = document.createElement("div");
        d.appendChild(document.createTextNode(message));
@@ -30,7 +52,11 @@ function receiveMessage(server, time, id, text) {
        if (!(seen_key in seen)) {
                seen[seen_key] = true;
                rcaddmessagetohistory(text);
+               for (i in servers) {
+                       rcchangeserverstatus(servers[i], "sad");
+               }
        }
+       rcchangeserverstatus(server, "happy");
 }
 
 function receiveMessageEvent(event)  
@@ -56,6 +82,14 @@ function rcconnect() {
                var iframe = document.createElement("iframe");
                iframe.setAttribute("src", rcserverbase(servers[i]) + "/frame");
                document.body.insertBefore(iframe, document.body.firstChild);
+               // Status bar entry
+               var status_indicator = document.createElement("span");
+               status_indicator.appendChild(document.createTextNode(servers[i]));
+               status_indicator.setAttribute("class", "sad");
+               document.getElementById("status").appendChild(status_indicator);
+       }
+       if (rcnick() == 'anonymous') {
+               rcaddmessagetohistory("-!- Set your nick with /nick");
        }
 }
 
@@ -75,9 +109,27 @@ function rcsend(d, message) {
 
 function rckeydown(event) {
        if (event.keyCode == 13) {
-               var d = rcaddmessagetohistory(document.input.say.value);
-               rcsend(d, document.input.say.value);
+               var input = document.input.say.value;
                document.input.say.value = "";
-               return false;
+               
+               // Check nick change
+               var message;
+               var re = /^\/nick (.*)/;
+               var match = re.exec(input);
+               if (match) {
+                       message = "*** " + rcnick() + " is now known as " + match[1];
+                       rcsetnick(match[1]);
+               } else {
+                       message = "<" + rcnick() + "> " + input;
+               }
+
+               // Remind people to set their nick
+               if (rcnick() == 'anonymous') {
+                       rcaddmessagetohistory("-!- Set your nick with /nick");
+               }
+
+               // Say the message
+               var d = rcaddmessagetohistory(message);
+               rcsend(d, message);
        }
 }