2024-02-15 23:01:22 +01:00
|
|
|
import Msg from "../../models/msg";
|
2022-06-18 16:25:21 -08:00
|
|
|
import LinkPrefetch from "./link";
|
2023-01-29 17:04:41 +01:00
|
|
|
import {cleanIrcMessage} from "../../../shared/irc";
|
2022-06-18 16:25:21 -08:00
|
|
|
import Helper from "../../helper";
|
|
|
|
import {IrcEventHandler} from "../../client";
|
2024-02-24 11:13:11 +01:00
|
|
|
import Chan from "../../models/chan";
|
2022-06-18 16:25:21 -08:00
|
|
|
import User from "../../models/user";
|
2024-02-15 23:01:22 +01:00
|
|
|
import {MessageType} from "../../../shared/types/msg";
|
2024-02-24 11:13:11 +01:00
|
|
|
import {ChanType} from "../../../shared/types/chan";
|
2024-04-15 07:54:12 +02:00
|
|
|
import {MessageEventArgs} from "irc-framework";
|
2016-10-09 15:14:02 -04:00
|
|
|
|
2017-12-25 14:43:37 +02:00
|
|
|
const nickRegExp = /(?:\x03[0-9]{1,2}(?:,[0-9]{1,2})?)?([\w[\]\\`^{|}-]+)/g;
|
2014-09-13 14:29:45 -07:00
|
|
|
|
2024-04-15 07:54:12 +02:00
|
|
|
type HandleInput = {
|
|
|
|
nick: string;
|
|
|
|
hostname: string;
|
|
|
|
ident: string;
|
|
|
|
target: string;
|
|
|
|
type: MessageType;
|
2024-05-16 22:15:39 +02:00
|
|
|
time?: number;
|
2024-04-15 07:54:12 +02:00
|
|
|
text?: string;
|
|
|
|
from_server?: boolean;
|
|
|
|
message: string;
|
|
|
|
group?: string;
|
|
|
|
};
|
|
|
|
|
|
|
|
function convertForHandle(type: MessageType, data: MessageEventArgs): HandleInput {
|
2024-05-16 22:15:39 +02:00
|
|
|
return {...data, type: type};
|
2024-04-15 07:54:12 +02:00
|
|
|
}
|
|
|
|
|
2022-06-18 16:25:21 -08:00
|
|
|
export default <IrcEventHandler>function (irc, network) {
|
2017-11-10 22:44:14 +02:00
|
|
|
const client = this;
|
2016-04-03 15:43:11 -04:00
|
|
|
|
2020-03-21 22:55:36 +02:00
|
|
|
irc.on("notice", function (data) {
|
2024-04-15 07:54:12 +02:00
|
|
|
handleMessage(convertForHandle(MessageType.NOTICE, data));
|
2016-03-07 23:09:42 +02:00
|
|
|
});
|
2014-09-27 08:46:32 -07:00
|
|
|
|
2020-03-21 22:55:36 +02:00
|
|
|
irc.on("action", function (data) {
|
2024-05-16 22:15:39 +02:00
|
|
|
handleMessage(convertForHandle(MessageType.ACTION, data));
|
2016-03-07 23:09:42 +02:00
|
|
|
});
|
|
|
|
|
2020-03-21 22:55:36 +02:00
|
|
|
irc.on("privmsg", function (data) {
|
2024-05-16 22:15:39 +02:00
|
|
|
handleMessage(convertForHandle(MessageType.MESSAGE, data));
|
2016-03-07 23:09:42 +02:00
|
|
|
});
|
|
|
|
|
2020-03-21 22:55:36 +02:00
|
|
|
irc.on("wallops", function (data) {
|
2016-10-01 00:29:49 +03:00
|
|
|
data.from_server = true;
|
2024-05-16 22:15:39 +02:00
|
|
|
handleMessage(convertForHandle(MessageType.WALLOPS, data));
|
2016-10-01 00:29:49 +03:00
|
|
|
});
|
|
|
|
|
2024-04-15 07:54:12 +02:00
|
|
|
function handleMessage(data: HandleInput) {
|
2022-06-18 16:25:21 -08:00
|
|
|
let chan: Chan | undefined;
|
|
|
|
let from: User;
|
2017-12-20 13:45:17 +02:00
|
|
|
let highlight = false;
|
|
|
|
let showInActive = false;
|
|
|
|
const self = data.nick === irc.user.nick;
|
2016-04-22 19:38:59 +03:00
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-03-11 15:17:57 -03:00
|
|
|
// Check if the sender is in our ignore list
|
2019-07-17 10:33:59 +01:00
|
|
|
const shouldIgnore =
|
|
|
|
!self &&
|
2020-03-21 22:55:36 +02:00
|
|
|
network.ignoreList.some(function (entry) {
|
2019-07-17 10:33:59 +01:00
|
|
|
return Helper.compareHostmask(entry, data);
|
|
|
|
});
|
2018-03-11 15:17:57 -03:00
|
|
|
|
2021-06-19 19:49:04 +01:00
|
|
|
// Server messages that aren't targeted at a channel go to the server window
|
|
|
|
if (
|
|
|
|
data.from_server &&
|
|
|
|
(!data.target ||
|
|
|
|
!network.getChannel(data.target) ||
|
2022-06-18 16:25:21 -08:00
|
|
|
network.getChannel(data.target)?.type !== ChanType.CHANNEL)
|
2021-06-19 19:49:04 +01:00
|
|
|
) {
|
2023-02-27 18:30:33 +01:00
|
|
|
chan = network.getLobby();
|
2017-12-20 13:45:17 +02:00
|
|
|
from = chan.getUser(data.nick);
|
2016-03-17 20:55:51 +02:00
|
|
|
} else {
|
2018-03-11 15:17:57 -03:00
|
|
|
if (shouldIgnore) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-11-10 22:44:14 +02:00
|
|
|
let target = data.target;
|
2016-03-17 20:55:51 +02:00
|
|
|
|
|
|
|
// 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 11:54:44 +03:00
|
|
|
chan = network.getChannel(target);
|
2017-11-10 22:44:14 +02:00
|
|
|
|
2016-03-08 12:13:36 +02:00
|
|
|
if (typeof chan === "undefined") {
|
2016-03-20 18:46:08 +02:00
|
|
|
// Send notices that are not targeted at us into the server window
|
2022-06-18 16:25:21 -08:00
|
|
|
if (data.type === MessageType.NOTICE) {
|
2017-12-20 13:45:17 +02:00
|
|
|
showInActive = true;
|
2023-02-27 18:30:33 +01:00
|
|
|
chan = network.getLobby();
|
2016-03-20 18:46:08 +02:00
|
|
|
} else {
|
2018-04-27 13:16:23 +03:00
|
|
|
chan = client.createChannel({
|
2022-06-18 16:25:21 -08:00
|
|
|
type: ChanType.QUERY,
|
2017-11-15 01:35:15 -05:00
|
|
|
name: target,
|
2016-03-20 18:46:08 +02:00
|
|
|
});
|
2018-03-12 12:42:59 +00:00
|
|
|
|
2016-03-20 18:46:08 +02:00
|
|
|
client.emit("join", {
|
2018-04-26 12:06:01 +03:00
|
|
|
network: network.uuid,
|
2017-12-03 16:29:50 +02:00
|
|
|
chan: chan.getFilteredClone(true),
|
2024-03-03 11:33:40 +01:00
|
|
|
shouldOpen: false,
|
2018-03-12 12:42:59 +00:00
|
|
|
index: network.addChannel(chan),
|
2016-03-20 18:46:08 +02:00
|
|
|
});
|
2018-03-12 12:42:59 +00:00
|
|
|
client.save();
|
2017-11-28 19:56:53 +02:00
|
|
|
chan.loadMessages(client, network);
|
2016-03-20 18:46:08 +02:00
|
|
|
}
|
2016-03-08 12:13:36 +02:00
|
|
|
}
|
2016-05-16 22:46:22 +03:00
|
|
|
|
2017-12-20 13:45:17 +02:00
|
|
|
from = chan.getUser(data.nick);
|
|
|
|
|
2022-02-10 17:56:17 -08:00
|
|
|
// Query messages (unless self or muted) always highlight
|
2022-06-18 16:25:21 -08:00
|
|
|
if (chan.type === ChanType.QUERY) {
|
2017-12-20 13:45:17 +02:00
|
|
|
highlight = !self;
|
2022-06-18 16:25:21 -08:00
|
|
|
} else if (chan.type === ChanType.CHANNEL) {
|
2017-12-19 13:45:33 +02:00
|
|
|
from.lastMessage = data.time || Date.now();
|
2016-05-16 22:46:22 +03:00
|
|
|
}
|
2014-09-13 14:29:45 -07:00
|
|
|
}
|
2014-09-21 09:46:43 -07:00
|
|
|
|
2017-12-20 13:45:17 +02:00
|
|
|
// msg is constructed down here because `from` is being copied in the constructor
|
|
|
|
const msg = new Msg({
|
|
|
|
type: data.type,
|
2024-05-16 22:15:39 +02:00
|
|
|
time: data.time ? new Date(data.time) : undefined,
|
2017-12-20 13:45:17 +02:00
|
|
|
text: data.message,
|
|
|
|
self: self,
|
|
|
|
from: from,
|
|
|
|
highlight: highlight,
|
|
|
|
users: [],
|
|
|
|
});
|
|
|
|
|
|
|
|
if (showInActive) {
|
|
|
|
msg.showInActive = true;
|
|
|
|
}
|
|
|
|
|
2020-07-22 17:28:12 +02:00
|
|
|
// remove IRC formatting for custom highlight testing
|
|
|
|
const cleanMessage = cleanIrcMessage(data.message);
|
|
|
|
|
2016-05-16 22:46:22 +03:00
|
|
|
// Self messages in channels are never highlighted
|
2016-03-08 01:50:52 -05:00
|
|
|
// Non-self messages are highlighted as soon as the nick is detected
|
2017-07-28 11:53:36 +03:00
|
|
|
if (!msg.highlight && !msg.self) {
|
2022-06-18 16:25:21 -08:00
|
|
|
msg.highlight = network.highlightRegex?.test(data.message);
|
2019-01-16 11:23:12 +02: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 17:28:12 +02:00
|
|
|
msg.highlight = client.highlightRegex.test(cleanMessage);
|
2019-01-16 11:23:12 +02:00
|
|
|
}
|
2016-04-22 19:38:59 +03:00
|
|
|
}
|
2014-09-21 09:46:43 -07:00
|
|
|
|
2020-07-22 17:28:12 +02:00
|
|
|
// if highlight exceptions match, do not highlight at all
|
|
|
|
if (msg.highlight && client.highlightExceptionRegex) {
|
|
|
|
msg.highlight = !client.highlightExceptionRegex.test(cleanMessage);
|
|
|
|
}
|
|
|
|
|
2020-04-24 11:46:39 +03:00
|
|
|
if (data.group) {
|
|
|
|
msg.statusmsgGroup = data.group;
|
|
|
|
}
|
|
|
|
|
2022-06-18 16:25:21 -08:00
|
|
|
let match: RegExpExecArray | null;
|
2018-02-20 09:28:04 +02:00
|
|
|
|
2017-11-14 14:36:45 -08:00
|
|
|
while ((match = nickRegExp.exec(data.message))) {
|
2017-11-27 13:58:10 -08:00
|
|
|
if (chan.findUser(match[1])) {
|
2017-07-28 11:53:36 +03:00
|
|
|
msg.users.push(match[1]);
|
2017-11-14 14:36:45 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-18 01:59:49 +02:00
|
|
|
// No prefetch URLs unless are simple MESSAGE or ACTION types
|
2022-06-18 16:25:21 -08:00
|
|
|
if ([MessageType.MESSAGE, MessageType.ACTION].includes(data.type)) {
|
2020-07-22 17:28:12 +02:00
|
|
|
LinkPrefetch(client, chan, msg, cleanMessage);
|
2016-12-18 01:59:49 +02:00
|
|
|
}
|
2017-07-19 01:26:29 -04:00
|
|
|
|
2017-07-28 11:53:36 +03:00
|
|
|
chan.pushMessage(client, msg, !msg.self);
|
2017-07-10 22:47:03 +03:00
|
|
|
|
2022-02-10 17:56:17 -08: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 18:57:07 +03:00
|
|
|
let title = chan.name;
|
2020-07-22 17:28:12 +02:00
|
|
|
let body = cleanMessage;
|
2017-07-10 22:47:03 +03:00
|
|
|
|
2022-06-18 16:25:21 -08:00
|
|
|
if (msg.type === MessageType.ACTION) {
|
2018-03-09 11:01:47 +02:00
|
|
|
// For actions, do not include colon in the message
|
|
|
|
body = `${data.nick} ${body}`;
|
2022-06-18 16:25:21 -08:00
|
|
|
} else if (chan.type !== ChanType.QUERY) {
|
2018-03-09 11:01:47 +02:00
|
|
|
// In channels, prepend sender nickname to the message
|
2017-09-03 18:57:07 +03: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 10:33:59 +01:00
|
|
|
title += ` (${chan.highlight} ${
|
2022-06-18 16:25:21 -08:00
|
|
|
chan.type === ChanType.QUERY ? "new message" : "mention"
|
2019-07-17 10:33:59 +01:00
|
|
|
}${chan.highlight > 1 ? "s" : ""})`;
|
2017-09-03 18:57:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (chan.highlight > 1) {
|
2019-07-17 10:33:59 +01:00
|
|
|
body += `\n\n… and ${chan.highlight - 1} other message${
|
|
|
|
chan.highlight > 2 ? "s" : ""
|
|
|
|
}`;
|
2017-07-10 22:47:03 +03:00
|
|
|
}
|
|
|
|
|
2019-07-17 10:33:59 +01:00
|
|
|
client.manager.webPush.push(
|
|
|
|
client,
|
|
|
|
{
|
|
|
|
type: "notification",
|
|
|
|
chanId: chan.id,
|
|
|
|
timestamp: data.time || Date.now(),
|
|
|
|
title: title,
|
|
|
|
body: body,
|
|
|
|
},
|
|
|
|
true
|
|
|
|
);
|
2017-07-10 22:47:03 +03:00
|
|
|
}
|
2019-11-04 11:21:05 +02:00
|
|
|
|
|
|
|
// Keep track of all mentions in channels for this client
|
2022-06-18 16:25:21 -08:00
|
|
|
if (msg.highlight && chan.type === ChanType.CHANNEL) {
|
2019-11-04 11:21:05 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
2016-03-07 23:09:42 +02:00
|
|
|
}
|
2014-09-13 14:29:45 -07:00
|
|
|
};
|