Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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

  • Loading branch information...
commit 165ba941717769b12c8620d58cd4f4a7a2e7f5bd 1 parent 0d46ca9
ry ry authored

Showing 3 changed files with 65 additions and 8 deletions. Show diff stats Hide diff stats

  1. +49 6 client.js
  2. +15 1 server.js
  3. +1 1  style.css
55 client.js
@@ -4,6 +4,25 @@ var CONFIG = { debug: false
4 4 , last_message_time: 0
5 5 };
6 6
  7 +var nicks = [];
  8 +
  9 +function userJoin(nick, timestamp) {
  10 + addMessage(nick, "joined", timestamp, "join");
  11 + for (var i = 0; i < nicks.length; i++)
  12 + if (nicks[i] == nick) return;
  13 + nicks.push(nick);
  14 +}
  15 +
  16 +function userPart(nick, timestamp) {
  17 + addMessage(nick, "left", timestamp, "part");
  18 + for (var i = 0; i < nicks.length; i++) {
  19 + if (nicks[i] == nick) {
  20 + nicks.splice(i,1)
  21 + return;
  22 + }
  23 + }
  24 +}
  25 +
7 26 // utility functions
8 27
9 28 util = {
@@ -158,11 +177,11 @@ function longPoll (data) {
158 177 break;
159 178
160 179 case "join":
161   - addMessage(message.nick, "joined", message.timestamp, "join");
  180 + userJoin(message.nick, message.timestamp);
162 181 break;
163 182
164 183 case "part":
165   - addMessage(message.nick, "left", message.timestamp, "part");
  184 + userPart(message.nick, message.timestamp);
166 185 break;
167 186 }
168 187 }
@@ -233,9 +252,16 @@ function onConnect (session) {
233 252 showChat(CONFIG.nick);
234 253 }
235 254
236   -$(window).unload(function () {
237   - jQuery.get("/part", {id: CONFIG.id}, function (data) { }, "json");
238   -});
  255 +function who (callback) {
  256 + jQuery.get("/who", {}, function (data, status) {
  257 + if (status != "success") return;
  258 + nicks = data.nicks;
  259 + var nick_string = nicks.length > 0 ? nicks.join(", ") : "(none)";
  260 +
  261 + addMessage("users:", nick_string, new Date(), "notice");
  262 + if (callback) callback();
  263 + }, "json");
  264 +}
239 265
240 266 $(document).ready(function() {
241 267
@@ -245,6 +271,19 @@ $(document).ready(function() {
245 271 $("#loading").show();
246 272 showLoad();
247 273 var nick = $("#nickInput").attr("value");
  274 +
  275 + if (nick.length > 50) {
  276 + alert("Nick too long. 50 character max.");
  277 + showConnect();
  278 + return false;
  279 + }
  280 +
  281 + if (/[^\w_\-^!]/.exec(nick)) {
  282 + alert("Bad character in nick. Can only have letters, numbers, and '_', '-', '^', '!'");
  283 + showConnect();
  284 + return false;
  285 + }
  286 +
248 287 $.ajax({ cache: false
249 288 , type: "GET" // XXX should be POST
250 289 , dataType: "json"
@@ -275,7 +314,11 @@ $(document).ready(function() {
275 314 // remove fixtures
276 315 $("#log table").remove();
277 316
278   - longPoll();
  317 + who(longPoll);
279 318
280 319 showConnect();
281 320 });
  321 +
  322 +$(window).unload(function () {
  323 + jQuery.get("/part", {id: CONFIG.id}, function (data) { }, "json");
  324 +});
16 server.js
@@ -53,6 +53,9 @@ var channel = new function () {
53 53 var sessions = {};
54 54
55 55 function createSession (nick) {
  56 + if (nick.length > 50) return null;
  57 + if (/[^\w_\-^!]/.exec(nick)) return null;
  58 +
56 59 for (var i in sessions) {
57 60 var session = sessions[i];
58 61 if (session && session.nick === nick) return null;
@@ -70,6 +73,7 @@ function createSession (nick) {
70 73 },
71 74
72 75 destroy: function () {
  76 + channel.appendMessage(session.nick, "part");
73 77 delete sessions[session.id];
74 78 }
75 79 };
@@ -100,6 +104,17 @@ function onLoad () {
100 104 fu.get("/client.js", fu.staticHandler("client.js"));
101 105 fu.get("/jquery-1.2.6.min.js", fu.staticHandler("jquery-1.2.6.min.js"));
102 106
  107 +
  108 + fu.get("/who", function (req, res) {
  109 + var nicks = [];
  110 + for (var id in sessions) {
  111 + if (!sessions.hasOwnProperty(id)) continue;
  112 + var session = sessions[id];
  113 + nicks.push(session.nick);
  114 + }
  115 + res.simpleJSON(200, { nicks: nicks });
  116 + });
  117 +
103 118 fu.get("/join", function (req, res) {
104 119 var nick = req.uri.params["nick"];
105 120 if (nick == null || nick.length == 0) {
@@ -120,7 +135,6 @@ function onLoad () {
120 135 var session;
121 136 if (id && sessions[id]) {
122 137 session = sessions[id];
123   - channel.appendMessage(session.nick, "part");
124 138 session.destroy();
125 139 }
126 140 res.simpleJSON(200, { });
2  style.css
@@ -97,7 +97,7 @@ form.horizontal .field label {display: inline; float: left; width: 140px; margin
97 97 }
98 98
99 99 /* not very important info */
100   -.rename td, .join td, .part td, .message .date { color: #555; }
  100 +.notice td, .join td, .part td, .message .date { color: #555; }
101 101
102 102 .message {
103 103 margin: 0.1em 0;

0 comments on commit 165ba94

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