hardlounge/server/plugins/irc-events/message.ts

235 lines
6.1 KiB
TypeScript
Raw Normal View History

import Msg, {MessageType} from "../../models/msg";
import LinkPrefetch from "./link";
import cleanIrcMessage from "../../../client/js/helpers/ircmessageparser/cleanIrcMessage";
import Helper from "../../helper";
import {IrcEventHandler} from "../../client";
import Chan, {ChanType} from "../../models/chan";
import User from "../../models/user";
const nickRegExp = /(?:\x03[0-9]{1,2}(?:,[0-9]{1,2})?)?([\w[\]\\`^{|}-]+)/g;
2014-09-13 21:29:45 +00:00
export default <IrcEventHandler>function (irc, network) {
const client = this;
irc.on("notice", function (data) {
data.type = MessageType.NOTICE;
type ModifiedData = typeof data & {
type: MessageType.NOTICE;
};
handleMessage(data as ModifiedData);
});
2014-09-27 15:46:32 +00:00
irc.on("action", function (data) {
data.type = MessageType.ACTION;
handleMessage(data);
});
irc.on("privmsg", function (data) {
data.type = MessageType.MESSAGE;
handleMessage(data);
});
irc.on("wallops", function (data) {
data.from_server = true;
data.type = MessageType.WALLOPS;
handleMessage(data);
});
function handleMessage(data: {
nick: string;
hostname: string;
ident: string;
target: string;
type: MessageType;
time: number;
text?: string;
from_server?: boolean;
message: string;
group?: string;
}) {
let chan: Chan | undefined;
let from: User;
let highlight = false;
let showInActive = false;
const self = data.nick === irc.user.nick;
2021-07-03 21:06:16 +00:00
// Some servers send messages without any nickname
if (!data.nick) {
data.from_server = true;
data.nick = data.hostname || network.host;
}
// Check if the sender is in our ignore list
2019-07-17 09:33:59 +00:00
const shouldIgnore =
!self &&
network.ignoreList.some(function (entry) {
2019-07-17 09:33:59 +00:00
return Helper.compareHostmask(entry, data);
});
// Server messages that aren't targeted at a channel go to the server window
if (
data.from_server &&
(!data.target ||
!network.getChannel(data.target) ||
network.getChannel(data.target)?.type !== ChanType.CHANNEL)
) {
chan = network.channels[0];
from = chan.getUser(data.nick);
} else {
if (shouldIgnore) {
return;
}
let target = data.target;
// If the message is targeted at us, use sender as target instead
if (target.toLowerCase() === irc.user.nick.toLowerCase()) {
target = data.nick;
}
2016-10-09 08:54:44 +00:00
chan = network.getChannel(target);
if (typeof chan === "undefined") {
// Send notices that are not targeted at us into the server window
if (data.type === MessageType.NOTICE) {
showInActive = true;
chan = network.channels[0];
} else {
chan = client.createChannel({
type: ChanType.QUERY,
name: target,
});
client.emit("join", {
network: network.uuid,
chan: chan.getFilteredClone(true),
index: network.addChannel(chan),
});
client.save();
2017-11-28 17:56:53 +00:00
chan.loadMessages(client, network);
}
}
from = chan.getUser(data.nick);
// Query messages (unless self or muted) always highlight
if (chan.type === ChanType.QUERY) {
highlight = !self;
} else if (chan.type === ChanType.CHANNEL) {
from.lastMessage = data.time || Date.now();
}
2014-09-13 21:29:45 +00:00
}
// msg is constructed down here because `from` is being copied in the constructor
const msg = new Msg({
type: data.type,
time: data.time as any,
text: data.message,
self: self,
from: from,
highlight: highlight,
users: [],
});
if (showInActive) {
msg.showInActive = true;
}
2020-07-22 15:28:12 +00:00
// remove IRC formatting for custom highlight testing
const cleanMessage = cleanIrcMessage(data.message);
// Self messages in channels are never highlighted
// Non-self messages are highlighted as soon as the nick is detected
2017-07-28 08:53:36 +00:00
if (!msg.highlight && !msg.self) {
msg.highlight = network.highlightRegex?.test(data.message);
2019-01-16 09:23:12 +00:00
// If we still don't have a highlight, test against custom highlights if there's any
if (!msg.highlight && client.highlightRegex) {
2020-07-22 15:28:12 +00:00
msg.highlight = client.highlightRegex.test(cleanMessage);
2019-01-16 09:23:12 +00:00
}
}
2020-07-22 15:28:12 +00:00
// if highlight exceptions match, do not highlight at all
if (msg.highlight && client.highlightExceptionRegex) {
msg.highlight = !client.highlightExceptionRegex.test(cleanMessage);
}
2020-04-24 08:46:39 +00:00
if (data.group) {
msg.statusmsgGroup = data.group;
}
let match: RegExpExecArray | null;
2017-11-14 22:36:45 +00:00
while ((match = nickRegExp.exec(data.message))) {
if (chan.findUser(match[1])) {
// @ts-expect-error Type 'string' is not assignable to type '{ mode: string; }'.ts(2345)
2017-07-28 08:53:36 +00:00
msg.users.push(match[1]);
2017-11-14 22:36:45 +00:00
}
}
// No prefetch URLs unless are simple MESSAGE or ACTION types
if ([MessageType.MESSAGE, MessageType.ACTION].includes(data.type)) {
2020-07-22 15:28:12 +00:00
LinkPrefetch(client, chan, msg, cleanMessage);
}
2017-07-28 08:53:36 +00:00
chan.pushMessage(client, msg, !msg.self);
2017-07-10 19:47:03 +00:00
// Do not send notifications if the channel is muted or for messages older than 15 minutes (znc buffer for example)
if (!chan.muted && msg.highlight && (!data.time || data.time > Date.now() - 900000)) {
2017-09-03 15:57:07 +00:00
let title = chan.name;
2020-07-22 15:28:12 +00:00
let body = cleanMessage;
2017-07-10 19:47:03 +00:00
if (msg.type === MessageType.ACTION) {
// For actions, do not include colon in the message
body = `${data.nick} ${body}`;
} else if (chan.type !== ChanType.QUERY) {
// In channels, prepend sender nickname to the message
2017-09-03 15:57:07 +00:00
body = `${data.nick}: ${body}`;
}
// If a channel is active on any client, highlight won't increment and notification will say (0 mention)
if (chan.highlight > 0) {
2019-07-17 09:33:59 +00:00
title += ` (${chan.highlight} ${
chan.type === ChanType.QUERY ? "new message" : "mention"
2019-07-17 09:33:59 +00:00
}${chan.highlight > 1 ? "s" : ""})`;
2017-09-03 15:57:07 +00:00
}
if (chan.highlight > 1) {
2019-07-17 09:33:59 +00:00
body += `\n\n… and ${chan.highlight - 1} other message${
chan.highlight > 2 ? "s" : ""
}`;
2017-07-10 19:47:03 +00:00
}
2019-07-17 09:33:59 +00:00
client.manager.webPush.push(
client,
{
type: "notification",
chanId: chan.id,
timestamp: data.time || Date.now(),
title: title,
body: body,
},
true
);
2017-07-10 19:47:03 +00:00
}
// Keep track of all mentions in channels for this client
if (msg.highlight && chan.type === ChanType.CHANNEL) {
client.mentions.push({
chanId: chan.id,
msgId: msg.id,
type: msg.type,
time: msg.time,
text: msg.text,
from: msg.from,
});
if (client.mentions.length > 100) {
client.mentions.splice(0, client.mentions.length - 100);
}
}
}
2014-09-13 21:29:45 +00:00
};