From cd1fc57841deab0e9b8bcfad49d527c6b263534c Mon Sep 17 00:00:00 2001 From: David Phillips Date: Tue, 10 Apr 2018 15:20:24 +1200 Subject: Correct capitalisation on module names --- Plugin/URL_Title.pm | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 Plugin/URL_Title.pm (limited to 'Plugin/URL_Title.pm') 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; -- cgit v1.1