| 1 | package Horris::Connection::Plugin::PeekURL; | ||
| 2 | # ABSTRACT: Fetches Links And Display Some Data On It | ||
| 3 | |||
| 4 | |||
| 5 | 1 | ▶ | use Moose; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 1 | ▶ | use AnyEvent::HTTP; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 1 | ▶ | use Encode qw(encode_utf8 decode FB_CROAK); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 1 | ▶ | use File::Temp; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | 1 | ▶ | use HTML::TreeBuilder; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 10 | 1 | ▶ | use Image::Size; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | 1 | ▶ | use URI; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 12 | 1 | ▶ | use WWW::Shorten 'TinyURL'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 13 | extends 'Horris::Connection::Plugin'; | ||
| 14 | with 'MooseX::Role::Pluggable::Plugin'; | ||
| 15 | |||
| 16 | sub irc_privmsg { | ||
| 17 | my ($self, $msg) = @_; | ||
| 18 | |||
| 19 | my $message = $msg->message; | ||
| 20 | while ( $message =~ m{((!)?(?:https?:)(?://[^\s/?#]*)[^\s?#]*(?:\?[^\s#]*)?(?:#.*)?)}g ) { | ||
| 21 | my $do_peek = defined($2) ? 0 : 1; | ||
| 22 | next unless $do_peek; | ||
| 23 | |||
| 24 | my $shorten_url; | ||
| 25 | my $uri = URI->new($1); | ||
| 26 | next unless $uri->scheme && $uri->scheme =~ /^http/i; | ||
| 27 | next unless $uri->authority; | ||
| 28 | |||
| 29 | if (length "$uri" > 50 && $uri->authority !~ /tinyurl|bit\.ly/) { | ||
| 30 | $shorten_url = makeashorterlink($uri); | ||
| 31 | $uri = URI->new($shorten_url); | ||
| 32 | # $self->connection->irc_notice({ | ||
| 33 | # channel => $msg->channel, | ||
| 34 | # message => "short url: $uri" | ||
| 35 | # }); | ||
| 36 | } | ||
| 37 | $shorten_url = $shorten_url ? " - $shorten_url" : ''; | ||
| 38 | |||
| 39 | my @ct; | ||
| 40 | my $ct = 0; # 0 - text, 1 - image, 2, other | ||
| 41 | my $file; | ||
| 42 | |||
| 43 | my $guard; $guard = http_get $uri, | ||
| 44 | timeout => 30, | ||
| 45 | recurse => 10, | ||
| 46 | on_header => sub { | ||
| 47 | my ($headers) = @_; | ||
| 48 | |||
| 49 | if ($headers->{Status} ne '200') { | ||
| 50 | undef $guard; | ||
| 51 | $self->connection->irc_notice({ | ||
| 52 | channel => $msg->channel, | ||
| 53 | message => "Request failed: $headers->{Reason} ($headers->{Status})", | ||
| 54 | }); | ||
| 55 | return; | ||
| 56 | } | ||
| 57 | @ct = split(/\s*,\s*/, $headers->{'content-type'}); | ||
| 58 | if (grep { /^image\/.+$/i } @ct) { | ||
| 59 | $ct = 1; | ||
| 60 | } elsif ( grep { !/^text\/.+$/i } @ct) { | ||
| 61 | # otherwise it's something we don't know about. | ||
| 62 | # don't spend the time and memory to load this guy | ||
| 63 | undef $guard; | ||
| 64 | $ct = 2; | ||
| 65 | $self->connection->irc_notice({ | ||
| 66 | channel => $msg->channel, | ||
| 67 | message => sprintf( "%s [%s]%s", $uri, $ct[0], $shorten_url) | ||
| 68 | }); | ||
| 69 | return; | ||
| 70 | } | ||
| 71 | return 1; | ||
| 72 | }, | ||
| 73 | on_body => sub { | ||
| 74 | # off load to the file system. | ||
| 75 | $file ||= File::Temp->new(UNLINK => 1); | ||
| 76 | |||
| 77 | print $file $_[0]; | ||
| 78 | return 1; | ||
| 79 | }, | ||
| 80 | sub { | ||
| 81 | undef $guard; | ||
| 82 | return unless $file; | ||
| 83 | seek($file, 0, 0); | ||
| 84 | if ($ct == 1) { | ||
| 85 | my($width, $height) = Image::Size::imgsize($file); | ||
| 86 | $self->connection->irc_notice({ | ||
| 87 | channel => $msg->channel, | ||
| 88 | message => sprintf( "%s [%s, w=%d, h=%d]%s", $uri, $ct[0], $width, $height, $shorten_url ) | ||
| 89 | }); | ||
| 90 | } else { | ||
| 91 | my $p; | ||
| 92 | my $data = do { local $/; <$file> }; | ||
| 93 | eval { | ||
| 94 | $p = HTML::TreeBuilder->new( | ||
| 95 | implicit_tags => 1, | ||
| 96 | ignore_unknoown => 1, | ||
| 97 | ignore_text => 0 | ||
| 98 | ); | ||
| 99 | $p->strict_comment(1); | ||
| 100 | |||
| 101 | my $charset; | ||
| 102 | |||
| 103 | if ($data =~ /charset=(?:'([^']+?)'|"([^"]+?)"|([a-zA-Z0-9_-]+)\b)/) { | ||
| 104 | my $cs = lc($1 || $2 || $3); | ||
| 105 | if ($cs =~ /^Shift[-_]?JIS$/i) { | ||
| 106 | $charset = 'cp949'; | ||
| 107 | } else { | ||
| 108 | $charset = $cs; | ||
| 109 | } | ||
| 110 | } | ||
| 111 | |||
| 112 | if (! $charset) { | ||
| 113 | foreach my $ct (@ct) { | ||
| 114 | if ($ct =~ s/charset=Shift_JIS/charset=cp949/i) { | ||
| 115 | $charset = 'cp949'; | ||
| 116 | } elsif ($ct =~ /charset=([a-zA-Z0-9_-]+)/) { | ||
| 117 | $charset = $1; | ||
| 118 | } | ||
| 119 | } | ||
| 120 | } | ||
| 121 | $charset ||= 'utf-8'; | ||
| 122 | |||
| 123 | eval { | ||
| 124 | $p->parse_content( | ||
| 125 | decode( $charset, $data, FB_CROAK ) ); | ||
| 126 | }; | ||
| 127 | if ($@) { | ||
| 128 | # if we got bad content, attempt to decode in order | ||
| 129 | foreach my $try_charset qw(cp949 euc-kr utf-8) { | ||
| 130 | eval { | ||
| 131 | $p->parse_content(decode($try_charset, $data, FB_CROAK ) ); | ||
| 132 | }; | ||
| 133 | |||
| 134 | $charset = $try_charset unless $@; | ||
| 135 | last unless $@; | ||
| 136 | } | ||
| 137 | } | ||
| 138 | |||
| 139 | my ($title) = $p->look_down(_tag => qr/^title$/i); | ||
| 140 | $title ||= $self->_get_dirty_title($data, $charset); | ||
| 141 | |||
| 142 | my $title_text; | ||
| 143 | if(ref(\$title) eq 'SCALAR') { | ||
| 144 | $title_text = $title; | ||
| 145 | } | ||
| 146 | else { | ||
| 147 | $title_text = $title ? $title->as_trimmed_text(skip_dels => 1) || '' : 'No title'; | ||
| 148 | } | ||
| 149 | |||
| 150 | $self->connection->irc_notice({ | ||
| 151 | channel => $msg->channel, | ||
| 152 | message => encode_utf8( | ||
| 153 | sprintf('%s [%s]%s', | ||
| 154 | $title_text, | ||
| 155 | $ct[0] || '?', | ||
| 156 | $shorten_url | ||
| 157 | ) | ||
| 158 | ) | ||
| 159 | }); | ||
| 160 | |||
| 161 | }; | ||
| 162 | if ($@) { | ||
| 163 | $self->connection->irc_notice({ | ||
| 164 | channel => $msg->channel, | ||
| 165 | message => encode_utf8( | ||
| 166 | sprintf("Error while retrieving URL: %s", $@) | ||
| 167 | ) | ||
| 168 | }); | ||
| 169 | } | ||
| 170 | if ($p) { | ||
| 171 | eval { $p->delete }; | ||
| 172 | } | ||
| 173 | } | ||
| 174 | } | ||
| 175 | ; | ||
| 176 | } | ||
| 177 | |||
| 178 | $self->pass; | ||
| 179 | } | ||
| 180 | |||
| 181 | sub _get_dirty_title { | ||
| 182 | my ($self, $data, $charset) = @_; | ||
| 183 | my $html = decode($charset, $data); | ||
| 184 | $html =~ m{<title>(.+)</title>}; | ||
| 185 | return $1; | ||
| 186 | } | ||
| 187 | |||
| 188 | __PACKAGE__->meta->make_immutable; | ||
| 189 | |||
| 190 | 1; | ||
| 191 | |||
| 192 | __END__ | ||