]> git.scottworley.com Git - reliable-chat/blobdiff - webclient/rc.html
Timestamps for incoming messages
[reliable-chat] / webclient / rc.html
index 8d1c84a02755210f083e4277fb739692d9a27e88..a062a176b2dc8a92e0bb7f62fee1ebf9cefa2d5a 100644 (file)
@@ -79,9 +79,9 @@
   <script type="text/javascript"><!--//--><![CDATA[//><!--
        var servers = ['chkno.net', 'rc2.chkno.net', 'echto.net', 'the-wes.com', 'vibrantlogic.com'];
 
   <script type="text/javascript"><!--//--><![CDATA[//><!--
        var servers = ['chkno.net', 'rc2.chkno.net', 'echto.net', 'the-wes.com', 'vibrantlogic.com'];
 
-       var session = Math.random();
-       var since = {};
-       var seen = {};
+       var session = Math.random();  // For outgoing message IDs
+       var since = {};  // server -> time: For fetch?since=
+       var seen = {};  // seen_key -> true
 
        function rcnick() {
                var nick = localStorage.getItem("nick");
 
        function rcnick() {
                var nick = localStorage.getItem("nick");
                }
        }
 
                }
        }
 
+       function rcformattime(t) {
+               var d = t.getDay();
+               d = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"][d];
+               var h = t.getHours();
+               var m = t.getMinutes();
+               var s = t.getSeconds();
+               function pad(x) {
+                       return (x < 10 ? "0" : "") + x;
+               }
+               return d + " " + pad(h) + ":" + pad(m) + ":" + pad(s);
+       }
+
        function rcaddmessagetohistory(message) {
                var d = document.createElement("div");
        function rcaddmessagetohistory(message) {
                var d = document.createElement("div");
-               d.appendChild(document.createTextNode(message));
+               var text = (message.Time ? rcformattime(message.Time) : "") + " " + message.Text;
+               d.appendChild(document.createTextNode(text));
                var h = document.getElementById("history");
                h.appendChild(d);
                window.scrollTo(0, document.body.scrollHeight);
                var h = document.getElementById("history");
                h.appendChild(d);
                window.scrollTo(0, document.body.scrollHeight);
 
        function rcreceivemessages(server, messages) {
                for (var i in messages) {
 
        function rcreceivemessages(server, messages) {
                for (var i in messages) {
-                       var seen_key = make_seen_key(messages[i]['ID'], messages[i]['Text']);
+                       var m = messages[i];
+                       m.Time = new Date(m.Time);
+                       var seen_key = make_seen_key(m.ID, m.Text);
                        if (!(seen_key in seen)) {
                                seen[seen_key] = true;
                        if (!(seen_key in seen)) {
                                seen[seen_key] = true;
-                               rcaddmessagetohistory(messages[i]['Text']);
+                               rcaddmessagetohistory(m);
                                for (var i in servers) {
                                        rcchangeserverstatus(servers[i], "sad");
                                }
                                for (var i in servers) {
                                        rcchangeserverstatus(servers[i], "sad");
                                }
                                                var messages = JSON.parse(rtxt);
                                                if (messages != null) {
                                                        rcreceivemessages(server, messages);
                                                var messages = JSON.parse(rtxt);
                                                if (messages != null) {
                                                        rcreceivemessages(server, messages);
-                                                       window.parent.postMessage(rtxt, "*");
                                                        delay = 40;
                                                        if (messages.length >= 1 && "Time" in messages[messages.length-1]) {
                                                        delay = 40;
                                                        if (messages.length >= 1 && "Time" in messages[messages.length-1]) {
-                                                               since[server] = messages[messages.length-1]["Time"];
+                                                               since[server] = messages[messages.length-1].Time;
                                                        }
                                                }
                                        }
                                                        }
                                                }
                                        }
                        status_indicator.setAttribute("class", "sad");
                        document.getElementById("status").appendChild(status_indicator);
                }
                        status_indicator.setAttribute("class", "sad");
                        document.getElementById("status").appendChild(status_indicator);
                }
-               if (rcnick() == 'anonymous') {
-                       rcaddmessagetohistory("-!- Set your nick with /nick");
-               }
        }
 
        function rcsend(d, message) {
        }
 
        function rcsend(d, message) {
                }
        }
 
                }
        }
 
+       function rcinput(input) {
+               var message;
+               var re = /^\/([a-z]+) (.*)/
+               var match = re.exec(input);
+               if (match && match[1] == 'me') {
+                       message = "* " + rcnick() + "  " + match[2];
+               } else if (match && match[1] == 'nick') {
+                       message = "*** " + rcnick() + " is now known as " + match[2];
+                       rcsetnick(match[2]);
+               } else {
+                       message = "<" + rcnick() + "> " + input;
+               }
+
+               var d = rcaddmessagetohistory({'Text': message});
+               rcsend(d, message);
+       }
+
        function rckeydown(event) {
                if (event.keyCode == 13) {
        function rckeydown(event) {
                if (event.keyCode == 13) {
-                       var input = document.input.say.value;
+                       rcinput(document.input.say.value);
                        document.input.say.value = "";
                        
                        document.input.say.value = "";
                        
-                       // 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;
-                       }
-
-                       // /me support
-                       var message;
-                       var re = /^\/me (.*)/;
-                       var match = re.exec(input);
-                       var inputme = input.substring(4);
-                       if (match) {
-                               message = "* " + rcnick() + "  " + inputme;
-                       } else {
-                               message = "<" + rcnick() + "> " + input;
-                       }
-
-                       // Say the message
-                       var d = rcaddmessagetohistory(message);
-                       rcsend(d, message);
-
-                       // Remind people to set their nick
-                       if (rcnick() == 'anonymous') {
-                               rcaddmessagetohistory("-!- Set your nick with /nick");
-                       }
                }
        }
   //--><!]]></script>
                }
        }
   //--><!]]></script>