mata_bot

some cheeky bot for #unix_surrealism
git clone https://git.pastanoggin.com/mata_bot.git
Log | Files | Refs | README | LICENSE

commit da3ba1c0f3134043355738d05c415f307dfead2a
parent 48c1da5086f9f301aad4bd750b0d6e2eae51bd14
Author: boredpasta <boredpasta@tutanota.com>
Date:   Sun, 16 Mar 2025 23:35:50 +0200

add link previews for web pages and files

Diffstat:
Mmata_bot.pl | 37+++++++++++++++++++++++++++++++++++++
1 file changed, 37 insertions(+), 0 deletions(-)

diff --git a/mata_bot.pl b/mata_bot.pl @@ -86,6 +86,43 @@ sub respond { } else { msg($sock, "failed to get title of video $video_id <[x~x]> (no videos matching ID $video_id!)"); } + } elsif ($message =~ m,(https?://[^\000\r\n ]+)$,) { + my $url = $1; + # get in it's HEAD to check if it's text/html + my $response = HTTP::Tiny->new->head($url); + unless ($response->{success}) { + msg($sock, "failed to get info about link: $url <[x~x]> ($response->{status} $response->{reason}!)"); + return 0; + } + unless ($response->{headers}->{'content-type'}) { + msg($sock, "failed to get info about link: $url <[x~x]> (no ``content-type'' header found in HTTP response!)"); + return 0; + } + unless ($response->{headers}->{'content-type'} eq 'text/html') { + # we got a non text/html content type + msg($sock, "File: $response->{headers}->{'content-type'}"); + return 0; + } + + # if it's text/html, GET it's title + $response = HTTP::Tiny->new->get($url); + unless ($response->{success}) { + msg($sock, "failed to get title of link $url <[x~x]> ($response->{status} $response->{reason}!)"); + return 0; + } + unless (length $response->{content}) { + msg($sock, "failed to get title of link $url <[x~x]> (HTTP response empty!)"); + return 0; + } + my $content = $response->{content}; + if ($content =~ m,<title[^>]*>([^<]+)</title[^>]*>,) { + my $title = $1; + $title =~ tr/[\000\r\n]//d; + $title = trim($title); + msg($sock, "Title: $title"); + } else { + msg($sock, "failed to get title of link $url <[x~x]> (no title found!)"); + } } return 0; }