From 574ef4435c9f86a715579d9c063ca4969d9d4e2f Mon Sep 17 00:00:00 2001 From: calzoneman Date: Sat, 25 Jan 2014 21:29:56 -0600 Subject: [PATCH] Fix some login race conditions/issues --- lib/channel-new.js | 127 ++++++++++++++++++++++--------------- lib/user.js | 4 +- www/assets/js/callbacks.js | 5 +- www/assets/js/util.js | 9 ++- www/css/cytube.css | 7 ++ 5 files changed, 93 insertions(+), 59 deletions(-) diff --git a/lib/channel-new.js b/lib/channel-new.js index 86f6e8ed..33a6df62 100644 --- a/lib/channel-new.js +++ b/lib/channel-new.js @@ -440,30 +440,86 @@ Channel.prototype.getIPRank = function (ip, callback) { }); }; +/** + * Called when a user attempts to join a channel. + * Handles password check + */ +Channel.prototype.preJoin = function (user, password) { + var self = this; + self.whenReady(function () { + user.whenLoggedIn(function () { + self.getRank(user.name, function (err, rank) { + if (err) { + user.rank = user.global_rank; + } else { + user.rank = Math.max(rank, user.global_rank); + } + + user.socket.emit("rank", user.rank); + user.emit("channelRank", user.rank); + }); + }); + + if (self.opts.password !== false && user.rank < 2) { + if (password !== self.opts.password) { + var checkPassword = function (pw) { + if (self.dead) { + return; + } + + if (pw !== self.opts.password) { + user.socket.emit("needPassword", true); + return; + } + + user.socket.listeners("channelPassword").splice( + user.socket.listeners("channelPassword").indexOf(checkPassword) + ); + + user.socket.emit("cancelNeedPassword"); + self.join(user); + }; + + + user.socket.on("channelPassword", checkPassword); + user.socket.emit("needPassword", typeof password !== "undefined"); + user.once("channelRank", function (r) { + if (!user.inChannel() && !self.dead && r >= 2) { + user.socket.emit("cancelNeedPassword"); + self.join(user); + } + }); + return; + } + } + + self.join(user); + }); +}; + /** * Called when a user joins a channel */ -Channel.prototype.join = function (user, password) { +Channel.prototype.join = function (user) { var self = this; - user.whenLoggedIn(function () { - self.getRank(user.name, function (err, rank) { - if (err) { - user.rank = user.global_rank; - } else { - user.rank = Math.max(rank, user.global_rank); - } - - user.socket.emit("rank", user.rank); - user.emit("channelRank", user.rank); - }); - }); - - var afterIPBanCheck = function () { + var afterLogin = function () { if (self.dead) { return; } + var lname = user.name.toLowerCase(); + for (var i = 0; i < self.users.length; i++) { + if (self.users[i].name.toLowerCase() === lname && self.users[i] !== user) { + self.users[i].kick("Duplicate login"); + } + } + + self.sendUserJoin(self.users, user); + self.sendUserlist([user]); + }; + + var afterIPBan = function () { user.autoAFK(); user.socket.join(self.uniqueName); user.channel = self; @@ -498,44 +554,13 @@ Channel.prototype.join = function (user, password) { Logger.syslog.log(user.ip + " joined channel " + self.name); }; - var afterLogin = function () { - if (self.dead) { + db.channels.isIPBanned(self.name, user.ip, function (err, banned) { + if (!err && banned) { + user.kick("You're banned!"); return; + } else { + afterIPBan(); } - - var lname = user.name.toLowerCase(); - for (var i = 0; i < self.users.length; i++) { - if (self.users[i].name.toLowerCase() === lname && self.users[i] !== user) { - self.users[i].kick("Duplicate login"); - } - } - - self.sendUserJoin(self.users, user); - self.sendUserlist([user]); - }; - - self.whenReady(function () { - if (self.opts.password !== false && user.rank < 2) { - if (password !== self.opts.password) { - user.socket.emit("needPassword", typeof password !== "undefined"); - user.once("channelRank", function (r) { - if (r >= 2) { - self.join(user); - } - }); - return; - } - } - - user.socket.emit("cancelNeedPassword"); - db.channels.isIPBanned(self.name, user.ip, function (err, banned) { - if (!err && banned) { - user.kick("You're banned!"); - return; - } else { - afterIPBanCheck(); - } - }); }); }; diff --git a/lib/user.js b/lib/user.js index a362d9a0..856267ce 100644 --- a/lib/user.js +++ b/lib/user.js @@ -175,7 +175,7 @@ User.prototype.initChannelCallbacks = function () { } }); - self.socket.on("joinChannel", typecheck("object", {}, function (data) { + self.socket.once("joinChannel", typecheck("object", {}, function (data) { if (self.inChannel()) { return; } @@ -195,7 +195,7 @@ User.prototype.initChannelCallbacks = function () { data.name = data.name.toLowerCase(); var chan = Server.getServer().getChannel(data.name); - chan.join(self, data.pw); + chan.preJoin(self, data.pw); })); wrapTypecheck("assignLeader", function (data) { diff --git a/www/assets/js/callbacks.js b/www/assets/js/callbacks.js index 0b65dbbc..0c30bd2a 100644 --- a/www/assets/js/callbacks.js +++ b/www/assets/js/callbacks.js @@ -138,10 +138,7 @@ Callbacks = { var parent = chatDialog(div); parent.attr("id", "needpw"); var sendpw = function () { - socket.emit("joinChannel", { - name: CHANNEL.name, - pw: pwbox.val() - }); + socket.emit("channelPassword", pwbox.val()); parent.remove(); }; submit.click(sendpw); diff --git a/www/assets/js/util.js b/www/assets/js/util.js index 06eccf69..e84416b6 100644 --- a/www/assets/js/util.js +++ b/www/assets/js/util.js @@ -1354,7 +1354,8 @@ function addChatMessage(data) { /* layouts */ function fluidLayout() { - $(".container").css("max-width", "100%"); + $(".container").removeClass("container").addClass("container-fluid"); + // TODO resize } function synchtubeLayout() { @@ -1547,7 +1548,11 @@ function unhidePlayer() { function chatDialog(div) { var parent = $("
").addClass("profile-box") - .css("padding", "10px") + .css({ + padding: "10px", + "z-index": "auto", + position: "absolute" + }) .appendTo($("body")); div.appendTo(parent); diff --git a/www/css/cytube.css b/www/css/cytube.css index 0fdc55e8..32fe922a 100644 --- a/www/css/cytube.css +++ b/www/css/cytube.css @@ -1,3 +1,10 @@ +.container-fluid { + padding-left: 15px; + padding-right: 15px; + margin-left: auto; + margin-right: auto; +} + #loginform > .form-group { margin-right: 5px; }