From 7b60d0948e7a8cddd8a46420e1bbdf5b8a6557f2 Mon Sep 17 00:00:00 2001 From: calzoneman Date: Wed, 19 Jun 2013 17:54:27 -0400 Subject: [PATCH] Fixes --- auth.js | 8 +-- channel.js | 11 ++- database.js | 2 +- notwebsocket.js | 1 + user.js | 132 +++++++++++++++++++--------------- www/account.html | 2 +- www/assets/js/account.js | 4 +- www/assets/js/callbacks.js | 62 ++++++++-------- www/assets/js/data.js | 2 + www/assets/js/iourl.js | 1 + www/assets/js/notwebsocket.js | 4 +- www/assets/js/ui.js | 7 +- www/assets/js/util.js | 8 +-- www/channel.html | 6 +- www/index.html | 2 +- 15 files changed, 139 insertions(+), 113 deletions(-) diff --git a/auth.js b/auth.js index 51cedbe1..171c7532 100644 --- a/auth.js +++ b/auth.js @@ -20,7 +20,7 @@ var Logger = require("./logger.js"); exports.isRegistered = function(name) { var db = Database.getConnection(); if(!db) { - return true; + throw "Database failure"; } var query = Database.createQuery( "SELECT * FROM `registrations` WHERE uname=?", @@ -90,7 +90,7 @@ exports.login = function(name, pw, session) { exports.loginPassword = function(name, pw) { var db = Database.getConnection(); if(!db) { - return false; + throw "Database failure"; } var query = Database.createQuery( "SELECT * FROM `registrations` WHERE uname=?", @@ -140,7 +140,7 @@ exports.createSession = function(name) { var hash = hashlib.sha256(salt + name); var db = Database.getConnection(); if(!db) { - return false; + throw "Database failure"; } var query = Database.createQuery( ["UPDATE `registrations` SET ", @@ -156,7 +156,7 @@ exports.createSession = function(name) { exports.loginSession = function(name, hash) { var db = Database.getConnection(); if(!db) { - return false; + throw "Database failure"; } var query = Database.createQuery( "SELECT * FROM `registrations` WHERE `uname`=?", diff --git a/channel.js b/channel.js index 7d794f72..b2dc3e2e 100644 --- a/channel.js +++ b/channel.js @@ -138,8 +138,15 @@ Channel.prototype.hasPermission = function(user, key) { Channel.prototype.loadDump = function() { fs.readFile("chandump/" + this.name, function(err, data) { if(err) { - Logger.errlog.log("Failed to open channel dump " + this.name); - Logger.errlog.log(err); + if(err.code == "ENOENT") { + Logger.errlog.log("WARN: missing dump for " + this.name); + this.initialized = true; + this.saveDump(); + } + else { + Logger.errlog.log("Failed to open channel dump " + this.name); + Logger.errlog.log(err); + } return; } try { diff --git a/database.js b/database.js index d6a4d095..187476fc 100644 --- a/database.js +++ b/database.js @@ -38,7 +38,7 @@ function getConnection() { db = mysql.createConnectionSync(); db.connectSync(SERVER, USER, PASSWORD, DATABASE); if(!db.connectedSync()) { - //Logger.errlog.log("DB connection failed"); + Logger.errlog.log("DB connection failed"); return false; } if(CONFIG.DEBUG) { diff --git a/notwebsocket.js b/notwebsocket.js index c4862ea4..6390cdab 100644 --- a/notwebsocket.js +++ b/notwebsocket.js @@ -141,6 +141,7 @@ function newConnection(req, res) { exports.newConnection = newConnection; function msgReceived(req, res) { + res.callback = req.query.callback; var h = req.params.hash; if(h in clients && clients[h] != null) { var str = req.params.str; diff --git a/user.js b/user.js index 97f75dc3..810c5dd9 100644 --- a/user.js +++ b/user.js @@ -541,73 +541,89 @@ User.prototype.login = function(name, pw, session) { return false; } } - // Sorry bud, can't take that name - if(Auth.isRegistered(name)) { - this.socket.emit("login", { - success: false, - error: "That username is already taken" - }); - return false; - } - // YOUR ARGUMENT IS INVALID - else if(!Auth.validateName(name)) { - this.socket.emit("login", { - success: false, - error: "Invalid username. Usernames must be 1-20 characters long and consist only of alphanumeric characters and underscores" - }); - } - else { - lastguestlogin[this.ip] = Date.now(); - this.rank = Rank.Guest; - Logger.syslog.log(this.ip + " signed in as " + name); - Database.recordVisit(this.ip, name); - this.name = name; - this.loggedIn = false; - this.socket.emit("login", { - success: true, - name: name - }); - this.socket.emit("rank", this.rank); - if(this.channel != null) { - this.channel.logger.log(this.ip + " signed in as " + name); - this.channel.broadcastNewUser(this); + try { + // Sorry bud, can't take that name + if(Auth.isRegistered(name)) { + this.socket.emit("login", { + success: false, + error: "That username is already taken" + }); + return false; } + // YOUR ARGUMENT IS INVALID + else if(!Auth.validateName(name)) { + this.socket.emit("login", { + success: false, + error: "Invalid username. Usernames must be 1-20 characters long and consist only of alphanumeric characters and underscores" + }); + } + else { + lastguestlogin[this.ip] = Date.now(); + this.rank = Rank.Guest; + Logger.syslog.log(this.ip + " signed in as " + name); + Database.recordVisit(this.ip, name); + this.name = name; + this.loggedIn = false; + this.socket.emit("login", { + success: true, + name: name + }); + this.socket.emit("rank", this.rank); + if(this.channel != null) { + this.channel.logger.log(this.ip + " signed in as " + name); + this.channel.broadcastNewUser(this); + } + } + } + catch(e) { + this.socket.emit("login", { + success: false, + error: e + }); } } else { - var row; - if((row = Auth.login(name, pw, session))) { - this.loggedIn = true; - this.socket.emit("login", { - success: true, - session: row.session_hash, - name: name - }); - Logger.syslog.log(this.ip + " logged in as " + name); - Database.recordVisit(this.ip, name); - this.profile = { - image: row.profile_image, - text: row.profile_text - }; - var chanrank = (this.channel != null) ? this.channel.getRank(name) - : Rank.Guest; - var rank = (chanrank > row.global_rank) ? chanrank - : row.global_rank; - this.rank = (this.rank > rank) ? this.rank : rank; - this.socket.emit("rank", this.rank); - this.name = name; - if(this.channel != null) { - this.channel.logger.log(this.ip + " logged in as " + name); - this.channel.broadcastNewUser(this); + try { + var row; + if((row = Auth.login(name, pw, session))) { + this.loggedIn = true; + this.socket.emit("login", { + success: true, + session: row.session_hash, + name: name + }); + Logger.syslog.log(this.ip + " logged in as " + name); + Database.recordVisit(this.ip, name); + this.profile = { + image: row.profile_image, + text: row.profile_text + }; + var chanrank = (this.channel != null) ? this.channel.getRank(name) + : Rank.Guest; + var rank = (chanrank > row.global_rank) ? chanrank + : row.global_rank; + this.rank = (this.rank > rank) ? this.rank : rank; + this.socket.emit("rank", this.rank); + this.name = name; + if(this.channel != null) { + this.channel.logger.log(this.ip + " logged in as " + name); + this.channel.broadcastNewUser(this); + } + } + // Wrong password + else { + this.socket.emit("login", { + success: false, + error: "Invalid session" + }); + return false; } } - // Wrong password - else { + catch(e) { this.socket.emit("login", { success: false, - error: "Invalid session" + error: e }); - return false; } } } diff --git a/www/account.html b/www/account.html index d3f865d8..fb4e3678 100644 --- a/www/account.html +++ b/www/account.html @@ -27,7 +27,7 @@
diff --git a/www/assets/js/account.js b/www/assets/js/account.js index 04d04de8..85ad8e9c 100644 --- a/www/assets/js/account.js +++ b/www/assets/js/account.js @@ -9,8 +9,8 @@ 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 uname = readCookie("sync_uname") || ""; -var session = readCookie("sync_session") || ""; +var uname = readCookie("cytube_uname") || ""; +var session = readCookie("cytube_session") || ""; var api = WEB_URL + "/api/json/"; var loggedin = false; diff --git a/www/assets/js/callbacks.js b/www/assets/js/callbacks.js index 14764a80..0600ad19 100644 --- a/www/assets/js/callbacks.js +++ b/www/assets/js/callbacks.js @@ -356,9 +356,10 @@ Callbacks = { banlist: function(entries) { var tbl = $("#banlist table"); - // dumb hack because of jquery UI - // sortable turns tables and lists into a mess of race conditions + // I originally added this check because of a race condition + // Now it seems to work without but I don't trust it if(!tbl.hasClass("table")) { + console.log("thing"); setTimeout(function() { Callbacks.banlist(entries); }, 100); @@ -391,8 +392,8 @@ Callbacks = { recentLogins: function(entries) { var tbl = $("#loginhistory table"); - // dumb hack because of jquery UI - // sortable turns tables and lists into a mess of race conditions + // I originally added this check because of a race condition + // Now it seems to work without but I don't trust it if(!tbl.hasClass("table")) { setTimeout(function() { Callbacks.recentLogins(entries); @@ -415,8 +416,10 @@ Callbacks = { channelRanks: function(entries) { var tbl = $("#channelranks table"); - // Dammit jQuery UI + // I originally added this check because of a race condition + // Now it seems to work without but I don't trust it if(!tbl.hasClass("table")) { + console.log("thing"); setTimeout(function() { Callbacks.channelRanks(entries); }, 100); @@ -935,35 +938,8 @@ Callbacks = { } } } - -/* -pl = []; -for(var i = 0; i < 10; i++) { - var m = { - title: "Test " + i, - type: "yt", - id: "test" + i, - seconds: 0, - duration: "00:00" - }; - pl.push(m); -} -setTimeout(function() { - Callbacks.playlist(pl); -}, 1000); -*/ - -$.getScript(IO_URL+"/socket.io/socket.io.js", function() { - try { - socket = io.connect(IO_URL); - setupCallbacks(); - } - catch(e) { - Callbacks.disconnect(); - } -}); - setupCallbacks = function() { + console.log(socket); for(var key in Callbacks) { (function(key) { socket.on(key, function(data) { @@ -973,3 +949,23 @@ setupCallbacks = function() { } } +if(USEROPTS.altsocket) { + socket = new NotWebsocket(); + setupCallbacks(); +} +else { + $.getScript(IO_URL+"/socket.io/socket.io.js", function() { + try { + if(NO_WEBSOCKETS) { + var i = io.transports.indexOf("websocket"); + if(i >= 0) + io.transports.splice(i, 1); + } + socket = io.connect(IO_URL); + setupCallbacks(); + } + catch(e) { + Callbacks.disconnect(); + } + }); +} diff --git a/www/assets/js/data.js b/www/assets/js/data.js index fe50c3b3..451062aa 100644 --- a/www/assets/js/data.js +++ b/www/assets/js/data.js @@ -1,3 +1,5 @@ +var CL_VERSION = "2.0.0"; + var CLIENT = { rank: -1, leader: false, diff --git a/www/assets/js/iourl.js b/www/assets/js/iourl.js index 19f76b90..9aa8b2ef 100644 --- a/www/assets/js/iourl.js +++ b/www/assets/js/iourl.js @@ -11,3 +11,4 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI var IO_URL = "http://localhost:1337"; var WEB_URL = "http://localhost:8080"; +var NO_WEBSOCKETS = false; diff --git a/www/assets/js/notwebsocket.js b/www/assets/js/notwebsocket.js index 7aba9f4c..8aaf6cdb 100644 --- a/www/assets/js/notwebsocket.js +++ b/www/assets/js/notwebsocket.js @@ -12,7 +12,7 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI var NotWebsocket = function() { this.connected = false; this.polltmr = false; - $.getJSON(WEB_URL + "/nws/connect", function(data) { + $.getJSON(WEB_URL + "/nws/connect?callback=?", function(data) { this.hash = data; this.connected = true; this.recv(["connect", undefined]); @@ -76,7 +76,7 @@ NotWebsocket.prototype.emit = function(msg, data) { } var pkt = [msg, data]; var str = escape(JSON.stringify(pkt)).replace(/\//g, "%2F"); - $.getJSON(WEB_URL+"/nws/"+this.hash+"/"+str, function() { + $.getJSON(WEB_URL+"/nws/"+this.hash+"/"+str+"?callback=?", function() { // Poll more quickly because sending a packet usually means // expecting some data to come back this.pollint = 100; diff --git a/www/assets/js/ui.js b/www/assets/js/ui.js index b6e896b7..61b34f1b 100644 --- a/www/assets/js/ui.js +++ b/www/assets/js/ui.js @@ -57,7 +57,7 @@ $("#chatline").keydown(function(ev) { msg: msg }); CHATHIST.push($("#chatline").val()); - CHATLISTIDX = CHATHIST.length; + CHATHISTIDX = CHATHIST.length; $("#chatline").val(""); } return; @@ -326,3 +326,8 @@ else { } }); } + +/* oh internet explorer, how I hate thee */ +$(":input:not(textarea)").keypress(function(ev) { + return ev.keyCode != 13; +}); diff --git a/www/assets/js/util.js b/www/assets/js/util.js index b790f504..492f34b0 100644 --- a/www/assets/js/util.js +++ b/www/assets/js/util.js @@ -111,8 +111,6 @@ function addUserDropdown(entry, name) { $("").text(name).appendTo(menu); $("
").appendTo(menu); - if(CLIENT.rank >= 2) - $("").addClass("user-aliases").appendTo(menu); if(hasPermission("kick")) { $("