Skip to content

Commit

Permalink
Add support for /whois command
Browse files Browse the repository at this point in the history
Add support for the /whois command using the raw_msg function on the
connection for the particular server.
  • Loading branch information
prateeknischal committed Jul 3, 2021
1 parent c353066 commit cbdc9cc
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
5 changes: 5 additions & 0 deletions crates/libtiny_client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,11 @@ impl Client {
self.state.is_nick_accepted()
}

/// Query the server for a nick to get more information about the user
pub fn whois(&mut self, nick: &str) {
self.raw_msg(&format!("WHOIS {}", nick));
}

/// Send a message directly to the server. "\r\n" suffix is added by this method.
pub fn raw_msg(&mut self, msg: &str) {
self.msg_chan
Expand Down
44 changes: 43 additions & 1 deletion crates/tiny/src/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ fn find_client<'a>(clients: &'a mut Vec<Client>, serv_name: &str) -> Option<&'a

////////////////////////////////////////////////////////////////////////////////////////////////////

static CMDS: [&Cmd; 9] = [
static CMDS: [&Cmd; 10] = [
&AWAY_CMD,
&CLOSE_CMD,
&CONNECT_CMD,
Expand All @@ -106,6 +106,7 @@ static CMDS: [&Cmd; 9] = [
&MSG_CMD,
&NAMES_CMD,
&NICK_CMD,
&WHOIS_CMD,
&HELP_CMD,
];

Expand Down Expand Up @@ -511,6 +512,47 @@ fn nick(args: CmdArgs) {
}
}

////////////////////////////////////////////////////////////////////////////////////////////////////

static WHOIS_CMD: Cmd = Cmd {
name: "whois",
cmd_fn: whois,
description: "whois lookup for a user",
usage: "`/whois <nick>`",
};

fn whois(args: CmdArgs) {
let CmdArgs {
args,
ui,
clients,
src,
..
} = args;

let words: Vec<&str> = args.split_whitespace().collect();

// Allow lookup for only one user at a time
if let Some(client) = find_client(clients, src.serv_name()) {
match words.get(0) {
Some(word) => client.whois(&word),
None => {
ui.add_client_err_msg(
&format!("Usage: {}", WHOIS_CMD.usage),
&MsgTarget::CurrentTab,
);
}
};
} else {
ui.add_client_err_msg(
&format!("Usage: {}", WHOIS_CMD.usage),
&MsgTarget::CurrentTab,
);
}
}

////////////////////////////////////////////////////////////////////////////////////////////////////

static HELP_CMD: Cmd = Cmd {
name: "help",
cmd_fn: help,
Expand Down

0 comments on commit cbdc9cc

Please sign in to comment.