From 7c16eaec9842be139abb34d49ae1d702906b1d75 Mon Sep 17 00:00:00 2001 From: Jonathan Flueren Date: Thu, 6 Jul 2023 19:22:42 +0200 Subject: [PATCH] Schmot --- Cargo.lock | 1 + Cargo.toml | 1 + src/main.rs | 128 ++++++++++++++++++++++++++++++++++++++------------- src/utils.rs | 74 +++++++++++++++++++++++++++++ 4 files changed, 171 insertions(+), 33 deletions(-) create mode 100644 src/utils.rs diff --git a/Cargo.lock b/Cargo.lock index 8ebe992..9aa3396 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1373,6 +1373,7 @@ version = "0.1.0" dependencies = [ "anyhow", "matrix-sdk", + "rand 0.8.5", "tokio", "tracing-subscriber", ] diff --git a/Cargo.toml b/Cargo.toml index 7d4b139..694575c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,5 +8,6 @@ edition = "2021" [dependencies] anyhow = "1.0.71" matrix-sdk = { git = "https://github.com/matrix-org/matrix-rust-sdk.git", package = "matrix-sdk" } +rand = "0.8.5" 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 291f845..d7d6832 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,12 +1,16 @@ use std::{env, process::exit}; +mod utils; +use crate::utils::schmfy; +use rand::Rng; + use matrix_sdk::{ config::SyncSettings, room::Room, ruma::events::{room::{ member::StrippedRoomMemberEvent, - message::{MessageType, OriginalSyncRoomMessageEvent, RoomMessageEventContent}, - }, relation::Annotation, reaction::ReactionEventContent}, + message::{MessageType, OriginalSyncRoomMessageEvent, RoomMessageEventContent, OriginalRoomMessageEvent}, + }, relation::Annotation, reaction::ReactionEventContent, OriginalMessageLikeEvent}, Client, }; use tokio::time::{sleep, Duration}; @@ -150,40 +154,40 @@ async fn on_room_message(event: OriginalSyncRoomMessageEvent, room: Room) { println!("message sent"); } - if text_content.body.contains("!timo") { - let reaction = ReactionEventContent::new( - Annotation::new( - event.event_id.to_owned(), - "TIMO".to_owned() - ) - ); + if is_allowed_room(room.name()) { + if text_content.body.contains("!timo") { + let reaction = ReactionEventContent::new( + Annotation::new( + event.event_id.to_owned(), + "TIMO".to_owned() + ) + ); - room.send(reaction, None).await.unwrap(); - } - - if text_content.body.contains("\\") { - let reaction = ReactionEventContent::new( - Annotation::new( - event.event_id.to_owned(), - text_content.body.replace("\\", "λ").replace("!lambda ","").to_owned() - ) - ); - - room.send(reaction, None).await.unwrap(); - } - - let room_name = match room.name() { - Some(name) => { - name - - }, - _ => { - String::from("") + room.send(reaction, None).await.unwrap(); } - }; + if event.sender.as_str().contains("conduit.rs") { + let reaction = ReactionEventContent::new( + Annotation::new( + event.event_id.to_owned(), + "⚡️".to_owned() + ) + ); + + room.send(reaction, None).await.unwrap(); + } + + if text_content.body.contains("\\") { + let reaction = ReactionEventContent::new( + Annotation::new( + event.event_id.to_owned(), + text_content.body.replace("\\", "λ").replace("!lambda ","").to_owned() + ) + ); + + room.send(reaction, None).await.unwrap(); + } - if room_name.contains("Spam") { if text_content.body.contains("!lambda") { let reaction = ReactionEventContent::new( Annotation::new( @@ -191,10 +195,68 @@ async fn on_room_message(event: OriginalSyncRoomMessageEvent, room: Room) { "FuPro".to_owned() ) ); - + room.send(reaction, None).await.unwrap(); } } + if is_allowed_room(room.name()) && is_allowed_user(event.sender.as_str()){ + if text_content.body.split_whitespace().count() < 2 { + + let reaction = ReactionEventContent::new( + Annotation::new( + event.event_id.to_owned(), + schmfy(text_content.body.to_lowercase()) + ) + ); + + room.send(reaction, None).await.unwrap(); + } + + if {let mut rng = rand::thread_rng(); rng.gen_range(0..2)} == 0 { + let msg = text_content.body + .split_whitespace() + .map(|x| schmfy(String::from(x.to_lowercase()))) + .collect::>() + .join(" "); + + let content = RoomMessageEventContent::text_plain(msg); + + //let content = event.content.make_reply_to(event.content., matrix_sdk::ruma::events::room::message::ForwardThread::Yes) + + room.send(content, None).await.unwrap(); + } + } +} + +fn is_allowed_user(user: &str) -> bool { + println!("User: {}, bool: {}", user, !( + user.contains("bot_jonathan") + || user.contains("bot_jan") + || user.contains("bot_") + )); + return !( + user.contains("bot_jonathan") + || user.contains("bot_jan") + || user.contains("bot_") + ) +} + +fn is_allowed_room(name: Option) -> bool { + let room_name = match name { + Some(name) => { + name + }, + _ => { + String::from("") + } + }; + + if room_name.to_lowercase().contains("spam") { + return true + } + + + false } \ No newline at end of file diff --git a/src/utils.rs b/src/utils.rs new file mode 100644 index 0000000..b7e9a52 --- /dev/null +++ b/src/utils.rs @@ -0,0 +1,74 @@ +// Schmfies any String +pub fn schmfy(source: String) -> String { + if source.starts_with("schm") { + return source; + } + + // if source is subsite (e.g. news/fsr), schmfy all parts separately + if source.contains('/') { + return source + .split('/') + .map(|s| schmfy(String::from(s))) + .collect::>() + .join("/"); + } + + if source.is_empty() { + return source; + } + + // schmfy first char if word is no longer than 3 + if source.len() <= 3 { + let (prefix, suffix) = source.split_at(1); + let c = prefix.chars().next().unwrap_or('-'); + return schmfy_char(c) + suffix; + } + + // Normal words - replace prefix before first vocal + // with "schm" + let vok_pos = source + .chars() + .position(|c| "aeiouäöü".contains(c)) + .unwrap_or(0); + + let (_, suffix) = source.split_at(vok_pos); + + String::from("schm") + suffix +} + +// Schmfies single char +fn schmfy_char(c: char) -> String { + let mut ret = String::from("schm"); + match c { + 'a' | 'e' | 'i' | 'o' | 'u' | 'ä' | 'ö' | 'ü' => { + ret.push(c); + } + 'b' | 'c' | 'd' | 'g' | 'p' | 't' | 'w' => ret.push('e'), + 'f' | 'l' | 'm' | 'n' | 'r' | 's' => { + ret.push('e'); + ret.push(c) + } + 'h' | 'k' => ret.push('a'), + 'j' => { + ret.push('o'); + ret.push('t') + } + 'q' => ret.push('u'), + 'v' => { + ret.push('a'); + ret.push('u') + } + 'x' => { + ret.push('i'); + ret.push('x') + } + 'y' => ret.push(c), + 'z' => { + ret.push('e'); + ret.push('t') + } + _ => ret.push(c), + } + + ret +}