mirror of
https://github.com/thelounge/thelounge.git
synced 2024-06-10 01:32:17 +02:00
96d96d1288
irc-framework sends the WHOX cuhsnfdaor flags and doesn't support receiving others, so we manually remove parameters the user doesn't request. I also added an uncaught exception handler on the server to stop a subset of crashes. The problem is people won't look at their server logs often, but bookworm and I agreed that's better than crashing. When we add an admin role we can report it to them. https://ircv3.net/specs/extensions/whox ref kiwiirc/irc-framework#331
90 lines
1.8 KiB
TypeScript
90 lines
1.8 KiB
TypeScript
import {ClientUser} from "../../../client/js/types";
|
|
import Msg, {MessageType} from "../../models/msg";
|
|
import {PluginInputHandler} from "./index";
|
|
|
|
const commands = ["who"];
|
|
|
|
const parseWhoxResponse = (user: ClientUser, args: string) => {
|
|
const parseFieldNameForLounge = (field: string) => {
|
|
switch (field) {
|
|
case "channel":
|
|
return "channel"; // no-op for now
|
|
case "nickname":
|
|
return "nick";
|
|
case "username":
|
|
return "account";
|
|
case "realname":
|
|
return "real_name";
|
|
case "op":
|
|
return "operator";
|
|
default:
|
|
return field;
|
|
}
|
|
};
|
|
|
|
const whoxFields = {
|
|
c: "channel",
|
|
u: "username",
|
|
h: "hostname",
|
|
s: "server",
|
|
n: "nickname",
|
|
f: "flags",
|
|
a: "account",
|
|
r: "realname",
|
|
};
|
|
|
|
if (!args) {
|
|
return user;
|
|
}
|
|
|
|
const filteredResponse = {};
|
|
|
|
for (const [token, fieldName] of Object.entries(whoxFields)) {
|
|
// TODO: throw on unknown field?
|
|
if (args.includes(token)) {
|
|
const expectedField = parseFieldNameForLounge(fieldName);
|
|
filteredResponse[expectedField] = user[expectedField];
|
|
}
|
|
}
|
|
|
|
return filteredResponse;
|
|
};
|
|
|
|
const input: PluginInputHandler = function ({irc}, chan, cmd, args) {
|
|
if (args.length === 0) {
|
|
return;
|
|
}
|
|
|
|
// We use the callback instead of listening for `wholist` because
|
|
// irc-framework doesn't support filtering WHOX.
|
|
// This has the added benefit of easily showing it in the same buffer
|
|
// as the WHO command.
|
|
irc.who(args[0], (event) => {
|
|
if (!event.users.length) {
|
|
chan.pushMessage(
|
|
this,
|
|
new Msg({
|
|
type: MessageType.ERROR,
|
|
text: `The server returned no matching users for ${args[0]}`,
|
|
})
|
|
);
|
|
}
|
|
|
|
for (const user of event.users) {
|
|
const filteredResponse = parseWhoxResponse(user, args[1]);
|
|
chan.pushMessage(
|
|
this,
|
|
new Msg({
|
|
type: MessageType.WHOIS,
|
|
whois: filteredResponse,
|
|
})
|
|
);
|
|
}
|
|
});
|
|
};
|
|
|
|
export default {
|
|
commands,
|
|
input,
|
|
};
|