# - Google.pl use Irssi qw(command_bind); use LWP::UserAgent; use Data::Dumper::Names; use strict; use vars qw($VERSION %IRSSI); $VERSION = '1.10'; %IRSSI = ( authors => 'Oddbjørn Kvalsund, with mods by Tamara Temple', contact => 'oddbjorn.kvalsund@hiof.no, tamara@tamaratemple.com', name => 'Google', description => 'This script queries google.com and returns the results.', license => 'Public Domain', ); ## Usage: ## /google [-p, prints to current window] [-, number of searchresults returned] [-debug, turn on debugging mode] search-criteria1 search-criteria2 ... ## ## History: ## - Sun May 19 2002 ## Version 0.1 - Initial release ## ------------------------------- #------------------------------------------------- my $nr_sites = 3; # Search-results returned my $prefix = ""; # Message printed before results my $debug = 0; # debugging turned off #------------------------------------------------- sub DebugPrint { my ($debugmsg) = @_; Irssi::print("%Y[$IRSSI{name}]%n " . $debugmsg) if $debug; } sub cmd_google { my ($data, $server, $witem) = @_; my $url = ""; my $nr_sites = 10; my $i = 0; my (@lines, @pages); my $mode = "quiet"; # Check for debug flag if ($data =~ /-debug/i) { $debug = 1; } else {$debug = 0} $data =~ s/-debug//ig; # remove debug flag from $data DebugPrint("Debugging turned on") if $debug; # If user supplied nr_sites, activate his setting if ( $data =~ /-(\d)+\s/ ) { $nr_sites = $1 } $data =~ s/-\d+//g; # remove nr_sites from $data DebugPrint("Number of sites: $nr_sites") if $debug; # Switch to public mode # and return error msg if invalid window if ( $data =~ /-p/ ) { $mode = "public"; if ( ! $witem ) { Irssi::active_win()->print("Must be run run in a valid window (CHANNEL|QUERY)"); return; } } $data =~ s/-p//g; # remove -p from $data DebugPrint("Mode of display: $mode") if $debug; # Format the query-string $data =~ URLEncode($data); my $query = $data; DebugPrint("Query: $query") if $debug; # Initialize LWP my $ua = new LWP::UserAgent; $ua->agent("Mozilla/5.0 " . $ua->agent); $ua->from('someone@example.com'); $ua->default_header('Accept-Language'=> "en"); $ua->timeout(10); DebugPrint(Dumper($ua)) if $debug; # Do the actual seach my $res = $ua->get("http://www.google.com/search?hl=en&q=$query"); DebugPrint(irssi_safe(Dumper($res))) if $debug; my $content = $res->content; DebugPrint(irssi_safe(Dumper($content))) if $debug; # Replace
with newlines # and remove tags $content =~ s/\]*\>/\n/g; #$content =~ s/\<.+?\>/ /sg; # Make array @pages of all search-results @lines = split("\n", $content); @pages = grep (/

/, @lines); DebugPrint("Number of pages: ".$#pages+1) if $debug; # Remove empty entries in @pages for ($i=0;$i<=$#pages;$i++) { $pages[$i] =~ s/\s+.*//g; if ($pages[$i] =~ /(^\n|\s+\n)/){ splice(@pages, $i, 1) }; if ($pages[$i] !~ /\./){ splice(@pages, $i, 1) }; } # Strip extraneous html and words for ($i=0;$i<=$#pages;$i++) { $pages[$i] =~ s/^.*

(.*)<\/h3>.*$/\1/g; $pages[$i] =~ s/^]*>/\1 /; $pages[$i] =~ s/<[^>]+>//g; } # Remove pages which aren't actually links @pages = grep(/https?:\/\//, @pages); if($nr_sites > $#pages) { $nr_sites = $#pages + 1}; # Print pages to current window if public-mode specified # else display a private notice of returned pages if ( $mode eq "public") { if ($prefix ne "") { $witem->command("/SAY $prefix") } ; for ($i=0; $i<$nr_sites; $i++) { $witem->command("/SAY $pages[$i]"); } } else { for ($i=0; $i<$nr_sites; $i++) { Irssi::active_win()->print("$pages[$i]"); } } } Irssi::command_bind google => \&cmd_google; sub cmd_lmgtfy { my ($data, $server, $witem) = @_; return 0 if (!$witem); my $codeddata = URLEncode($data); my $result = sprintf("/say http://lmgtfy.com/?q=%s",$codeddata); $witem->command($result); return 1; } Irssi::command_bind('lmgtfy', 'cmd_lmgtfy'); sub URLEncode { my $theURL = $_[0]; $theURL =~ s/([\W])/uc(sprintf("%%%02x",ord($1)))/eg; return $theURL; } sub irssi_safe { my $s = $_[0]; $s =~ s/%/%%/g; return $s; }