Remove whois irc-event event handler, send whois/whowas response to current buffer

This commit is contained in:
Max Leiter 2022-07-24 21:25:49 -07:00
parent 57ed37c1fd
commit a38096c752
No known key found for this signature in database
GPG key ID: A3512F2F2F17EBDA
3 changed files with 68 additions and 71 deletions

View file

@ -47,7 +47,6 @@ const events = [
"sasl",
"topic",
"welcome",
"whois",
];
type ClientPushSubscription = {

View file

@ -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;
}
};

View file

@ -1,62 +0,0 @@
import {IrcEventHandler} from "../../client";
import {ChanType} from "../../models/chan";
import Msg, {MessageType} from "../../models/msg";
export default <IrcEventHandler>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);
}
};