hardlounge/src/plugins/irc-events/message.js

136 lines
3.5 KiB
JavaScript
Raw Normal View History

"use strict";
const Chan = require("../../models/chan");
const Msg = require("../../models/msg");
const LinkPrefetch = require("./link");
const cleanIrcMessage = require("../../../client/js/libs/handlebars/ircmessageparser/cleanIrcMessage");
2014-09-13 21:29:45 +00:00
module.exports = function(irc, network) {
const client = this;
irc.on("notice", function(data) {
// Some servers send notices without any nickname
if (!data.nick) {
data.from_server = true;
data.nick = network.host;
}
data.type = Msg.Type.NOTICE;
handleMessage(data);
});
2014-09-27 15:46:32 +00:00
irc.on("action", function(data) {
data.type = Msg.Type.ACTION;
handleMessage(data);
});
irc.on("privmsg", function(data) {
data.type = Msg.Type.MESSAGE;
handleMessage(data);
});
irc.on("wallops", function(data) {
data.from_server = true;
data.type = Msg.Type.NOTICE;
handleMessage(data);
});
function handleMessage(data) {
2016-10-09 08:54:44 +00:00
let chan;
let user;
2016-10-09 08:54:44 +00:00
let highlight = false;
const self = data.nick === irc.user.nick;
// Server messages go to server window, no questions asked
if (data.from_server) {
chan = network.channels[0];
user = chan.getUser(data.nick);
} else {
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 === Msg.Type.NOTICE) {
chan = network.channels[0];
} else {
chan = new Chan({
type: Chan.Type.QUERY,
name: target,
});
network.channels.push(chan);
client.emit("join", {
network: network.id,
chan: chan,
});
}
}
user = chan.getUser(data.nick);
// Query messages (unless self) always highlight
if (chan.type === Chan.Type.QUERY) {
highlight = !self;
} else if (chan.type === Chan.Type.CHANNEL) {
user.lastMessage = data.time || Date.now();
}
2014-09-13 21:29:45 +00:00
}
// Self messages in channels are never highlighted
// Non-self messages are highlighted as soon as the nick is detected
if (!highlight && !self) {
2016-05-12 11:15:38 +00:00
highlight = network.highlightRegex.test(data.message);
}
const msg = new Msg({
type: data.type,
2016-03-12 09:36:55 +00:00
time: data.time,
from: user,
2016-03-15 09:41:11 +00:00
text: data.message,
2016-02-23 10:38:51 +00:00
self: self,
highlight: highlight,
2014-09-13 21:29:45 +00:00
});
// No prefetch URLs unless are simple MESSAGE or ACTION types
if ([Msg.Type.MESSAGE, Msg.Type.ACTION].indexOf(data.type) !== -1) {
LinkPrefetch(client, chan, msg);
}
chan.pushMessage(client, msg, !self);
2017-07-10 19:47:03 +00:00
// Do not send notifications for messages older than 15 minutes (znc buffer for example)
if (highlight && (!data.time || data.time > Date.now() - 900000)) {
2017-09-03 15:57:07 +00:00
let title = chan.name;
let body = cleanIrcMessage(data.message);
2017-07-10 19:47:03 +00:00
2017-09-03 15:57:07 +00:00
// In channels, prepend sender nickname to the message
2017-07-10 19:47:03 +00:00
if (chan.type !== Chan.Type.QUERY) {
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) {
title += ` (${chan.highlight} ${chan.type === Chan.Type.QUERY ? "new message" : "mention"}${chan.highlight > 1 ? "s" : ""})`;
}
if (chan.highlight > 1) {
body += `\n\n… and ${chan.highlight - 1} other message${chan.highlight > 2 ? "s" : ""}`;
2017-07-10 19:47:03 +00:00
}
client.manager.webPush.push(client, {
type: "notification",
chanId: chan.id,
timestamp: data.time || Date.now(),
2017-09-03 15:57:07 +00:00
title: title,
body: body,
2017-07-10 19:47:03 +00:00
}, true);
}
}
2014-09-13 21:29:45 +00:00
};