Merge pull request #280 from calzoneman/closurefix

Closurefix
This commit is contained in:
Calvin Montgomery 2013-09-18 16:30:38 -07:00
commit 326a839cc0
4 changed files with 177 additions and 61 deletions

View file

@ -1,3 +1,13 @@
Wed Sep 18 18:26 2013 CDT
* lib/user.js: Change channel checks to include checking for whether
the channel is dead
Wed Sep 18 18:14 2013 CDT
* lib/channel.js: Add a bunch of checks to prevent callbacks from doing
things with a dead channel
* tests/channelDeadRace.js: Add a few client tests that cause exceptions
in the pre-patched channel code
Tue Sep 17 22:24 2013 CDT Tue Sep 17 22:24 2013 CDT
* lib/user.js: Fix what I assume was a race condition that caused an error message * lib/user.js: Fix what I assume was a race condition that caused an error message
when a user's login callback fired after the channel unloaded. when a user's login callback fired after the channel unloaded.

View file

@ -214,11 +214,12 @@ Channel.prototype.loadDump = function() {
} }
// Current // Current
else if(data.playlist) { else if(data.playlist) {
var chan = self;
self.playlist.load(data.playlist, function() { self.playlist.load(data.playlist, function() {
chan.sendAll("playlist", chan.playlist.items.toArray()); if (self.dead)
chan.broadcastPlaylistMeta(); return;
chan.playlist.startPlayback(data.playlist.time); self.sendAll("playlist", self.playlist.items.toArray());
self.broadcastPlaylistMeta();
self.playlist.startPlayback(data.playlist.time);
}); });
} }
for(var key in data.opts) { for(var key in data.opts) {
@ -270,7 +271,7 @@ Channel.prototype.loadDump = function() {
self.js = data.js || ""; self.js = data.js || "";
self.sendAll("channelCSSJS", {css: self.css, js: self.js}); self.sendAll("channelCSSJS", {css: self.css, js: self.js});
self.initialized = true; self.initialized = true;
setTimeout(function() { incrementalDump(self); }.bind(self), 300000); setTimeout(function() { incrementalDump(self); }, 300000);
} }
catch(e) { catch(e) {
Logger.errlog.log("Channel dump load failed: "); Logger.errlog.log("Channel dump load failed: ");
@ -281,6 +282,8 @@ Channel.prototype.loadDump = function() {
} }
Channel.prototype.saveDump = function() { Channel.prototype.saveDump = function() {
if (this.dead)
return;
if(!this.initialized || this.name === "") if(!this.initialized || this.name === "")
return; return;
var filts = new Array(this.filters.length); var filts = new Array(this.filters.length);
@ -306,7 +309,7 @@ Channel.prototype.saveDump = function() {
// Save channel dumps every 5 minutes, in case of crash // Save channel dumps every 5 minutes, in case of crash
function incrementalDump(chan) { function incrementalDump(chan) {
if(chan && chan.users && chan.users.length > 0) { if(!chan.dead && chan.users && chan.users.length > 0) {
chan.saveDump(); chan.saveDump();
setTimeout(function() { incrementalDump(chan); }, 300000); setTimeout(function() { incrementalDump(chan); }, 300000);
} }
@ -415,6 +418,8 @@ Channel.prototype.tryRegister = function (user) {
self.server.actionlog.record(user.ip, user.name, self.server.actionlog.record(user.ip, user.name,
"channel-register-success", self.name); "channel-register-success", self.name);
if (self.dead)
return;
self.registered = true; self.registered = true;
self.initialized = true; self.initialized = true;
self.saveDump(); self.saveDump();
@ -454,14 +459,18 @@ Channel.prototype.unregister = function (user) {
return; return;
} }
self.registered = false;
user.socket.emit("unregisterChannel", { success: true }); user.socket.emit("unregisterChannel", { success: true });
if (!self.dead)
self.registered = false;
}); });
} }
Channel.prototype.getRank = function (name, callback) { Channel.prototype.getRank = function (name, callback) {
var self = this; var self = this;
self.server.db.getGlobalRank(name, function (err, global) { self.server.db.getGlobalRank(name, function (err, global) {
if (self.dead)
return;
if(err) { if(err) {
callback(err, null); callback(err, null);
return; return;
@ -495,8 +504,13 @@ Channel.prototype.saveRank = function (user, callback) {
Channel.prototype.getIPRank = function (ip, callback) { Channel.prototype.getIPRank = function (ip, callback) {
var self = this; var self = this;
self.server.db.listAliases(ip, function (err, names) { self.server.db.listAliases(ip, function (err, names) {
if (self.dead)
return;
self.server.db.listChannelUserRanks(self.name, names, self.server.db.listChannelUserRanks(self.name, names,
function (err, res) { function (err, res) {
if (self.dead)
return;
if(err) { if(err) {
callback(err, null); callback(err, null);
return; return;
@ -550,6 +564,8 @@ Channel.prototype.tryNameBan = function(actor, name) {
} }
self.getRank(name, function (err, rank) { self.getRank(name, function (err, rank) {
if (self.dead)
return;
if(err) { if(err) {
actor.socket.emit("errorMsg", { actor.socket.emit("errorMsg", {
msg: "Internal error " + err msg: "Internal error " + err
@ -604,6 +620,8 @@ Channel.prototype.unbanName = function(actor, name) {
self.logger.log("*** " + actor.name + " un-namebanned " + name); self.logger.log("*** " + actor.name + " un-namebanned " + name);
self.server.db.clearChannelNameBan(self.name, name, function (err, res) { self.server.db.clearChannelNameBan(self.name, name, function (err, res) {
if (self.dead)
return;
self.users.forEach(function(u) { self.users.forEach(function(u) {
self.sendBanlist(u); self.sendBanlist(u);
@ -627,6 +645,9 @@ Channel.prototype.tryIPBan = function(actor, name, range) {
return; return;
} }
self.server.db.listIPsForName(name, function (err, ips) { self.server.db.listIPsForName(name, function (err, ips) {
if (self.dead)
return;
if(err) { if(err) {
actor.socket.emit("errorMsg", { actor.socket.emit("errorMsg", {
msg: "Internal error: " + err msg: "Internal error: " + err
@ -637,6 +658,9 @@ Channel.prototype.tryIPBan = function(actor, name, range) {
if(range) if(range)
ip = ip.replace(/(\d+)\.(\d+)\.(\d+)\.(\d+)/, "$1.$2.$3"); ip = ip.replace(/(\d+)\.(\d+)\.(\d+)\.(\d+)/, "$1.$2.$3");
self.getIPRank(ip, function (err, rank) { self.getIPRank(ip, function (err, rank) {
if (self.dead)
return;
if(err) { if(err) {
actor.socket.emit("errorMsg", { actor.socket.emit("errorMsg", {
msg: "Internal error: " + err msg: "Internal error: " + err
@ -669,6 +693,9 @@ Channel.prototype.tryIPBan = function(actor, name, range) {
self.server.db.addChannelBan(self.name, ip, name, self.server.db.addChannelBan(self.name, ip, name,
actor.name, actor.name,
function (err, res) { function (err, res) {
if (self.dead)
return;
var notice = { var notice = {
username: "[server]", username: "[server]",
msg: actor.name + " banned " + $util.maskIP(ip) + msg: actor.name + " banned " + $util.maskIP(ip) +
@ -782,6 +809,8 @@ Channel.prototype.userJoin = function(user) {
this.broadcastVoteskipUpdate(); this.broadcastVoteskipUpdate();
if(user.name != "") { if(user.name != "") {
self.getRank(user.name, function (err, rank) { self.getRank(user.name, function (err, rank) {
if (self.dead)
return;
if(err) { if(err) {
user.rank = user.global_rank; user.rank = user.global_rank;
user.saverank = false; user.saverank = false;
@ -1343,6 +1372,9 @@ Channel.prototype.addMedia = function(data, user) {
: this.opts.maxlength; : this.opts.maxlength;
var postAdd = function (item, cached) { var postAdd = function (item, cached) {
if (self.dead)
return;
if(item.media.type === "cu" && data.title) { if(item.media.type === "cu" && data.title) {
var t = data.title; var t = data.title;
if(t.length > 100) if(t.length > 100)
@ -1362,6 +1394,9 @@ Channel.prototype.addMedia = function(data, user) {
// No need to check library for livestreams - they aren't cached // No need to check library for livestreams - they aren't cached
if(isLive(data.type)) { if(isLive(data.type)) {
self.playlist.addMedia(data, function (err, data) { self.playlist.addMedia(data, function (err, data) {
if (self.dead)
return;
if(err) { if(err) {
if(err === true) if(err === true)
err = false; err = false;
@ -1379,6 +1414,9 @@ Channel.prototype.addMedia = function(data, user) {
// Don't search library if the channel isn't registered // Don't search library if the channel isn't registered
if(!self.registered) { if(!self.registered) {
self.playlist.addMedia(data, function(err, item) { self.playlist.addMedia(data, function(err, item) {
if (self.dead)
return;
if(err) { if(err) {
if(err === true) if(err === true)
err = false; err = false;
@ -1393,6 +1431,9 @@ Channel.prototype.addMedia = function(data, user) {
} }
self.server.db.getLibraryItem(self.name, data.id, self.server.db.getLibraryItem(self.name, data.id,
function (err, item) { function (err, item) {
if (self.dead)
return;
if(err) { if(err) {
user.socket.emit("queueFail", "Internal error: " + err); user.socket.emit("queueFail", "Internal error: " + err);
return; return;
@ -1407,6 +1448,9 @@ Channel.prototype.addMedia = function(data, user) {
data.media = m; data.media = m;
self.playlist.addCachedMedia(data, function (err, item) { self.playlist.addCachedMedia(data, function (err, item) {
if (self.dead)
return;
if(err) { if(err) {
if(err === true) if(err === true)
err = false; err = false;
@ -1419,6 +1463,9 @@ Channel.prototype.addMedia = function(data, user) {
}); });
} else { } else {
self.playlist.addMedia(data, function(err, item) { self.playlist.addMedia(data, function(err, item) {
if (self.dead)
return;
if(err) { if(err) {
if(err === true) if(err === true)
err = false; err = false;
@ -1438,6 +1485,9 @@ Channel.prototype.addMedia = function(data, user) {
Channel.prototype.addMediaList = function(data, user) { Channel.prototype.addMediaList = function(data, user) {
var chan = this; var chan = this;
this.playlist.addMediaList(data, function(err, item) { this.playlist.addMediaList(data, function(err, item) {
if (chan.dead)
return;
if(err) { if(err) {
if(err === true) if(err === true)
err = false; err = false;
@ -1477,6 +1527,9 @@ Channel.prototype.tryQueuePlaylist = function(user, data) {
self.server.db.getUserPlaylist(user.name, data.name, self.server.db.getUserPlaylist(user.name, data.name,
function (err, pl) { function (err, pl) {
if (self.dead)
return;
if(err) { if(err) {
user.socket.emit("errorMsg", { user.socket.emit("errorMsg", {
msg: "Playlist load failed: " + err msg: "Playlist load failed: " + err
@ -1552,6 +1605,9 @@ Channel.prototype.tryUncache = function(user, data) {
} }
self.server.db.removeFromLibrary(self.name, data.id, self.server.db.removeFromLibrary(self.name, data.id,
function (err, res) { function (err, res) {
if (self.dead)
return;
if(err) if(err)
return; return;
@ -1662,6 +1718,9 @@ Channel.prototype.tryUpdate = function(user, data) {
Channel.prototype.move = function(data, user) { Channel.prototype.move = function(data, user) {
var chan = this; var chan = this;
function afterMove() { function afterMove() {
if (chan.dead)
return;
var moveby = user && user.name ? user.name : null; var moveby = user && user.name ? user.name : null;
if(typeof data.moveby !== "undefined") if(typeof data.moveby !== "undefined")
moveby = data.moveby; moveby = data.moveby;
@ -2117,6 +2176,9 @@ Channel.prototype.trySetRank = function(user, data) {
receiver.rank = data.rank; receiver.rank = data.rank;
if(receiver.loggedIn) { if(receiver.loggedIn) {
self.saveRank(receiver, function (err, res) { self.saveRank(receiver, function (err, res) {
if (self.dead)
return;
self.logger.log("*** " + user.name + " set " + self.logger.log("*** " + user.name + " set " +
data.user + "'s rank to " + data.rank); data.user + "'s rank to " + data.rank);
self.sendAllWithPermission("acl", "setChannelRank", data); self.sendAllWithPermission("acl", "setChannelRank", data);
@ -2125,6 +2187,9 @@ Channel.prototype.trySetRank = function(user, data) {
self.broadcastUserUpdate(receiver); self.broadcastUserUpdate(receiver);
} else if(self.registered) { } else if(self.registered) {
self.getRank(data.user, function (err, rrank) { self.getRank(data.user, function (err, rrank) {
if (self.dead)
return;
if(err) if(err)
return; return;
if(rrank >= user.rank) if(rrank >= user.rank)
@ -2132,6 +2197,9 @@ Channel.prototype.trySetRank = function(user, data) {
self.server.db.setChannelRank(self.name, data.user, self.server.db.setChannelRank(self.name, data.user,
data.rank, function (err, res) { data.rank, function (err, res) {
if (self.dead)
return;
self.logger.log("*** " + user.name + " set " + self.logger.log("*** " + user.name + " set " +
data.user + "'s rank to " + data.rank); data.user + "'s rank to " + data.rank);
self.sendAllWithPermission("acl", "setChannelRank", data); self.sendAllWithPermission("acl", "setChannelRank", data);

View file

@ -47,6 +47,10 @@ var User = function(socket, Server) {
} }
}; };
User.prototype.inChannel = function () {
return this.channel !== null && !this.channel.dead;
};
// Throttling/cooldown // Throttling/cooldown
User.prototype.noflood = function(name, hz) { User.prototype.noflood = function(name, hz) {
var time = new Date().getTime(); var time = new Date().getTime();
@ -82,7 +86,7 @@ User.prototype.noflood = function(name, hz) {
} }
User.prototype.setAFK = function (afk) { User.prototype.setAFK = function (afk) {
if(this.channel === null) if(!this.inChannel())
return; return;
if(this.meta.afk === afk) if(this.meta.afk === afk)
return; return;
@ -105,7 +109,7 @@ User.prototype.autoAFK = function () {
if(this.awaytimer) if(this.awaytimer)
clearTimeout(this.awaytimer); clearTimeout(this.awaytimer);
if(this.channel === null || this.channel.opts.afk_timeout == 0) if(!this.inChannel() || this.channel.opts.afk_timeout == 0)
return; return;
this.awaytimer = setTimeout(function () { this.awaytimer = setTimeout(function () {
@ -117,12 +121,12 @@ User.prototype.initCallbacks = function() {
var self = this; var self = this;
self.socket.on("disconnect", function() { self.socket.on("disconnect", function() {
self.awaytimer && clearTimeout(self.awaytimer); self.awaytimer && clearTimeout(self.awaytimer);
if(self.channel != null) if(self.inChannel())
self.channel.userLeave(self); self.channel.userLeave(self);
}); });
self.socket.on("joinChannel", function(data) { self.socket.on("joinChannel", function(data) {
if(self.channel != null) if(self.inChannel())
return; return;
if(typeof data.name != "string") if(typeof data.name != "string")
return; return;
@ -180,49 +184,49 @@ User.prototype.initCallbacks = function() {
}); });
self.socket.on("assignLeader", function(data) { self.socket.on("assignLeader", function(data) {
if(self.channel != null) { if(self.inChannel()) {
self.channel.tryChangeLeader(self, data); self.channel.tryChangeLeader(self, data);
} }
}); });
self.socket.on("promote", function(data) { self.socket.on("promote", function(data) {
if(self.channel != null) { if(self.inChannel()) {
self.channel.tryPromoteUser(self, data); self.channel.tryPromoteUser(self, data);
} }
}); });
self.socket.on("demote", function(data) { self.socket.on("demote", function(data) {
if(self.channel != null) { if(self.inChannel()) {
self.channel.tryDemoteUser(self, data); self.channel.tryDemoteUser(self, data);
} }
}); });
self.socket.on("setChannelRank", function(data) { self.socket.on("setChannelRank", function(data) {
if(self.channel != null) { if(self.inChannel()) {
self.channel.trySetRank(self, data); self.channel.trySetRank(self, data);
} }
}); });
self.socket.on("banName", function(data) { self.socket.on("banName", function(data) {
if(self.channel != null) { if(self.inChannel()) {
self.channel.banName(self, data.name || ""); self.channel.banName(self, data.name || "");
} }
}); });
self.socket.on("banIP", function(data) { self.socket.on("banIP", function(data) {
if(self.channel != null) { if(self.inChannel()) {
self.channel.tryIPBan(self, data); self.channel.tryIPBan(self, data);
} }
}); });
self.socket.on("unban", function(data) { self.socket.on("unban", function(data) {
if(self.channel != null) { if(self.inChannel()) {
self.channel.tryUnban(self, data); self.channel.tryUnban(self, data);
} }
}); });
self.socket.on("chatMsg", function(data) { self.socket.on("chatMsg", function(data) {
if(self.channel != null) { if(self.inChannel()) {
if(data.msg.indexOf("/afk") != 0) { if(data.msg.indexOf("/afk") != 0) {
self.setAFK(false); self.setAFK(false);
self.autoAFK(); self.autoAFK();
@ -232,91 +236,91 @@ User.prototype.initCallbacks = function() {
}); });
self.socket.on("newPoll", function(data) { self.socket.on("newPoll", function(data) {
if(self.channel != null) { if(self.inChannel()) {
self.channel.tryOpenPoll(self, data); self.channel.tryOpenPoll(self, data);
} }
}); });
self.socket.on("playerReady", function() { self.socket.on("playerReady", function() {
if(self.channel != null) { if(self.inChannel()) {
self.channel.sendMediaUpdate(self); self.channel.sendMediaUpdate(self);
} }
}); });
self.socket.on("requestPlaylist", function() { self.socket.on("requestPlaylist", function() {
if(self.channel != null) { if(self.inChannel()) {
self.channel.sendPlaylist(self); self.channel.sendPlaylist(self);
} }
}); });
self.socket.on("queue", function(data) { self.socket.on("queue", function(data) {
if(self.channel != null) { if(self.inChannel()) {
self.channel.tryQueue(self, data); self.channel.tryQueue(self, data);
} }
}); });
self.socket.on("setTemp", function(data) { self.socket.on("setTemp", function(data) {
if(self.channel != null) { if(self.inChannel()) {
self.channel.trySetTemp(self, data); self.channel.trySetTemp(self, data);
} }
}); });
self.socket.on("delete", function(data) { self.socket.on("delete", function(data) {
if(self.channel != null) { if(self.inChannel()) {
self.channel.tryDequeue(self, data); self.channel.tryDequeue(self, data);
} }
}); });
self.socket.on("uncache", function(data) { self.socket.on("uncache", function(data) {
if(self.channel != null) { if(self.inChannel()) {
self.channel.tryUncache(self, data); self.channel.tryUncache(self, data);
} }
}); });
self.socket.on("moveMedia", function(data) { self.socket.on("moveMedia", function(data) {
if(self.channel != null) { if(self.inChannel()) {
self.channel.tryMove(self, data); self.channel.tryMove(self, data);
} }
}); });
self.socket.on("jumpTo", function(data) { self.socket.on("jumpTo", function(data) {
if(self.channel != null) { if(self.inChannel()) {
self.channel.tryJumpTo(self, data); self.channel.tryJumpTo(self, data);
} }
}); });
self.socket.on("playNext", function() { self.socket.on("playNext", function() {
if(self.channel != null) { if(self.inChannel()) {
self.channel.tryPlayNext(self); self.channel.tryPlayNext(self);
} }
}); });
self.socket.on("clearPlaylist", function() { self.socket.on("clearPlaylist", function() {
if(self.channel != null) { if(self.inChannel()) {
self.channel.tryClearqueue(self); self.channel.tryClearqueue(self);
} }
}); });
self.socket.on("shufflePlaylist", function() { self.socket.on("shufflePlaylist", function() {
if(self.channel != null) { if(self.inChannel()) {
self.channel.tryShufflequeue(self); self.channel.tryShufflequeue(self);
} }
}); });
self.socket.on("togglePlaylistLock", function() { self.socket.on("togglePlaylistLock", function() {
if(self.channel != null) { if(self.inChannel()) {
self.channel.tryToggleLock(self); self.channel.tryToggleLock(self);
} }
}); });
self.socket.on("mediaUpdate", function(data) { self.socket.on("mediaUpdate", function(data) {
if(self.channel != null) { if(self.inChannel()) {
self.channel.tryUpdate(self, data); self.channel.tryUpdate(self, data);
} }
}); });
self.socket.on("searchMedia", function(data) { self.socket.on("searchMedia", function(data) {
if(self.channel != null) { if(self.inChannel()) {
if(data.source == "yt") { if(data.source == "yt") {
var searchfn = self.server.infogetter.Getters["ytSearch"]; var searchfn = self.server.infogetter.Getters["ytSearch"];
searchfn(data.query.split(" "), function (e, vids) { searchfn(data.query.split(" "), function (e, vids) {
@ -337,19 +341,19 @@ User.prototype.initCallbacks = function() {
}); });
self.socket.on("closePoll", function() { self.socket.on("closePoll", function() {
if(self.channel != null) { if(self.inChannel()) {
self.channel.tryClosePoll(self); self.channel.tryClosePoll(self);
} }
}); });
self.socket.on("vote", function(data) { self.socket.on("vote", function(data) {
if(self.channel != null) { if(self.inChannel()) {
self.channel.tryVote(self, data); self.channel.tryVote(self, data);
} }
}); });
self.socket.on("registerChannel", function(data) { self.socket.on("registerChannel", function(data) {
if(self.channel == null) { if(!self.inChannel()) {
self.socket.emit("channelRegistration", { self.socket.emit("channelRegistration", {
success: false, success: false,
error: "You're not in any channel!" error: "You're not in any channel!"
@ -361,80 +365,80 @@ User.prototype.initCallbacks = function() {
}); });
self.socket.on("unregisterChannel", function() { self.socket.on("unregisterChannel", function() {
if(self.channel == null) { if(!self.inChannel()) {
return; return;
} }
self.channel.unregister(self); self.channel.unregister(self);
}); });
self.socket.on("setOptions", function(data) { self.socket.on("setOptions", function(data) {
if(self.channel != null) { if(self.inChannel()) {
self.channel.tryUpdateOptions(self, data); self.channel.tryUpdateOptions(self, data);
} }
}); });
self.socket.on("setPermissions", function(data) { self.socket.on("setPermissions", function(data) {
if(self.channel != null) { if(self.inChannel()) {
self.channel.tryUpdatePermissions(self, data); self.channel.tryUpdatePermissions(self, data);
} }
}); });
self.socket.on("setChannelCSS", function(data) { self.socket.on("setChannelCSS", function(data) {
if(self.channel != null) { if(self.inChannel()) {
self.channel.trySetCSS(self, data); self.channel.trySetCSS(self, data);
} }
}); });
self.socket.on("setChannelJS", function(data) { self.socket.on("setChannelJS", function(data) {
if(self.channel != null) { if(self.inChannel()) {
self.channel.trySetJS(self, data); self.channel.trySetJS(self, data);
} }
}); });
self.socket.on("updateFilter", function(data) { self.socket.on("updateFilter", function(data) {
if(self.channel != null) { if(self.inChannel()) {
self.channel.tryUpdateFilter(self, data); self.channel.tryUpdateFilter(self, data);
} }
}); });
self.socket.on("removeFilter", function(data) { self.socket.on("removeFilter", function(data) {
if(self.channel != null) { if(self.inChannel()) {
self.channel.tryRemoveFilter(self, data); self.channel.tryRemoveFilter(self, data);
} }
}); });
self.socket.on("moveFilter", function(data) { self.socket.on("moveFilter", function(data) {
if(self.channel != null) { if(self.inChannel()) {
self.channel.tryMoveFilter(self, data); self.channel.tryMoveFilter(self, data);
} }
}); });
self.socket.on("setMotd", function(data) { self.socket.on("setMotd", function(data) {
if(self.channel != null) { if(self.inChannel()) {
self.channel.tryUpdateMotd(self, data); self.channel.tryUpdateMotd(self, data);
} }
}); });
self.socket.on("requestLoginHistory", function() { self.socket.on("requestLoginHistory", function() {
if(self.channel != null) { if(self.inChannel()) {
self.channel.sendLoginHistory(self); self.channel.sendLoginHistory(self);
} }
}); });
self.socket.on("requestBanlist", function() { self.socket.on("requestBanlist", function() {
if(self.channel != null) { if(self.inChannel()) {
self.channel.sendBanlist(self); self.channel.sendBanlist(self);
} }
}); });
self.socket.on("requestChatFilters", function() { self.socket.on("requestChatFilters", function() {
if(self.channel != null) { if(self.inChannel()) {
self.channel.sendChatFilters(self); self.channel.sendChatFilters(self);
} }
}); });
self.socket.on("requestChannelRanks", function() { self.socket.on("requestChannelRanks", function() {
if(self.channel != null) { if(self.inChannel()) {
if(self.noflood("requestChannelRanks", 0.25)) if(self.noflood("requestChannelRanks", 0.25))
return; return;
self.channel.sendChannelRanks(self); self.channel.sendChannelRanks(self);
@ -442,7 +446,7 @@ User.prototype.initCallbacks = function() {
}); });
self.socket.on("voteskip", function(data) { self.socket.on("voteskip", function(data) {
if(self.channel != null) { if(self.inChannel()) {
self.channel.tryVoteskip(self); self.channel.tryVoteskip(self);
} }
}); });
@ -477,7 +481,7 @@ User.prototype.initCallbacks = function() {
return; return;
} }
if(self.channel == null) { if(!self.inChannel()) {
self.socket.emit("savePlaylist", { self.socket.emit("savePlaylist", {
success: false, success: false,
error: "Not in a channel" error: "Not in a channel"
@ -519,7 +523,7 @@ User.prototype.initCallbacks = function() {
}); });
self.socket.on("queuePlaylist", function(data) { self.socket.on("queuePlaylist", function(data) {
if(self.channel != null) { if(self.inChannel()) {
self.channel.tryQueuePlaylist(self, data); self.channel.tryQueuePlaylist(self, data);
} }
}); });
@ -546,7 +550,7 @@ User.prototype.initCallbacks = function() {
}); });
self.socket.on("readChanLog", function () { self.socket.on("readChanLog", function () {
if(self.channel !== null) { if(self.inChannel()) {
self.channel.tryReadLog(self); self.channel.tryReadLog(self);
} }
}); });
@ -564,7 +568,7 @@ User.prototype.initCallbacks = function() {
self.rank = rank; self.rank = rank;
self.socket.emit("rank", rank); self.socket.emit("rank", rank);
if(self.channel != null) if(self.inChannel())
self.channel.broadcastUserUpdate(self); self.channel.broadcastUserUpdate(self);
}); });
@ -615,7 +619,7 @@ User.prototype.login = function(name, pw, session) {
return; return;
} }
if(self.channel != null) { if(self.inChannel()) {
for(var i = 0; i < self.channel.users.length; i++) { for(var i = 0; i < self.channel.users.length; i++) {
if(self.channel.users[i].name == name) { if(self.channel.users[i].name == name) {
self.socket.emit("login", { self.socket.emit("login", {
@ -637,7 +641,7 @@ User.prototype.login = function(name, pw, session) {
name: name name: name
}); });
self.socket.emit("rank", self.rank); self.socket.emit("rank", self.rank);
if(self.channel != null) { if(self.inChannel()) {
self.channel.logger.log(self.ip + " signed in as " + name); self.channel.logger.log(self.ip + " signed in as " + name);
self.channel.broadcastNewUser(self); self.channel.broadcastNewUser(self);
} }
@ -654,7 +658,7 @@ User.prototype.login = function(name, pw, session) {
}); });
return; return;
} }
if(self.channel !== null && !self.channel.dead) { if(self.inChannel()) {
for(var i = 0; i < self.channel.users.length; i++) { for(var i = 0; i < self.channel.users.length; i++) {
if(self.channel.users[i].name.toLowerCase() == name.toLowerCase()) { if(self.channel.users[i].name.toLowerCase() == name.toLowerCase()) {
if (self.channel.users[i] == self) { if (self.channel.users[i] == self) {
@ -685,13 +689,13 @@ User.prototype.login = function(name, pw, session) {
var afterRankLookup = function () { var afterRankLookup = function () {
self.socket.emit("rank", self.rank); self.socket.emit("rank", self.rank);
self.name = name; self.name = name;
if(self.channel != null) { if(self.inChannel()) {
self.channel.logger.log(self.ip + " logged in as " + self.channel.logger.log(self.ip + " logged in as " +
name); name);
self.channel.broadcastNewUser(self); self.channel.broadcastNewUser(self);
} }
}; };
if(self.channel !== null) { if(self.inChannel()) {
self.channel.getRank(name, function (err, rank) { self.channel.getRank(name, function (err, rank) {
if(!err) { if(!err) {
self.saverank = true; self.saverank = true;

34
tests/channelDeadRace.js Normal file
View file

@ -0,0 +1,34 @@
var io = require('socket.io-client');
function testLogin() {
var socket = io.connect('http://localhost:1337');
socket.on('connect', function () {
socket.emit('login', { name: 'test', pw: 'test' });
socket.emit('joinChannel', { name: 'test' });
socket.disconnect();
});
}
function testBan() {
var socket = io.connect('http://localhost:1337');
socket.on('connect', function () {
socket.emit('login', { name: 'test', pw: 'test' });
socket.emit('joinChannel', { name: 'test' });
socket.emit('chatMsg', { msg: '/ban asdf' });
socket.disconnect();
});
}
function testRankChange() {
var socket = io.connect('http://localhost:1337');
socket.on('connect', function () {
socket.emit('login', { name: 'test', pw: 'test' });
socket.emit('joinChannel', { name: 'test' });
socket.emit('setChannelRank', { user: 'test2', rank: 2 });
socket.disconnect();
});
}
testLogin();
testBan();
testRankChange();