Change channel checks in user.js

This commit is contained in:
calzoneman 2013-09-18 18:27:42 -05:00
parent 39fe452e96
commit 1d9845f6d5
2 changed files with 61 additions and 53 deletions

View File

@ -1,3 +1,7 @@
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 Wed Sep 18 18:14 2013 CDT
* lib/channel.js: Add a bunch of checks to prevent callbacks from doing * lib/channel.js: Add a bunch of checks to prevent callbacks from doing
things with a dead channel things with a dead channel

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;