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]] [[package]]
name = "matrix-sdk" name = "matrix-sdk"
version = "0.6.2" 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 = [ dependencies = [
"anymap2", "anymap2",
"async-stream", "async-stream",
@ -1422,7 +1422,7 @@ dependencies = [
[[package]] [[package]]
name = "matrix-sdk-base" name = "matrix-sdk-base"
version = "0.6.1" 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 = [ dependencies = [
"async-trait", "async-trait",
"bitflags 2.3.3", "bitflags 2.3.3",
@ -1444,7 +1444,7 @@ dependencies = [
[[package]] [[package]]
name = "matrix-sdk-common" name = "matrix-sdk-common"
version = "0.6.0" 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 = [ dependencies = [
"futures-core", "futures-core",
"futures-util", "futures-util",
@ -1460,7 +1460,7 @@ dependencies = [
[[package]] [[package]]
name = "matrix-sdk-crypto" name = "matrix-sdk-crypto"
version = "0.6.0" 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 = [ dependencies = [
"aes", "aes",
"async-std", "async-std",
@ -1496,7 +1496,7 @@ dependencies = [
[[package]] [[package]]
name = "matrix-sdk-indexeddb" name = "matrix-sdk-indexeddb"
version = "0.2.0" 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 = [ dependencies = [
"anyhow", "anyhow",
"async-trait", "async-trait",
@ -1521,7 +1521,7 @@ dependencies = [
[[package]] [[package]]
name = "matrix-sdk-sqlite" name = "matrix-sdk-sqlite"
version = "0.1.0" 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 = [ dependencies = [
"async-trait", "async-trait",
"deadpool-sqlite", "deadpool-sqlite",
@ -1543,7 +1543,7 @@ dependencies = [
[[package]] [[package]]
name = "matrix-sdk-store-encryption" name = "matrix-sdk-store-encryption"
version = "0.2.0" 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 = [ dependencies = [
"blake3", "blake3",
"chacha20poly1305", "chacha20poly1305",
@ -2242,9 +2242,9 @@ dependencies = [
[[package]] [[package]]
name = "schmfy" name = "schmfy"
version = "0.1.1" version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4c61d72da9ef0aed4dbf5ed4d312d3b15733b1e862295af1a6660c4202a2b416" checksum = "397fd51f89255cb054de7f63fec096ba636f0972bf31d168fe17dd0a05ad5e0c"
[[package]] [[package]]
name = "scopeguard" name = "scopeguard"

View file

@ -9,6 +9,6 @@ edition = "2021"
anyhow = "1.0.71" anyhow = "1.0.71"
matrix-sdk = { git = "https://github.com/matrix-org/matrix-rust-sdk.git", package = "matrix-sdk" } matrix-sdk = { git = "https://github.com/matrix-org/matrix-rust-sdk.git", package = "matrix-sdk" }
rand = "0.8.5" rand = "0.8.5"
schmfy = "0.1.0" schmfy = "0.2.0"
tokio = { version = "1.29.1", features = ["macros", "rt-multi-thread"] } tokio = { version = "1.29.1", features = ["macros", "rt-multi-thread"] }
tracing-subscriber = "0.3.17" 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 // 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 // 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 // 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"); 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 is_allowed_room(room.name()) {
if text_content.body.to_lowercase().contains("timo") { if text_content.body.to_lowercase().contains("timo") {
let reaction = ReactionEventContent::new( let reaction = ReactionEventContent::new(
@ -239,36 +288,35 @@ async fn on_room_message(event: OriginalSyncRoomMessageEvent, room: Room) {
let reaction = ReactionEventContent::new( let reaction = ReactionEventContent::new(
Annotation::new( Annotation::new(
event.event_id.to_owned(), 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(); 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();
} }
} }