aboutsummaryrefslogtreecommitdiff
path: root/Plugin/URL_Title.pm
diff options
context:
space:
mode:
Diffstat (limited to 'Plugin/URL_Title.pm')
-rw-r--r--Plugin/URL_Title.pm67
1 files changed, 67 insertions, 0 deletions
diff --git a/Plugin/URL_Title.pm b/Plugin/URL_Title.pm
new file mode 100644
index 0000000..495df8e
--- /dev/null
+++ b/Plugin/URL_Title.pm
@@ -0,0 +1,67 @@
+#!/usr/bin/env perl
+
+package Plugin::URL_Title;
+
+use strict;
+use warnings;
+use HTTP::Tiny;
+use HTML::HeadParser;
+use utf8;
+
+my %config;
+
+sub configure {
+ my $self = $_[0];
+ my $cmdref = $_[1];
+ my $cref = $_[2];
+ %config = %$cref;
+ return $self;
+}
+
+sub message
+{
+ my ($self, $logger, $me, $who, $where, $raw_what, $what, $irc) = @_;
+ my $url;
+
+ return if ($config{url_on} == 0);
+
+ if ($what =~ /(https?:\/\/[^ ]+)/i) {
+ $url = $1;
+ }
+ return unless $url;
+
+ my $http = HTTP::Tiny->new((default_headers => {'Range' => "bytes=0-65536", 'Accept' => 'text/html'}, timeout => 3));
+
+ my $response = $http->get($url);
+
+ if (!$response->{success}) {
+ $logger->("Something broke: $response->{reason}");
+ return;
+ }
+
+ if (!($response->{headers}->{"content-type"} =~ m,text/html ?,)) {
+ $logger->("Not html, giving up now");
+ return;
+ }
+
+ my $html = $response->{content};
+
+ my $parser = HTML::HeadParser->new;
+ $parser->parse($html);
+
+ # get title and unpack from utf8 (assumption)
+ my $title = $parser->header("title");
+ utf8::upgrade($title);
+ return unless $title;
+
+ my $shorturl = $url;
+ $shorturl = (substr $url, 0, $config{url_len}) . "…" if length ($url) > $config{url_len};
+
+ # remove http(s):// to avoid triggering other poorly configured bots
+ $shorturl =~ s,^https?://,,g;
+ $shorturl =~ s,/$,,g;
+
+ my $composed_title = "$title ($shorturl)";
+ return $composed_title;
+}
+1;