Skip to content

Commit

Permalink
Split handle_user_mode() out of dispatch function
Browse files Browse the repository at this point in the history
It's more consistent this way.
  • Loading branch information
progval authored and spb committed Apr 18, 2024
1 parent fb38e57 commit b4d95c0
Showing 1 changed file with 67 additions and 55 deletions.
122 changes: 67 additions & 55 deletions sable_ircd/src/command/handlers/mode.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use super::*;

#[command_handler("MODE")]
async fn handle_user(
async fn handle_mode(
server: &ClientServer,
source: UserSource<'_>,
cmd: &dyn Command,
Expand All @@ -15,73 +15,85 @@ async fn handle_user(
handle_channel_mode(server, &source, cmd, response, chan, mode_str, args).await
}
TargetParameter::User(user) => {
if source.id() != user.id() {
return numeric_error!(CantChangeOtherUserMode);
}
handle_user_mode(server, &source, cmd, response, user, mode_str, args).await
}
}
}

let mode = source.mode();
async fn handle_user_mode(
server: &ClientServer,
source: &wrapper::User<'_>,
cmd: &dyn Command,
response: &dyn CommandResponse,
user: wrapper::User<'_>,
mode_str: Option<&str>,
_args: ArgList<'_>,
) -> CommandResult {
if source.id() != user.id() {
return numeric_error!(CantChangeOtherUserMode);
}

let mut sent_unknown = false;
let mut added = UserModeSet::new();
let mut removed = UserModeSet::new();
let mode = source.mode();

enum Direction {
Add,
Rem,
Query,
}
let mut dir = Direction::Query;
let mut sent_unknown = false;
let mut added = UserModeSet::new();
let mut removed = UserModeSet::new();

let Some(mode_str) = mode_str else {
response.numeric(make_numeric!(UserModeIs, &mode.format()));
return Ok(());
};
enum Direction {
Add,
Rem,
Query,
}
let mut dir = Direction::Query;

for c in mode_str.chars() {
match c {
'+' => {
dir = Direction::Add;
}
'-' => {
dir = Direction::Rem;
}
'=' => {
dir = Direction::Query;
let Some(mode_str) = mode_str else {
response.numeric(make_numeric!(UserModeIs, &mode.format()));
return Ok(());
};

for c in mode_str.chars() {
match c {
'+' => {
dir = Direction::Add;
}
'-' => {
dir = Direction::Rem;
}
'=' => {
dir = Direction::Query;
}
_ => {
if let Some(flag) = UserModeSet::flag_for(c) {
if server.policy().can_set_umode(&source, flag).is_err() {
continue;
}
_ => {
if let Some(flag) = UserModeSet::flag_for(c) {
if server.policy().can_set_umode(&source, flag).is_err() {
continue;
}

match dir {
Direction::Add => {
added |= flag;
}
Direction::Rem => {
removed |= flag;
}
_ => {}
}
} else if !sent_unknown {
response.numeric(make_numeric!(UnknownMode, c));
sent_unknown = true;
match dir {
Direction::Add => {
added |= flag;
}
Direction::Rem => {
removed |= flag;
}
_ => {}
}
} else if !sent_unknown {
response.numeric(make_numeric!(UnknownMode, c));
sent_unknown = true;
}
}
if !added.is_empty() || !removed.is_empty() {
let detail = event::UserModeChange {
changed_by: source.id().into(),
added,
removed,
};
cmd.new_event_with_response(source.id(), detail).await;
}

Ok(())
}
}
if !added.is_empty() || !removed.is_empty() {
let detail = event::UserModeChange {
changed_by: source.id().into(),
added,
removed,
};
cmd.new_event_with_response(source.id(), detail).await;
}

Ok(())
}

async fn handle_channel_mode(
Expand Down

0 comments on commit b4d95c0

Please sign in to comment.