#!/usr/bin/perl package Xchat::b0at::Google; use warnings; use strict; use LWP::UserAgent; use Storable; # by: b0at # license: public domain # with help from some scripts listed on xchat.org: # http://pragma.kicks-ass.org/scripts/google.pl # http://utenti.lycos.it/amiciweb/google.pl # To use the search sub in your script, simply do # if( $res = Xchat::b0at::Google::search("some terms here") ) { # stuff with $res } my $name = "Google Search"; my $vrsn = '0.0.0-2'; Xchat::register($name, $vrsn); Xchat::print("\cB$name $vrsn\cB"); Xchat::print("\cB$name\cB commands: GOOGLE, GOOGLE_SET"); my $pkg = __PACKAGE__; Xchat::hook_command( 'GOOGLE', "${pkg}::command", { help_text => "Usage: GOOGLE ", } ); Xchat::hook_command( 'GOOGLE_SET', "${pkg}::configure", { help_text => "Usage: GOOGLE_SET [new value]\n". " preferences:\n". " num_results number of results to request\n". " user_agent user agent to report to search engine\n". " query_uri URI to use to query (not including terms or number of results)", } ); my $ua = LWP::UserAgent->new; my $conf= './Xchat-b0at-Google.store'; my %cfg = ( num_results => '5', # number of results, "&num=$num" user_agent => 'Opera/7.23 (Windows NT 5.1; U) [en]', query_uri => 'http://www.google.com/search?q=', # http://www.google.com/search?q= # http://webwarper.net/ww/~GZ/www.google.com/search?q= ); if( -e $conf ) { # load prior configuration %cfg = %{ retrieve $conf }; } else { # store configuration store \%cfg, $conf; } sub command { my $terms = $_[1][1]; if( $terms ) { Xchat::print( search($terms) || 'Google not available.' ); } else { Xchat::print("No search terms specified. Try /help $_[0][0]."); } return Xchat::EAT_ALL; # make this command exclusive } sub configure { my ($pref, $value) = (lc(${$_[0]}[1]), join(' ', @{$_[0]}[2..$#{$_[0]}])); if( not defined $pref or not exists $cfg{$pref} ) { Xchat::print("Not a valid preference, try /help ${$_[0]}[0]."); } # display/alter a preference elsif( $pref eq 'num_results' ) { if( not $value or $value =~ /\D/ ) { Xchat::print("Current number of results: $cfg{num_results}"); } else { $cfg{num_results} = $value; Xchat::print("Now showing $cfg{num_results} results per search."); store \%cfg, $conf; } } elsif( $pref eq 'user_agent' ) { if( not $value ) { Xchat::print("Current user agent string: '$cfg{user_agent}'"); } else { $cfg{user_agent} = $value; Xchat::print("Now reporting '$cfg{user_agent}' as user agent."); store \%cfg, $conf; } } elsif( $pref eq 'query_uri' ) { if( not defined $value or $value =! m|^http://| ) { Xchat::print("Current search URI: '$cfg{query_uri}'"); } else { $cfg{query_uri} = $value; Xchat::print("Now searching with url: '$cfg{query_uri}'"); store \%cfg, $conf; } } return Xchat::EAT_ALL; # make this command exclusive } sub search { my $terms = shift @_; # space-separated words my $query = $terms; $query =~ tr/ /+/; $ua->agent($cfg{user_agent}); # in case it changes my $res = $ua->get("$cfg{query_uri}$query&num=$cfg{num_results}"); return if( not $res->is_success ); my $text = $res->content; if( $text =~ m|No pages were found containing|g ) { return 'No matches'; } # build output my $msg = ''; # number of results if( $text =~ m|Results (.+?) - (.+?)(?:.*?(.+?))?|g ) { $msg .= "$1-$2".($3?" of $3":'').' '; } # search terms $msg .= "For '$terms': "; # we've retrieved exactly how many we need, grab them all my @match = ($text =~ m|class=g>|g); $msg .= join(' , ', @match); # ' , ' => easily clickable for irc # alternative results if( $text =~ m|Did you mean.+?(.+?)|g ) { $msg .= " (Did you mean $1?)"; } # output return $msg; } 1; __END__