From: pdbogen Date: Tue, 28 Oct 2008 15:49:01 +0000 (+0000) Subject: Add a 5-second timeout to anything that uses LWP::UserAgent, and add the TITLE command. X-Git-Url: http://git.mmlx.us/?a=commitdiff_plain;h=22b615e3fc4c24947914cbdf1b6955ce91f63738;p=destult.git Add a 5-second timeout to anything that uses LWP::UserAgent, and add the TITLE command. git-svn-id: https://www.cernu.us/~pdbogen/svn/destult2@73 088b83a4-0077-4247-935c-42ec02c2848b --- diff --git a/commands.yaml b/commands.yaml index 922c3af..fbd1b0e 100644 --- a/commands.yaml +++ b/commands.yaml @@ -755,6 +755,7 @@ BASH: |- if( $time < 5 ) { use LWP::UserAgent; my $ua = LWP::UserAgent->new; + $ua->timeout(5); my $req = HTTP::Request->new( GET => 'http://bash.org/?'.$what ); my $res = $ua->request( $req ); my @quotes; @@ -794,6 +795,7 @@ QDB: |- use LWP::UserAgent; my $ua = LWP::UserAgent->new; + $ua->timeout(5); my $req = HTTP::Request->new( GET => 'http://qdb.us/?'.$what ); my $res = $ua->request( $req ); my @quotes; @@ -1226,6 +1228,7 @@ WEBTENDER: |- my $ua = LWP::UserAgent->new( agent => "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8) Gecko/20051224 Debian/1.5.dfsg-3 Firefox/1.5" ); + $ua->timeout(5); my $response = $ua->get( 'http://www.webtender.com/cgi-bin/search?name="'.$what.'"' ); if( $response->{ '_rc' } != 200 ) { $kernel->post( $src, $replypath, "Failed to retrieve search results.", $dest ); @@ -1459,6 +1462,7 @@ GROUPHUG: |- my( $kernel, $heap, $who, $what, $src, $dest, $replypath ) = @_; use LWP::UserAgent; my $ua = LWP::UserAgent->new; + $ua->timeout(5); my $req = HTTP::Request->new( GET => 'http://grouphug.us/random' ); my $res = $ua->request( $req ); my @hugs; @@ -1709,3 +1713,30 @@ KARMA: |- } } } + +TITLE: |- + sub { + my( $kernel, $heap, $who, $what, $src, $dest, $replypath ) = @_; + if( $what !~ /^https?:\/\/.+$/ ) { + $kernel->post( $src, $replypath, "Usage: TITLE ", $dest ); + return; + } + + use WWW::Shorten::TinyURL; + + use LWP::UserAgent; + my $ua = LWP::UserAgent->new; + $ua->timeout(5); + my $req = HTTP::Request->new( GET => $what ); + my $res = $ua->request( $req ); + if( $res->is_success ) { + my $content = $res->content; + if( $content =~ /(.*?)<\/title>/si ) { + use HTML::Entities; + my $title = decode_entities( $1 ); + $kernel->post( $src, $replypath, "Title: $title (".makeashorterlink( $what ).")", $dest ); + } else { + $kernel->post( $src, $replypath, "Title: None (".makeashorterlink( $what ).")", $dest ); + } + } + }