Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add /who handling #4547

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 10 additions & 8 deletions client/components/MessageTypes/whois.vue
@@ -1,6 +1,6 @@
<template>
<span class="content">
<p>
<p v-if="message.whois.nick">
<Username :user="{nick: message.whois.nick}" />
<span v-if="message.whois.whowas"> is offline, last information:</span>
</p>
Expand All @@ -11,13 +11,15 @@
<dd>{{ message.whois.account }}</dd>
</template>

<dt>Host mask:</dt>
<dd class="hostmask">
<ParsedMessage
:network="network"
:text="message.whois.ident + '@' + message.whois.hostname"
/>
</dd>
<template v-if="message.whois.ident && message.whois.hostname">
<dt>Host mask:</dt>
<dd class="hostmask">
<ParsedMessage
:network="network"
:text="message.whois.ident + '@' + message.whois.hostname"
/>
</dd>
</template>

<template v-if="message.whois.actual_hostname">
<dt>Actual host:</dt>
Expand Down
1 change: 1 addition & 0 deletions server/plugins/inputs/index.ts
Expand Up @@ -55,6 +55,7 @@ const builtInInputs = [
"raw",
"rejoin",
"topic",
"who",
"whois",
"mute",
];
Expand Down
87 changes: 87 additions & 0 deletions server/plugins/inputs/who.ts
@@ -0,0 +1,87 @@
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 "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",
};

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, why are you doing it this way?
You are creating a mapping that's never used, plus a function to translate it to what you actually want.

Can't we just directly create the mapping we need from single char to field name of TL?

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