diff --git a/channel.js b/channel.js index ddf5b650..b4f79408 100644 --- a/channel.js +++ b/channel.js @@ -9,6 +9,7 @@ The above copyright notice and this permission notice shall be included in all c 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 fs = require("fs"); var mysql = require("mysql-libmysqlclient"); var Config = require("./config.js"); var Rank = require("./rank.js"); @@ -44,6 +45,29 @@ var Channel = function(name) { this.ipbans = {}; this.logger = new Logger.Logger("chanlogs/" + this.name + ".log"); + fs.readFile("chandump/" + this.name, function(err, data) { + if(err) { + return; + } + try { + this.logger.log("*** Loading channel dump from disk"); + data = JSON.parse(data); + for(var i = 0; i < data.queue.length; i++) { + var e = data.queue[i]; + this.queue.push(new Media(e.id, e.title, e.seconds, e.type)); + } + this.sendAll("playlist", { + pl: this.queue + }); + this.currentPosition = data.currentPosition - 1; + this.playNext(); + this.opts = data.opts; + } + catch(e) { + Logger.errlog.log("Channel dump load failed: "); + Logger.errlog.log(e); + } + }.bind(this)); // Autolead stuff // Accumulator @@ -667,6 +691,26 @@ Channel.prototype.playNext = function() { } } +Channel.prototype.jumpTo = function(pos) { + if(this.queue.length == 0) + return; + if(pos >= this.queue.length || pos < 0) + return; + this.currentPosition = pos; + this.currentMedia = this.queue[this.currentPosition]; + this.currentMedia.currentTime = 0; + + this.sendAll("mediaUpdate", this.currentMedia.packupdate()); + this.sendAll("updatePlaylistIdx", { + idx: this.currentPosition + }); + // Enable autolead for non-twitch + if(this.leader == null && this.currentMedia.type != "tw" && this.currentMedia.type != "li") { + this.time = new Date().getTime(); + channelVideoUpdate(this, this.currentMedia.id); + } +} + Channel.prototype.setLock = function(locked) { this.qlocked = locked; this.sendAll("queueLock", {locked: locked}); @@ -701,12 +745,14 @@ Channel.prototype.moveMedia = function(data) { dest: data.dest }); - if(data.src < this.currentPosition && data.dest >= this.currentPosition) { + if(data.src < this.currentPosition && data.dest > this.currentPosition) { this.currentPosition--; } if(data.src > this.currentPosition && data.dest < this.currentPosition) { this.currentPosition++ } + if(data.src == this.currentPosition) + this.currentPosition = data.dest; } // Chat message from a user diff --git a/database.js b/database.js index 866530de..7b9b0801 100644 --- a/database.js +++ b/database.js @@ -22,6 +22,10 @@ exports.init = function() { var db = mysql.createConnectionSync(); db.connectSync(Config.MYSQL_SERVER, Config.MYSQL_USER, Config.MYSQL_PASSWORD, Config.MYSQL_DB); + if(!db.connectedSync()) { + Logger.errlog.log("database.init: DB connection failed"); + return false; + } var query = "CREATE TABLE IF NOT EXISTS `channels` \ (`id` INT NOT NULL, \ `name` VARCHAR(255) NOT NULL, \ diff --git a/rank.js b/rank.js index 91fc4fd8..c8baa454 100644 --- a/rank.js +++ b/rank.js @@ -28,6 +28,7 @@ var permissions = { poll : exports.Moderator, shout : exports.Moderator, channelOpts : exports.Moderator, + jump : exports.Moderator, search : exports.Guest, chat : exports.Guest, }; diff --git a/server.js b/server.js index 2b8cedcf..7f565f1c 100644 --- a/server.js +++ b/server.js @@ -9,6 +9,7 @@ The above copyright notice and this permission notice shall be included in all c 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 fs = require("fs"); var Logger = require("./logger.js"); var Config = require("./config.js"); var connect = require("connect"); @@ -25,3 +26,27 @@ exports.io.sockets.on("connection", function(socket) { var user = new User(socket, socket.handshake.address.address); Logger.syslog.log("Accepted connection from /" + user.ip); }); + +process.on("uncaughtException", function(err) { + Logger.errlog.log("[SEVERE] Uncaught Exception: " + err); +}); + +process.on("exit", shutdown); +process.on("SIGINT", shutdown); + +function shutdown() { + Logger.syslog.log("Unloading channels..."); + for(var name in exports.channels) { + var chan = exports.channels[name]; + var dump = { + currentPosition: chan.currentPosition, + queue: chan.queue, + opts: chan.opts + }; + var text = JSON.stringify(dump); + fs.writeFileSync("chandump/" + name, text); + chan.logger.flush(); + } + Logger.syslog.log("Shutting Down"); + process.exit(0); +} diff --git a/user.js b/user.js index a8c9fd32..1e13d007 100644 --- a/user.js +++ b/user.js @@ -42,8 +42,12 @@ User.prototype.initCallbacks = function() { }.bind(this)); this.socket.on("joinChannel", function(data) { + if(data.name == undefined) + return; if(!data.name.match(/^[a-zA-Z0-9]+$/)) return; + if(data.name.length > 100) + return; // Channel already loaded if(data.name in Server.channels) { this.channel = Server.channels[data.name]; @@ -58,15 +62,25 @@ User.prototype.initCallbacks = function() { }.bind(this)); this.socket.on("login", function(data) { + if(data.name == undefined || data.pw == undefined) + return; + if(data.pw.length > 100) + data.pw = data.pw.substring(0, 100); if(this.name == "") this.login(data.name, data.pw); }.bind(this)); this.socket.on("register", function(data) { + if(data.name == undefined || data.pw == undefined) + return; + if(data.pw.length > 100) + data.pw = data.pw.substring(0, 100); this.register(data.name, data.pw); }.bind(this)); this.socket.on("assignLeader", function(data) { + if(data.name == undefined) + return; if(Rank.hasPermission(this, "assignLeader")) { if(this.channel != null) this.channel.changeLeader(data.name); @@ -74,6 +88,8 @@ User.prototype.initCallbacks = function() { }.bind(this)); this.socket.on("promote", function(data) { + if(data.name == undefined) + return; if(Rank.hasPermission(this, "promote")) { if(this.channel != null) { this.channel.promoteUser(this, data.name); @@ -137,6 +153,15 @@ User.prototype.initCallbacks = function() { } }.bind(this)); + this.socket.on("jumpTo", function(data) { + if(this.channel == null || data.pos == undefined) + return; + if(Rank.hasPermission(this, "jump") || + this.channel.leader == this) { + this.channel.jumpTo(data.pos); + } + }.bind(this)); + this.socket.on("playNext", function() { if(this.channel == null) return; diff --git a/www/assets/js/callbacks.js b/www/assets/js/callbacks.js index 953ec79c..6bb1b185 100644 --- a/www/assets/js/callbacks.js +++ b/www/assets/js/callbacks.js @@ -68,7 +68,7 @@ function initCallbacks() { $("#opt_qopen_allow_playnext").prop("checked", opts.qopen_allow_playnext); $("#opt_pagetitle").attr("placeholder", opts.pagetitle); document.title = opts.pagetitle; - $("opt_customcss").val(opts.customcss); + $("#opt_customcss").attr("placeholder", opts.customcss); $("#customCss").remove(); if(opts.customcss != "") { $("").attr("rel", "stylesheet") @@ -82,6 +82,8 @@ function initCallbacks() { $("#queue_next").attr("disabled", false); if(opts.qopen_allow_playnext) $("#play_next").attr("disabled", false); + else if(RANK < Rank.Moderator && !LEADER) + $("#play_next").attr("disabled", true); rebuildPlaylist(); }); diff --git a/www/assets/js/client.js b/www/assets/js/client.js index f7c425f7..35f4bc63 100644 --- a/www/assets/js/client.js +++ b/www/assets/js/client.js @@ -17,6 +17,8 @@ var POSITION = -1; var RANK = 0; var OPENQUEUE = false; var CHANNELOPTS = {}; +var GRABBEDLI = null; +var OLDINDEX = -1; var uname = readCookie("sync_uname"); var pw = readCookie("sync_pw"); @@ -218,13 +220,16 @@ $("#opt_submit").click(function() { var ptitle = $("#opt_pagetitle").val(); if(ptitle == "") ptitle = $("#opt_pagetitle").attr("placeholder") + var css = $("#opt_customcss").val(); + if(css == "") + css = $("#opt_customcss").attr("placeholder"); opts = { qopen_allow_qnext: $("#opt_qopen_allow_qnext").prop("checked"), qopen_allow_move: $("#opt_qopen_allow_move").prop("checked"), qopen_allow_delete: $("#opt_qopen_allow_delete").prop("checked"), qopen_allow_playnext: $("#opt_qopen_allow_playnext").prop("checked"), pagetitle: ptitle, - customcss: $("#opt_customcss").val() + customcss: css }; socket.emit("channelOpts", opts); }); diff --git a/www/assets/js/functions.js b/www/assets/js/functions.js index e6fb62a6..83d6c3fb 100644 --- a/www/assets/js/functions.js +++ b/www/assets/js/functions.js @@ -1,11 +1,11 @@ /* 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. */ @@ -166,39 +166,57 @@ function makeQueueEntry(video) { function addQueueButtons(li) { var btnstrip = $("
").attr("class", "btn-group qe_buttons").prependTo(li); + var btnMove = $("