hardlounge/server/plugins/irc-events/whois.ts
Reto Brunner fade6a8d2e network: add getLobby accessor
This documents what we actually want and allows us to shift the
logic to the network
2023-02-27 18:30:33 +01:00

63 lines
1.4 KiB
TypeScript

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.getLobby();
} 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);
}
};