2014-09-16 19:47:01 +00:00
|
|
|
var fs = require("fs");
|
|
|
|
var mkdirp = require("mkdirp");
|
|
|
|
var moment = require("moment");
|
|
|
|
var Helper = require("./helper");
|
|
|
|
|
2014-10-14 19:25:04 +00:00
|
|
|
module.exports.write = function(user, network, chan, msg) {
|
|
|
|
try {
|
|
|
|
var path = Helper.HOME + "/logs/" + user + "/" + network;
|
|
|
|
mkdirp.sync(path);
|
2015-09-30 22:39:57 +00:00
|
|
|
} catch (e) {
|
2014-10-14 19:25:04 +00:00
|
|
|
console.log(e);
|
|
|
|
return;
|
|
|
|
}
|
2014-09-16 19:47:01 +00:00
|
|
|
|
2014-10-14 19:25:04 +00:00
|
|
|
var config = Helper.getConfig();
|
|
|
|
var format = (config.logs || {}).format || "YYYY-MM-DD HH:mm:ss";
|
|
|
|
var tz = (config.logs || {}).timezone || "UTC+00:00";
|
2014-09-16 19:47:01 +00:00
|
|
|
|
2014-10-14 19:25:04 +00:00
|
|
|
var time = moment().zone(tz).format(format);
|
|
|
|
var line = "[" + time + "] ";
|
2014-09-16 19:47:01 +00:00
|
|
|
|
2014-10-14 19:25:04 +00:00
|
|
|
var type = msg.type.trim();
|
2015-09-30 22:39:57 +00:00
|
|
|
if (type === "message" || type === "highlight") {
|
2014-10-14 19:25:04 +00:00
|
|
|
// 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;
|
2014-09-16 19:47:01 +00:00
|
|
|
}
|
2014-10-14 19:25:04 +00:00
|
|
|
}
|
2014-09-16 19:47:01 +00:00
|
|
|
|
2014-10-14 19:25:04 +00:00
|
|
|
fs.appendFile(
|
2015-10-14 20:15:57 +00: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 19:25:04 +00:00
|
|
|
line + "\n",
|
|
|
|
function(e) {
|
|
|
|
if (e) {
|
2015-09-30 22:39:57 +00:00
|
|
|
console.log("Log#write():\n" + e);
|
2014-09-30 20:20:36 +00:00
|
|
|
}
|
2014-10-14 19:25:04 +00:00
|
|
|
}
|
|
|
|
);
|
2014-09-16 19:47:01 +00:00
|
|
|
};
|