thelounge/src/plugins/irc-events/ctcp.js

92 lines
2.2 KiB
JavaScript
Raw Normal View History

"use strict";
const _ = require("lodash");
2017-12-31 10:20:20 +01:00
const Helper = require("../../helper");
2017-07-14 19:40:09 +02:00
const Msg = require("../../models/msg");
const User = require("../../models/user");
2017-12-31 10:20:20 +01:00
const pkg = require("../../../package.json");
const ctcpResponses = {
2019-07-17 11:33:59 +02:00
CLIENTINFO: () =>
2020-05-11 21:39:17 +02:00
Object.getOwnPropertyNames(ctcpResponses)
2019-07-17 11:33:59 +02:00
.filter((key) => key !== "CLIENTINFO" && typeof ctcpResponses[key] === "function")
.join(" "),
2017-12-31 10:20:20 +01:00
PING: ({message}) => message.substring(5),
SOURCE: () => pkg.repository.url,
VERSION: () => pkg.name + " " + Helper.getVersion() + " -- " + pkg.homepage,
};
module.exports = function (irc, network) {
2017-07-14 19:40:09 +02:00
const client = this;
const lobby = network.channels[0];
irc.on("ctcp response", function (data) {
const shouldIgnore = network.ignoreList.some(function (entry) {
return Helper.compareHostmask(entry, data);
});
if (shouldIgnore) {
return;
}
2017-07-14 19:40:09 +02:00
let chan = network.getChannel(data.nick);
if (typeof chan === "undefined") {
chan = lobby;
}
2017-07-14 19:40:09 +02:00
const msg = new Msg({
type: Msg.Type.CTCP,
time: data.time,
from: chan.getUser(data.nick),
ctcpMessage: data.message,
});
chan.pushMessage(client, msg, true);
});
2014-09-03 23:43:27 +02:00
// Limit requests to a rate of one per second max
2019-07-17 11:33:59 +02:00
irc.on(
"ctcp request",
_.throttle(
(data) => {
// Ignore echoed ctcp requests that aren't targeted at us
// See https://github.com/kiwiirc/irc-framework/issues/225
if (
data.nick === irc.user.nick &&
data.nick !== data.target &&
network.irc.network.cap.isEnabled("echo-message")
) {
return;
}
const shouldIgnore = network.ignoreList.some(function (entry) {
2019-07-17 11:33:59 +02:00
return Helper.compareHostmask(entry, data);
});
2019-07-17 11:33:59 +02:00
if (shouldIgnore) {
return;
}
const target = data.from_server ? data.hostname : data.nick;
2019-07-17 11:33:59 +02:00
const response = ctcpResponses[data.type];
2017-12-31 10:20:20 +01:00
2019-07-17 11:33:59 +02:00
if (response) {
irc.ctcpResponse(target, data.type, response(data));
2019-07-17 11:33:59 +02:00
}
2019-07-17 11:33:59 +02:00
// Let user know someone is making a CTCP request against their nick
const msg = new Msg({
type: Msg.Type.CTCP_REQUEST,
time: data.time,
from: new User({nick: target}),
2019-07-17 11:33:59 +02:00
hostmask: data.ident + "@" + data.hostname,
ctcpMessage: data.message,
});
lobby.pushMessage(client, msg, true);
},
1000,
{trailing: false}
)
);
2014-09-03 23:43:27 +02:00
};