Permalink
Browse files

WebTitle adopted

  • Loading branch information...
tadzik committed May 23, 2010
1 parent 8b1b536 commit 4ece89e03423de7ba8db2c87234a1fe8ac467253
Showing with 17 additions and 8 deletions.
  1. +17 −8 a8bot/Plugin/WebTitle.pm
View
@@ -1,17 +1,26 @@
-package WebTitle;
+package a8bot::Plugin::WebTitle;
+use Moose;
use LWP::Simple qw /get $ua/;
+use Sys::SigAction qw /timeout_call/;
+with 'a8bot::Plugin';
-sub init {
- return { publicmsg => \&pubmsg };
+sub BUILD {
+ my $self = shift;
+ $self->passive_cb(sub { pubmsg(@_) });
}
sub pubmsg {
- my ($bot, $data) = @_;
- if ($data->{msg} =~ /(http:\/\/[^ ]+)/) {
+ my (%data) = @_;
+ if ($data{msg} =~ /(http:\/\/[^ ]+)/) {
$ua->max_size(1024);
- my $site = get($1);
- my ($title) = $site =~ /<title>([^<]+)<\/title>/;
- return "[ $title ]" if $title;
+ our $site;
+ if (timeout_call(8, sub {$site = get($1)})){
+ return undef;
+ } else {
+ my ($title) = $site =~ /<title>([^<]+)<\/title>/i;
+ $title =~ s/\n/ /g;
+ return "[ $title ]" if $title;
+ }
}
return undef;
}

0 comments on commit 4ece89e

Please sign in to comment.