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