diff --git a/.eslintrc.yml b/.eslintrc.yml index 8ac14f95..7274f1f6 100644 --- a/.eslintrc.yml +++ b/.eslintrc.yml @@ -30,6 +30,7 @@ rules: spaced-comment: [2, always] globals: + log: false $: false Favico: false Handlebars: false diff --git a/package.json b/package.json index 1fee2d65..3b74fc98 100644 --- a/package.json +++ b/package.json @@ -29,6 +29,7 @@ "dependencies": { "bcrypt-nodejs": "0.0.3", "cheerio": "0.20.0", + "colors": "1.1.2", "commander": "2.9.0", "event-stream": "3.3.2", "express": "4.13.4", diff --git a/src/client.js b/src/client.js index fde1cae0..561492ce 100644 --- a/src/client.js +++ b/src/client.js @@ -1,7 +1,7 @@ var _ = require("lodash"); var Chan = require("./models/chan"); var crypto = require("crypto"); -var log = require("./log"); +var userLog = require("./userLog"); var Msg = require("./models/msg"); var Network = require("./models/network"); var ircFramework = require("irc-framework"); @@ -75,6 +75,8 @@ function Client(manager, name, config) { delay += 1000; }); } + + log.info("User '" + name + "' loaded"); } Client.prototype.emit = function(event, data) { @@ -90,7 +92,7 @@ Client.prototype.emit = function(event, data) { if (target.chan.type === Chan.Type.LOBBY) { chan = target.network.host; } - log.write( + userLog.write( this.name, target.network.host, chan, diff --git a/src/clientManager.js b/src/clientManager.js index 185ba39f..a982ed0a 100644 --- a/src/clientManager.js +++ b/src/clientManager.js @@ -31,7 +31,7 @@ ClientManager.prototype.loadUser = function(name) { try { var json = this.readUserConfig(name); } catch (e) { - console.log(e); + log.error("Failed to read user config", e); return; } if (!this.findClient(name)) { @@ -40,9 +40,6 @@ ClientManager.prototype.loadUser = function(name) { name, json )); - console.log( - "User '" + name + "' loaded." - ); } }; @@ -58,7 +55,7 @@ ClientManager.prototype.getUsers = function() { } }); } catch (e) { - console.log(e); + log.error("Failed to get users", e); return; } return users; @@ -107,7 +104,7 @@ ClientManager.prototype.updateUser = function(name, opts) { JSON.stringify(user, null, " ") ); } catch (e) { - console.log(e); + log.error("Failed to update user", e); return; } return true; @@ -160,9 +157,7 @@ ClientManager.prototype.autoload = function(/* sockets */) { if (client) { client.quit(); self.clients = _.without(self.clients, client); - console.log( - "User '" + name + "' disconnected." - ); + log.info("User '" + name + "' disconnected"); } }); }, 1000); diff --git a/src/command-line/add.js b/src/command-line/add.js index 3ae52e86..24905c14 100644 --- a/src/command-line/add.js +++ b/src/command-line/add.js @@ -13,10 +13,8 @@ program try { mkdirp.sync(path); } catch (e) { - console.log(""); - console.log("Could not create " + path); - console.log("Try running the command as sudo."); - console.log(""); + log.error("Could not create", path); + log.info("Try running the command as sudo."); return; } try { @@ -24,18 +22,14 @@ program fs.mkdirSync(test); fs.rmdirSync(test); } catch (e) { - console.log(""); - console.log("You have no permissions to write to " + path); - console.log("Try running the command as sudo."); - console.log(""); + log.error("You have no permissions to write to", path); + log.info("Try running the command as sudo."); return; } var manager = new ClientManager(); var users = manager.getUsers(); if (users.indexOf(name) !== -1) { - console.log(""); - console.log("User '" + name + "' already exists."); - console.log(""); + log.error("User '" + name + "' already exists."); return; } require("read")({ @@ -47,14 +41,12 @@ program }); function add(manager, name, password) { - console.log(""); var salt = bcrypt.genSaltSync(8); var hash = bcrypt.hashSync(password, salt); manager.addUser( name, hash ); - console.log("User '" + name + "' created:"); - console.log(Helper.HOME + "/users/" + name + ".json"); - console.log(""); + log.info("User '" + name + "' created:"); + log.info(Helper.HOME + "/users/" + name + ".json"); } diff --git a/src/command-line/edit.js b/src/command-line/edit.js index 70f1ebc3..f9671102 100644 --- a/src/command-line/edit.js +++ b/src/command-line/edit.js @@ -9,9 +9,7 @@ program .action(function(name) { var users = new ClientManager().getUsers(); if (users.indexOf(name) === -1) { - console.log(""); - console.log("User '" + name + "' doesn't exist."); - console.log(""); + log.error("User '" + name + "' doesn't exist."); return; } child.spawn( diff --git a/src/command-line/index.js b/src/command-line/index.js index 4c4bbd8d..2a2b7fcd 100644 --- a/src/command-line/index.js +++ b/src/command-line/index.js @@ -1,3 +1,5 @@ +GLOBAL.log = require("../log.js"); + var program = require("commander"); var pkg = require("../../package.json"); var fs = require("fs"); @@ -20,8 +22,7 @@ if (!fs.existsSync(config)) { config, fs.readFileSync(__dirname + "/../../defaults/config.js") ); - console.log("Config created:"); - console.log(config); + log.info("Config created:", config); } require("./start"); diff --git a/src/command-line/list.js b/src/command-line/list.js index f94e20b4..4e8f8bc3 100644 --- a/src/command-line/list.js +++ b/src/command-line/list.js @@ -7,15 +7,11 @@ program .action(function() { var users = new ClientManager().getUsers(); if (!users.length) { - console.log(""); - console.log("No users found!"); - console.log(""); + log.warn("No users found!"); } else { - console.log(""); console.log("Users:"); for (var i = 0; i < users.length; i++) { console.log(" " + (i + 1) + ". " + users[i]); } - console.log(""); } }); diff --git a/src/command-line/remove.js b/src/command-line/remove.js index bdab70c4..683892f5 100644 --- a/src/command-line/remove.js +++ b/src/command-line/remove.js @@ -13,20 +13,14 @@ program fs.mkdirSync(test); fs.rmdirSync(test); } catch (e) { - console.log(""); - console.log("You have no permissions to delete from " + path); - console.log("Try running the command as sudo."); - console.log(""); + log.warn("You have no permissions to delete from " + path); + log.info("Try running the command as sudo."); return; } var manager = new ClientManager(); if (manager.removeUser(name)) { - console.log(""); - console.log("Removed '" + name + "'."); - console.log(""); + log.info("Removed user '" + name + "'."); } else { - console.log(""); - console.log("User '" + name + "' doesn't exist."); - console.log(""); + log.error("User '" + name + "' doesn't exist."); } }); diff --git a/src/command-line/reset.js b/src/command-line/reset.js index 4414de38..d953ce66 100644 --- a/src/command-line/reset.js +++ b/src/command-line/reset.js @@ -10,9 +10,7 @@ program .action(function(name) { var users = new ClientManager().getUsers(); if (users.indexOf(name) === -1) { - console.log(""); - console.log("User '" + name + "' doesn't exist."); - console.log(""); + log.error("User '" + name + "' doesn't exist."); return; } var file = Helper.HOME + "/users/" + name + ".json"; @@ -21,7 +19,6 @@ program prompt: "[thelounge] New password: ", silent: true }, function(err, password) { - console.log(""); if (err) { return; } @@ -32,7 +29,6 @@ program file, JSON.stringify(user, null, " ") ); - console.log("Successfully reset password for '" + name + "'."); - console.log(""); + log.info("Successfully reset password for '" + name + "'."); }); }); diff --git a/src/command-line/start.js b/src/command-line/start.js index ac56cee6..58f7a4ba 100644 --- a/src/command-line/start.js +++ b/src/command-line/start.js @@ -21,10 +21,8 @@ program mode = false; } if (!mode && !users.length) { - console.log(""); - console.log("No users found!"); - console.log("Create a new user with 'lounge add '."); - console.log(""); + log.warn("No users found!"); + log.info("Create a new user with 'lounge add '."); } else { server({ host: program.host || process.env.IP || config.host, diff --git a/src/identd.js b/src/identd.js index 76773568..3d626dad 100644 --- a/src/identd.js +++ b/src/identd.js @@ -4,7 +4,9 @@ var net = require("net"); var users = {}; module.exports.start = function(port) { - net.createServer(init).listen(port || 113); + port = port || 113; + log.info("Starting identd server on port", port); + net.createServer(init).listen(port); }; module.exports.hook = function(stream, user) { @@ -44,4 +46,3 @@ function parse(data) { data = data.split(","); return parseInt(data[0]) + ", " + parseInt(data[1]); } - diff --git a/src/log.js b/src/log.js index 63d93b06..b973e503 100644 --- a/src/log.js +++ b/src/log.js @@ -1,47 +1,31 @@ -var fs = require("fs"); -var mkdirp = require("mkdirp"); +var colors = require("colors/safe"); var moment = require("moment"); var Helper = require("./helper"); -module.exports.write = function(user, network, chan, msg) { - try { - var path = Helper.HOME + "/logs/" + user + "/" + network; - mkdirp.sync(path); - } catch (e) { - console.log(e); - return; - } - +function timestamp(type, messageArgs) { var config = Helper.getConfig(); var format = (config.logs || {}).format || "YYYY-MM-DD HH:mm:ss"; var tz = (config.logs || {}).timezone || "UTC+00:00"; var time = moment().utcOffset(tz).format(format); - var line = "[" + time + "] "; - var type = msg.type.trim(); - if (type === "message" || type === "highlight") { - // Format: - // [2014-01-01 00:00:00] Put that cookie down.. Now!! - line += "<" + msg.from + "> " + msg.text; - } else { - // Format: - // [2014-01-01 00:00:00] * Arnold quit - line += "* " + msg.from + " " + msg.type; - if (msg.text) { - line += " " + msg.text; - } - } + Array.prototype.unshift.call(messageArgs, colors.dim(time), type); - fs.appendFile( - // Quick fix to escape pre-escape channel names that contain % using %%, - // and / using %. **This does not escape all reserved words** - path + "/" + chan.replace(/%/g, "%%").replace(/\//g, "%") + ".log", - line + "\n", - function(e) { - if (e) { - console.log("Log#write():\n" + e); - } - } - ); + return messageArgs; +} + +exports.err = function() { + console.error.apply(console, timestamp(colors.red("[ERROR]"), arguments)); +}; + +exports.warn = function() { + console.error.apply(console, timestamp(colors.yellow("[WARN]"), arguments)); +}; + +exports.info = function() { + console.log.apply(console, timestamp(colors.blue("[INFO]"), arguments)); +}; + +exports.debug = function() { + console.log.apply(console, timestamp(colors.green("[DEBUG]"), arguments)); }; diff --git a/src/plugins/irc-events/connection.js b/src/plugins/irc-events/connection.js index 6d07bb7d..26676297 100644 --- a/src/plugins/irc-events/connection.js +++ b/src/plugins/irc-events/connection.js @@ -26,7 +26,7 @@ module.exports = function(irc, network) { }); irc.on("socket error", function(err) { - console.log(err); + log.debug("IRC socket error", err); network.channels[0].pushMessage(client, new Msg({ type: Msg.Type.ERROR, text: "Socket error: " + err diff --git a/src/plugins/irc-events/error.js b/src/plugins/irc-events/error.js index 08a5507d..001acd22 100644 --- a/src/plugins/irc-events/error.js +++ b/src/plugins/irc-events/error.js @@ -5,12 +5,12 @@ module.exports = function(irc, network) { // TODO: remove later irc.on("irc_error", function(data) { - console.log("Got an irc_error"); + log.debug("Got an irc_error"); irc.emit("error", data); }); irc.on("error", function(data) { - console.log("error", data); + log.debug("error", data); var text = data.error; if (data.reason) { text = data.reason + " (" + text + ")"; diff --git a/src/server.js b/src/server.js index 5ff0226e..895e7312 100644 --- a/src/server.js +++ b/src/server.js @@ -56,10 +56,8 @@ module.exports = function(options) { manager.sockets = sockets; - console.log(""); - console.log("The Lounge is now running on " + protocol + "://" + config.host + ":" + config.port + "/"); - console.log("Press ctrl-c to stop"); - console.log(""); + log.info("The Lounge is now running on", protocol + "://" + config.host + ":" + config.port + "/"); + log.info("Press ctrl-c to stop"); if (!config.public) { manager.loadUsers(); diff --git a/src/userLog.js b/src/userLog.js new file mode 100644 index 00000000..7cbe5064 --- /dev/null +++ b/src/userLog.js @@ -0,0 +1,45 @@ +var fs = require("fs"); +var mkdirp = require("mkdirp"); +var moment = require("moment"); +var Helper = require("./helper"); + +module.exports.write = function(user, network, chan, msg) { + try { + var path = Helper.HOME + "/logs/" + user + "/" + network; + mkdirp.sync(path); + } catch (e) { + log.error("Unabled to create logs directory", e); + return; + } + + var config = Helper.getConfig(); + var format = (config.logs || {}).format || "YYYY-MM-DD HH:mm:ss"; + var tz = (config.logs || {}).timezone || "UTC+00:00"; + + var time = moment().utcOffset(tz).format(format); + var line = "[" + time + "] "; + + var type = msg.type.trim(); + if (type === "message" || type === "highlight") { + // Format: + // [2014-01-01 00:00:00] Put that cookie down.. Now!! + line += "<" + msg.from + "> " + msg.text; + } else { + // Format: + // [2014-01-01 00:00:00] * Arnold quit + line += "* " + msg.from + " " + msg.type; + if (msg.text) { + line += " " + msg.text; + } + } + + fs.appendFile( + // Quick fix to escape pre-escape channel names that contain % using %%, + // and / using %. **This does not escape all reserved words** + path + "/" + chan.replace(/%/g, "%%").replace(/\//g, "%") + ".log", + line + "\n", + function(e) { + log.error("Failed to write user log", e); + } + ); +};