2016-10-09 21:14:02 +02:00
|
|
|
"use strict";
|
|
|
|
|
2016-12-09 21:46:53 +01:00
|
|
|
const Chan = require("../../models/chan");
|
|
|
|
const Msg = require("../../models/msg");
|
|
|
|
const LinkPrefetch = require("./link");
|
2019-11-05 11:36:44 +01:00
|
|
|
const cleanIrcMessage = require("../../../client/js/helpers/ircmessageparser/cleanIrcMessage");
|
2018-03-11 19:17:57 +01:00
|
|
|
const Helper = require("../../helper");
|
2017-12-25 13:43:37 +01:00
|
|
|
const nickRegExp = /(?:\x03[0-9]{1,2}(?:,[0-9]{1,2})?)?([\w[\]\\`^{|}-]+)/g;
|
2014-09-13 23:29:45 +02:00
|
|
|
|
2020-03-21 21:55:36 +01:00
|
|
|
module.exports = function (irc, network) {
|
2017-11-10 21:44:14 +01:00
|
|
|
const client = this;
|
2016-04-03 21:43:11 +02:00
|
|
|
|
2020-03-21 21:55:36 +01:00
|
|
|
irc.on("notice", function (data) {
|
2016-03-20 17:46:08 +01:00
|
|
|
// Some servers send notices without any nickname
|
|
|
|
if (!data.nick) {
|
|
|
|
data.from_server = true;
|
2020-01-18 23:53:03 +01:00
|
|
|
data.nick = data.hostname || network.host;
|
2016-03-20 17:46:08 +01:00
|
|
|
}
|
|
|
|
|
2016-03-07 22:09:42 +01:00
|
|
|
data.type = Msg.Type.NOTICE;
|
|
|
|
handleMessage(data);
|
|
|
|
});
|
2014-09-27 17:46:32 +02:00
|
|
|
|
2020-03-21 21:55:36 +01:00
|
|
|
irc.on("action", function (data) {
|
2016-03-07 22:09:42 +01:00
|
|
|
data.type = Msg.Type.ACTION;
|
|
|
|
handleMessage(data);
|
|
|
|
});
|
|
|
|
|
2020-03-21 21:55:36 +01:00
|
|
|
irc.on("privmsg", function (data) {
|
2016-03-07 22:09:42 +01:00
|
|
|
data.type = Msg.Type.MESSAGE;
|
|
|
|
handleMessage(data);
|
|
|
|
});
|
|
|
|
|
2020-03-21 21:55:36 +01:00
|
|
|
irc.on("wallops", function (data) {
|
2016-09-30 23:29:49 +02:00
|
|
|
data.from_server = true;
|
2021-06-22 12:50:22 +02:00
|
|
|
data.type = Msg.Type.WALLOPS;
|
2016-09-30 23:29:49 +02:00
|
|
|
handleMessage(data);
|
|
|
|
});
|
|
|
|
|
2016-03-07 22:09:42 +01:00
|
|
|
function handleMessage(data) {
|
2016-10-09 10:54:44 +02:00
|
|
|
let chan;
|
2017-12-20 12:45:17 +01:00
|
|
|
let from;
|
|
|
|
let highlight = false;
|
|
|
|
let showInActive = false;
|
|
|
|
const self = data.nick === irc.user.nick;
|
2016-04-22 18:38:59 +02:00
|
|
|
|
2018-03-11 19:17:57 +01:00
|
|
|
// Check if the sender is in our ignore list
|
2019-07-17 11:33:59 +02:00
|
|
|
const shouldIgnore =
|
|
|
|
!self &&
|
2020-03-21 21:55:36 +01:00
|
|
|
network.ignoreList.some(function (entry) {
|
2019-07-17 11:33:59 +02:00
|
|
|
return Helper.compareHostmask(entry, data);
|
|
|
|
});
|
2018-03-11 19:17:57 +01:00
|
|
|
|
2021-06-19 20:49:04 +02: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) ||
|
|
|
|
network.getChannel(data.target).type !== Chan.Type.CHANNEL)
|
|
|
|
) {
|
2016-03-17 19:55:51 +01:00
|
|
|
chan = network.channels[0];
|
2017-12-20 12:45:17 +01:00
|
|
|
from = chan.getUser(data.nick);
|
2016-03-17 19:55:51 +01:00
|
|
|
} else {
|
2018-03-11 19:17:57 +01:00
|
|
|
if (shouldIgnore) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-11-10 21:44:14 +01:00
|
|
|
let target = data.target;
|
2016-03-17 19:55:51 +01: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 10:54:44 +02:00
|
|
|
chan = network.getChannel(target);
|
2017-11-10 21:44:14 +01:00
|
|
|
|
2016-03-08 11:13:36 +01:00
|
|
|
if (typeof chan === "undefined") {
|
2016-03-20 17:46:08 +01:00
|
|
|
// Send notices that are not targeted at us into the server window
|
2016-03-22 12:08:10 +01:00
|
|
|
if (data.type === Msg.Type.NOTICE) {
|
2017-12-20 12:45:17 +01:00
|
|
|
showInActive = true;
|
2016-03-20 17:46:08 +01:00
|
|
|
chan = network.channels[0];
|
|
|
|
} else {
|
2018-04-27 12:16:23 +02:00
|
|
|
chan = client.createChannel({
|
2016-03-20 17:46:08 +01:00
|
|
|
type: Chan.Type.QUERY,
|
2017-11-15 07:35:15 +01:00
|
|
|
name: target,
|
2016-03-20 17:46:08 +01:00
|
|
|
});
|
2018-03-12 13:42:59 +01:00
|
|
|
|
2016-03-20 17:46:08 +01:00
|
|
|
client.emit("join", {
|
2018-04-26 11:06:01 +02:00
|
|
|
network: network.uuid,
|
2017-12-03 15:29:50 +01:00
|
|
|
chan: chan.getFilteredClone(true),
|
2018-03-12 13:42:59 +01:00
|
|
|
index: network.addChannel(chan),
|
2016-03-20 17:46:08 +01:00
|
|
|
});
|
2018-03-12 13:42:59 +01:00
|
|
|
client.save();
|
2017-11-28 18:56:53 +01:00
|
|
|
chan.loadMessages(client, network);
|
2016-03-20 17:46:08 +01:00
|
|
|
}
|
2016-03-08 11:13:36 +01:00
|
|
|
}
|
2016-05-16 21:46:22 +02:00
|
|
|
|
2017-12-20 12:45:17 +01:00
|
|
|
from = chan.getUser(data.nick);
|
|
|
|
|
2016-05-16 21:46:22 +02:00
|
|
|
// Query messages (unless self) always highlight
|
|
|
|
if (chan.type === Chan.Type.QUERY) {
|
2017-12-20 12:45:17 +01:00
|
|
|
highlight = !self;
|
2017-07-10 12:56:37 +02:00
|
|
|
} else if (chan.type === Chan.Type.CHANNEL) {
|
2017-12-19 12:45:33 +01:00
|
|
|
from.lastMessage = data.time || Date.now();
|
2016-05-16 21:46:22 +02:00
|
|
|
}
|
2014-09-13 23:29:45 +02:00
|
|
|
}
|
2014-09-21 18:46:43 +02:00
|
|
|
|
2017-12-20 12:45:17 +01:00
|
|
|
// msg is constructed down here because `from` is being copied in the constructor
|
|
|
|
const msg = new Msg({
|
|
|
|
type: data.type,
|
|
|
|
time: data.time,
|
|
|
|
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 21:46:22 +02:00
|
|
|
// Self messages in channels are never highlighted
|
2016-03-08 07:50:52 +01:00
|
|
|
// Non-self messages are highlighted as soon as the nick is detected
|
2017-07-28 10:53:36 +02:00
|
|
|
if (!msg.highlight && !msg.self) {
|
|
|
|
msg.highlight = network.highlightRegex.test(data.message);
|
2019-01-16 10:23:12 +01: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 10:23:12 +01:00
|
|
|
}
|
2016-04-22 18:38:59 +02:00
|
|
|
}
|
2014-09-21 18:46:43 +02: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 10:46:39 +02:00
|
|
|
if (data.group) {
|
|
|
|
msg.statusmsgGroup = data.group;
|
|
|
|
}
|
|
|
|
|
2017-11-14 23:36:45 +01:00
|
|
|
let match;
|
2018-02-20 08:28:04 +01:00
|
|
|
|
2017-11-14 23:36:45 +01:00
|
|
|
while ((match = nickRegExp.exec(data.message))) {
|
2017-11-27 22:58:10 +01:00
|
|
|
if (chan.findUser(match[1])) {
|
2017-07-28 10:53:36 +02:00
|
|
|
msg.users.push(match[1]);
|
2017-11-14 23:36:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-18 00:59:49 +01:00
|
|
|
// No prefetch URLs unless are simple MESSAGE or ACTION types
|
2017-11-22 07:39:32 +01:00
|
|
|
if ([Msg.Type.MESSAGE, Msg.Type.ACTION].includes(data.type)) {
|
2020-07-22 17:28:12 +02:00
|
|
|
LinkPrefetch(client, chan, msg, cleanMessage);
|
2016-12-18 00:59:49 +01:00
|
|
|
}
|
2017-07-19 07:26:29 +02:00
|
|
|
|
2017-07-28 10:53:36 +02:00
|
|
|
chan.pushMessage(client, msg, !msg.self);
|
2017-07-10 21:47:03 +02:00
|
|
|
|
|
|
|
// Do not send notifications for messages older than 15 minutes (znc buffer for example)
|
2017-07-28 10:53:36 +02:00
|
|
|
if (msg.highlight && (!data.time || data.time > Date.now() - 900000)) {
|
2017-09-03 17:57:07 +02:00
|
|
|
let title = chan.name;
|
2020-07-22 17:28:12 +02:00
|
|
|
let body = cleanMessage;
|
2017-07-10 21:47:03 +02:00
|
|
|
|
2018-03-09 10:01:47 +01:00
|
|
|
if (msg.type === Msg.Type.ACTION) {
|
|
|
|
// For actions, do not include colon in the message
|
|
|
|
body = `${data.nick} ${body}`;
|
|
|
|
} else if (chan.type !== Chan.Type.QUERY) {
|
|
|
|
// In channels, prepend sender nickname to the message
|
2017-09-03 17:57:07 +02: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 11:33:59 +02:00
|
|
|
title += ` (${chan.highlight} ${
|
|
|
|
chan.type === Chan.Type.QUERY ? "new message" : "mention"
|
|
|
|
}${chan.highlight > 1 ? "s" : ""})`;
|
2017-09-03 17:57:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (chan.highlight > 1) {
|
2019-07-17 11:33:59 +02:00
|
|
|
body += `\n\n… and ${chan.highlight - 1} other message${
|
|
|
|
chan.highlight > 2 ? "s" : ""
|
|
|
|
}`;
|
2017-07-10 21:47:03 +02:00
|
|
|
}
|
|
|
|
|
2019-07-17 11:33:59 +02:00
|
|
|
client.manager.webPush.push(
|
|
|
|
client,
|
|
|
|
{
|
|
|
|
type: "notification",
|
|
|
|
chanId: chan.id,
|
|
|
|
timestamp: data.time || Date.now(),
|
|
|
|
title: title,
|
|
|
|
body: body,
|
|
|
|
},
|
|
|
|
true
|
|
|
|
);
|
2017-07-10 21:47:03 +02:00
|
|
|
}
|
2019-11-04 10:21:05 +01:00
|
|
|
|
|
|
|
// Keep track of all mentions in channels for this client
|
|
|
|
if (msg.highlight && chan.type === Chan.Type.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);
|
|
|
|
}
|
|
|
|
}
|
2016-03-07 22:09:42 +01:00
|
|
|
}
|
2014-09-13 23:29:45 +02:00
|
|
|
};
|