From a38096c75252325909d88643c1dab343714476f0 Mon Sep 17 00:00:00 2001 From: Max Leiter Date: Sun, 24 Jul 2022 21:25:49 -0700 Subject: [PATCH] Remove whois irc-event event handler, send whois/whowas response to current buffer --- server/client.ts | 1 - server/plugins/inputs/whois.ts | 76 ++++++++++++++++++++++++++---- server/plugins/irc-events/whois.ts | 62 ------------------------ 3 files changed, 68 insertions(+), 71 deletions(-) delete mode 100644 server/plugins/irc-events/whois.ts diff --git a/server/client.ts b/server/client.ts index 39b8bacb..3255898c 100644 --- a/server/client.ts +++ b/server/client.ts @@ -47,7 +47,6 @@ const events = [ "sasl", "topic", "welcome", - "whois", ]; type ClientPushSubscription = { diff --git a/server/plugins/inputs/whois.ts b/server/plugins/inputs/whois.ts index e4ed953d..5b0002f0 100644 --- a/server/plugins/inputs/whois.ts +++ b/server/plugins/inputs/whois.ts @@ -1,16 +1,76 @@ +import Msg, {MessageType} from "../../models/msg"; import {PluginInputHandler} from "./index"; const commands = ["whois"]; +type DataError = { + error: string; +}; + +type DataToSend = { + whowas: string; + idleTime: number; + logonTime: number; + logon: number; + idle: number; +}; + +const isDataError = (data: any): data is DataError => { + return data.error !== undefined; +}; + +const isDataToSend = (data: any): data is DataToSend => { + return !isDataError(data); +}; + const input: PluginInputHandler = function ({irc}, chan, cmd, args) { - if (args.length === 1) { - // This queries server of the other user and not of the current user, which - // does not know idle time. - // See http://superuser.com/a/272069/208074. - irc.raw("WHOIS", args[0], args[0]); - } else { - // Re-assembling the command parsed in client.js - irc.raw(`${cmd} ${args.join(" ")}`); + const client = this; + const target = args[0]; + const targetNick = args[1] ? args[1] : target; + + const sendToClient = (data: DataError | DataToSend) => { + if (!isDataToSend(data)) { + chan.pushMessage( + client, + new Msg({ + type: MessageType.ERROR, + error: data.error, + nick: targetNick, + }) + ); + } else { + // Absolute datetime in milliseconds since nick is idle + data.idleTime = Date.now() - data.idle * 1000; + // Absolute datetime in milliseconds when nick logged on. + data.logonTime = data.logon * 1000; + chan.pushMessage( + client, + new Msg({ + type: MessageType.WHOIS, + whois: data, + }) + ); + } + }; + + if (!target) { + sendToClient({ + error: `/${cmd} needs a target nick`, + }); + return; + } + + switch (cmd) { + case "whois": + // @ts-ignore + irc.whois(target, targetNick, sendToClient); + break; + case "whowas": + irc.whowas(target, (data: any) => { + data.whowas = true; + sendToClient(data); + }); + break; } }; diff --git a/server/plugins/irc-events/whois.ts b/server/plugins/irc-events/whois.ts deleted file mode 100644 index 9c62e7fb..00000000 --- a/server/plugins/irc-events/whois.ts +++ /dev/null @@ -1,62 +0,0 @@ -import {IrcEventHandler} from "../../client"; -import {ChanType} from "../../models/chan"; - -import Msg, {MessageType} from "../../models/msg"; - -export default function (irc, network) { - const client = this; - - irc.on("whois", handleWhois); - - irc.on("whowas", (data) => { - data.whowas = true; - - handleWhois(data); - }); - - function handleWhois(data) { - let chan = network.getChannel(data.nick); - - if (typeof chan === "undefined") { - // Do not create new windows for errors as they may contain illegal characters - if (data.error) { - chan = network.channels[0]; - } else { - chan = client.createChannel({ - type: ChanType.QUERY, - name: data.nick, - }); - - client.emit("join", { - shouldOpen: true, - network: network.uuid, - chan: chan.getFilteredClone(true), - index: network.addChannel(chan), - }); - chan.loadMessages(client, network); - client.save(); - } - } - - let msg; - - if (data.error) { - msg = new Msg({ - type: MessageType.ERROR, - // eslint-disable-next-line @typescript-eslint/restrict-plus-operands - text: "No such nick: " + data.nick, - }); - } else { - // Absolute datetime in milliseconds since nick is idle - data.idleTime = Date.now() - data.idle * 1000; - // Absolute datetime in milliseconds when nick logged on. - data.logonTime = data.logon * 1000; - msg = new Msg({ - type: MessageType.WHOIS, - whois: data, - }); - } - - chan.pushMessage(client, msg); - } -};