diff --git a/lib/log.js b/lib/log.js new file mode 100644 index 00000000..c9594d2f --- /dev/null +++ b/lib/log.js @@ -0,0 +1,34 @@ +var config = require("../config") || {}; +var fs = require("fs"); +var moment = require("moment"); + +module.exports = function log(chan, msg) { + if (!config.log) { + return; + } + + var network = chan.network.name; + var dir = "logs/" + network + "/"; + if (!fs.existsSync(dir)) { + fs.mkdir(dir); + } + + var date = moment().format("YYYY-MM-DD HH:mm"); + var line = "[" + date + "] "; + if (msg.type == "normal") { + line += "<" + + msg.from + "> " + + msg.text; + } else { + line += "* " + + msg.from + " " + + msg.type + " " + + msg.text; + } + + var file = dir + chan.name + ".log"; + fs.appendFile( + file, + line + "\n" + ); +}; diff --git a/lib/models/chan.js b/lib/models/chan.js index 67dcf096..40705887 100644 --- a/lib/models/chan.js +++ b/lib/models/chan.js @@ -1,6 +1,5 @@ var _ = require("lodash"); -var config = require("../../config") || {}; -var fs = require('fs'); +var log = require("../log"); var moment = require("moment"); module.exports = Chan; @@ -19,35 +18,9 @@ function Chan(attr) { Chan.prototype.addMsg = function(msg) { this.messages.push(msg); - if (config.log != true || this.type == "lobby") { - return; + if (this.type != "lobby") { + log(this, msg); } - - var dir = "logs/"; - dir += this.network + "/"; - if (!fs.existsSync(dir)) { - fs.mkdir(dir); - } - - var line = "[" + msg.time + "] "; - if (msg.type == "normal") { - // Format: - // [00:00] Put that cookie down.. Now!! - line += "<" + msg.from + "> " + msg.text; - } else { - // Format: - // [00:00] * Arnold quit - line += "* " + msg.from + " " + msg.type; - if (msg.text) { - line += " " + msg.text; - } - } - - var file = dir + this.name + ".log"; - fs.appendFile( - file, - line + "\n" - ); }; Chan.prototype.addUser = function(user) { @@ -74,7 +47,7 @@ Chan.prototype.sortUsers = function() { }; Chan.prototype.toJSON = function() { - var clone = _.clone(this); + var clone = _.omit(this, ["network"]); clone.count = clone.messages.length; clone.messages = clone.messages.slice(-100); return clone; diff --git a/lib/models/network.js b/lib/models/network.js index 428099cd..e31454f8 100644 --- a/lib/models/network.js +++ b/lib/models/network.js @@ -22,7 +22,7 @@ function Network(attr) { }; Network.prototype.addChan = function(chan) { - chan.network = this.name; + chan.network = this; this.channels.push(chan); };