Refactor and clean code, fixes #3 and #7 #10

Manually merged
froge merged 1 commit from fixes/refactor into master 2023-06-29 01:20:50 +00:00
2 changed files with 16 additions and 16 deletions
Showing only changes of commit bed9beb1f2 - Show all commits

View file

@ -41,8 +41,8 @@ pub fn parse_metadata(page: &str) -> Embed {
let title = doc_body.select(&title_selector).next(); let title = doc_body.select(&title_selector).next();
let desc = doc_body.select(&description_selector).next(); let desc = doc_body.select(&description_selector).next();
// Clean up meta info and store it as a string // Clean up meta info and store it as a string
let mut meta_title = String::from("None"); let mut meta_title = String::default();
let mut meta_description = String::from("None"); let mut meta_description = String::default();
if let Some(title) = title { if let Some(title) = title {
meta_title = title.text().collect(); 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 // If we find any urls, push them into the urls vec
for regex_match in RE.find_iter(message) { 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 the url points to localhost, we don't want to embed it, so we ignore it
if regex_match.as_str().contains("localhost") if regex_match.as_str().to_lowercase().contains("localhost")
|| regex_match.as_str().contains("127.0.0.1") || regex_match.as_str().to_lowercase().contains("127.0.0.1")
{ {
warn!("This is probably a malicious URL, ignoring!"); warn!("This is probably a malicious URL, ignoring!");
} else { } else {
@ -96,10 +96,18 @@ pub async fn embed_handler(event: OriginalSyncRoomMessageEvent, room: Room, clie
if let Room::Joined(room) = room { if let Room::Joined(room) = room {
let full_reply_event = event.clone().into_full_event(room.room_id().to_owned()); 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 // 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. // 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 { 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; return;
} }
@ -109,15 +117,7 @@ pub async fn embed_handler(event: OriginalSyncRoomMessageEvent, room: Room, clie
return; return;
}; };
// If the sender ID matches our client, ignore the message let urls = get_urls_from_message(&text_content.body);
// 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 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(); 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();

View file

@ -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 /// 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. /// 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) { pub async fn reject_stale_invites(client: &Client, config: &Config) {
warn!("Rejecting stale invites"); warn!("Checking invites");
for room in client.invited_rooms() { for room in client.invited_rooms() {
let room_name = room.name().unwrap_or_default(); let room_name = room.name().unwrap_or_default();
if !room.is_space() 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(); room.reject_invitation().await.unwrap_or_default();
} }
} }
warn!("Finished rejecting stale invites"); warn!("Finished checking old invites");
} }
/// Run frogbot /// Run frogbot