From 37305a5465e378a94206888c20fe0459bef3f78b Mon Sep 17 00:00:00 2001 From: Jonathan Flueren Date: Fri, 7 Jul 2023 11:29:57 +0200 Subject: [PATCH] Move schmfication to crate --- Cargo.lock | 7 +++++ Cargo.toml | 1 + src/main.rs | 39 +++++++++++++++++++++++---- src/utils.rs | 74 ---------------------------------------------------- 4 files changed, 42 insertions(+), 79 deletions(-) delete mode 100644 src/utils.rs diff --git a/Cargo.lock b/Cargo.lock index 9aa3396..dbbc646 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1374,6 +1374,7 @@ dependencies = [ "anyhow", "matrix-sdk", "rand 0.8.5", + "schmfy", "tokio", "tracing-subscriber", ] @@ -2238,6 +2239,12 @@ dependencies = [ "windows-sys", ] +[[package]] +name = "schmfy" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9250f38131044f00dc302af28c08a2e121b47c6cf2ad614859604128f002c428" + [[package]] name = "scopeguard" version = "1.1.0" diff --git a/Cargo.toml b/Cargo.toml index 694575c..655dbc0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,5 +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" 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 7f566df..5deee2d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,6 @@ use std::{env, process::exit}; -mod utils; -use crate::utils::schmfy; +use schmfy::schmfy; use rand::Rng; use matrix_sdk::{ @@ -9,8 +8,8 @@ use matrix_sdk::{ room::Room, ruma::events::{room::{ member::StrippedRoomMemberEvent, - message::{MessageType, OriginalSyncRoomMessageEvent, RoomMessageEventContent, OriginalRoomMessageEvent}, - }, relation::Annotation, reaction::ReactionEventContent, OriginalMessageLikeEvent}, + message::{MessageType, OriginalSyncRoomMessageEvent, RoomMessageEventContent}, + }, relation::Annotation, reaction::ReactionEventContent}, Client, }; use tokio::time::{sleep, Duration}; @@ -155,7 +154,7 @@ async fn on_room_message(event: OriginalSyncRoomMessageEvent, room: Room) { } if is_allowed_room(room.name()) { - if text_content.body.contains("!timo") { + if text_content.body.to_lowercase().contains("timo") { let reaction = ReactionEventContent::new( Annotation::new( event.event_id.to_owned(), @@ -165,6 +164,36 @@ async fn on_room_message(event: OriginalSyncRoomMessageEvent, room: Room) { room.send(reaction, None).await.unwrap(); } + if text_content.body.to_lowercase().contains("jan") { + let reaction = ReactionEventContent::new( + Annotation::new( + event.event_id.to_owned(), + "JAN".to_owned() + ) + ); + + room.send(reaction, None).await.unwrap(); + } + if text_content.body.to_lowercase().contains("fabian") { + let reaction = ReactionEventContent::new( + Annotation::new( + event.event_id.to_owned(), + "FABIAN".to_owned() + ) + ); + + room.send(reaction, None).await.unwrap(); + } + if text_content.body.to_lowercase().contains("second") || text_content.body.to_lowercase().contains("dennis") { + let reaction = ReactionEventContent::new( + Annotation::new( + event.event_id.to_owned(), + "SECOND".to_owned() + ) + ); + + room.send(reaction, None).await.unwrap(); + } if event.sender.as_str().contains("conduit.rs") { let reaction = ReactionEventContent::new( diff --git a/src/utils.rs b/src/utils.rs deleted file mode 100644 index b7e9a52..0000000 --- a/src/utils.rs +++ /dev/null @@ -1,74 +0,0 @@ -// 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 -}