2022-06-19 00:25:21 +00:00
|
|
|
/* eslint-disable @typescript-eslint/restrict-template-expressions */
|
2022-11-01 21:51:40 +00:00
|
|
|
import fs from "fs/promises";
|
2022-06-19 00:25:21 +00:00
|
|
|
import path from "path";
|
|
|
|
import filenamify from "filenamify";
|
|
|
|
|
|
|
|
import Config from "../../config";
|
|
|
|
import {MessageStorage} from "./types";
|
|
|
|
import Client from "../../client";
|
|
|
|
import Channel from "../../models/chan";
|
|
|
|
import {Message, MessageType} from "../../models/msg";
|
|
|
|
import Network from "../../models/network";
|
|
|
|
|
|
|
|
class TextFileMessageStorage implements MessageStorage {
|
|
|
|
client: Client;
|
|
|
|
isEnabled: boolean;
|
|
|
|
|
|
|
|
constructor(client: Client) {
|
2018-04-17 08:06:08 +00:00
|
|
|
this.client = client;
|
|
|
|
this.isEnabled = false;
|
|
|
|
}
|
|
|
|
|
2022-11-01 21:51:40 +00:00
|
|
|
// eslint-disable-next-line @typescript-eslint/require-await
|
|
|
|
async enable() {
|
2018-04-17 08:06:08 +00:00
|
|
|
this.isEnabled = true;
|
|
|
|
}
|
|
|
|
|
2022-11-01 21:51:40 +00:00
|
|
|
// eslint-disable-next-line @typescript-eslint/require-await
|
|
|
|
async close() {
|
2018-04-17 08:06:08 +00:00
|
|
|
this.isEnabled = false;
|
|
|
|
}
|
|
|
|
|
2022-11-01 21:51:40 +00:00
|
|
|
async index(network: Network, channel: Channel, msg: Message) {
|
2018-04-17 08:06:08 +00:00
|
|
|
if (!this.isEnabled) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-07-17 09:33:59 +00:00
|
|
|
const logPath = path.join(
|
2022-05-01 19:12:39 +00:00
|
|
|
Config.getUserLogsPath(),
|
2019-07-17 09:33:59 +00:00
|
|
|
this.client.name,
|
|
|
|
TextFileMessageStorage.getNetworkFolderName(network)
|
|
|
|
);
|
2018-04-17 08:06:08 +00:00
|
|
|
|
|
|
|
try {
|
2022-11-01 21:51:40 +00:00
|
|
|
await fs.mkdir(logPath, {recursive: true});
|
|
|
|
} catch (e) {
|
|
|
|
throw new Error(`Unable to create logs directory: ${e}`);
|
2018-04-17 08:06:08 +00:00
|
|
|
}
|
|
|
|
|
2018-09-20 11:15:05 +00:00
|
|
|
let line = `[${msg.time.toISOString()}] `;
|
2018-04-17 08:06:08 +00:00
|
|
|
|
2018-06-02 16:27:14 +00:00
|
|
|
// message types from src/models/msg.js
|
|
|
|
switch (msg.type) {
|
2022-06-19 00:25:21 +00:00
|
|
|
case MessageType.ACTION:
|
2019-07-17 09:33:59 +00:00
|
|
|
// [2014-01-01 00:00:00] * @Arnold is eating cookies
|
|
|
|
line += `* ${msg.from.mode}${msg.from.nick} ${msg.text}`;
|
|
|
|
break;
|
2022-06-19 00:25:21 +00:00
|
|
|
case MessageType.JOIN:
|
2019-07-17 09:33:59 +00:00
|
|
|
// [2014-01-01 00:00:00] *** Arnold (~arnold@foo.bar) joined
|
|
|
|
line += `*** ${msg.from.nick} (${msg.hostmask}) joined`;
|
|
|
|
break;
|
2022-06-19 00:25:21 +00:00
|
|
|
case MessageType.KICK:
|
2019-07-17 09:33:59 +00:00
|
|
|
// [2014-01-01 00:00:00] *** Arnold was kicked by Bernie (Don't steal my cookies!)
|
|
|
|
line += `*** ${msg.target.nick} was kicked by ${msg.from.nick} (${msg.text})`;
|
|
|
|
break;
|
2022-06-19 00:25:21 +00:00
|
|
|
case MessageType.MESSAGE:
|
2019-07-17 09:33:59 +00:00
|
|
|
// [2014-01-01 00:00:00] <@Arnold> Put that cookie down.. Now!!
|
|
|
|
line += `<${msg.from.mode}${msg.from.nick}> ${msg.text}`;
|
|
|
|
break;
|
2022-06-19 00:25:21 +00:00
|
|
|
case MessageType.MODE:
|
2019-07-17 09:33:59 +00:00
|
|
|
// [2014-01-01 00:00:00] *** Arnold set mode +o Bernie
|
|
|
|
line += `*** ${msg.from.nick} set mode ${msg.text}`;
|
|
|
|
break;
|
2022-06-19 00:25:21 +00:00
|
|
|
case MessageType.NICK:
|
2019-07-17 09:33:59 +00:00
|
|
|
// [2014-01-01 00:00:00] *** Arnold changed nick to Bernie
|
|
|
|
line += `*** ${msg.from.nick} changed nick to ${msg.new_nick}`;
|
|
|
|
break;
|
2022-06-19 00:25:21 +00:00
|
|
|
case MessageType.NOTICE:
|
2019-07-17 09:33:59 +00:00
|
|
|
// [2014-01-01 00:00:00] -Arnold- pssst, I have cookies!
|
|
|
|
line += `-${msg.from.nick}- ${msg.text}`;
|
|
|
|
break;
|
2022-06-19 00:25:21 +00:00
|
|
|
case MessageType.PART:
|
2019-07-17 09:33:59 +00:00
|
|
|
// [2014-01-01 00:00:00] *** Arnold (~arnold@foo.bar) left (Bye all!)
|
|
|
|
line += `*** ${msg.from.nick} (${msg.hostmask}) left (${msg.text})`;
|
|
|
|
break;
|
2022-06-19 00:25:21 +00:00
|
|
|
case MessageType.QUIT:
|
2019-07-17 09:33:59 +00:00
|
|
|
// [2014-01-01 00:00:00] *** Arnold (~arnold@foo.bar) quit (Connection reset by peer)
|
|
|
|
line += `*** ${msg.from.nick} (${msg.hostmask}) quit (${msg.text})`;
|
|
|
|
break;
|
2022-06-19 00:25:21 +00:00
|
|
|
case MessageType.CHGHOST:
|
2019-07-17 09:33:59 +00:00
|
|
|
// [2014-01-01 00:00:00] *** Arnold changed host to: new@fancy.host
|
|
|
|
line += `*** ${msg.from.nick} changed host to '${msg.new_ident}@${msg.new_host}'`;
|
|
|
|
break;
|
2022-06-19 00:25:21 +00:00
|
|
|
case MessageType.TOPIC:
|
2019-07-17 09:33:59 +00:00
|
|
|
// [2014-01-01 00:00:00] *** Arnold changed topic to: welcome everyone!
|
|
|
|
line += `*** ${msg.from.nick} changed topic to '${msg.text}'`;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
// unhandled events will not be logged
|
|
|
|
return;
|
2018-04-17 08:06:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
line += "\n";
|
|
|
|
|
2022-11-01 21:51:40 +00:00
|
|
|
try {
|
|
|
|
await fs.appendFile(
|
|
|
|
path.join(logPath, TextFileMessageStorage.getChannelFileName(channel)),
|
|
|
|
line
|
|
|
|
);
|
|
|
|
} catch (e) {
|
|
|
|
throw new Error(`Failed to write user log: ${e}`);
|
2020-01-30 08:52:29 +00:00
|
|
|
}
|
2022-11-01 21:51:40 +00:00
|
|
|
}
|
2020-01-30 08:52:29 +00:00
|
|
|
|
2022-11-01 21:51:40 +00:00
|
|
|
async deleteChannel() {
|
|
|
|
// Not implemented for text log files
|
2018-04-17 08:06:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
getMessages() {
|
|
|
|
// Not implemented for text log files
|
|
|
|
// They do not contain enough data to fully re-create message objects
|
|
|
|
// Use sqlite storage instead
|
|
|
|
return Promise.resolve([]);
|
|
|
|
}
|
|
|
|
|
|
|
|
canProvideMessages() {
|
|
|
|
return false;
|
|
|
|
}
|
2018-09-09 10:09:30 +00:00
|
|
|
|
2022-06-19 00:25:21 +00:00
|
|
|
static getNetworkFolderName(network: Network) {
|
2018-09-09 10:09:30 +00:00
|
|
|
// Limit network name in the folder name to 23 characters
|
|
|
|
// So we can still fit 12 characters of the uuid for de-duplication
|
|
|
|
const networkName = cleanFilename(network.name.substring(0, 23).replace(/ /g, "-"));
|
|
|
|
|
|
|
|
return `${networkName}-${network.uuid.substring(networkName.length + 1)}`;
|
|
|
|
}
|
2020-01-30 08:52:29 +00:00
|
|
|
|
2022-06-19 00:25:21 +00:00
|
|
|
static getChannelFileName(channel: Channel) {
|
2020-01-30 08:52:29 +00:00
|
|
|
return `${cleanFilename(channel.name)}.log`;
|
|
|
|
}
|
2018-04-17 08:06:08 +00:00
|
|
|
}
|
|
|
|
|
2022-06-19 00:25:21 +00:00
|
|
|
export default TextFileMessageStorage;
|
2018-04-17 08:06:08 +00:00
|
|
|
|
2022-06-19 00:25:21 +00:00
|
|
|
function cleanFilename(name: string) {
|
2018-04-17 08:06:08 +00:00
|
|
|
name = filenamify(name, {replacement: "_"});
|
|
|
|
name = name.toLowerCase();
|
|
|
|
|
2018-07-21 21:39:22 +00:00
|
|
|
return name;
|
2018-04-17 08:06:08 +00:00
|
|
|
}
|