package Xchat::saivert::google;
# Nome: google.pl
# Autore: michele <michele.facchin@virgilio.it>
# Changes made by Saivert
# - Rewritten for the new X-Chat 2 syntax
# - Improved code. Supports !google trigger sent to client
#
# GNU General Public License 2
#
use LWP::UserAgent;
my $search_url = "http://www.google.com/search?q=";
Xchat::register ("X-Google", "1.2", "googles the web for you");
Xchat::print("\00305\002X-Google\002 1.2 by michele loaded (X-Chat 2 version by Saivert)\n");
my $helptext = "X-Google help\n".
"/google -c2 linux\n" .
"/google linux\n" .
"/google -o linux (send results to channel)\n" .
"/google -c4 linux e irc\n\n" .
"Others can send you a PM with !google <searchword>\n";
Xchat::hook_command ("google", \&handle_google, {help_text => $helptext});
Xchat::hook_print ('Private Message to Dialog', \&msg_handler);
Xchat::hook_print ('Private Message', \&msg_handler);
Xchat::hook_print ('Channel Message', \&msg_handler);
Xchat::hook_print ('Your Message', \&msg_handler);
sub msg_handler
{
# It took me a while before I realised I had to use Xchat::strip_code
# I guess I'll never forget it know. DAMN COLOR CODES!
my $their_nick = Xchat::strip_code($_[0][0]);
my $their_msg = Xchat::strip_code($_[0][1]);
#Xchat::print ("Got msg from $their_nick with \"$their_msg\"\n");
unless ($their_msg =~ m/^\s*!google\s+(.*)/i)
{
return Xchat::EAT_NONE;
}
my $args = $1;
Xchat::command("MSG $their_nick \00308Search results for \"$args\"");
Xchat::command("MSG $their_nick \00308Displaying first 10 results only");
@urls = search_google($args, 10);
foreach (@urls) {
Xchat::command("MSG $their_nick \00310\-\003 $_");
}
Xchat::command("MSG $their_nick * End of list");
return Xchat::EAT_ALL;
}
sub handle_google
{
my $args = $_[1][1]; # string like "arg1 arg2 arg3"
my $i = 0, my $c = 3, my $do_say = 0;
if ($args =~ /.*?\-c(\d+).*?/) {
$c = $1;
}
if ($args =~ /.*?\-o.*?/) {
$do_say = 1;
}
# Get rid of options from the string
$args =~ s/\s?-(c\d*|o)\s?//ig;
Xchat::print ("\00308Search results for \"$args\"\n") if (!$do_say);
@urls = search_google($args, $c);
foreach (@urls) {
if ($do_say) {
my $text = Xchat::strip_code($_);
Xchat::command ("SAY [Google] $text");
} else {
Xchat::print ("\00310\-\003 $_\n");
}
}
Xchat::print ("\n") if (!$do_say);
return Xchat::EAT_XCHAT;
}
sub search_google
{
my ($query, $c) = @_;
my @result, $i = 0;
$agent = new LWP::UserAgent;
$request = new HTTP::Request ("GET", $search_url . $query);
$agent -> agent ("Netscape 4.78/U.S., 25-Jun-01; (c) 1995-2000");
$buf = $agent -> request ($request);
$buf = $buf -> content;
# What exists in the HTML from Google
# <a class=l href="http://en.wikipedia.org/wiki/Israeli-Palestinian_conflict" onmousedown="return rwt(this,'','','res','3','&sig2=YMZxbq-xPs8l6fQaBmUpRA')">Wikipedia - <b>Israeli</b>-Palestinian <b>Conflict</b></a>
while (($buf =~ m/<a class=l href="(.+?)".+?>(.+?)<\/a>/gi) && ($i < $c)) {
my $result_url = $1;
my $result_title = $2;
#$result_title =~ s/<b>//gi;
$result_title =~ s/<\/?b>//gi;
$result[$i] = "\00311$result_title\00300 :: $result_url";
$i++;
}
return @result;
}