From 50d5e664e8fa21252b760bd54dd4a025d3f06585 Mon Sep 17 00:00:00 2001 From: David Phillips Date: Sat, 5 Jan 2019 16:19:55 +1300 Subject: URL_Title: add direct command --- Plugin/URL_Title.pm | 44 +++++++++++++++++++++++++++++++++----------- 1 file changed, 33 insertions(+), 11 deletions(-) diff --git a/Plugin/URL_Title.pm b/Plugin/URL_Title.pm index c5c10d0..19b7437 100644 --- a/Plugin/URL_Title.pm +++ b/Plugin/URL_Title.pm @@ -19,6 +19,8 @@ sub configure { IdaliusConfig::assert_scalar($config, $self, "url_len"); die "url_len must be positive" if $config->{url_len} <= 0; + $cmdref->($self, "title of", sub { $self->get_title_cmd(@_); }); + return $self; } @@ -33,16 +35,16 @@ sub start_handler "tagname,self"); } -sub on_message +sub get_title { - my ($self, $logger, $who, $where, $raw_what, $what, $irc) = @_; + my ($what) = @_; my $url; # Drawn from RFC 3986ยง2 if ($what =~ /(https?:\/\/[a-z0-9\-\._~:\/\?#\[\]@\!\$&'()\*\+,;=%]+)/i) { $url = $1; } - return unless $url; + return (undef, "No URL found in that string") unless $url; # FIXME add more XML-based formats that we can theoretically extract titles from # FIXME factor out accepted formats and response match into accepted formats array @@ -51,18 +53,16 @@ sub on_message my $response = $http->get($url); if (!$response->{success}) { - $logger->("Something broke: $response->{reason}"); - return; + chomp $response->{content}; + return (undef, "Error: HTTP client: $response->{reason} ($response->{content})"); } if (not $response->{headers}->{"content-type"}) { - $logger->("No content-type in reponse header, not continuing"); - return; + return (undef, "No content-type in reponse header, not continuing"); } if (!($response->{headers}->{"content-type"} =~ m,(text/html|image/svg\+xml),)) { - $logger->("I don't think I can parse titles from $response->{headers}->{'content-type'} - stopping here"); - return; + return (undef, "I don't think I can parse titles from $response->{headers}->{'content-type'} - stopping here"); } my $html = $response->{content}; @@ -72,13 +72,13 @@ sub on_message my $p = HTML::Parser->new(api_version => 3); $p->handler( start => \&start_handler, "tagname,self"); $p->parse($html); - $logger->("Error parsing HTML: $!") if $!; + return (undef, "Error parsing HTML: $!") if $!; $title =~ s/\s+/ /g; $title =~ s/(^\s+|\s+$)//g; utf8::upgrade($title); - return unless $title; + return (undef, "Error: No title") unless $title; my $shorturl = $url; # remove http(s):// to avoid triggering other poorly configured bots @@ -92,6 +92,28 @@ sub on_message return $composed_title; } +sub get_title_cmd +{ + my ($self, $irc, $logger, $who, $where, $ided, $rest, @arguments) = @_; + + my ($title, $error) = get_title($rest); + $logger->($error) if $error; + + return $error if $error; + return $title if $title; + +} + +sub on_message +{ + my ($self, $logger, $who, $where, $raw_what, $what, $irc) = @_; + + my ($title, $error) = get_title($what); + $logger->($error) if $error; + + return $title if $title; +} + sub on_action { on_message(@_); } -- cgit v1.1