Add !schmfy command

This commit is contained in:
Jonathan Flueren 2023-07-07 18:55:53 +02:00
parent 1fd304ddd0
commit 7a87c7fde0
3 changed files with 83 additions and 35 deletions

18
Cargo.lock generated
View file

@ -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"

View file

@ -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"

View file

@ -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::<Vec<&str>>()
.join(">");
schmfy(shortened.as_str())
})
.collect::<Vec<String>>()
.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::<Vec<String>>()
.join(" ")
})
.collect::<Vec<String>>()
.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::<Vec<String>>()
.join(" ")
})
.collect::<Vec<String>>()
.join("\n");
let content = RoomMessageEventContent::text_plain(msg)
.make_reply_to(&full_event, ForwardThread::Yes);
room.send(content, None).await.unwrap();
}
}