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 = [ dependencies = [
"anyhow", "anyhow",
"matrix-sdk", "matrix-sdk",
"rand 0.8.5",
"tokio", "tokio",
"tracing-subscriber", "tracing-subscriber",
] ]

View file

@ -8,5 +8,6 @@ edition = "2021"
[dependencies] [dependencies]
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"
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

@ -1,12 +1,16 @@
use std::{env, process::exit}; use std::{env, process::exit};
mod utils;
use crate::utils::schmfy;
use rand::Rng;
use matrix_sdk::{ use matrix_sdk::{
config::SyncSettings, config::SyncSettings,
room::Room, room::Room,
ruma::events::{room::{ ruma::events::{room::{
member::StrippedRoomMemberEvent, member::StrippedRoomMemberEvent,
message::{MessageType, OriginalSyncRoomMessageEvent, RoomMessageEventContent}, message::{MessageType, OriginalSyncRoomMessageEvent, RoomMessageEventContent, OriginalRoomMessageEvent},
}, relation::Annotation, reaction::ReactionEventContent}, }, relation::Annotation, reaction::ReactionEventContent, OriginalMessageLikeEvent},
Client, Client,
}; };
use tokio::time::{sleep, Duration}; use tokio::time::{sleep, Duration};
@ -150,40 +154,40 @@ async fn on_room_message(event: OriginalSyncRoomMessageEvent, room: Room) {
println!("message sent"); println!("message sent");
} }
if text_content.body.contains("!timo") { if is_allowed_room(room.name()) {
let reaction = ReactionEventContent::new( if text_content.body.contains("!timo") {
Annotation::new( let reaction = ReactionEventContent::new(
event.event_id.to_owned(), Annotation::new(
"TIMO".to_owned() event.event_id.to_owned(),
) "TIMO".to_owned()
); )
);
room.send(reaction, None).await.unwrap(); 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("")
} }
};
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") { if text_content.body.contains("!lambda") {
let reaction = ReactionEventContent::new( let reaction = ReactionEventContent::new(
Annotation::new( Annotation::new(
@ -197,4 +201,62 @@ async fn on_room_message(event: OriginalSyncRoomMessageEvent, room: Room) {
} }
} }
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
}