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

Split handle_user_mode() out of dispatch function #114

Merged
merged 1 commit into from
Apr 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading