From 521c786cdc11a325a72dee8ef7044f51187bfef8 Mon Sep 17 00:00:00 2001 From: calzoneman Date: Sun, 5 Jan 2014 19:42:09 -0600 Subject: [PATCH] Start the long process of making it work again --- index.js | 1 + lib/channel-new.js | 23 +- lib/playlist.js | 10 + lib/user-new.js | 431 ----------------- lib/user-old.js | 787 ++++++++++++++++++++++++++++++ lib/user.js | 950 ++++++++++++------------------------- www/assets/js/callbacks.js | 1 + 7 files changed, 1116 insertions(+), 1087 deletions(-) delete mode 100644 lib/user-new.js create mode 100644 lib/user-old.js diff --git a/index.js b/index.js index 77c32321..c35fb13d 100644 --- a/index.js +++ b/index.js @@ -14,6 +14,7 @@ var Config = require("./lib/config"); var Logger = require("./lib/logger"); Config.load("cfg.json", function (cfg) { + cfg["debug"] = true; var sv = Server.init(cfg); if(!cfg["debug"]) { process.on("uncaughtException", function (err) { diff --git a/lib/channel-new.js b/lib/channel-new.js index d703b40f..3f5e258d 100644 --- a/lib/channel-new.js +++ b/lib/channel-new.js @@ -223,7 +223,7 @@ Channel.prototype.loadState = function () { // Configurables if ("opts" in data) { for (var key in data.opts) { - self.opts[key] = data.opts; + self.opts[key] = data.opts[key]; } } @@ -482,7 +482,7 @@ Channel.prototype.join = function (user, password) { self.sendMediaUpdate([user]); self.sendPlaylistLock([user]); self.sendUserlist([user]); - self.sendRecentchat([user]); + self.sendRecentChat([user]); self.sendCSSJS([user]); self.sendPoll([user]); self.sendOpts([user]); @@ -938,6 +938,7 @@ Channel.prototype.sendMediaUpdate = function (users) { * Send the userlist */ Channel.prototype.sendUserlist = function (toUsers) { + var self = this; var users = []; var detailedUsers = []; @@ -968,8 +969,8 @@ Channel.prototype.sendUserlist = function (toUsers) { u.socket.emit("userlist", users); } - if (this.leader !== null) { - u.socket.emit("setLeader", this.leader.name); + if (self.leader != null) { + u.socket.emit("setLeader", self.leader.name); } }); }; @@ -1139,6 +1140,20 @@ Channel.prototype.sendVoteskipUpdate = function (users) { }); }; +/** + * Sends the inline CSS and JS + */ +Channel.prototype.sendCSSJS = function (users) { + var data = { + css: this.css, + js: this.js + }; + + users.forEach(function (u) { + u.socket.emit("channelCSSJS", data); + }); +}; + /** * Sends the MOTD */ diff --git a/lib/playlist.js b/lib/playlist.js index 36ebe898..4d590b5c 100644 --- a/lib/playlist.js +++ b/lib/playlist.js @@ -322,6 +322,16 @@ Playlist.prototype.count = function (id) { return count; } +Playlist.prototype.getFullUpdate = function () { + if (!this.current) { + return null; + } else if (!this.current.media) { + return null; + } else { + return this.current.media.fullupdate(); + } +}; + Playlist.prototype.lead = function(lead) { this.leading = lead; var pl = this; diff --git a/lib/user-new.js b/lib/user-new.js deleted file mode 100644 index 99010d61..00000000 --- a/lib/user-new.js +++ /dev/null @@ -1,431 +0,0 @@ -var Logger = require("./logger"); -var Server = require("./server"); -var util = require("./utilities"); -var MakeEmitter = require("./emitter"); - -function User(socket) { - var self = this; - MakeEmitter(self); - self.socket = socket; - self.ip = socket._ip; - self.loggedIn = false; - self.loggingIn = false; - self.rank = -1; - self.global_rank = -1; - self.channel = null; - self.name = ""; - self.canonicalName = ""; - self.profile = { - image: "", - text: "" - }; - self.meta = { - afk: false, - muted: false, - smuted: false, - aliases: [] - }; - self.queueLimiter = util.newRateLimiter(); - self.chatLimiter = util.newRateLimiter(); - self.awaytimer = false; - - self.socket.once("initChannelCallbacks", function () { - self.initChannelCallbacks(); - }); - - var announcement = Server.getServer().announcement; - if (announcement != null) { - self.socket.emit("announcement", announcement); - } -} - -/** - * Checks whether the user is in a valid channel - */ -User.prototype.inChannel = function () { - return this.channel != null && !this.channel.dead; -}; - -/** - * Changes a user's AFK status, updating the channel voteskip if necessary - */ -User.prototype.setAFK = function (afk) { - if (!this.inChannel()) { - return; - } - - if (this.meta.afk === afk) { - return; - } - - var chan = this.channel; - if (afk) { - if (chan.voteskip) { - chan.voteskip.unvote(this.ip); - } - } else { - this.autoAFK(); - } - - chan.checkVoteskipPass(); - chan.sendAll("setAFK", { - name: this.name, - afk: afk - }); -}; - -/** - * Sets a timer to automatically mark the user as AFK after - * a period of inactivity - */ -User.prototype.autoAFK = function () { - var self = this; - if (self.awaytimer) { - clearTimeout(self.awaytimer); - } - - if (!self.inChannel()) { - return; - } - - var timeout = parseFloat(self.channel.opts.afk_timeout); - if (isNaN(timeout) || timeout <= 0) { - return; - } - - self.awaytimer = setTimeout(function () { - self.setAFK(true); - }, timeout * 1000); -}; - -/** - * Sends a kick message and disconnects the user - */ -User.prototype.kick = function (reason) { - this.socket.emit("kick", { reason: reason }); - this.socket.disconnect(true); -}; - -/** - * Initializes socket message callbacks for a channel user - */ -User.prototype.initChannelCallbacks = function () { - var self = this; - - // Verifies datatype before calling a function - // Passes a default value if the typecheck fails - var typecheck = function (type, def, fn) { - return function (data) { - if (typeof data !== type) { - fn(def); - } else { - fn(data); - } - }; - }; - - // Verify that the user is in a channel, and that the passed data is an Object - var wrapTypecheck = function (msg, fn) { - self.socket.on(msg, typecheck("object", {}, function (data) { - if (self.inChannel()) { - fn(data); - } - })); - }; - - // Verify that the user is in a channel, but don't typecheck the data - var wrap = function (msg, fn) { - self.socket.on(msg, function (data) { - if (self.inChannel()) { - fn(data); - } - }); - }; - - self.socket.on("disconnect", function () { - if (self.awaytimer) { - clearTimeout(self.awaytimer); - } - - if (self.inChannel()) { - self.channel.leave(self); - } - }); - - wrapTypecheck("joinChannel", function (data) { - if (typeof data.name !== "string") { - return; - } - - if (!util.isValidChannelName(name)) { - self.socket.emit("errorMsg", { - msg: "Invalid channel name. Channel names may consist of 1-30 " + - "characters in the set a-z, A-Z, 0-9, -, and _" - }); - self.kick("Invalid channel name"); - return; - } - - data.name = data.name.toLowerCase(); - var chan = Server.getServer().getChannel(data.name); - chan.join(self, data.pw); - }); - - wrapTypecheck("assignLeader", function (data) { - self.channel.handleChangeLeader(self, data); - }); - - wrapTypecheck("setChannelRank", function (data) { - self.channel.handleSetRank(self, data); - }); - - wrapTypecheck("chatMsg", function (data) { - if (typeof data.msg !== "string") { - return; - } - - if (data.msg.indexOf("/afk") !== 0) { - self.setAFK(false); - self.autoAFK(); - } - - self.channel.handleChat(self, data); - }); - - wrapTypecheck("newPoll", function (data) { - self.channel.handlePoll(self, data); - }); - - wrapTypecheck("vote", function (data) { - self.channel.handleVote(self, data); - }); - - wrap("closePoll", function () { - self.channel.handleClosePoll(self); - }); - - wrap("playerReady", function () { - self.channel.sendMediaUpdate([self]); - }); - - wrap("requestPlaylist", function () { - self.channel.sendPlaylist([self]); - }); - - wrapTypecheck("queue", function (data) { - self.channel.handleQueue(self, data); - }); - - wrapTypecheck("setTemp", function (data) { - self.channel.handleSetTemp(self, data); - }); - - wrapTypecheck("moveMedia", function (data) { - self.channel.handleMove(self, data); - }); - - wrapTypecheck("delete", function (data) { - self.channel.handleDelete(self, data); - }); - - wrapTypecheck("uncache", function (data) { - self.channel.handleUncache(self, data); - }); - - wrapTypecheck("jumpto", function (data) { - self.channel.handleJump(self, data); - }); - - wrap("playNext", function () { - self.channel.handlePlayNext(self); - }); - - wrap("clearPlaylist", function () { - self.channel.handleClear(self); - }); - - wrap("shufflePlaylist", function () { - self.channel.handleShuffle(self); - }); - - wrap("togglePlaylistLock", function () { - self.channel.handleToggleLock(self); - }); - - wrapTypecheck("mediaUpdate", function (data) { - self.channel.handleMediaUpdate(self, data); - }); - - wrapTypecheck("searchMedia", function (data) { - if (typeof data.query !== "string") { - return; - } - data.query = data.query.substring(0, 255); - - var searchYT = function () { - InfoGetter.Getters.ytSearch(data.query.split(" "), function (e, vids) { - if (!e) { - self.socket.emit("searchResults", { - source: "yt", - results: vids - }); - } - }); - }; - - if (data.source === "yt") { - searchYT(); - } else { - self.channel.search(data.query, function (vids) { - if (vids.length === 0) { - searchYT(); - } else { - self.socket.emit("searchResults", { - source: "library", - results: vids - }); - } - }); - } - - }); - - wrapTypecheck("setOptions", function (data) { - self.channel.handleUpdateOptions(self, data); - }); - - wrapTypecheck("setPermissions", function (data) { - self.channel.handleSetPermissions(self, data); - }); - - wrapTypecheck("setChannelCSS", function (data) { - self.channel.handleSetCSS(self, data); - }); - - wrapTypecheck("setChannelJS", function (data) { - self.channel.handleSetJS(self, data); - }); - - wrapTypecheck("setMotd", function (data) { - self.channel.handleUpdateMotd(self, data); - }); - - wrapTypecheck("updateFilter", function (data) { - self.channel.handleUpdateFilter(self, data); - }); - - // REMOVE FILTER - // https://www.youtube.com/watch?v=SxUU3zncVmI - wrapTypecheck("removeFilter", function (data) { - self.channel.handleRemoveFilter(self, data); - }); - - wrapTypecheck("moveFilter", function (data) { - self.channel.handleMoveFilter(self, data); - }); - - wrap("requestBanlist", function () { - self.channel.sendBanlist([self]); - }); - - wrap("requestChatFilter", function () { - self.channel.sendChatFilters([self]); - }); - - wrap("voteskip", function () { - self.channel.handleVoteskip(self); - }); - - wrap("readChanLog", function () { - self.channel.handleReadLog(self); - }); -}; - -User.prototype.whenLoggedIn = function (fn) { - if (self.loggedIn) { - fn(); - } else { - self.once("login", fn); - } -}; - -var lastguestlogin = {}; -User.prototype.guestLogin = function (name) { - var self = this; - var srv = Server.getServer(); - - if (self.ip in lastguestlogin) { - var diff = (Date.now() - lastguestlogin[self.ip]) / 1000; - if (diff < srv.cfg["guest-login-delay"]) { - self.socket.emit("login", { - success: false, - error: "Guest logins are restricted to one per IP address per " + - srv.cfg["guest-login-delay"] + " seconds." - }); - return; - } - } - - if (!util.isValidUserName(name)) { - self.socket.emit("login", { - success: false, - error: "Invalid username. Usernames must be 1-20 characters long and " + - "consist only of characters a-z, A-Z, 0-9, -, _, and accented " + - "letters." - }); - return; - } - - // Prevent duplicate logins - self.loggingIn = true; - db.users.isUsernameTaken(name, function (err, taken) { - self.loggingIn = false; - if (err) { - self.socket.emit("login", { - success: false, - error: err - }); - return; - } - - if (taken) { - self.socket.emit("login", { - success: false, - error: "That username is registered." - }); - return; - } - - if (self.inChannel()) { - var nameLower = name.toLowerCase(); - for (var i = 0; i < self.channel.users.length; i++) { - if (self.channel.users[i].name.toLowerCase() === nameLower) { - self.socket.emit("login", { - success: false, - error: "That name is already in use on this channel." - }); - return; - } - } - } - - // Login succeeded - lastguestlogin[self.ip] = Date.now(); - self.rank = 0; - self.global_rank = 0; - self.socket.emit("rank", 0); - self.name = name; - self.loggedIn = true; - self.socket.emit("login", { - success: true, - name: name - }); - - // TODO you shouldn't be able to guest login without being in a channel - if (self.inChannel()) { - self.channel.logger.log(self.ip + " signed in as " + name); - } - - self.emit("login"); - }); -}; diff --git a/lib/user-old.js b/lib/user-old.js new file mode 100644 index 00000000..d8b89422 --- /dev/null +++ b/lib/user-old.js @@ -0,0 +1,787 @@ +/* +The MIT License (MIT) +Copyright (c) 2013 Calvin Montgomery + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +*/ + +var Channel = require("./channel.js").Channel; +var Logger = require("./logger.js"); +var $util = require("./utilities"); +var ActionLog = require("./actionlog"); +var Server = require("./server"); +var ACP = require("./acp"); +var InfoGetter = require("./get-info"); + +// Represents a client connected via socket.io +var User = function (socket) { + this.ip = socket._ip; + this.server = Server.getServer(); + this.socket = socket; + this.loggedIn = false; + this.loggingIn = false; + this.saverank = false; + this.rank = -1 + this.global_rank = -1; + this.channel = null; + this.pendingChannel = null; + this.name = ""; + this.meta = { + afk: false, + icon: false, + aliases: [] + }; + this.queueLimiter = $util.newRateLimiter(); + this.chatLimiter = $util.newRateLimiter(); + this.rankListLimiter = $util.newRateLimiter(); + this.profile = { + image: "", + text: "" + }; + this.awaytimer = false; + this.autoAFK(); + + this.initCallbacks(); + if (this.server.announcement !== null) { + this.socket.emit("announcement", this.server.announcement); + } +}; + +User.prototype.inChannel = function () { + return this.channel !== null && !this.channel.dead; +}; + +User.prototype.inPendingChannel = function () { + return this.pendingChannel != null && !this.pendingChannel.dead; +}; + +User.prototype.setAFK = function (afk) { + if (!this.inChannel()) + return; + if (this.meta.afk === afk) + return; + var chan = this.channel; + this.meta.afk = afk; + if (afk) { + if (chan.voteskip) + chan.voteskip.unvote(this.ip); + } else { + this.autoAFK(); + } + chan.checkVoteskipPass(); + chan.sendAll("setAFK", { + name: this.name, + afk: afk + }); +}; + +User.prototype.autoAFK = function () { + var self = this; + if (self.awaytimer) + clearTimeout(self.awaytimer); + + if (!self.inChannel()) { + return; + } + + var timeout = parseFloat(self.channel.opts.afk_timeout); + if (isNaN(timeout)) { + return; + } + + if (timeout <= 0) { + return; + } + + self.awaytimer = setTimeout(function () { + self.setAFK(true); + }, timeout * 1000); +}; + +User.prototype.kick = function (reason) { + this.socket.emit("kick", { reason: reason }); + this.socket.disconnect(true); +}; + +User.prototype.initCallbacks = function () { + var self = this; + self.socket.on("disconnect", function () { + self.awaytimer && clearTimeout(self.awaytimer); + if (self.inChannel()) + self.channel.userLeave(self); + else if (self.inPendingChannel()) + self.pendingChannel.userLeave(self); + }); + + self.socket.on("joinChannel", function (data) { + data = (typeof data !== "object") ? {} : data; + if (self.inChannel() || self.inPendingChannel()) { + return; + } + if (typeof data.name != "string") { + return; + } + if (!data.name.match(/^[\w-_]{1,30}$/)) { + self.socket.emit("errorMsg", { + msg: "Invalid channel name. Channel names may consist of"+ + " 1-30 characters in the set a-z, A-Z, 0-9, -, and _" + }); + self.kick("Invalid channel name"); + return; + } + data.name = data.name.toLowerCase(); + self.pendingChannel = self.server.getChannel(data.name); + if (self.loggedIn) { + // TODO fix + // I'm not sure what I meant by "fix", but I suppose I'll find out soon + self.pendingChannel.getRank(self.name, function (err, rank) { + if (!err && rank > self.rank) + self.rank = rank; + }); + } + self.pendingChannel.join(self); + }); + + self.socket.on("channelPassword", function (pw) { + if (!self.inChannel() && self.inPendingChannel()) { + self.pendingChannel.userJoin(self, pw); + } + }); + + self.socket.on("login", function (data) { + data = (typeof data !== "object") ? {} : data; + var name = (typeof data.name === "string") ? data.name : ""; + var pw = (typeof data.pw === "string") ? data.pw : ""; + var session = (typeof data.session === "string") ? data.session : ""; + if (pw.length > 100) + pw = pw.substring(0, 100); + if (session.length > 64) + session = session.substring(0, 64); + + if (self.loggedIn) + return; + + if (self.loggingIn) { + var j = 0; + // Wait until current login finishes + var i = setInterval(function () { + j++; + if (!self.loggingIn) { + clearInterval(i); + if (!self.loggedIn) + self.login(name, pw, session); + return; + } + // Just in case to prevent the interval from going wild + if (j >= 4) + clearInterval(i); + }, 1000); + } else { + self.login(name, pw, session); + } + }); + + self.socket.on("assignLeader", function (data) { + data = (typeof data !== "object") ? {} : data; + if (self.inChannel()) { + self.channel.tryChangeLeader(self, data); + } + }); + + self.socket.on("setChannelRank", function (data) { + data = (typeof data !== "object") ? {} : data; + if (self.inChannel()) { + self.channel.trySetRank(self, data); + } + }); + + self.socket.on("unban", function (data) { + data = (typeof data !== "object") ? {} : data; + if (self.inChannel()) { + self.channel.tryUnban(self, data); + } + }); + + self.socket.on("chatMsg", function (data) { + data = (typeof data !== "object") ? {} : data; + if (self.inChannel()) { + if (typeof data.msg !== "string") { + return; + } + if (data.msg.indexOf("/afk") !== 0) { + self.setAFK(false); + self.autoAFK(); + } + self.channel.tryChat(self, data); + } + }); + + self.socket.on("newPoll", function (data) { + data = (typeof data !== "object") ? {} : data; + if (self.inChannel()) { + self.channel.tryOpenPoll(self, data); + } + }); + + self.socket.on("playerReady", function () { + if (self.inChannel()) { + self.channel.sendMediaUpdate(self); + } + }); + + self.socket.on("requestPlaylist", function () { + if (self.inChannel()) { + self.channel.sendPlaylist(self); + } + }); + + self.socket.on("queue", function (data) { + data = (typeof data !== "object") ? {} : data; + if (self.inChannel()) { + self.channel.tryQueue(self, data); + } + }); + + self.socket.on("setTemp", function (data) { + data = (typeof data !== "object") ? {} : data; + if (self.inChannel()) { + self.channel.trySetTemp(self, data); + } + }); + + self.socket.on("delete", function (data) { + if (self.inChannel()) { + self.channel.tryDequeue(self, data); + } + }); + + self.socket.on("uncache", function (data) { + data = (typeof data !== "object") ? {} : data; + if (self.inChannel()) { + self.channel.tryUncache(self, data); + } + }); + + self.socket.on("moveMedia", function (data) { + data = (typeof data !== "object") ? {} : data; + if (self.inChannel()) { + self.channel.tryMove(self, data); + } + }); + + self.socket.on("jumpTo", function (data) { + if (self.inChannel()) { + self.channel.tryJumpTo(self, data); + } + }); + + self.socket.on("playNext", function () { + if (self.inChannel()) { + self.channel.tryPlayNext(self); + } + }); + + self.socket.on("clearPlaylist", function () { + if (self.inChannel()) { + self.channel.tryClearqueue(self); + } + }); + + self.socket.on("shufflePlaylist", function () { + if (self.inChannel()) { + self.channel.tryShufflequeue(self); + } + }); + + self.socket.on("togglePlaylistLock", function () { + if (self.inChannel()) { + self.channel.tryToggleLock(self); + } + }); + + self.socket.on("mediaUpdate", function (data) { + data = (typeof data !== "object") ? {} : data; + if (self.inChannel()) { + self.channel.tryUpdate(self, data); + } + }); + + self.socket.on("searchMedia", function (data) { + data = (typeof data !== "object") ? {} : data; + if (self.inChannel()) { + if (typeof data.query !== "string") { + return; + } + // Soft limit to prevent someone from making a massive query + if (data.query.length > 255) { + data.query = data.query.substring(0, 255); + } + if (data.source === "yt") { + var searchfn = InfoGetter.Getters.ytSearch; + searchfn(data.query.split(" "), function (e, vids) { + if (!e) { + self.socket.emit("searchResults", { + source: "yt", + results: vids + }); + } + }); + } else { + self.channel.search(data.query, function (vids) { + if (vids.length === 0) { + var searchfn = InfoGetter.Getters.ytSearch; + searchfn(data.query.split(" "), function (e, vids) { + if (!e) { + self.socket.emit("searchResults", { + source: "yt", + results: vids + }); + } + }); + return; + } + self.socket.emit("searchResults", { + source: "library", + results: vids + }); + }); + } + } + }); + + self.socket.on("closePoll", function () { + if (self.inChannel()) { + self.channel.tryClosePoll(self); + } + }); + + self.socket.on("vote", function (data) { + data = (typeof data !== "object") ? {} : data; + if (self.inChannel()) { + self.channel.tryVote(self, data); + } + }); + + self.socket.on("registerChannel", function (data) { + if (!self.inChannel()) { + self.socket.emit("channelRegistration", { + success: false, + error: "You're not in any channel!" + }); + } else { + self.channel.tryRegister(self); + } + }); + + self.socket.on("unregisterChannel", function () { + if (!self.inChannel()) { + return; + } + if (self.rank < 10) { + self.kick("Attempted unregisterChannel with insufficient permission"); + return; + } + self.channel.unregister(self); + }); + + self.socket.on("setOptions", function (data) { + data = (typeof data !== "object") ? {} : data; + if (self.inChannel()) { + self.channel.tryUpdateOptions(self, data); + } + }); + + self.socket.on("setPermissions", function (data) { + data = (typeof data !== "object") ? {} : data; + if (self.inChannel()) { + self.channel.tryUpdatePermissions(self, data); + } + }); + + self.socket.on("setChannelCSS", function (data) { + data = (typeof data !== "object") ? {} : data; + if (self.inChannel()) { + self.channel.trySetCSS(self, data); + } + }); + + self.socket.on("setChannelJS", function (data) { + data = (typeof data !== "object") ? {} : data; + if (self.inChannel()) { + self.channel.trySetJS(self, data); + } + }); + + self.socket.on("updateFilter", function (data) { + data = (typeof data !== "object") ? {} : data; + if (self.inChannel()) { + self.channel.tryUpdateFilter(self, data); + } + }); + + self.socket.on("removeFilter", function (data) { + data = (typeof data !== "object") ? {} : data; + if (self.inChannel()) { + self.channel.tryRemoveFilter(self, data); + } + }); + + self.socket.on("moveFilter", function (data) { + data = (typeof data !== "object") ? {} : data; + if (self.inChannel()) { + self.channel.tryMoveFilter(self, data); + } + }); + + self.socket.on("setMotd", function (data) { + data = (typeof data !== "object") ? {} : data; + if (self.inChannel()) { + self.channel.tryUpdateMotd(self, data); + } + }); + + self.socket.on("requestLoginHistory", function () { + if (self.inChannel()) { + self.channel.sendLoginHistory(self); + } + }); + + self.socket.on("requestBanlist", function () { + if (self.inChannel()) { + self.channel.sendBanlist(self); + } + }); + + self.socket.on("requestChatFilters", function () { + if (self.inChannel()) { + self.channel.sendChatFilters(self); + } + }); + + self.socket.on("requestChannelRanks", function () { + if (self.inChannel()) { + if (self.rankListLimiter.throttle({ + burst: 0, + sustained: 0.1, + cooldown: 10 + })) { + self.socket.emit("noflood", { + action: "channel ranks", + msg: "You may only refresh channel ranks once every 10 seconds" + }); + } else { + self.channel.sendChannelRanks(self); + } + } + }); + + self.socket.on("voteskip", function (data) { + data = (typeof data !== "object") ? {} : data; + if (self.inChannel()) { + self.channel.tryVoteskip(self); + } + }); + + self.socket.on("listPlaylists", function (data) { + data = (typeof data !== "object") ? {} : data; + if (self.name === "" || self.rank < 1) { + self.socket.emit("listPlaylists", { + pllist: [], + error: "You must be logged in to manage playlists" + }); + return; + } + + self.server.db.listUserPlaylists(self.name, function (err, list) { + if (err) + list = []; + for(var i = 0; i < list.length; i++) { + list[i].time = $util.formatTime(list[i].time); + } + self.socket.emit("listPlaylists", { + pllist: list + }); + }); + }); + + self.socket.on("savePlaylist", function (data) { + data = (typeof data !== "object") ? {} : data; + if (typeof data.name !== "string") { + return; + } + // Soft limit to prevent someone from saving a list with a massive name + if (data.name.length > 200) { + data.name = data.name.substring(0, 200); + } + if (self.rank < 1) { + self.socket.emit("savePlaylist", { + success: false, + error: "You must be logged in to manage playlists" + }); + return; + } + + if (!self.inChannel()) { + self.socket.emit("savePlaylist", { + success: false, + error: "Not in a channel" + }); + return; + } + + if (typeof data.name != "string") { + return; + } + + var pl = self.channel.playlist.items.toArray(); + self.server.db.saveUserPlaylist(pl, self.name, data.name, + function (err, res) { + if (err) { + self.socket.emit("savePlaylist", { + success: false, + error: err + }); + return; + } + + self.socket.emit("savePlaylist", { + success: true + }); + + self.server.db.listUserPlaylists(self.name, + function (err, list) { + if (err) + list = []; + for(var i = 0; i < list.length; i++) { + list[i].time = $util.formatTime(list[i].time); + } + self.socket.emit("listPlaylists", { + pllist: list + }); + }); + }); + }); + + self.socket.on("queuePlaylist", function (data) { + data = (typeof data !== "object") ? {} : data; + if (self.inChannel()) { + self.channel.tryQueuePlaylist(self, data); + } + }); + + self.socket.on("deletePlaylist", function (data) { + data = (typeof data !== "object") ? {} : data; + if (typeof data.name != "string") { + return; + } + + self.server.db.deleteUserPlaylist(self.name, data.name, + function () { + self.server.db.listUserPlaylists(self.name, + function (err, list) { + if (err) + list = []; + for(var i = 0; i < list.length; i++) { + list[i].time = $util.formatTime(list[i].time); + } + self.socket.emit("listPlaylists", { + pllist: list + }); + }); + }); + }); + + self.socket.on("readChanLog", function () { + if (self.inChannel()) { + self.channel.tryReadLog(self); + } + }); + + self.socket.on("acp-init", function () { + if (self.global_rank >= 255) + ACP.init(self); + }); + + self.socket.on("borrow-rank", function (rank) { + if (self.global_rank < 255) + return; + if (rank > self.global_rank) + return; + + self.rank = rank; + self.socket.emit("rank", rank); + if (self.inChannel()) { + self.channel.sendAll("setUserRank", { + name: self.name, + rank: rank + }); + } + + }); +}; + +var lastguestlogin = {}; +User.prototype.guestLogin = function (name) { + var self = this; + + if (self.ip in lastguestlogin) { + var diff = (Date.now() - lastguestlogin[self.ip])/1000; + if (diff < self.server.cfg["guest-login-delay"]) { + self.socket.emit("login", { + success: false, + error: "Guest logins are restricted to one per IP address "+ + "per " + self.server.cfg["guest-login-delay"] + + " seconds." + }); + return false; + } + } + + if (!$util.isValidUserName(name)) { + self.socket.emit("login", { + success: false, + error: "Invalid username. Usernames must be 1-20 characters "+ + "long and consist only of characters a-z, A-Z, 0-9, -, "+ + "and _" + }); + return; + } + + // Set the loggingIn flag to avoid race conditions with the callback + self.loggingIn = true; + self.server.db.isUsernameTaken(name, function (err, taken) { + self.loggingIn = false; + if (err) { + self.socket.emit("login", { + success: false, + error: "Internal error: " + err + }); + return; + } + + if (taken) { + self.socket.emit("login", { + success: false, + error: "That username is registered and protected." + }); + return; + } + + if (self.inChannel()) { + var lname = name.toLowerCase(); + for(var i = 0; i < self.channel.users.length; i++) { + if (self.channel.users[i].name.toLowerCase() === lname) { + self.socket.emit("login", { + success: false, + error: "That name is already in use on this channel" + }); + return; + } + } + } + lastguestlogin[self.ip] = Date.now(); + self.rank = 0; + Logger.syslog.log(self.ip + " signed in as " + name); + self.server.db.recordVisit(self.ip, name); + self.name = name; + self.loggedIn = false; + self.socket.emit("login", { + success: true, + name: name + }); + self.socket.emit("rank", self.rank); + if (self.inChannel()) { + self.channel.logger.log(self.ip + " signed in as " + name); + self.channel.broadcastNewUser(self); + } + }); +}; + +// Attempt to login +User.prototype.login = function (name, pw, session) { + var self = this; + // No password => try guest login + if (pw === "" && session === "") { + this.guestLogin(name); + } else { + self.loggingIn = true; + self.server.db.userLogin(name, pw, session, function (err, row) { + if (err) { + self.loggingIn = false; + ActionLog.record(self.ip, name, "login-failure", + err); + self.socket.emit("login", { + success: false, + error: err + }); + return; + } + if (self.inChannel()) { + var n = name.toLowerCase(); + for(var i = 0; i < self.channel.users.length; i++) { + if (self.channel.users[i].name.toLowerCase() === n) { + if (self.channel.users[i] === self) { + Logger.errlog.log("Wat: user.login() but user "+ + "already logged in on channel"); + break; + } + self.channel.kick(self.channel.users[i], + "Duplicate login"); + } + } + } + // Record logins for administrator accounts + if (self.global_rank >= 255) + ActionLog.record(self.ip, name, "login-success"); + self.loggedIn = true; + self.loggingIn = false; + self.socket.emit("login", { + success: true, + session: row.session_hash, + name: name + }); + Logger.syslog.log(self.ip + " logged in as " + name); + self.server.db.recordVisit(self.ip, name); + self.profile = { + image: row.profile_image, + text: row.profile_text + }; + self.global_rank = row.global_rank; + var afterRankLookup = function () { + self.socket.emit("rank", self.rank); + self.name = name; + if (self.inChannel()) { + self.channel.logger.log(self.ip + " logged in as " + + name); + self.channel.broadcastNewUser(self); + } else if (self.inPendingChannel()) { + self.pendingChannel.userJoin(self); + } + }; + if (self.inChannel() || self.inPendingChannel()) { + var chan = self.channel != null ? self.channel : self.pendingChannel; + chan.getRank(name, function (err, rank) { + if (!err) { + self.saverank = true; + self.rank = rank; + } else { + // If there was an error in retrieving the rank, + // don't overwrite it with a bad value + self.saverank = false; + self.rank = self.global_rank; + } + afterRankLookup(); + }); + } else { + self.rank = self.global_rank; + afterRankLookup(); + } + }); + } +}; + +module.exports = User; diff --git a/lib/user.js b/lib/user.js index d8b89422..d4fc98fc 100644 --- a/lib/user.js +++ b/lib/user.js @@ -1,77 +1,72 @@ -/* -The MIT License (MIT) -Copyright (c) 2013 Calvin Montgomery - -Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -*/ - -var Channel = require("./channel.js").Channel; -var Logger = require("./logger.js"); -var $util = require("./utilities"); -var ActionLog = require("./actionlog"); +var Logger = require("./logger"); var Server = require("./server"); -var ACP = require("./acp"); -var InfoGetter = require("./get-info"); +var util = require("./utilities"); +var MakeEmitter = require("./emitter"); -// Represents a client connected via socket.io -var User = function (socket) { - this.ip = socket._ip; - this.server = Server.getServer(); - this.socket = socket; - this.loggedIn = false; - this.loggingIn = false; - this.saverank = false; - this.rank = -1 - this.global_rank = -1; - this.channel = null; - this.pendingChannel = null; - this.name = ""; - this.meta = { - afk: false, - icon: false, - aliases: [] - }; - this.queueLimiter = $util.newRateLimiter(); - this.chatLimiter = $util.newRateLimiter(); - this.rankListLimiter = $util.newRateLimiter(); - this.profile = { +function User(socket) { + var self = this; + MakeEmitter(self); + self.socket = socket; + self.ip = socket._ip; + self.loggedIn = false; + self.loggingIn = false; + self.rank = -1; + self.global_rank = -1; + self.channel = null; + self.name = ""; + self.canonicalName = ""; + self.profile = { image: "", text: "" }; - this.awaytimer = false; - this.autoAFK(); + self.meta = { + afk: false, + muted: false, + smuted: false, + aliases: [] + }; + self.queueLimiter = util.newRateLimiter(); + self.chatLimiter = util.newRateLimiter(); + self.awaytimer = false; - this.initCallbacks(); - if (this.server.announcement !== null) { - this.socket.emit("announcement", this.server.announcement); + self.socket.once("initChannelCallbacks", function () { + self.initChannelCallbacks(); + }); + + var announcement = Server.getServer().announcement; + if (announcement != null) { + self.socket.emit("announcement", announcement); } -}; +} +/** + * Checks whether the user is in a valid channel + */ User.prototype.inChannel = function () { - return this.channel !== null && !this.channel.dead; -}; - -User.prototype.inPendingChannel = function () { - return this.pendingChannel != null && !this.pendingChannel.dead; + return this.channel != null && !this.channel.dead; }; +/** + * Changes a user's AFK status, updating the channel voteskip if necessary + */ User.prototype.setAFK = function (afk) { - if (!this.inChannel()) + if (!this.inChannel()) { return; - if (this.meta.afk === afk) + } + + if (this.meta.afk === afk) { return; + } + var chan = this.channel; - this.meta.afk = afk; if (afk) { - if (chan.voteskip) + if (chan.voteskip) { chan.voteskip.unvote(this.ip); + } } else { this.autoAFK(); } + chan.checkVoteskipPass(); chan.sendAll("setAFK", { name: this.name, @@ -79,21 +74,22 @@ User.prototype.setAFK = function (afk) { }); }; +/** + * Sets a timer to automatically mark the user as AFK after + * a period of inactivity + */ User.prototype.autoAFK = function () { var self = this; - if (self.awaytimer) + if (self.awaytimer) { clearTimeout(self.awaytimer); + } if (!self.inChannel()) { return; } var timeout = parseFloat(self.channel.opts.afk_timeout); - if (isNaN(timeout)) { - return; - } - - if (timeout <= 0) { + if (isNaN(timeout) || timeout <= 0) { return; } @@ -102,563 +98,292 @@ User.prototype.autoAFK = function () { }, timeout * 1000); }; +/** + * Sends a kick message and disconnects the user + */ User.prototype.kick = function (reason) { this.socket.emit("kick", { reason: reason }); this.socket.disconnect(true); }; -User.prototype.initCallbacks = function () { +/** + * Initializes socket message callbacks for a channel user + */ +User.prototype.initChannelCallbacks = function () { var self = this; + + // Verifies datatype before calling a function + // Passes a default value if the typecheck fails + var typecheck = function (type, def, fn) { + return function (data) { + if (typeof data !== type) { + fn(def); + } else { + fn(data); + } + }; + }; + + // Verify that the user is in a channel, and that the passed data is an Object + var wrapTypecheck = function (msg, fn) { + self.socket.on(msg, typecheck("object", {}, function (data) { + if (self.inChannel()) { + fn(data); + } + })); + }; + + // Verify that the user is in a channel, but don't typecheck the data + var wrap = function (msg, fn) { + self.socket.on(msg, function (data) { + if (self.inChannel()) { + fn(data); + } + }); + }; + self.socket.on("disconnect", function () { - self.awaytimer && clearTimeout(self.awaytimer); - if (self.inChannel()) - self.channel.userLeave(self); - else if (self.inPendingChannel()) - self.pendingChannel.userLeave(self); + if (self.awaytimer) { + clearTimeout(self.awaytimer); + } + + if (self.inChannel()) { + self.channel.leave(self); + } }); - self.socket.on("joinChannel", function (data) { - data = (typeof data !== "object") ? {} : data; - if (self.inChannel() || self.inPendingChannel()) { + self.socket.on("joinChannel", typecheck("object", {}, function (data) { + if (typeof data.name !== "string") { return; } - if (typeof data.name != "string") { - return; - } - if (!data.name.match(/^[\w-_]{1,30}$/)) { + + if (!util.isValidChannelName(data.name)) { self.socket.emit("errorMsg", { - msg: "Invalid channel name. Channel names may consist of"+ - " 1-30 characters in the set a-z, A-Z, 0-9, -, and _" + msg: "Invalid channel name. Channel names may consist of 1-30 " + + "characters in the set a-z, A-Z, 0-9, -, and _" }); self.kick("Invalid channel name"); return; } + data.name = data.name.toLowerCase(); - self.pendingChannel = self.server.getChannel(data.name); - if (self.loggedIn) { - // TODO fix - // I'm not sure what I meant by "fix", but I suppose I'll find out soon - self.pendingChannel.getRank(self.name, function (err, rank) { - if (!err && rank > self.rank) - self.rank = rank; - }); - } - self.pendingChannel.join(self); + var chan = Server.getServer().getChannel(data.name); + chan.join(self, data.pw); + })); + + wrapTypecheck("assignLeader", function (data) { + self.channel.handleChangeLeader(self, data); }); - self.socket.on("channelPassword", function (pw) { - if (!self.inChannel() && self.inPendingChannel()) { - self.pendingChannel.userJoin(self, pw); - } + wrapTypecheck("setChannelRank", function (data) { + self.channel.handleSetRank(self, data); }); - self.socket.on("login", function (data) { - data = (typeof data !== "object") ? {} : data; - var name = (typeof data.name === "string") ? data.name : ""; - var pw = (typeof data.pw === "string") ? data.pw : ""; - var session = (typeof data.session === "string") ? data.session : ""; - if (pw.length > 100) - pw = pw.substring(0, 100); - if (session.length > 64) - session = session.substring(0, 64); - - if (self.loggedIn) + wrapTypecheck("chatMsg", function (data) { + if (typeof data.msg !== "string") { return; + } - if (self.loggingIn) { - var j = 0; - // Wait until current login finishes - var i = setInterval(function () { - j++; - if (!self.loggingIn) { - clearInterval(i); - if (!self.loggedIn) - self.login(name, pw, session); - return; + if (data.msg.indexOf("/afk") !== 0) { + self.setAFK(false); + self.autoAFK(); + } + + self.channel.handleChat(self, data); + }); + + wrapTypecheck("newPoll", function (data) { + self.channel.handlePoll(self, data); + }); + + wrapTypecheck("vote", function (data) { + self.channel.handleVote(self, data); + }); + + wrap("closePoll", function () { + self.channel.handleClosePoll(self); + }); + + wrap("playerReady", function () { + self.channel.sendMediaUpdate([self]); + }); + + wrap("requestPlaylist", function () { + self.channel.sendPlaylist([self]); + }); + + wrapTypecheck("queue", function (data) { + self.channel.handleQueue(self, data); + }); + + wrapTypecheck("setTemp", function (data) { + self.channel.handleSetTemp(self, data); + }); + + wrapTypecheck("moveMedia", function (data) { + self.channel.handleMove(self, data); + }); + + wrapTypecheck("delete", function (data) { + self.channel.handleDelete(self, data); + }); + + wrapTypecheck("uncache", function (data) { + self.channel.handleUncache(self, data); + }); + + wrapTypecheck("jumpto", function (data) { + self.channel.handleJump(self, data); + }); + + wrap("playNext", function () { + self.channel.handlePlayNext(self); + }); + + wrap("clearPlaylist", function () { + self.channel.handleClear(self); + }); + + wrap("shufflePlaylist", function () { + self.channel.handleShuffle(self); + }); + + wrap("togglePlaylistLock", function () { + self.channel.handleToggleLock(self); + }); + + wrapTypecheck("mediaUpdate", function (data) { + self.channel.handleMediaUpdate(self, data); + }); + + wrapTypecheck("searchMedia", function (data) { + if (typeof data.query !== "string") { + return; + } + data.query = data.query.substring(0, 255); + + var searchYT = function () { + InfoGetter.Getters.ytSearch(data.query.split(" "), function (e, vids) { + if (!e) { + self.socket.emit("searchResults", { + source: "yt", + results: vids + }); } - // Just in case to prevent the interval from going wild - if (j >= 4) - clearInterval(i); - }, 1000); + }); + }; + + if (data.source === "yt") { + searchYT(); } else { - self.login(name, pw, session); - } - }); - - self.socket.on("assignLeader", function (data) { - data = (typeof data !== "object") ? {} : data; - if (self.inChannel()) { - self.channel.tryChangeLeader(self, data); - } - }); - - self.socket.on("setChannelRank", function (data) { - data = (typeof data !== "object") ? {} : data; - if (self.inChannel()) { - self.channel.trySetRank(self, data); - } - }); - - self.socket.on("unban", function (data) { - data = (typeof data !== "object") ? {} : data; - if (self.inChannel()) { - self.channel.tryUnban(self, data); - } - }); - - self.socket.on("chatMsg", function (data) { - data = (typeof data !== "object") ? {} : data; - if (self.inChannel()) { - if (typeof data.msg !== "string") { - return; - } - if (data.msg.indexOf("/afk") !== 0) { - self.setAFK(false); - self.autoAFK(); - } - self.channel.tryChat(self, data); - } - }); - - self.socket.on("newPoll", function (data) { - data = (typeof data !== "object") ? {} : data; - if (self.inChannel()) { - self.channel.tryOpenPoll(self, data); - } - }); - - self.socket.on("playerReady", function () { - if (self.inChannel()) { - self.channel.sendMediaUpdate(self); - } - }); - - self.socket.on("requestPlaylist", function () { - if (self.inChannel()) { - self.channel.sendPlaylist(self); - } - }); - - self.socket.on("queue", function (data) { - data = (typeof data !== "object") ? {} : data; - if (self.inChannel()) { - self.channel.tryQueue(self, data); - } - }); - - self.socket.on("setTemp", function (data) { - data = (typeof data !== "object") ? {} : data; - if (self.inChannel()) { - self.channel.trySetTemp(self, data); - } - }); - - self.socket.on("delete", function (data) { - if (self.inChannel()) { - self.channel.tryDequeue(self, data); - } - }); - - self.socket.on("uncache", function (data) { - data = (typeof data !== "object") ? {} : data; - if (self.inChannel()) { - self.channel.tryUncache(self, data); - } - }); - - self.socket.on("moveMedia", function (data) { - data = (typeof data !== "object") ? {} : data; - if (self.inChannel()) { - self.channel.tryMove(self, data); - } - }); - - self.socket.on("jumpTo", function (data) { - if (self.inChannel()) { - self.channel.tryJumpTo(self, data); - } - }); - - self.socket.on("playNext", function () { - if (self.inChannel()) { - self.channel.tryPlayNext(self); - } - }); - - self.socket.on("clearPlaylist", function () { - if (self.inChannel()) { - self.channel.tryClearqueue(self); - } - }); - - self.socket.on("shufflePlaylist", function () { - if (self.inChannel()) { - self.channel.tryShufflequeue(self); - } - }); - - self.socket.on("togglePlaylistLock", function () { - if (self.inChannel()) { - self.channel.tryToggleLock(self); - } - }); - - self.socket.on("mediaUpdate", function (data) { - data = (typeof data !== "object") ? {} : data; - if (self.inChannel()) { - self.channel.tryUpdate(self, data); - } - }); - - self.socket.on("searchMedia", function (data) { - data = (typeof data !== "object") ? {} : data; - if (self.inChannel()) { - if (typeof data.query !== "string") { - return; - } - // Soft limit to prevent someone from making a massive query - if (data.query.length > 255) { - data.query = data.query.substring(0, 255); - } - if (data.source === "yt") { - var searchfn = InfoGetter.Getters.ytSearch; - searchfn(data.query.split(" "), function (e, vids) { - if (!e) { - self.socket.emit("searchResults", { - source: "yt", - results: vids - }); - } - }); - } else { - self.channel.search(data.query, function (vids) { - if (vids.length === 0) { - var searchfn = InfoGetter.Getters.ytSearch; - searchfn(data.query.split(" "), function (e, vids) { - if (!e) { - self.socket.emit("searchResults", { - source: "yt", - results: vids - }); - } - }); - return; - } + self.channel.search(data.query, function (vids) { + if (vids.length === 0) { + searchYT(); + } else { self.socket.emit("searchResults", { source: "library", results: vids }); - }); - } - } - }); - - self.socket.on("closePoll", function () { - if (self.inChannel()) { - self.channel.tryClosePoll(self); - } - }); - - self.socket.on("vote", function (data) { - data = (typeof data !== "object") ? {} : data; - if (self.inChannel()) { - self.channel.tryVote(self, data); - } - }); - - self.socket.on("registerChannel", function (data) { - if (!self.inChannel()) { - self.socket.emit("channelRegistration", { - success: false, - error: "You're not in any channel!" - }); - } else { - self.channel.tryRegister(self); - } - }); - - self.socket.on("unregisterChannel", function () { - if (!self.inChannel()) { - return; - } - if (self.rank < 10) { - self.kick("Attempted unregisterChannel with insufficient permission"); - return; - } - self.channel.unregister(self); - }); - - self.socket.on("setOptions", function (data) { - data = (typeof data !== "object") ? {} : data; - if (self.inChannel()) { - self.channel.tryUpdateOptions(self, data); - } - }); - - self.socket.on("setPermissions", function (data) { - data = (typeof data !== "object") ? {} : data; - if (self.inChannel()) { - self.channel.tryUpdatePermissions(self, data); - } - }); - - self.socket.on("setChannelCSS", function (data) { - data = (typeof data !== "object") ? {} : data; - if (self.inChannel()) { - self.channel.trySetCSS(self, data); - } - }); - - self.socket.on("setChannelJS", function (data) { - data = (typeof data !== "object") ? {} : data; - if (self.inChannel()) { - self.channel.trySetJS(self, data); - } - }); - - self.socket.on("updateFilter", function (data) { - data = (typeof data !== "object") ? {} : data; - if (self.inChannel()) { - self.channel.tryUpdateFilter(self, data); - } - }); - - self.socket.on("removeFilter", function (data) { - data = (typeof data !== "object") ? {} : data; - if (self.inChannel()) { - self.channel.tryRemoveFilter(self, data); - } - }); - - self.socket.on("moveFilter", function (data) { - data = (typeof data !== "object") ? {} : data; - if (self.inChannel()) { - self.channel.tryMoveFilter(self, data); - } - }); - - self.socket.on("setMotd", function (data) { - data = (typeof data !== "object") ? {} : data; - if (self.inChannel()) { - self.channel.tryUpdateMotd(self, data); - } - }); - - self.socket.on("requestLoginHistory", function () { - if (self.inChannel()) { - self.channel.sendLoginHistory(self); - } - }); - - self.socket.on("requestBanlist", function () { - if (self.inChannel()) { - self.channel.sendBanlist(self); - } - }); - - self.socket.on("requestChatFilters", function () { - if (self.inChannel()) { - self.channel.sendChatFilters(self); - } - }); - - self.socket.on("requestChannelRanks", function () { - if (self.inChannel()) { - if (self.rankListLimiter.throttle({ - burst: 0, - sustained: 0.1, - cooldown: 10 - })) { - self.socket.emit("noflood", { - action: "channel ranks", - msg: "You may only refresh channel ranks once every 10 seconds" - }); - } else { - self.channel.sendChannelRanks(self); - } - } - }); - - self.socket.on("voteskip", function (data) { - data = (typeof data !== "object") ? {} : data; - if (self.inChannel()) { - self.channel.tryVoteskip(self); - } - }); - - self.socket.on("listPlaylists", function (data) { - data = (typeof data !== "object") ? {} : data; - if (self.name === "" || self.rank < 1) { - self.socket.emit("listPlaylists", { - pllist: [], - error: "You must be logged in to manage playlists" - }); - return; - } - - self.server.db.listUserPlaylists(self.name, function (err, list) { - if (err) - list = []; - for(var i = 0; i < list.length; i++) { - list[i].time = $util.formatTime(list[i].time); - } - self.socket.emit("listPlaylists", { - pllist: list - }); - }); - }); - - self.socket.on("savePlaylist", function (data) { - data = (typeof data !== "object") ? {} : data; - if (typeof data.name !== "string") { - return; - } - // Soft limit to prevent someone from saving a list with a massive name - if (data.name.length > 200) { - data.name = data.name.substring(0, 200); - } - if (self.rank < 1) { - self.socket.emit("savePlaylist", { - success: false, - error: "You must be logged in to manage playlists" - }); - return; - } - - if (!self.inChannel()) { - self.socket.emit("savePlaylist", { - success: false, - error: "Not in a channel" - }); - return; - } - - if (typeof data.name != "string") { - return; - } - - var pl = self.channel.playlist.items.toArray(); - self.server.db.saveUserPlaylist(pl, self.name, data.name, - function (err, res) { - if (err) { - self.socket.emit("savePlaylist", { - success: false, - error: err - }); - return; - } - - self.socket.emit("savePlaylist", { - success: true - }); - - self.server.db.listUserPlaylists(self.name, - function (err, list) { - if (err) - list = []; - for(var i = 0; i < list.length; i++) { - list[i].time = $util.formatTime(list[i].time); } - self.socket.emit("listPlaylists", { - pllist: list - }); - }); - }); - }); - - self.socket.on("queuePlaylist", function (data) { - data = (typeof data !== "object") ? {} : data; - if (self.inChannel()) { - self.channel.tryQueuePlaylist(self, data); - } - }); - - self.socket.on("deletePlaylist", function (data) { - data = (typeof data !== "object") ? {} : data; - if (typeof data.name != "string") { - return; - } - - self.server.db.deleteUserPlaylist(self.name, data.name, - function () { - self.server.db.listUserPlaylists(self.name, - function (err, list) { - if (err) - list = []; - for(var i = 0; i < list.length; i++) { - list[i].time = $util.formatTime(list[i].time); - } - self.socket.emit("listPlaylists", { - pllist: list - }); - }); - }); - }); - - self.socket.on("readChanLog", function () { - if (self.inChannel()) { - self.channel.tryReadLog(self); - } - }); - - self.socket.on("acp-init", function () { - if (self.global_rank >= 255) - ACP.init(self); - }); - - self.socket.on("borrow-rank", function (rank) { - if (self.global_rank < 255) - return; - if (rank > self.global_rank) - return; - - self.rank = rank; - self.socket.emit("rank", rank); - if (self.inChannel()) { - self.channel.sendAll("setUserRank", { - name: self.name, - rank: rank }); } }); + + wrapTypecheck("setOptions", function (data) { + self.channel.handleUpdateOptions(self, data); + }); + + wrapTypecheck("setPermissions", function (data) { + self.channel.handleSetPermissions(self, data); + }); + + wrapTypecheck("setChannelCSS", function (data) { + self.channel.handleSetCSS(self, data); + }); + + wrapTypecheck("setChannelJS", function (data) { + self.channel.handleSetJS(self, data); + }); + + wrapTypecheck("setMotd", function (data) { + self.channel.handleUpdateMotd(self, data); + }); + + wrapTypecheck("updateFilter", function (data) { + self.channel.handleUpdateFilter(self, data); + }); + + // REMOVE FILTER + // https://www.youtube.com/watch?v=SxUU3zncVmI + wrapTypecheck("removeFilter", function (data) { + self.channel.handleRemoveFilter(self, data); + }); + + wrapTypecheck("moveFilter", function (data) { + self.channel.handleMoveFilter(self, data); + }); + + wrap("requestBanlist", function () { + self.channel.sendBanlist([self]); + }); + + wrap("requestChatFilter", function () { + self.channel.sendChatFilters([self]); + }); + + wrap("voteskip", function () { + self.channel.handleVoteskip(self); + }); + + wrap("readChanLog", function () { + self.channel.handleReadLog(self); + }); +}; + +User.prototype.whenLoggedIn = function (fn) { + if (this.loggedIn) { + fn(); + } else { + this.once("login", fn); + } }; var lastguestlogin = {}; User.prototype.guestLogin = function (name) { var self = this; + var srv = Server.getServer(); if (self.ip in lastguestlogin) { - var diff = (Date.now() - lastguestlogin[self.ip])/1000; - if (diff < self.server.cfg["guest-login-delay"]) { + var diff = (Date.now() - lastguestlogin[self.ip]) / 1000; + if (diff < srv.cfg["guest-login-delay"]) { self.socket.emit("login", { success: false, - error: "Guest logins are restricted to one per IP address "+ - "per " + self.server.cfg["guest-login-delay"] + - " seconds." + error: "Guest logins are restricted to one per IP address per " + + srv.cfg["guest-login-delay"] + " seconds." }); - return false; + return; } } - if (!$util.isValidUserName(name)) { + if (!util.isValidUserName(name)) { self.socket.emit("login", { success: false, - error: "Invalid username. Usernames must be 1-20 characters "+ - "long and consist only of characters a-z, A-Z, 0-9, -, "+ - "and _" + error: "Invalid username. Usernames must be 1-20 characters long and " + + "consist only of characters a-z, A-Z, 0-9, -, _, and accented " + + "letters." }); return; } - // Set the loggingIn flag to avoid race conditions with the callback + // Prevent duplicate logins self.loggingIn = true; - self.server.db.isUsernameTaken(name, function (err, taken) { + db.users.isUsernameTaken(name, function (err, taken) { self.loggingIn = false; if (err) { self.socket.emit("login", { success: false, - error: "Internal error: " + err + error: err }); return; } @@ -666,122 +391,43 @@ User.prototype.guestLogin = function (name) { if (taken) { self.socket.emit("login", { success: false, - error: "That username is registered and protected." + error: "That username is registered." }); return; } if (self.inChannel()) { - var lname = name.toLowerCase(); - for(var i = 0; i < self.channel.users.length; i++) { - if (self.channel.users[i].name.toLowerCase() === lname) { + var nameLower = name.toLowerCase(); + for (var i = 0; i < self.channel.users.length; i++) { + if (self.channel.users[i].name.toLowerCase() === nameLower) { self.socket.emit("login", { success: false, - error: "That name is already in use on this channel" + error: "That name is already in use on this channel." }); return; } } } + + // Login succeeded lastguestlogin[self.ip] = Date.now(); self.rank = 0; - Logger.syslog.log(self.ip + " signed in as " + name); - self.server.db.recordVisit(self.ip, name); + self.global_rank = 0; + self.socket.emit("rank", 0); self.name = name; - self.loggedIn = false; + self.loggedIn = true; self.socket.emit("login", { success: true, name: name }); - self.socket.emit("rank", self.rank); + + // TODO you shouldn't be able to guest login without being in a channel if (self.inChannel()) { self.channel.logger.log(self.ip + " signed in as " + name); - self.channel.broadcastNewUser(self); } + + self.emit("login"); }); }; -// Attempt to login -User.prototype.login = function (name, pw, session) { - var self = this; - // No password => try guest login - if (pw === "" && session === "") { - this.guestLogin(name); - } else { - self.loggingIn = true; - self.server.db.userLogin(name, pw, session, function (err, row) { - if (err) { - self.loggingIn = false; - ActionLog.record(self.ip, name, "login-failure", - err); - self.socket.emit("login", { - success: false, - error: err - }); - return; - } - if (self.inChannel()) { - var n = name.toLowerCase(); - for(var i = 0; i < self.channel.users.length; i++) { - if (self.channel.users[i].name.toLowerCase() === n) { - if (self.channel.users[i] === self) { - Logger.errlog.log("Wat: user.login() but user "+ - "already logged in on channel"); - break; - } - self.channel.kick(self.channel.users[i], - "Duplicate login"); - } - } - } - // Record logins for administrator accounts - if (self.global_rank >= 255) - ActionLog.record(self.ip, name, "login-success"); - self.loggedIn = true; - self.loggingIn = false; - self.socket.emit("login", { - success: true, - session: row.session_hash, - name: name - }); - Logger.syslog.log(self.ip + " logged in as " + name); - self.server.db.recordVisit(self.ip, name); - self.profile = { - image: row.profile_image, - text: row.profile_text - }; - self.global_rank = row.global_rank; - var afterRankLookup = function () { - self.socket.emit("rank", self.rank); - self.name = name; - if (self.inChannel()) { - self.channel.logger.log(self.ip + " logged in as " + - name); - self.channel.broadcastNewUser(self); - } else if (self.inPendingChannel()) { - self.pendingChannel.userJoin(self); - } - }; - if (self.inChannel() || self.inPendingChannel()) { - var chan = self.channel != null ? self.channel : self.pendingChannel; - chan.getRank(name, function (err, rank) { - if (!err) { - self.saverank = true; - self.rank = rank; - } else { - // If there was an error in retrieving the rank, - // don't overwrite it with a bad value - self.saverank = false; - self.rank = self.global_rank; - } - afterRankLookup(); - }); - } else { - self.rank = self.global_rank; - afterRankLookup(); - } - }); - } -}; - module.exports = User; diff --git a/www/assets/js/callbacks.js b/www/assets/js/callbacks.js index 33a33826..b37ec3e7 100644 --- a/www/assets/js/callbacks.js +++ b/www/assets/js/callbacks.js @@ -43,6 +43,7 @@ Callbacks = { /* fired when socket connection completes */ connect: function() { + socket.emit("initChannelCallbacks"); socket.emit("joinChannel", { name: CHANNEL.name });