blib/lib/Morris/Plugin/PeekURL.pm untested

filter: Statementstmt 0.0% 0 / 109 Branchbran 0.0% 0 / 34 Conditioncond 0.0% 0 / 16 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 177 CC 18 SCAR 54.7
CC
18
Cov
0%
handle_message
342.0
BEGIN
2.0
BEGIN
2.0
CRAP 237.4
1  
2 package Morris::Plugin::PeekURL;
3 use Moose;
4 use AnyEvent::HTTP;
5 use Encode qw(encode_utf8 decode FB_CROAK);
6 use File::Temp;
7 use HTML::TreeBuilder;
8 use Image::Size;
9 use URI;
10 use namespace::clean -except => qw(meta);
11  
12 extends 'Morris::Plugin';
13  
14 after register => sub {
15 my ($self, $conn) = @_;
16 $conn->register_hook( 'chat.privmsg', sub { $self->handle_message(@_) } );
17 };
18  
19 sub handle_message {
20 my ($self, $msg) = @_;
21  
22 my $message = $msg->message;
23 while ( $message =~ m{(!)?(?:(https?):)(?://([^\s/?#]*))([^\s?#]*)(?:\?([^\s#]*))?(?:#(.*))?}g ) {
24 my $do_peek = defined($1) ? 0 : 1;
25 my ($scheme, $authority, $path, $query, $fragment) = ($2, $3, $4, $5, $6);
26 next unless $do_peek;
27 next unless $scheme && $scheme =~ /^http/i;
28 next unless $authority;
29  
30 my $uri = URI->new();
31 $uri->scheme($scheme);
32 $uri->authority($authority);
33 $uri->path($path);
34 $uri->query($query);
35 $uri->fragment($fragment);
36  
37 my @ct;
38 my $ct = 0; # 0 - text, 1 - image, 2, other
39 my $file;
40  
41 my $guard; $guard = http_get $uri,
42 timeout => 30,
43 recurse => 10,
44 on_header => sub {
45 my ($headers) = @_;
46  
47 if ($headers->{Status} ne '200') {
48 undef $guard;
49 $self->connection->irc_notice({
50 channel => $msg->channel,
51 message => "Request failed: $headers->{Reason} ($headers->{Status})",
52 });
53 return;
54 }
55 @ct = split(/\s*,\s*/, $headers->{'content-type'});
56 if (grep { /^image\/.+$/i } @ct) {
57 $ct = 1;
58 } elsif ( grep { !/^text\/.+$/i } @ct) {
59 # otherwise it's something we don't know about.
60 # don't spend the time and memory to load this guy
61 undef $guard;
62 $ct = 2;
63 $self->connection->irc_notice({
64 channel => $msg->channel,
65 message => sprintf( "%s [%s]", $uri, $ct[0])
66 });
67 return;
68 }
69 return 1;
70 },
71 on_body => sub {
72 # off load to the file system.
73 $file ||= File::Temp->new(UNLINK => 1);
74  
75 print $file $_[0];
76 return 1;
77 },
78 sub {
79 undef $guard;
80 return unless $file;
81 seek($file, 0, 0);
82 if ($ct == 1) {
83 my($width, $height) = Image::Size::imgsize($file);
84 $self->connection->irc_notice({
85 channel => $msg->channel,
86 message => sprintf( "%s [%s, w=%d, h=%d]", $uri, $ct[0], $width, $height )
87 });
88 } else {
89 my $p;
90 my $data = do { local $/; <$file> };
91 eval {
92 $p = HTML::TreeBuilder->new(
93 implicit_tags => 1,
94 ignore_unknoown => 1,
95 ignore_text => 0
96 );
97 $p->strict_comment(1);
98
99 my $charset = 'cp932';
100
101 foreach my $ct (@ct) {
102 if ($ct =~ s/charset=Shift_JIS/charset=cp932/) {
103 $charset = 'cp932';
104 }
105 }
106
107 if ($data =~ /charset=(?:'([^']+)'|"([^"]+)"|(.+)\b)/) {
108 my $cs = lc($1 || $2 || $3);
109 if ($cs =~ /^Shift[-_]?JIS$/i) {
110 $charset = 'cp932';
111 } else {
112 $charset = $cs;
113 }
114 }
115
116 eval {
117 $p->parse_content(
118 decode( $charset, $data, FB_CROAK ) );
119 };
120 if ($@) {
121 # if we got bad content, attempt to decode in order
122 foreach my $charset qw(cp932 euc-jp iso-2022-jp utf-8) {
123 eval {
124 $p->parse_content(decode($charset, $data, FB_CROAK ) );
125 };
126
127 last unless $@;
128 }
129 }
130
131 my ($title) = $p->look_down(_tag => qr/^title$/i);
132 $self->connection->irc_notice({
133 channel => $msg->channel,
134 message => encode_utf8(
135 sprintf('%s [%s]',
136 $title ? $title->as_trimmed_text(skip_dels => 1) || '' : 'No title',
137 $ct[0] || '?'
138 )
139 )
140 });
141 };
142 if ($@) {
143 $self->connection->irc_notice({
144 channel => $msg->channel,
145 message => encode_utf8(
146 sprintf("Error while retrieving URL: %s", $@)
147 )
148 });
149 }
150 if ($p) {
151 eval { $p->delete };
152 }
153 }
154 }
155 ;
156 }
157 }
158  
159 __PACKAGE__->meta->make_immutable;
160  
161 1;
162  
163 __END__