From bed9beb1f219936ec02fb18b4dd01b85377b1f1c Mon Sep 17 00:00:00 2001 From: froge Date: Thu, 29 Jun 2023 11:09:47 +1000 Subject: [PATCH] Refactor and clean code, fixes #3 and #7 Also clean up log output so the messaging is clear and makes more sense --- src/embeds.rs | 28 ++++++++++++++-------------- src/lib.rs | 4 ++-- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/src/embeds.rs b/src/embeds.rs index 8791b61..9f76a51 100644 --- a/src/embeds.rs +++ b/src/embeds.rs @@ -41,8 +41,8 @@ pub fn parse_metadata(page: &str) -> Embed { let title = doc_body.select(&title_selector).next(); let desc = doc_body.select(&description_selector).next(); // Clean up meta info and store it as a string - let mut meta_title = String::from("None"); - let mut meta_description = String::from("None"); + let mut meta_title = String::default(); + let mut meta_description = String::default(); if let Some(title) = title { meta_title = title.text().collect(); @@ -76,8 +76,8 @@ fn get_urls_from_message(message: &str) -> Vec<&str> { // If we find any urls, push them into the urls vec for regex_match in RE.find_iter(message) { // If the url points to localhost, we don't want to embed it, so we ignore it - if regex_match.as_str().contains("localhost") - || regex_match.as_str().contains("127.0.0.1") + if regex_match.as_str().to_lowercase().contains("localhost") + || regex_match.as_str().to_lowercase().contains("127.0.0.1") { warn!("This is probably a malicious URL, ignoring!"); } else { @@ -96,10 +96,18 @@ pub async fn embed_handler(event: OriginalSyncRoomMessageEvent, room: Room, clie if let Room::Joined(room) = room { let full_reply_event = event.clone().into_full_event(room.room_id().to_owned()); + // If the sender ID matches our client, ignore the message + // We don't want to reply to ourselves + let client_user_id = client.user_id().unwrap(); + if event.sender == client_user_id { + return; + } + // Do not make an embed if someone replies to a URL // Unfortunately, this makes it so that if your reply has a URL, it will not embed. + // TODO: Fix this by scanning replies and only generating embeds for new URLs in future. if let Some(Relation::Reply { in_reply_to: _ }) = &event.content.relates_to { - warn!("Ignoring message, it's a reply to someone else"); + warn!("Ignoring message, it's a reply to someone else!"); return; } @@ -109,15 +117,7 @@ pub async fn embed_handler(event: OriginalSyncRoomMessageEvent, room: Room, clie return; }; - // If the sender ID matches our client, ignore the message - // We don't want to reply to ourselves - let client_user_id = client.user_id().unwrap(); - if event.sender == client_user_id { - return; - } - - let message = text_content.body.to_lowercase(); - let urls = get_urls_from_message(&message); + let urls = get_urls_from_message(&text_content.body); let reqwest_client = reqwest::Client::builder().user_agent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36").build().unwrap(); diff --git a/src/lib.rs b/src/lib.rs index 5cd1b56..829cec6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -90,7 +90,7 @@ pub async fn delete_old_encryption_devices(client: &Client, config: &Config) -> /// The bot will reject invites to spaces and DMs, as well as invites to any rooms it wasn't /// configured to explicitly join, while accepting invites to any rooms it was configured to join. pub async fn reject_stale_invites(client: &Client, config: &Config) { - warn!("Rejecting stale invites"); + warn!("Checking invites"); for room in client.invited_rooms() { let room_name = room.name().unwrap_or_default(); if !room.is_space() @@ -114,7 +114,7 @@ pub async fn reject_stale_invites(client: &Client, config: &Config) { room.reject_invitation().await.unwrap_or_default(); } } - warn!("Finished rejecting stale invites"); + warn!("Finished checking old invites"); } /// Run frogbot