This commit is contained in:
Jonathan Flueren 2023-07-06 19:22:42 +02:00
parent d92a067934
commit 7c16eaec98
4 changed files with 171 additions and 33 deletions

1
Cargo.lock generated
View file

@ -1373,6 +1373,7 @@ version = "0.1.0"
dependencies = [
"anyhow",
"matrix-sdk",
"rand 0.8.5",
"tokio",
"tracing-subscriber",
]

View file

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

View file

@ -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::<Vec<String>>()
.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<String>) -> bool {
let room_name = match name {
Some(name) => {
name
},
_ => {
String::from("")
}
};
if room_name.to_lowercase().contains("spam") {
return true
}
false
}

74
src/utils.rs Normal file
View file

@ -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::<Vec<String>>()
.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
}