updates. (acidvegas level commit msg)

This commit is contained in:
wrk 2023-05-30 23:56:59 +02:00
parent 1a51ebcd44
commit bebde517ec
4 changed files with 94 additions and 21 deletions

View File

@ -64,15 +64,12 @@ impl Irc {
channel: &str, channel: &str,
message: &str, message: &str,
) { ) {
let context = self.context.read().await; let config = self.context.read().await.config.clone();
if channel == &context.config.nick { if channel == &config.nick {
if message.ends_with(&format!( if message.ends_with(&format!("\x02{}\x02 isn't registered.", config.nick)) {
"\x02{}\x02 isn't registered.", let nickserv_pass = config.nickserv_pass.as_ref().unwrap().to_string();
context.config.nick let nickserv_email = config.nickserv_email.as_ref().unwrap().to_string();
)) {
let nickserv_pass = context.config.nickserv_pass.as_ref().unwrap().to_string();
let nickserv_email = context.config.nickserv_email.as_ref().unwrap().to_string();
info!("Registering to nickserv now."); info!("Registering to nickserv now.");
let mut context = self.context.write().await; let mut context = self.context.write().await;
context.privmsg( context.privmsg(
@ -106,13 +103,14 @@ impl Irc {
channel: &str, channel: &str,
message: &str, message: &str,
) { ) {
let mut elements;
let sys_name; let sys_name;
{ {
let context = self.context.read().await; let context = self.context.read().await;
if !message.starts_with(&context.config.cmdkey) { if !message.starts_with(&context.config.cmdkey) {
return; return;
} }
let mut elements = message.split_whitespace(); elements = message.split_whitespace();
sys_name = elements.next().unwrap()[1..].to_owned(); sys_name = elements.next().unwrap()[1..].to_owned();
if context.is_owner(prefix) && sys_name == "raw" { if context.is_owner(prefix) && sys_name == "raw" {
@ -130,7 +128,9 @@ impl Irc {
if !context.systems.contains_key(&sys_name) { if !context.systems.contains_key(&sys_name) {
return; return;
} }
let response = context.run_system(prefix, &sys_name).await; let response = context
.run_system(prefix, elements.collect(), &sys_name)
.await;
if response.0.is_none() { if response.0.is_none() {
return; return;

View File

@ -139,13 +139,13 @@ impl<'a> From<&'a str> for IrcMessage<'a> {
} }
} }
#[derive(Serialize, Deserialize)] #[derive(Clone, Serialize, Deserialize)]
pub struct IrcConfig { pub struct IrcConfig {
host: String, host: String,
port: u16, port: u16,
ssl: bool, ssl: bool,
channels: HashSet<String>,
nick: String, nick: String,
channels: HashSet<String>,
user: String, user: String,
real: String, real: String,
nickserv_pass: Option<String>, nickserv_pass: Option<String>,
@ -295,9 +295,14 @@ impl Context {
self self
} }
pub async fn run_system<'a>(&mut self, prefix: &'a IrcPrefix<'a>, name: &str) -> Response { pub async fn run_system<'a>(
&mut self,
prefix: &'a IrcPrefix<'a>,
arguments: Vec<&'a str>,
name: &str,
) -> Response {
let system = self.systems.get_mut(name).unwrap(); let system = self.systems.get_mut(name).unwrap();
system.run(prefix, &mut *self.factory.write().await) system.run(prefix, &arguments, &mut *self.factory.write().await)
} }
pub async fn run_interval_tasks(&mut self, tx: mpsc::Sender<Vec<String>>) { pub async fn run_interval_tasks(&mut self, tx: mpsc::Sender<Vec<String>>) {
@ -313,6 +318,7 @@ impl Context {
user: None, user: None,
host: None, host: None,
}, },
&[],
&mut *fact.write().await, &mut *fact.write().await,
); );
if resp.0.is_none() { if resp.0.is_none() {

View File

@ -8,7 +8,7 @@ pub struct FunctionSystem<Input, F> {
} }
pub trait System { pub trait System {
fn run(&mut self, prefix: &IrcPrefix, factory: &mut Factory) -> Response; fn run(&mut self, prefix: &IrcPrefix, arguments: &[&str], factory: &mut Factory) -> Response;
} }
pub trait IntoSystem<Input> { pub trait IntoSystem<Input> {
@ -29,7 +29,7 @@ macro_rules! impl_system {
FnMut( $($params),* ) -> R + FnMut( $($params),* ) -> R +
FnMut( $(<$params as SystemParam>::Item<'b>),* ) -> R FnMut( $(<$params as SystemParam>::Item<'b>),* ) -> R
{ {
fn run(&mut self, prefix: &IrcPrefix, factory: &mut Factory) -> Response { fn run(&mut self, prefix: &IrcPrefix, arguments: &[&str], factory: &mut Factory) -> Response {
fn call_inner<'a, R: IntoResponse, $($params),*>( fn call_inner<'a, R: IntoResponse, $($params),*>(
mut f: impl FnMut($($params),*) -> R, mut f: impl FnMut($($params),*) -> R,
$($params: $params),* $($params: $params),*
@ -38,7 +38,7 @@ macro_rules! impl_system {
} }
$( $(
let $params = $params::retrieve(prefix, &factory); let $params = $params::retrieve(prefix, arguments, &factory);
)* )*
call_inner(&mut self.f, $($params),*) call_inner(&mut self.f, $($params),*)
@ -74,18 +74,26 @@ impl_system!(T1);
impl_system!(T1, T2); impl_system!(T1, T2);
impl_system!(T1, T2, T3); impl_system!(T1, T2, T3);
impl_system!(T1, T2, T3, T4); impl_system!(T1, T2, T3, T4);
impl_system!(T1, T2, T3, T4, T5);
impl_system!(T1, T2, T3, T4, T5, T6);
impl_system!(T1, T2, T3, T4, T5, T6, T7);
impl_system!(T1, T2, T3, T4, T5, T6, T7, T8);
impl_into_system!(); impl_into_system!();
impl_into_system!(T1); impl_into_system!(T1);
impl_into_system!(T1, T2); impl_into_system!(T1, T2);
impl_into_system!(T1, T2, T3); impl_into_system!(T1, T2, T3);
impl_into_system!(T1, T2, T3, T4); impl_into_system!(T1, T2, T3, T4);
impl_into_system!(T1, T2, T3, T4, T5);
impl_into_system!(T1, T2, T3, T4, T5, T6);
impl_into_system!(T1, T2, T3, T4, T5, T6, T7);
impl_into_system!(T1, T2, T3, T4, T5, T6, T7, T8);
pub(crate) type StoredSystem = Box<dyn for<'a> System + Send + Sync>; pub(crate) type StoredSystem = Box<dyn for<'a> System + Send + Sync>;
pub(crate) trait SystemParam { pub(crate) trait SystemParam {
type Item<'new>; type Item<'new>;
fn retrieve<'r>(prefix: &'r IrcPrefix, factory: &'r Factory) -> Self::Item<'r>; fn retrieve<'r>(prefix: &'r IrcPrefix, arguments: &'r[&'r str], factory: &'r Factory) -> Self::Item<'r>;
} }
#[derive(Clone)] #[derive(Clone)]
@ -119,6 +127,31 @@ impl IntoResponse for Msg {
} }
} }
impl<O, E> IntoResponse for Result<O, E>
where
O: IntoResponse,
E: IntoResponse,
{
fn response(self) -> Response {
match self {
Ok(o) => o.response(),
Err(e) => e.response(),
}
}
}
impl<S> IntoResponse for Option<S>
where
S: IntoResponse,
{
fn response(self) -> Response {
match self {
Some(s) => s.response(),
None => Response(None),
}
}
}
impl<T: std::fmt::Display> IntoResponse for Vec<T> { impl<T: std::fmt::Display> IntoResponse for Vec<T> {
fn response(self) -> Response { fn response(self) -> Response {
Response(Some( Response(Some(

View File

@ -28,7 +28,11 @@ impl<'a, T: 'static> AsRef<T> for Res<'a, T> {
impl<'res, T: 'static> SystemParam for Res<'res, T> { impl<'res, T: 'static> SystemParam for Res<'res, T> {
type Item<'new> = Res<'new, T>; type Item<'new> = Res<'new, T>;
fn retrieve<'r>(_prefix: &'r IrcPrefix, factory: &'r Factory) -> Self::Item<'r> { fn retrieve<'r>(
_prefix: &'r IrcPrefix,
_arguments: &'r [&'r str],
factory: &'r Factory,
) -> Self::Item<'r> {
Res { Res {
value: &factory value: &factory
.resources .resources
@ -73,7 +77,11 @@ impl<'a, T: 'static> AsMut<T> for ResMut<'a, T> {
impl<'res, T: 'static> SystemParam for ResMut<'res, T> { impl<'res, T: 'static> SystemParam for ResMut<'res, T> {
type Item<'new> = ResMut<'new, T>; type Item<'new> = ResMut<'new, T>;
fn retrieve<'r>(_prefix: &'r IrcPrefix, factory: &'r Factory) -> Self::Item<'r> { fn retrieve<'r>(
_prefix: &'r IrcPrefix,
_arguments: &'r [&'r str],
factory: &'r Factory,
) -> Self::Item<'r> {
let const_ptr = &factory.resources as *const HashMap<TypeId, Box<dyn Any + Send + Sync>>; let const_ptr = &factory.resources as *const HashMap<TypeId, Box<dyn Any + Send + Sync>>;
let mut_ptr = const_ptr as *mut HashMap<TypeId, Box<dyn Any>>; let mut_ptr = const_ptr as *mut HashMap<TypeId, Box<dyn Any>>;
let res_mut = unsafe { &mut *mut_ptr }; let res_mut = unsafe { &mut *mut_ptr };
@ -91,7 +99,33 @@ impl<'res, T: 'static> SystemParam for ResMut<'res, T> {
impl<'a> SystemParam for IrcPrefix<'a> { impl<'a> SystemParam for IrcPrefix<'a> {
type Item<'new> = IrcPrefix<'new>; type Item<'new> = IrcPrefix<'new>;
fn retrieve<'r>(prefix: &'r IrcPrefix, _factory: &'r Factory) -> Self::Item<'r> { fn retrieve<'r>(
prefix: &'r IrcPrefix,
_arguments: &'r [&'r str],
_factory: &'r Factory,
) -> Self::Item<'r> {
prefix.clone() prefix.clone()
} }
} }
pub struct Arguments<'a>(&'a [&'a str]);
impl<'a> Deref for Arguments<'a> {
type Target = &'a [&'a str];
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl<'a> SystemParam for Arguments<'a> {
type Item<'new> = Arguments<'new>;
fn retrieve<'r>(
_prefix: &'r IrcPrefix,
arguments: &'r [&'r str],
_factory: &'r Factory,
) -> Self::Item<'r> {
Arguments(arguments)
}
}