All pastes #135640 Raw Edit

Saivert

public text v1 · immutable
#135640 ·published 2006-08-17 02:58 UTC
rendered paste body
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".
				"\00308/google -c2 linux\n" .
				"\00308/google linux\n" .
				"\00308/google -o linux   (send results to channel)\n" .
				"\00308/google -c4 linux e irc\n\n";
Xchat::hook_command ("google", "handle_google", {help_text => $helptext});

Xchat::hook_print ('Channel Message', 'msg_handler');

sub msg_handler
{
	my $their_nick = $_[0][0];
	my $their_msg = $_[0][1];

	unless ($their_msg =~ m/\s+!google\s+/i)
	{
		return 0;
	}
	
	my $result = handle_google();
	
	Xchat::command("say $their_nick: @stuff_to_say[int(rand(@stuff_to_say))]$suffix");
}

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/-(c\d|o)//ig;
	
	Xchat::print ("\00308Search results for \"$args\"\n") if (!$do_say);
	@urls = search_google($args, $c);
	foreach (@urls) {
		if ($do_say) {
			Xchat::command ("SAY [Google search] $_");
		} else {
			Xchat::print ("\00310\-\003 $_\n");
		}
	}
	Xchat::print ("\n") if (!$do_say);
	return (1);
}

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;

	while (($buf =~ m/<br><font color=#008000>(.+?)\s\-\s/gi) && ($i < $c)) {
		my $result = $1;
		$result =~ s/<b>(.*?)<\/b>/\1/gi;

		$result[$i] = "http:\/\/$result";
		$i++;
	}
	
	return @result;
}