From 7a87c7fde05fd9446898d93ccbf03faf0d7565e6 Mon Sep 17 00:00:00 2001 From: Jonathan Flueren Date: Fri, 7 Jul 2023 18:55:53 +0200 Subject: [PATCH] Add !schmfy command --- Cargo.lock | 18 +++++----- Cargo.toml | 2 +- src/main.rs | 98 +++++++++++++++++++++++++++++++++++++++-------------- 3 files changed, 83 insertions(+), 35 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 14be4c9..c892e95 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1382,7 +1382,7 @@ dependencies = [ [[package]] name = "matrix-sdk" version = "0.6.2" -source = "git+https://github.com/matrix-org/matrix-rust-sdk.git#0ef819bca7173fafb883c969512f980457b99a02" +source = "git+https://github.com/matrix-org/matrix-rust-sdk.git#fa4c1ef00b398541377ac1dd0d4335e960a9939d" dependencies = [ "anymap2", "async-stream", @@ -1422,7 +1422,7 @@ dependencies = [ [[package]] name = "matrix-sdk-base" version = "0.6.1" -source = "git+https://github.com/matrix-org/matrix-rust-sdk.git#0ef819bca7173fafb883c969512f980457b99a02" +source = "git+https://github.com/matrix-org/matrix-rust-sdk.git#fa4c1ef00b398541377ac1dd0d4335e960a9939d" dependencies = [ "async-trait", "bitflags 2.3.3", @@ -1444,7 +1444,7 @@ dependencies = [ [[package]] name = "matrix-sdk-common" version = "0.6.0" -source = "git+https://github.com/matrix-org/matrix-rust-sdk.git#0ef819bca7173fafb883c969512f980457b99a02" +source = "git+https://github.com/matrix-org/matrix-rust-sdk.git#fa4c1ef00b398541377ac1dd0d4335e960a9939d" dependencies = [ "futures-core", "futures-util", @@ -1460,7 +1460,7 @@ dependencies = [ [[package]] name = "matrix-sdk-crypto" version = "0.6.0" -source = "git+https://github.com/matrix-org/matrix-rust-sdk.git#0ef819bca7173fafb883c969512f980457b99a02" +source = "git+https://github.com/matrix-org/matrix-rust-sdk.git#fa4c1ef00b398541377ac1dd0d4335e960a9939d" dependencies = [ "aes", "async-std", @@ -1496,7 +1496,7 @@ dependencies = [ [[package]] name = "matrix-sdk-indexeddb" version = "0.2.0" -source = "git+https://github.com/matrix-org/matrix-rust-sdk.git#0ef819bca7173fafb883c969512f980457b99a02" +source = "git+https://github.com/matrix-org/matrix-rust-sdk.git#fa4c1ef00b398541377ac1dd0d4335e960a9939d" dependencies = [ "anyhow", "async-trait", @@ -1521,7 +1521,7 @@ dependencies = [ [[package]] name = "matrix-sdk-sqlite" version = "0.1.0" -source = "git+https://github.com/matrix-org/matrix-rust-sdk.git#0ef819bca7173fafb883c969512f980457b99a02" +source = "git+https://github.com/matrix-org/matrix-rust-sdk.git#fa4c1ef00b398541377ac1dd0d4335e960a9939d" dependencies = [ "async-trait", "deadpool-sqlite", @@ -1543,7 +1543,7 @@ dependencies = [ [[package]] name = "matrix-sdk-store-encryption" version = "0.2.0" -source = "git+https://github.com/matrix-org/matrix-rust-sdk.git#0ef819bca7173fafb883c969512f980457b99a02" +source = "git+https://github.com/matrix-org/matrix-rust-sdk.git#fa4c1ef00b398541377ac1dd0d4335e960a9939d" dependencies = [ "blake3", "chacha20poly1305", @@ -2242,9 +2242,9 @@ dependencies = [ [[package]] name = "schmfy" -version = "0.1.1" +version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4c61d72da9ef0aed4dbf5ed4d312d3b15733b1e862295af1a6660c4202a2b416" +checksum = "397fd51f89255cb054de7f63fec096ba636f0972bf31d168fe17dd0a05ad5e0c" [[package]] name = "scopeguard" diff --git a/Cargo.toml b/Cargo.toml index 655dbc0..2ff8dc1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,6 +9,6 @@ edition = "2021" anyhow = "1.0.71" matrix-sdk = { git = "https://github.com/matrix-org/matrix-rust-sdk.git", package = "matrix-sdk" } rand = "0.8.5" -schmfy = "0.1.0" +schmfy = "0.2.0" tokio = { version = "1.29.1", features = ["macros", "rt-multi-thread"] } tracing-subscriber = "0.3.17" diff --git a/src/main.rs b/src/main.rs index 18b97dc..ba75dad 100644 --- a/src/main.rs +++ b/src/main.rs @@ -130,6 +130,22 @@ async fn on_stripped_state_member( } } +fn schmfy_strip_reply(txt: &str) -> String { + txt.split("\n") + .filter(|line| line.starts_with("> ")) + .map(|line| { + let shortened = line.split(">") + .enumerate() + .filter(|(i,_)| *i > 1) + .map(|(_,txt)| txt) + .collect::>() + .join(">"); + schmfy(shortened.as_str()) + }) + .collect::>() + .join("\n") +} + // This fn is called whenever we see a new room message event. You notice that // the difference between this and the other function that we've given to the // handler lies only in their input parameters. However, that is enough for the @@ -156,6 +172,39 @@ async fn on_room_message(event: OriginalSyncRoomMessageEvent, room: Room) { println!("message sent"); } + if text_content.body.contains("!schmfy") && is_allowed_user(event.sender.as_str()) { + match event.content.relates_to { + Some(_) => { + let plain = schmfy_strip_reply(text_content.body.as_str()); + + let formatted = match text_content.formatted { + Some(formatted) => { + schmfy_strip_reply(formatted.body.as_str()) + }, + None => { + String::from("") + } + }; + + let content = RoomMessageEventContent::text_html(plain, formatted) + .make_reply_to(&full_event, ForwardThread::Yes); + + room.send(content, None).await.unwrap(); + }, + None => { + // react on invalid message + let reaction = ReactionEventContent::new( + Annotation::new( + event.event_id.to_owned(), + "WRONG".to_owned() + ) + ); + + room.send(reaction, None).await.unwrap(); + } + } + } + if is_allowed_room(room.name()) { if text_content.body.to_lowercase().contains("timo") { let reaction = ReactionEventContent::new( @@ -239,36 +288,35 @@ async fn on_room_message(event: OriginalSyncRoomMessageEvent, room: Room) { let reaction = ReactionEventContent::new( Annotation::new( event.event_id.to_owned(), - schmfy(text_content.body.to_lowercase()) + schmfy(text_content.body.to_lowercase().as_str()) ) ); room.send(reaction, None).await.unwrap(); + } else { /* + let msg = text_content.body + .split_terminator("\n") + .map(|line| { + line + .split_whitespace() + .map(|x| { + if {let mut rng = rand::thread_rng(); rng.gen_range(0..200)} <= x.len()^2 { + schmfy(x.to_lowercase().as_str()) + } else { + x.to_lowercase() + } + }) + .collect::>() + .join(" ") + }) + .collect::>() + .join("\n"); + + let content = RoomMessageEventContent::text_plain(msg) + .make_reply_to(&full_event, ForwardThread::Yes); + + room.send(content, None).await.unwrap(); */ } - - - let msg = text_content.body - .split_terminator("\n") - .map(|line| { - line - .split_whitespace() - .map(|x| { - if {let mut rng = rand::thread_rng(); rng.gen_range(0..200)} <= x.len()^2 { - schmfy(String::from(x.to_lowercase())) - } else { - x.to_lowercase() - } - }) - .collect::>() - .join(" ") - }) - .collect::>() - .join("\n"); - - let content = RoomMessageEventContent::text_plain(msg) - .make_reply_to(&full_event, ForwardThread::Yes); - - room.send(content, None).await.unwrap(); } }