1# - Google.pl 2 3# - You have to modify this line to the path 4# - of your LWP-dir 5 6use lib '/usr/lib/perl5/vendor_perl/5.6.1'; 7 8use Irssi; 9use LWP::UserAgent; 10use strict; 11use vars qw($VERSION %IRSSI); 12 13$VERSION = '1.00'; 14%IRSSI = ( 15 authors => 'Oddbj�rn Kvalsund', 16 contact => 'oddbjorn.kvalsund@hiof.no', 17 name => 'Google', 18 description => 'This script queries google.com and returns the results.', 19 license => 'Public Domain', 20); 21 22## Usage: 23## /google [-p, prints to current window] [-<number>, number of searchresults returned] search-criteria1 search-criteria2 ... 24## 25## History: 26## - Sun May 19 2002 27## Version 0.1 - Initial release 28## ------------------------------- 29 30#------------------------------------------------- 31my $nr_sites = 3; # Search-results returned 32my $prefix = ""; # Message printed before results 33#------------------------------------------------- 34 35sub cmd_google { 36 37 my ($data, $server, $witem) = @_; 38 my $url = ""; 39 my $nr_sites = 3; 40 my $i = 0; 41 my (@lines, @pages); 42 my $mode = "quiet"; 43 44 # If user supplied nr_sites, activate his setting 45 if ( $data =~ /-(\d\s)/ ) { $nr_sites = $1 }; 46 if ($data =~ /-10/) { $nr_sites = 10 }; 47 $data =~ s/-\d+//g; # remove nr_sites from $data 48 49 # Switch to public mode 50 # and return error msg if invalid window 51 if ( $data =~ /-p/ ) { 52 $mode = "public"; 53 if ( ! $witem ) { 54 Irssi::active_win()->print("Must be run run in a valid window (CHANNEL|QUERY)"); 55 return; 56 } 57 } 58 $data =~ s/-p//g; # remove -p from $data 59 60 # Format the query-string 61 $data =~ s/\s/+/g; 62 my $query = $data; 63 64 # Initialize LWP 65 my $ua = new LWP::UserAgent; 66 $ua->agent("AgentName/0.1 " . $ua->agent); 67 68 # Do the actual seach 69 my $req = new HTTP::Request GET => "http://www.google.com/search?hl=en&q=$query"; 70 my $res = $ua->request($req); 71 my $content = $res->content; 72 73 # Replace <br> with newlines 74 # and remove tags 75 $content =~ s/\<br\>/\n/g; 76 $content =~ s/\<.+?\>//sg; 77 78 # Make array @pages of all search-results 79 @lines = split("\n", $content); 80 @pages = grep (/pages$/, @lines); 81 82 # Remove empty entries in @pages 83 for ($i=0;$i<=$#pages;$i++) { 84 $pages[$i] =~ s/\s+.*//g; 85 if ($pages[$i] =~ /(^\n|\s+\n)/){ splice(@pages, $i, 1) }; 86 if ($pages[$i] !~ /\./){ splice(@pages, $i, 1) }; 87 } 88 89 if($nr_sites > $#pages) { $nr_sites = $#pages + 1}; 90 91 # Print pages to current window if public-mode specified 92 # else display a private notice of returned pages 93 if ( $mode eq "public") { 94 if ($prefix ne "") { $witem->command("/SAY $prefix") } ; 95 for ($i=0; $i<$nr_sites; $i++) { 96 $pages[$i] =~ s/\s+.*//g; 97 $witem->command("/SAY http://$pages[$i]"); 98 } 99 } 100 else { 101 for ($i=0; $i<$nr_sites; $i++) { 102 $pages[$i] =~ s/\s+.*//g; 103 Irssi::active_win()->print("http://$pages[$i]"); 104 } 105 } 106} 107 108Irssi::command_bind('google', 'cmd_google'); 109