Skip to content
Browse files

Fix unload parting, add /who resource, sanitize nicks.

  • Loading branch information...
1 parent 0d46ca9 commit 165ba941717769b12c8620d58cd4f4a7a2e7f5bd @ry ry committed Jun 2, 2009
Showing with 65 additions and 8 deletions.
  1. +49 −6 client.js
  2. +15 −1 server.js
  3. +1 −1 style.css
View
55 client.js
@@ -4,6 +4,25 @@ var CONFIG = { debug: false
, last_message_time: 0
};
+var nicks = [];
+
+function userJoin(nick, timestamp) {
+ addMessage(nick, "joined", timestamp, "join");
+ for (var i = 0; i < nicks.length; i++)
+ if (nicks[i] == nick) return;
+ nicks.push(nick);
+}
+
+function userPart(nick, timestamp) {
+ addMessage(nick, "left", timestamp, "part");
+ for (var i = 0; i < nicks.length; i++) {
+ if (nicks[i] == nick) {
+ nicks.splice(i,1)
+ return;
+ }
+ }
+}
+
// utility functions
util = {
@@ -158,11 +177,11 @@ function longPoll (data) {
break;
case "join":
- addMessage(message.nick, "joined", message.timestamp, "join");
+ userJoin(message.nick, message.timestamp);
break;
case "part":
- addMessage(message.nick, "left", message.timestamp, "part");
+ userPart(message.nick, message.timestamp);
break;
}
}
@@ -233,9 +252,16 @@ function onConnect (session) {
showChat(CONFIG.nick);
}
-$(window).unload(function () {
- jQuery.get("/part", {id: CONFIG.id}, function (data) { }, "json");
-});
+function who (callback) {
+ jQuery.get("/who", {}, function (data, status) {
+ if (status != "success") return;
+ nicks = data.nicks;
+ var nick_string = nicks.length > 0 ? nicks.join(", ") : "(none)";
+
+ addMessage("users:", nick_string, new Date(), "notice");
+ if (callback) callback();
+ }, "json");
+}
$(document).ready(function() {
@@ -245,6 +271,19 @@ $(document).ready(function() {
$("#loading").show();
showLoad();
var nick = $("#nickInput").attr("value");
+
+ if (nick.length > 50) {
+ alert("Nick too long. 50 character max.");
+ showConnect();
+ return false;
+ }
+
+ if (/[^\w_\-^!]/.exec(nick)) {
+ alert("Bad character in nick. Can only have letters, numbers, and '_', '-', '^', '!'");
+ showConnect();
+ return false;
+ }
+
$.ajax({ cache: false
, type: "GET" // XXX should be POST
, dataType: "json"
@@ -275,7 +314,11 @@ $(document).ready(function() {
// remove fixtures
$("#log table").remove();
- longPoll();
+ who(longPoll);
showConnect();
});
+
+$(window).unload(function () {
+ jQuery.get("/part", {id: CONFIG.id}, function (data) { }, "json");
+});
View
16 server.js
@@ -53,6 +53,9 @@ var channel = new function () {
var sessions = {};
function createSession (nick) {
+ if (nick.length > 50) return null;
+ if (/[^\w_\-^!]/.exec(nick)) return null;
+
for (var i in sessions) {
var session = sessions[i];
if (session && session.nick === nick) return null;
@@ -70,6 +73,7 @@ function createSession (nick) {
},
destroy: function () {
+ channel.appendMessage(session.nick, "part");
delete sessions[session.id];
}
};
@@ -100,6 +104,17 @@ function onLoad () {
fu.get("/client.js", fu.staticHandler("client.js"));
fu.get("/jquery-1.2.6.min.js", fu.staticHandler("jquery-1.2.6.min.js"));
+
+ fu.get("/who", function (req, res) {
+ var nicks = [];
+ for (var id in sessions) {
+ if (!sessions.hasOwnProperty(id)) continue;
+ var session = sessions[id];
+ nicks.push(session.nick);
+ }
+ res.simpleJSON(200, { nicks: nicks });
+ });
+
fu.get("/join", function (req, res) {
var nick = req.uri.params["nick"];
if (nick == null || nick.length == 0) {
@@ -120,7 +135,6 @@ function onLoad () {
var session;
if (id && sessions[id]) {
session = sessions[id];
- channel.appendMessage(session.nick, "part");
session.destroy();
}
res.simpleJSON(200, { });
View
2 style.css
@@ -97,7 +97,7 @@ form.horizontal .field label {display: inline; float: left; width: 140px; margin
}
/* not very important info */
-.rename td, .join td, .part td, .message .date { color: #555; }
+.notice td, .join td, .part td, .message .date { color: #555; }
.message {
margin: 0.1em 0;

0 comments on commit 165ba94

Please sign in to comment.
Something went wrong with that request. Please try again.