diff --git a/src/context.rs b/src/context.rs index 954d2f4..e4a310b 100644 --- a/src/context.rs +++ b/src/context.rs @@ -45,4 +45,8 @@ impl IrcContext { pub fn mode(&mut self, channel: &str, modes: &str) { self.queue(&format!("MODE {} {}", channel, modes)) } + + pub fn who(&mut self, channel: &str) { + self.queue(&format!("WHO {}", channel)) + } } diff --git a/src/irc_command.rs b/src/irc_command.rs index 8c359ad..e126060 100644 --- a/src/irc_command.rs +++ b/src/irc_command.rs @@ -6,7 +6,7 @@ macro_rules! make_irc_command_enum { ($($variant:ident: $value:expr),+) => { #[allow(non_camel_case_types)] - #[derive(Debug)] + #[derive(Debug, PartialEq, Eq, Hash)] pub enum IrcCommand { UNKNOWN, $($variant),+ diff --git a/src/lib.rs b/src/lib.rs index 7d33159..2a07d38 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -194,6 +194,7 @@ pub struct Irc { default_system: Option, invalid_system: Option, systems: HashMap, + event_systems: HashMap, tasks: Vec<(Duration, StoredSystem)>, factory: Arc>, } @@ -220,6 +221,7 @@ impl Irc { default_system: None, invalid_system: None, systems: HashMap::default(), + event_systems: HashMap::default(), tasks: Vec::new(), factory: Arc::new(RwLock::new(Factory::default())), }) @@ -235,6 +237,16 @@ impl Irc { self } + pub async fn add_event_system System + Send + Sync + 'static>( + &mut self, + evt: IrcCommand, + system: impl for<'a> IntoSystem, + ) -> &mut Self { + self.event_systems + .insert(evt, Box::new(system.into_system())); + self + } + pub async fn add_default_system System + Send + Sync + 'static>( &mut self, system: impl for<'a> IntoSystem, @@ -385,6 +397,7 @@ impl Irc { } async fn handle_message<'a>(&mut self, message: &'a IrcMessage<'a>) { + self.run_event_system(message).await; match message.command { IrcCommand::PING => self.event_ping(&message.parameters[0]).await, IrcCommand::RPL_WELCOME => self.event_welcome(&message.parameters[1..].join(" ")).await, @@ -468,6 +481,18 @@ impl Irc { ) } + pub async fn run_event_system<'a>(&mut self, message: &'a IrcMessage<'a>) { + let Some(system) = self.event_systems.get_mut(&message.command) else { return; }; + + system.run( + &message.prefix.clone().unwrap_or_default(), + "", + &message.parameters, + &mut *self.context.write().await, + &mut *self.factory.write().await, + ); + } + pub async fn run_default_system<'a>( &mut self, prefix: &'a IrcPrefix<'a>, diff --git a/src/system_params.rs b/src/system_params.rs index bf82a3e..6f166c3 100644 --- a/src/system_params.rs +++ b/src/system_params.rs @@ -137,6 +137,7 @@ impl<'a> SystemParam for Channel<'a> { } } +#[derive(Debug)] pub struct AnyArguments<'a>(&'a [&'a str]); impl<'a> Deref for AnyArguments<'a> {