2022-06-19 00:25:21 +00:00
|
|
|
import _ from "lodash";
|
|
|
|
import {IrcEventHandler} from "../../client";
|
|
|
|
import Helper from "../../helper";
|
|
|
|
import Msg, {MessageType} from "../../models/msg";
|
|
|
|
import User from "../../models/user";
|
|
|
|
import pkg from "../../../package.json";
|
2017-12-31 09:20:20 +00:00
|
|
|
|
|
|
|
const ctcpResponses = {
|
2019-07-17 09:33:59 +00:00
|
|
|
CLIENTINFO: () =>
|
2020-05-11 19:39:17 +00:00
|
|
|
Object.getOwnPropertyNames(ctcpResponses)
|
2019-07-17 09:33:59 +00:00
|
|
|
.filter((key) => key !== "CLIENTINFO" && typeof ctcpResponses[key] === "function")
|
|
|
|
.join(" "),
|
2022-06-19 00:25:21 +00:00
|
|
|
PING: ({message}: {message: string}) => message.substring(5),
|
2017-12-31 09:20:20 +00:00
|
|
|
SOURCE: () => pkg.repository.url,
|
|
|
|
VERSION: () => pkg.name + " " + Helper.getVersion() + " -- " + pkg.homepage,
|
|
|
|
};
|
2016-03-27 15:57:59 +00:00
|
|
|
|
2022-06-19 00:25:21 +00:00
|
|
|
export default <IrcEventHandler>function (irc, network) {
|
2017-07-14 17:40:09 +00:00
|
|
|
const client = this;
|
2018-01-02 05:29:29 +00:00
|
|
|
const lobby = network.channels[0];
|
2016-03-27 15:57:59 +00:00
|
|
|
|
2020-03-21 20:55:36 +00:00
|
|
|
irc.on("ctcp response", function (data) {
|
|
|
|
const shouldIgnore = network.ignoreList.some(function (entry) {
|
2018-07-01 18:48:37 +00:00
|
|
|
return Helper.compareHostmask(entry, data);
|
|
|
|
});
|
|
|
|
|
|
|
|
if (shouldIgnore) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-07-14 17:40:09 +00:00
|
|
|
let chan = network.getChannel(data.nick);
|
2017-11-10 20:44:14 +00:00
|
|
|
|
2016-03-27 15:57:59 +00:00
|
|
|
if (typeof chan === "undefined") {
|
2018-01-02 05:29:29 +00:00
|
|
|
chan = lobby;
|
2016-03-27 15:57:59 +00:00
|
|
|
}
|
|
|
|
|
2017-07-14 17:40:09 +00:00
|
|
|
const msg = new Msg({
|
2022-06-19 00:25:21 +00:00
|
|
|
type: MessageType.CTCP,
|
2016-03-27 15:57:59 +00:00
|
|
|
time: data.time,
|
2017-11-10 20:44:14 +00:00
|
|
|
from: chan.getUser(data.nick),
|
2017-11-15 06:35:15 +00:00
|
|
|
ctcpMessage: data.message,
|
2016-03-27 15:57:59 +00:00
|
|
|
});
|
2018-06-03 22:47:30 +00:00
|
|
|
chan.pushMessage(client, msg, true);
|
2016-03-27 15:57:59 +00:00
|
|
|
});
|
2014-09-03 21:43:27 +00:00
|
|
|
|
2018-01-03 00:53:42 +00:00
|
|
|
// Limit requests to a rate of one per second max
|
2019-07-17 09:33:59 +00:00
|
|
|
irc.on(
|
|
|
|
"ctcp request",
|
|
|
|
_.throttle(
|
|
|
|
(data) => {
|
2020-01-01 16:06:42 +00:00
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
2020-03-21 20:55:36 +00:00
|
|
|
const shouldIgnore = network.ignoreList.some(function (entry) {
|
2019-07-17 09:33:59 +00:00
|
|
|
return Helper.compareHostmask(entry, data);
|
|
|
|
});
|
2018-07-01 18:48:37 +00:00
|
|
|
|
2019-07-17 09:33:59 +00:00
|
|
|
if (shouldIgnore) {
|
|
|
|
return;
|
|
|
|
}
|
2018-07-01 18:48:37 +00:00
|
|
|
|
2020-05-11 18:55:30 +00:00
|
|
|
const target = data.from_server ? data.hostname : data.nick;
|
2019-07-17 09:33:59 +00:00
|
|
|
const response = ctcpResponses[data.type];
|
2017-12-31 09:20:20 +00:00
|
|
|
|
2019-07-17 09:33:59 +00:00
|
|
|
if (response) {
|
2020-05-11 18:55:30 +00:00
|
|
|
irc.ctcpResponse(target, data.type, response(data));
|
2019-07-17 09:33:59 +00:00
|
|
|
}
|
2018-01-02 05:29:29 +00:00
|
|
|
|
2019-07-17 09:33:59 +00:00
|
|
|
// Let user know someone is making a CTCP request against their nick
|
|
|
|
const msg = new Msg({
|
2022-06-19 00:25:21 +00:00
|
|
|
type: MessageType.CTCP_REQUEST,
|
2019-07-17 09:33:59 +00:00
|
|
|
time: data.time,
|
2020-05-11 18:55:30 +00:00
|
|
|
from: new User({nick: target}),
|
2022-06-19 00:25:21 +00:00
|
|
|
// eslint-disable-next-line @typescript-eslint/restrict-plus-operands
|
2019-07-17 09:33:59 +00:00
|
|
|
hostmask: data.ident + "@" + data.hostname,
|
|
|
|
ctcpMessage: data.message,
|
|
|
|
});
|
|
|
|
lobby.pushMessage(client, msg, true);
|
|
|
|
},
|
|
|
|
1000,
|
|
|
|
{trailing: false}
|
|
|
|
)
|
|
|
|
);
|
2014-09-03 21:43:27 +00:00
|
|
|
};
|