diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..7fb4255f --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +*.swp +cfg.json +chandump +chanlogs +*.log +node_modules diff --git a/changelog b/changelog index 89cef10e..0c4d0bf8 100644 --- a/changelog +++ b/changelog @@ -1,3 +1,14 @@ +Thu Sep 5 13:45 2013 CDT + * acp.js, actionlog.js, api.js, channel.js, chatcommand.js, config.js, + customembed.js, database.js, filter.js, get-info.js, logger.js, + media.js, notwebsocket.js, playlist.js, poll.js, rank.js, server.js, + stats.js, ullist.js, user.js, utilities.js: move server + files into lib/ folder to clean up the root directory of the project. + * api.js: replace regex with $util.isValidChannelName (L68); + fix relative file paths (per moving api.js to lib/) + * server.js: fix relative file paths + * channel.js: fix relative file paths + Wed Sep 4 22:45 2013 CDT * changelog: initialize changelog file diff --git a/lib/.logger.js.swp b/lib/.logger.js.swp new file mode 100644 index 00000000..1b5f4aff Binary files /dev/null and b/lib/.logger.js.swp differ diff --git a/acp.js b/lib/acp.js similarity index 100% rename from acp.js rename to lib/acp.js diff --git a/actionlog.js b/lib/actionlog.js similarity index 100% rename from actionlog.js rename to lib/actionlog.js diff --git a/api.js b/lib/api.js similarity index 96% rename from api.js rename to lib/api.js index 0a9df9bc..c461eca8 100644 --- a/api.js +++ b/lib/api.js @@ -11,6 +11,7 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI var Logger = require("./logger"); var fs = require("fs"); +var path = require("path"); var $util = require("./utilities"); module.exports = function (Server) { @@ -60,16 +61,16 @@ module.exports = function (Server) { }); /* REGION channels */ - + /* data about a specific channel */ app.get("/api/channels/:channel", function (req, res) { Server.stats.record("api", "/api/channels/:channel"); var name = req.params.channel; - if(!name.match(/^[\w-_]+$/)) { + if(!$util.isValidChannelName(name)) { res.send(404); return; } - + var data = { name: name, loaded: false @@ -125,7 +126,7 @@ module.exports = function (Server) { } // If we get here, the filter is public channels - + var channels = []; for(var key in Server.channels) { var channel = Server.channels[key]; @@ -170,11 +171,11 @@ module.exports = function (Server) { }); return; } - + // Only record login-success for admins if(row.global_rank >= 255) ActionLog.record(getIP(req), name, "login-success"); - + res.jsonp({ success: true, name: name, @@ -201,7 +202,7 @@ module.exports = function (Server) { }); return; } - + if(toomany) { ActionLog.record(ip, name, "register-failure", "Too many recent registrations"); @@ -225,7 +226,7 @@ module.exports = function (Server) { if(!$util.isValidUserName(name)) { - ActionLog.record(ip, name, "register-failure", + ActionLog.record(ip, name, "register-failure", "Invalid name"); res.jsonp({ success: false, @@ -338,11 +339,11 @@ module.exports = function (Server) { } var msg = "A password reset request was issued for your " + - "account '"+ name + "' on " + Server.cfg["domain"] + + "account '"+ name + "' on " + Server.cfg["domain"] + ". This request is valid for 24 hours. If you did "+ "not initiate this, there is no need to take action."+ " To reset your password, copy and paste the " + - "following link into your browser: " + + "following link into your browser: " + Server.cfg["domain"] + "/reset.html?"+hash; var mail = { @@ -435,7 +436,7 @@ module.exports = function (Server) { }); return; } - + db.setUserProfile(name, { image: img, text: text }, function (err, dbres) { if(err) { @@ -483,8 +484,7 @@ module.exports = function (Server) { } if(email.match(/.*@(localhost|127\.0\.0\.1)/i)) { - res.jsonp({ - success: false, + res.jsonp({ success: false, error: "Nice try, but no" }); return; @@ -583,7 +583,7 @@ module.exports = function (Server) { ActionLog.listActions(types, function (err, actions) { if(err) actions = []; - + res.jsonp(actions); }); }); @@ -597,10 +597,11 @@ module.exports = function (Server) { return; } var start = data.size - len; - if(start < 0) { + if(start < 0) start = 0; - } var end = data.size - 1; + if(end < 0) + end = 0; fs.createReadStream(file, { start: start, end: end }) .pipe(res); }); @@ -630,7 +631,7 @@ module.exports = function (Server) { return; } - pipeLast(res, "sys.log", 1048576); + pipeLast(res, path.join(__dirname, "../sys.log"), 1048576); }); }); @@ -658,7 +659,7 @@ module.exports = function (Server) { return; } - pipeLast(res, "error.log", 1048576); + pipeLast(res, path.join(__dirname, "../error.log"), 1048576); }); }); @@ -692,9 +693,11 @@ module.exports = function (Server) { return; } - fs.exists("chanlogs/" + chan + ".log", function(exists) { + fs.exists(path.join(__dirname, "../chanlogs", chan + ".log"), + function(exists) { if(exists) { - pipeLast(res, "chanlogs/" + chan + ".log", 1048576); + pipeLast(res, path.join(__dirname, "../chanlogs", + chan + ".log"), 1048576); } else { res.send(404); } diff --git a/channel.js b/lib/channel.js similarity index 98% rename from channel.js rename to lib/channel.js index f7ac65ee..c2e5d610 100644 --- a/channel.js +++ b/lib/channel.js @@ -11,6 +11,7 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI */ var fs = require("fs"); +var path = require("path"); var Poll = require("./poll.js").Poll; var Media = require("./media.js").Media; var Logger = require("./logger.js"); @@ -101,7 +102,8 @@ var Channel = function(name, Server) { self.ip_alias = {}; self.name_alias = {}; self.login_hist = []; - self.logger = new Logger.Logger("chanlogs/" + self.canonical_name + ".log"); + self.logger = new Logger.Logger(path.join(__dirname, "../chanlogs", + self.canonical_name + ".log")); self.i = 0; self.time = new Date().getTime(); self.plmeta = { @@ -146,7 +148,8 @@ Channel.prototype.loadDump = function() { var self = this; if(self.name === "") return; - fs.stat("chandump/" + self.name, function (err, stats) { + fs.stat(path.join(__dirname, "../chandump", self.name), + function (err, stats) { if(!err) { var mb = stats.size / 1048576; mb = parseInt(mb * 100) / 100; @@ -154,13 +157,14 @@ Channel.prototype.loadDump = function() { Logger.errlog.log("Large chandump detected: " + self.name + " (" + mb + " MB)"); self.updateMotd("Your channel file has exceeded the " + - "maximum size of 1MB and cannot be " + - "loaded. Please ask an administrator " + + "maximum size of 1MB and cannot be " + + "loaded. Please ask an administrator " + "for assistance in restoring it."); return; } } - fs.readFile("chandump/" + self.name, function(err, data) { + fs.readFile(path.join(__dirname, "../chandump", self.name), + function(err, data) { if(err) { if(err.code == "ENOENT") { Logger.errlog.log("WARN: missing dump for " + self.name); @@ -286,7 +290,7 @@ Channel.prototype.saveDump = function() { js: this.js }; var text = JSON.stringify(dump); - fs.writeFileSync("chandump/" + this.name, text); + fs.writeFileSync(path.join(__dirname, "../chandump", this.name), text); } // Save channel dumps every 5 minutes, in case of crash @@ -398,7 +402,7 @@ Channel.prototype.tryRegister = function (user) { return; } - self.server.actionlog.record(user.ip, user.name, + self.server.actionlog.record(user.ip, user.name, "channel-register-success", self.name); self.registered = true; self.initialized = true; @@ -438,7 +442,7 @@ Channel.prototype.unregister = function (user) { }); return; } - + self.registered = false; user.socket.emit("unregisterChannel", { success: true }); }); @@ -638,7 +642,7 @@ Channel.prototype.tryIPBan = function(actor, name, range) { } self.ipbans[ip] = [name, actor.name]; - self.logger.log("*** " + actor.name + " banned " + ip + + self.logger.log("*** " + actor.name + " banned " + ip + " (" + name + ")"); for(var i = 0; i < self.users.length; i++) { @@ -1271,7 +1275,7 @@ Channel.prototype.tryQueue = function(user, data) { Channel.prototype.addMedia = function(data, user) { var self = this; - if(data.type === "yp" && + if(data.type === "yp" && !self.hasPermission(user, "playlistaddlist")) { user.socket.emit("queueFail", "You don't have permission to add " + "playlists"); @@ -1488,7 +1492,7 @@ Channel.prototype.tryDequeue = function(user, data) { if(typeof data !== "number") return; - + var plitem = this.playlist.items.find(data); if(plitem && plitem.media) this.logger.log("### " + user.name + " deleted " + plitem.media.title); @@ -1508,7 +1512,7 @@ Channel.prototype.tryUncache = function(user, data) { if(err) return; - self.logger.log("*** " + user.name + " deleted " + data.id + + self.logger.log("*** " + user.name + " deleted " + data.id + " from library"); }); } @@ -1619,12 +1623,12 @@ Channel.prototype.move = function(data, user) { if(typeof data.moveby !== "undefined") moveby = data.moveby; - + var fromit = chan.playlist.items.find(data.from); var afterit = chan.playlist.items.find(data.after); var aftertitle = afterit && afterit.media ? afterit.media.title : ""; if(fromit) { - chan.logger.log("### " + user.name + " moved " + fromit.media.title + chan.logger.log("### " + user.name + " moved " + fromit.media.title + (aftertitle ? " after " + aftertitle : "")); } @@ -2066,7 +2070,7 @@ Channel.prototype.trySetRank = function(user, data) { receiver.rank = data.rank; if(receiver.loggedIn) { self.saveRank(receiver, function (err, res) { - self.logger.log("*** " + user.name + " set " + + self.logger.log("*** " + user.name + " set " + data.user + "'s rank to " + data.rank); self.sendAllWithPermission("acl", "setChannelRank", data); }); @@ -2080,8 +2084,8 @@ Channel.prototype.trySetRank = function(user, data) { return; self.server.db.setChannelRank(self.name, data.user, data.rank, function (err, res) { - - self.logger.log("*** " + user.name + " set " + + + self.logger.log("*** " + user.name + " set " + data.user + "'s rank to " + data.rank); self.sendAllWithPermission("acl", "setChannelRank", data); }); diff --git a/chatcommand.js b/lib/chatcommand.js similarity index 100% rename from chatcommand.js rename to lib/chatcommand.js diff --git a/config.js b/lib/config.js similarity index 100% rename from config.js rename to lib/config.js diff --git a/customembed.js b/lib/customembed.js similarity index 100% rename from customembed.js rename to lib/customembed.js diff --git a/database.js b/lib/database.js similarity index 100% rename from database.js rename to lib/database.js diff --git a/filter.js b/lib/filter.js similarity index 100% rename from filter.js rename to lib/filter.js diff --git a/get-info.js b/lib/get-info.js similarity index 100% rename from get-info.js rename to lib/get-info.js diff --git a/logger.js b/lib/logger.js similarity index 93% rename from logger.js rename to lib/logger.js index 85d5748d..578accaa 100644 --- a/logger.js +++ b/lib/logger.js @@ -10,6 +10,7 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI */ var fs = require("fs"); +var path = require("path"); function getTimeString() { var d = new Date(); @@ -51,8 +52,8 @@ Logger.prototype.close = function () { } } -var errlog = new Logger("error.log"); -var syslog = new Logger("sys.log"); +var errlog = new Logger(path.join(__dirname, "../error.log")); +var syslog = new Logger(path.join(__dirname, "../sys.log")); errlog.actualLog = errlog.log; errlog.log = function(what) { console.log(what); this.actualLog(what); } syslog.actualLog = syslog.log; diff --git a/media.js b/lib/media.js similarity index 100% rename from media.js rename to lib/media.js diff --git a/notwebsocket.js b/lib/notwebsocket.js similarity index 100% rename from notwebsocket.js rename to lib/notwebsocket.js diff --git a/playlist.js b/lib/playlist.js similarity index 100% rename from playlist.js rename to lib/playlist.js diff --git a/poll.js b/lib/poll.js similarity index 100% rename from poll.js rename to lib/poll.js diff --git a/rank.js b/lib/rank.js similarity index 100% rename from rank.js rename to lib/rank.js diff --git a/server.js b/lib/server.js similarity index 90% rename from server.js rename to lib/server.js index 65118009..bbf26563 100644 --- a/server.js +++ b/lib/server.js @@ -96,7 +96,8 @@ var Server = { this.db = new Database(self.cfg); this.db.init(); this.actionlog = require("./actionlog")(self); - this.httpaccess = new Logger.Logger("httpaccess.log"); + this.httpaccess = new Logger.Logger(path.join(__dirname, + "../httpaccess.log")); this.app = express(); this.app.use(express.bodyParser()); // channel path @@ -108,7 +109,9 @@ var Server = { else { self.stats.record("http", "/r/" + c); self.logHTTP(req); - res.sendfile(__dirname + "/www/channel.html"); + res.sendfile("channel.html", { + root: path.join(__dirname, "../www") + }); } }); @@ -118,13 +121,15 @@ var Server = { self.app.get("/", function (req, res, next) { self.logHTTP(req); self.stats.record("http", "/"); - res.sendfile(__dirname + "/www/index.html"); + res.sendfile("index.html", { + root: path.join(__dirname, "../www") + }); }); // default path self.app.get("/:thing(*)", function (req, res, next) { var opts = { - root: __dirname + "/www", + root: path.join(__dirname, "../www"), maxAge: self.cfg["asset-cache-ttl"] } res.sendfile(req.params.thing, opts, function (err) { @@ -231,15 +236,17 @@ var Server = { Logger.syslog.log("Starting CyTube v" + VERSION); -fs.exists("chanlogs", function (exists) { - exists || fs.mkdir("chanlogs"); +var chanlogpath = path.join(__dirname, "../chanlogs"); +fs.exists(chanlogpath, function (exists) { + exists || fs.mkdir(chanlogpath); }); -fs.exists("chandump", function (exists) { - exists || fs.mkdir("chandump"); +var chandumppath = path.join(__dirname, "../chandump"); +fs.exists(chandumppath, function (exists) { + exists || fs.mkdir(chandumppath); }); -Config.load(Server, "cfg.json", function () { +Config.load(Server, path.join(__dirname, "../cfg.json"), function () { Server.init(); if(!Server.cfg["debug"]) { process.on("uncaughtException", function (err) { diff --git a/stats.js b/lib/stats.js similarity index 100% rename from stats.js rename to lib/stats.js diff --git a/ullist.js b/lib/ullist.js similarity index 100% rename from ullist.js rename to lib/ullist.js diff --git a/user.js b/lib/user.js similarity index 100% rename from user.js rename to lib/user.js diff --git a/utilities.js b/lib/utilities.js similarity index 100% rename from utilities.js rename to lib/utilities.js diff --git a/run.sh b/run.sh index 7dfc93d1..ef2d896e 100755 --- a/run.sh +++ b/run.sh @@ -2,7 +2,7 @@ while : do - node server.js + node lib/server.js sleep 2 done diff --git a/update.js b/update.js index 513132ae..8986b3fc 100644 --- a/update.js +++ b/update.js @@ -1,5 +1,5 @@ -var Config = require("./config.js"); -var Database = require("./database.js"); +var Config = require("./lib/config.js"); +var Database = require("./lib/database.js"); var updates = { "2013-08-20-utf8fix": fixDBUnicode,