Skip to content

Commit

Permalink
Implement USERHOST
Browse files Browse the repository at this point in the history
  • Loading branch information
progval authored and spb committed Apr 14, 2024
1 parent 12111eb commit 97f3fc7
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 1 deletion.
45 changes: 45 additions & 0 deletions sable_ircd/src/command/handlers/userhost.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
use super::*;
use itertools::Itertools;

/// "The USERHOST command takes up to five nicknames, each a separate parameters."
/// -- https://modern.ircdocs.horse/#userhost-message
const MAX_TARGETS: usize = 5;

fn away_char(user: &wrapper::User) -> char {
if user.away_reason().is_some() {
'-'
} else {
'+'
}
}

#[command_handler("USERHOST")]
/// Syntax: USERHOST <nickname>{ <nickname>}
fn userhost_handler(
response: &dyn CommandResponse,
network: &Network,
args: ArgList,
) -> CommandResult {
if args.is_empty() {
Err(CommandError::NotEnoughParameters)
} else {
let reply = args
.iter()
.take(MAX_TARGETS)
.flat_map(Nickname::convert)
.flat_map(|nick| network.user_by_nick(&nick))
.map(|user| {
format!(
"{}={}{}@{}",
user.nick(),
away_char(&user),
user.user(),
user.visible_host()
)
})
.join(" ");

response.numeric(make_numeric!(Userhost, &reply));
Ok(())
}
}
1 change: 1 addition & 0 deletions sable_ircd/src/command/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ mod handlers {
mod rename;
mod topic;
mod user;
mod userhost;
mod who;
mod whois;

Expand Down
6 changes: 5 additions & 1 deletion sable_ircd/src/command/plumbing/argument_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,12 @@ impl<'a> ArgListIter<'a> {
pub fn peek(&self) -> Option<&'a str> {
self.list.get(self.index).map(AsRef::as_ref)
}
}

impl<'a> Iterator for ArgListIter<'a> {
type Item = &'a str;

pub fn next(&mut self) -> Option<&'a str> {
fn next(&mut self) -> Option<&'a str> {
let idx = self.index;
self.index += 1;
self.list.get(idx).map(AsRef::as_ref)
Expand Down
1 change: 1 addition & 0 deletions sable_ircd/src/messages/numeric.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ define_messages! {

221(UserModeIs) => { (modestring: &str) => ":{modestring}" },
301(Away) => { (nick: &User.nick(), reason: &AwayReason) => "{nick} :{reason}" },
302(Userhost) => { (reply: &str) => ":{reply}" },
305(Unaway) => { () => ":You are no longer marked as being away" },
306(NowAway) => { () => ":You have been marked as being away" },
311(WhoisUser) => { (nick: &User.nick(), user=nick.user(), host=nick.visible_host(), realname=nick.realname())
Expand Down

0 comments on commit 97f3fc7

Please sign in to comment.