Added logging

This commit is contained in:
Mattias Erming 2014-09-16 12:47:01 -07:00
parent 56b72071ec
commit 695e76a544
5 changed files with 91 additions and 2 deletions

View File

@ -52,6 +52,33 @@ module.exports = {
// //
debug: false, debug: false,
//
// Log settings
//
// Logging has to be enabled per user. If enabled, logs will be stored in
// the '/users/<user>/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. // Default values for the 'Connect' form.
// //

View File

@ -1,6 +1,7 @@
var _ = require("lodash"); var _ = require("lodash");
var config = require("../config"); var config = require("../config");
var crypto = require("crypto"); var crypto = require("crypto");
var log = require("./log");
var net = require("net"); var net = require("net");
var Msg = require("./models/msg"); var Msg = require("./models/msg");
var Network = require("./models/network"); var Network = require("./models/network");
@ -45,11 +46,11 @@ var inputs = [
"whois" "whois"
]; ];
function Client(sockets, config) { function Client(sockets, name, config) {
_.merge(this, { _.merge(this, {
config: config, config: config,
id: id++, id: id++,
name: "", name: name,
networks: [], networks: [],
sockets: sockets sockets: sockets
}); });
@ -72,6 +73,14 @@ Client.prototype.emit = function(event, data) {
if (this.sockets !== null) { if (this.sockets !== null) {
this.sockets.in(this.id).emit(event, data); 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) { Client.prototype.find = function(id) {

View File

@ -31,6 +31,7 @@ ClientManager.prototype.loadUsers = function(sockets) {
if (!this.findClient(name)) { if (!this.findClient(name)) {
this.clients.push(new Client( this.clients.push(new Client(
sockets, sockets,
name,
json json
)); ));
} }

51
src/log.js Normal file
View File

@ -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] <Arnold> 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;
}
}
};

View File

@ -1,6 +1,7 @@
{ {
"user": "example", "user": "example",
"password": "password", "password": "password",
"log": false,
"networks": [{ "networks": [{
"name": "Freenode", "name": "Freenode",
"host": "irc.freenode.net", "host": "irc.freenode.net",