mrsbfh.meta

Issues/PRs archive for MTRNord/mrsbfh
git clone git://archive.git.mtrnord.blog/MTRNord/mrsbfh.meta.git
Log | Files | Refs

4.diff (6505B)


      1 diff --git a/mrsbfh-macros/src/lib.rs b/mrsbfh-macros/src/lib.rs
      2 index f1c1bf9..2179698 100644
      3 --- a/mrsbfh-macros/src/lib.rs
      4 +++ b/mrsbfh-macros/src/lib.rs
      5 @@ -229,41 +229,41 @@ pub fn autojoin(_: TokenStream, input: TokenStream) -> TokenStream {
      6              if method.sig.ident == "on_stripped_state_member" {
      7                  let original = method.block.clone();
      8                  let new_block = syn::parse_quote! {
      9 -                        {
     10 -                            #original
     11 +                    {
     12 +                        #original
     13  
     14 -                            // Autojoin logic
     15 -                            if room_member.state_key != self.client.user_id().await.unwrap() {
     16 -                                warn!("Got invite that isn't for us");
     17 -                                return;
     18 -                            }
     19 -                            if let matrix_sdk::room::Room::Invited(room) = room {
     20 -                                info!("Autojoining room {}", room.room_id());
     21 -                                let mut delay = 2;
     22 -
     23 -                                while let Err(err) = room.accept_invitation().await {
     24 -                                    // retry autojoin due to synapse sending invites, before the
     25 -                                    // invited user can join for more information see
     26 -                                    // https://github.com/matrix-org/synapse/issues/4345
     27 -                                    error!(
     28 -                                        "Failed to join room {} ({:?}), retrying in {}s",
     29 -                                        room.room_id(),
     30 -                                        err,
     31 -                                        delay
     32 -                                    );
     33 -
     34 -                                    tokio::time::sleep(tokio::time::Duration::from_secs(delay)).await;
     35 -                                    delay *= 2;
     36 -
     37 -                                    if delay > 3600 {
     38 -                                        error!("Can't join room {} ({:?})", room.room_id(), err);
     39 -                                        break;
     40 -                                    }
     41 +                        // Autojoin logic
     42 +                        if room_member.state_key != self.client.user_id().await.unwrap() {
     43 +                            warn!("Got invite that isn't for us");
     44 +                            return;
     45 +                        }
     46 +                        if let matrix_sdk::room::Room::Invited(room) = room {
     47 +                            info!("Autojoining room {}", room.room_id());
     48 +                            let mut delay = 2;
     49 +
     50 +                            while let Err(err) = room.accept_invitation().await {
     51 +                                // retry autojoin due to synapse sending invites, before the
     52 +                                // invited user can join for more information see
     53 +                                // https://github.com/matrix-org/synapse/issues/4345
     54 +                                error!(
     55 +                                    "Failed to join room {} ({:?}), retrying in {}s",
     56 +                                    room.room_id(),
     57 +                                    err,
     58 +                                    delay
     59 +                                );
     60 +
     61 +                                tokio::time::sleep(tokio::time::Duration::from_secs(delay)).await;
     62 +                                delay *= 2;
     63 +
     64 +                                if delay > 3600 {
     65 +                                    error!("Can't join room {} ({:?})", room.room_id(), err);
     66 +                                    break;
     67                                  }
     68 -                                info!("Successfully joined room {}", room.room_id());
     69                              }
     70 +                            info!("Successfully joined room {}", room.room_id());
     71                          }
     72 -                    };
     73 +                    }
     74 +                };
     75                  method.block = new_block;
     76              }
     77          }
     78 @@ -334,17 +334,24 @@ pub fn commands(_: TokenStream, input: TokenStream) -> TokenStream {
     79                              let cloned_client = self.client.clone();
     80                              tokio::spawn(async move {
     81                                  let whitespace_deduplicator_magic = regex::Regex::new(r"\s+").unwrap();
     82 +                                let command_matcher_magic = regex::Regex::new(r"!([\w-]+)").unwrap();
     83                                  let normalized_body = whitespace_deduplicator_magic.replace_all(&msg_body, " ");
     84                                  let mut split = msg_body.split_whitespace();
     85  
     86 -                                let command_raw = split.next().expect("This is not a command");
     87 -                                let command = command_raw.to_lowercase();
     88 -                                info!("Got command: {}", command);
     89 -
     90 +                                let command_raw = split.next().expect("This is not a command").to_lowercase();
     91 +                                let command = command_matcher_magic.captures(command_raw.as_str())
     92 +                                                                   .map_or(String::new(), |caps| {
     93 +                                                                        caps.get(1)
     94 +                                                                            .map_or(String::new(),
     95 +                                                                                    |m| String::from(m.as_str()))
     96 +                                                                   });
     97 +                                if !command.is_empty() {
     98 +                                   info!("Got command: {}", command);
     99 +                                }
    100                                  // Make sure this is immutable
    101                                  let args: Vec<&str> = split.collect();
    102                                  if let Err(e) = match_command(
    103 -                                    command.replace("!", "").as_str(),
    104 +                                    command.as_str(),
    105                                      cloned_client.clone(),
    106                                      cloned_config.clone(),
    107                                      tx,
    108 @@ -355,6 +362,7 @@ pub fn commands(_: TokenStream, input: TokenStream) -> TokenStream {
    109                                  {
    110                                      error!("{}", e);
    111                                  }
    112 +
    113                              });
    114  
    115                              while let Some(v) = rx.recv().await {