Skip to content

Commit

Permalink
Consistently access TargetParameter by pattern-matching
Browse files Browse the repository at this point in the history
This is already done this way in the MODE handler, and it's probably
better to always check for exhaustiveness instead of chaining 'if' blocks
  • Loading branch information
progval authored and spb committed Sep 3, 2023
1 parent 0c6e3e3 commit 2b2ad5f
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 34 deletions.
20 changes: 11 additions & 9 deletions sable_ircd/src/command/handlers/notice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,18 @@ async fn handle_notice(
return Ok(());
}

if let Some(user) = target.user() {
if user.is_alias_user().is_some() {
// This is a notice which doesn't expect a response; drop it
return Ok(());
match &target {
TargetParameter::User(user) => {
if user.is_alias_user().is_some() {
// This is a notice which doesn't expect a response; drop it
return Ok(());
}
}
}
if let Some(channel) = target.channel() {
if server.policy().can_send(&source, &channel, msg).is_err() {
// Silent error, see above
return Ok(());
TargetParameter::Channel(channel) => {
if server.policy().can_send(&source, &channel, msg).is_err() {
// Silent error, see above
return Ok(());
}
}
}

Expand Down
24 changes: 13 additions & 11 deletions sable_ircd/src/command/handlers/privmsg.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use super::*;
use sable_network::network::config::AliasUser;

#[command_handler("PRIVMSG")]
async fn handle_privmsg(
Expand All @@ -13,19 +14,20 @@ async fn handle_privmsg(
return numeric_error!(NoTextToSend);
}

if let Some(user) = target.user() {
if let Some(alias) = user.is_alias_user() {
return super::services::dispatch_alias_command(cmd, &user, &alias.command_alias, msg)
.await;
}
match &target {
TargetParameter::User(user) => {
if let Some(AliasUser { command_alias, .. }) = user.is_alias_user() {
return super::services::dispatch_alias_command(cmd, &user, &command_alias, msg)
.await;
}

if let Some(away_reason) = user.away_reason() {
response.numeric(make_numeric!(Away, &user, away_reason));
if let Some(away_reason) = user.away_reason() {
response.numeric(make_numeric!(Away, &user, away_reason));
}
}
TargetParameter::Channel(channel) => {
server.policy().can_send(&source, &channel, msg)?;
}
}

if let Some(channel) = target.channel() {
server.policy().can_send(&source, &channel, msg)?;
}

let details = event::details::NewMessage {
Expand Down
14 changes: 0 additions & 14 deletions sable_ircd/src/command/plumbing/target_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,6 @@ pub enum TargetParameter<'a> {
}

impl TargetParameter<'_> {
pub fn user(&self) -> Option<&wrapper::User> {
match self {
Self::User(u) => Some(&u),
Self::Channel(_) => None,
}
}

pub fn channel(&self) -> Option<&wrapper::Channel> {
match self {
Self::User(_) => None,
Self::Channel(c) => Some(&c),
}
}

pub fn object_id(&self) -> ObjectId {
match self {
Self::User(u) => u.id().into(),
Expand Down

0 comments on commit 2b2ad5f

Please sign in to comment.