thelounge/src/userLog.js

59 lines
1.4 KiB
JavaScript
Raw Normal View History

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