From 695e76a544ae6b9ab7bbf7e25cbb33c64c674737 Mon Sep 17 00:00:00 2001 From: Mattias Erming Date: Tue, 16 Sep 2014 12:47:01 -0700 Subject: [PATCH] Added logging --- config.js | 27 ++++++++++++++++++++++ src/client.js | 13 +++++++++-- src/clientManager.js | 1 + src/log.js | 51 +++++++++++++++++++++++++++++++++++++++++ users/example/user.json | 1 + 5 files changed, 91 insertions(+), 2 deletions(-) create mode 100644 src/log.js diff --git a/config.js b/config.js index 44648d80..c3dcb791 100644 --- a/config.js +++ b/config.js @@ -52,6 +52,33 @@ module.exports = { // debug: false, + // + // Log settings + // + // Logging has to be enabled per user. If enabled, logs will be stored in + // the '/users//logs/' folder. + // + // @type object + // @default {} + // + logs: { + // + // Timestamp format + // + // @type string + // @default "YYYY-MM-DD HH:mm:ss" + // + format: "YYYY-MM-DD HH:mm:ss", + + // + // Timezone + // + // @type string + // @default "UTC+00:00" + // + timezone: "UTC+00:00" + }, + // // Default values for the 'Connect' form. // diff --git a/src/client.js b/src/client.js index c8e998e2..8e52f12b 100644 --- a/src/client.js +++ b/src/client.js @@ -1,6 +1,7 @@ var _ = require("lodash"); var config = require("../config"); var crypto = require("crypto"); +var log = require("./log"); var net = require("net"); var Msg = require("./models/msg"); var Network = require("./models/network"); @@ -45,11 +46,11 @@ var inputs = [ "whois" ]; -function Client(sockets, config) { +function Client(sockets, name, config) { _.merge(this, { config: config, id: id++, - name: "", + name: name, networks: [], sockets: sockets }); @@ -72,6 +73,14 @@ Client.prototype.emit = function(event, data) { if (this.sockets !== null) { this.sockets.in(this.id).emit(event, data); } + if (this.config.log === true) { + if (event == "msg") { + var target = this.find(data.chan); + if (target) { + log.write(this, target.network, target.chan, data.msg); + } + } + } }; Client.prototype.find = function(id) { diff --git a/src/clientManager.js b/src/clientManager.js index 11977245..fa4f2781 100644 --- a/src/clientManager.js +++ b/src/clientManager.js @@ -31,6 +31,7 @@ ClientManager.prototype.loadUsers = function(sockets) { if (!this.findClient(name)) { this.clients.push(new Client( sockets, + name, json )); } diff --git a/src/log.js b/src/log.js new file mode 100644 index 00000000..e4c2ecd6 --- /dev/null +++ b/src/log.js @@ -0,0 +1,51 @@ +var config = require("../config"); +var fs = require("fs"); +var mkdirp = require("mkdirp"); +var moment = require("moment"); +var Helper = require("./helper"); + +module.exports = { + write: function(client, network, chan, msg) { + var path = Helper.resolveHomePath( + "users", + client.name, + "logs", + network.host + ); + + try { + mkdirp.sync(path); + } catch(e) { + return; + } + + var format = (config.logs || {}).format || "YYYY-MM-DD HH:mm:ss"; + var tz = (config.logs || {}).timezone || "UTC+00:00"; + + var time = moment().zone(tz).format(format); + var name = chan.type == "lobby" ? network.host : chan.name; + var line = "[" + time + "] "; + + if (msg.type == "message") { + // 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; + } + } + + try { + fs.appendFile( + path + "/" + name + ".log", + line + "\n" + ); + } catch(e) { + return; + } + } +}; diff --git a/users/example/user.json b/users/example/user.json index 3eb72d34..f0fe9f79 100644 --- a/users/example/user.json +++ b/users/example/user.json @@ -1,6 +1,7 @@ { "user": "example", "password": "password", + "log": false, "networks": [{ "name": "Freenode", "host": "irc.freenode.net",