commit 821926312b1265978bf4a8deb200d7c930d194e1
parent d6286f018a2635329db85eebda8153b6f5e7bc80
Author: donicrosby <donicrosby1995@gmail.com>
Date: Fri, 22 Oct 2021 18:02:47 -0400
Squashed commit of the following:
commit 228716ef5f4f035f39ec4fb8b3decf7338c4dc6a
Author: donicrosby <donicrosby1995@gmail.com>
Date: Fri Oct 22 17:36:56 2021 -0400
Wrong Regex group selected...
commit 83986612c3a274e20972dd7714d74d89fa398b96
Author: donicrosby <donicrosby1995@gmail.com>
Date: Fri Oct 22 17:18:05 2021 -0400
Command now have a regex that checks for !cmd
This fixes a bug where the messages "!cmd" and "cmd" would trigger the bot.
This would cause unwated spam and triggering of the bot
Diffstat:
1 file changed, 44 insertions(+), 36 deletions(-)
diff --git a/mrsbfh-macros/src/lib.rs b/mrsbfh-macros/src/lib.rs
@@ -229,41 +229,41 @@ pub fn autojoin(_: TokenStream, input: TokenStream) -> TokenStream {
if method.sig.ident == "on_stripped_state_member" {
let original = method.block.clone();
let new_block = syn::parse_quote! {
- {
- #original
+ {
+ #original
- // Autojoin logic
- if room_member.state_key != self.client.user_id().await.unwrap() {
- warn!("Got invite that isn't for us");
- return;
- }
- if let matrix_sdk::room::Room::Invited(room) = room {
- info!("Autojoining room {}", room.room_id());
- let mut delay = 2;
-
- while let Err(err) = room.accept_invitation().await {
- // retry autojoin due to synapse sending invites, before the
- // invited user can join for more information see
- // https://github.com/matrix-org/synapse/issues/4345
- error!(
- "Failed to join room {} ({:?}), retrying in {}s",
- room.room_id(),
- err,
- delay
- );
-
- tokio::time::sleep(tokio::time::Duration::from_secs(delay)).await;
- delay *= 2;
-
- if delay > 3600 {
- error!("Can't join room {} ({:?})", room.room_id(), err);
- break;
- }
+ // Autojoin logic
+ if room_member.state_key != self.client.user_id().await.unwrap() {
+ warn!("Got invite that isn't for us");
+ return;
+ }
+ if let matrix_sdk::room::Room::Invited(room) = room {
+ info!("Autojoining room {}", room.room_id());
+ let mut delay = 2;
+
+ while let Err(err) = room.accept_invitation().await {
+ // retry autojoin due to synapse sending invites, before the
+ // invited user can join for more information see
+ // https://github.com/matrix-org/synapse/issues/4345
+ error!(
+ "Failed to join room {} ({:?}), retrying in {}s",
+ room.room_id(),
+ err,
+ delay
+ );
+
+ tokio::time::sleep(tokio::time::Duration::from_secs(delay)).await;
+ delay *= 2;
+
+ if delay > 3600 {
+ error!("Can't join room {} ({:?})", room.room_id(), err);
+ break;
}
- info!("Successfully joined room {}", room.room_id());
}
+ info!("Successfully joined room {}", room.room_id());
}
- };
+ }
+ };
method.block = new_block;
}
}
@@ -334,17 +334,24 @@ pub fn commands(_: TokenStream, input: TokenStream) -> TokenStream {
let cloned_client = self.client.clone();
tokio::spawn(async move {
let whitespace_deduplicator_magic = regex::Regex::new(r"\s+").unwrap();
+ let command_matcher_magic = regex::Regex::new(r"!([\w-]+)").unwrap();
let normalized_body = whitespace_deduplicator_magic.replace_all(&msg_body, " ");
let mut split = msg_body.split_whitespace();
- let command_raw = split.next().expect("This is not a command");
- let command = command_raw.to_lowercase();
- info!("Got command: {}", command);
-
+ let command_raw = split.next().expect("This is not a command").to_lowercase();
+ let command = command_matcher_magic.captures(command_raw.as_str())
+ .map_or(String::new(), |caps| {
+ caps.get(1)
+ .map_or(String::new(),
+ |m| String::from(m.as_str()))
+ });
+ if !command.is_empty() {
+ info!("Got command: {}", command);
+ }
// Make sure this is immutable
let args: Vec<&str> = split.collect();
if let Err(e) = match_command(
- command.replace("!", "").as_str(),
+ command.as_str(),
cloned_client.clone(),
cloned_config.clone(),
tx,
@@ -355,6 +362,7 @@ pub fn commands(_: TokenStream, input: TokenStream) -> TokenStream {
{
error!("{}", e);
}
+
});
while let Some(v) = rx.recv().await {