2016-10-09 15:14:02 -04:00
|
|
|
"use strict";
|
|
|
|
|
2014-09-16 15:47:01 -04:00
|
|
|
var fs = require("fs");
|
2016-06-09 18:42:23 -04:00
|
|
|
var fsextra = require("fs-extra");
|
2014-09-16 15:47:01 -04:00
|
|
|
var moment = require("moment");
|
|
|
|
var Helper = require("./helper");
|
|
|
|
|
2014-10-14 15:25:04 -04:00
|
|
|
module.exports.write = function(user, network, chan, msg) {
|
2016-10-09 04:54:44 -04:00
|
|
|
const path = Helper.getUserLogsPath(user, network);
|
|
|
|
|
2014-10-14 15:25:04 -04:00
|
|
|
try {
|
2016-06-09 18:42:23 -04:00
|
|
|
fsextra.ensureDirSync(path);
|
2015-09-30 18:39:57 -04:00
|
|
|
} catch (e) {
|
2017-08-25 11:58:16 -04:00
|
|
|
log.error("Unable to create logs directory", e);
|
2014-10-14 15:25:04 -04:00
|
|
|
return;
|
|
|
|
}
|
2014-09-16 15:47:01 -04:00
|
|
|
|
2016-06-08 05:26:24 -04:00
|
|
|
var format = Helper.config.logs.format || "YYYY-MM-DD HH:mm:ss";
|
|
|
|
var tz = Helper.config.logs.timezone || "UTC+00:00";
|
2014-09-16 15:47:01 -04:00
|
|
|
|
2017-09-14 03:42:21 -04:00
|
|
|
var time = moment(msg.time).utcOffset(tz).format(format);
|
2016-10-03 19:40:26 -04:00
|
|
|
var line = `[${time}] `;
|
2014-09-16 15:47:01 -04:00
|
|
|
|
2014-10-14 15:25:04 -04:00
|
|
|
var type = msg.type.trim();
|
2015-09-30 18:39:57 -04:00
|
|
|
if (type === "message" || type === "highlight") {
|
2014-10-14 15:25:04 -04:00
|
|
|
// Format:
|
|
|
|
// [2014-01-01 00:00:00] <Arnold> Put that cookie down.. Now!!
|
2018-01-05 08:41:03 -05:00
|
|
|
line += `<${msg.from.nick}> ${msg.text}`;
|
2014-10-14 15:25:04 -04:00
|
|
|
} else {
|
|
|
|
// Format:
|
|
|
|
// [2014-01-01 00:00:00] * Arnold quit
|
2018-01-05 08:41:03 -05:00
|
|
|
line += `* ${msg.from.nick} `;
|
2016-10-03 19:40:26 -04:00
|
|
|
|
|
|
|
if (msg.hostmask) {
|
|
|
|
line += `(${msg.hostmask}) `;
|
|
|
|
}
|
|
|
|
|
|
|
|
line += msg.type;
|
|
|
|
|
2016-10-10 13:08:23 -04:00
|
|
|
if (msg.new_nick) { // `/nick <new_nick>`
|
|
|
|
line += ` ${msg.new_nick}`;
|
|
|
|
} else if (msg.text) {
|
2016-10-03 19:40:26 -04:00
|
|
|
line += ` ${msg.text}`;
|
2014-09-16 15:47:01 -04:00
|
|
|
}
|
2014-10-14 15:25:04 -04:00
|
|
|
}
|
2014-09-16 15:47:01 -04:00
|
|
|
|
2014-10-14 15:25:04 -04:00
|
|
|
fs.appendFile(
|
2015-10-14 16:15:57 -04:00
|
|
|
// 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",
|
2014-10-14 15:25:04 -04:00
|
|
|
line + "\n",
|
|
|
|
function(e) {
|
2016-05-15 04:15:20 -04:00
|
|
|
if (e) {
|
|
|
|
log.error("Failed to write user log", e);
|
|
|
|
}
|
2014-10-14 15:25:04 -04:00
|
|
|
}
|
|
|
|
);
|
2014-09-16 15:47:01 -04:00
|
|
|
};
|