All pastes #85543 Raw Edit

the fail

public text v1 · immutable
·published 2006-07-12 00:51 UTC
rendered paste body
#!/usr/bin/perl -w

use strict;
#use warnings;

use Data::Dumper;
use Net::Google;

BEGIN {
	unless (exists $::{"Irssi::"}) {
		require Pod::Usage;
	Pod::Usage::pod2usage(-verbose => 2);
	}
}

#use SOAP::Lite;
use Text::ParseWords;
use vars qw($VERSION %IRSSI);

use constant LOCAL_GOOGLE_KEY => "LZg/OcRQFHKpmARryqzGZTirUiRXk+78";

use Irssi qw(
		command_bind settings_get_bool
		settings_add_bool settings_get_str
		settings_add_str signal_add
);

$VERSION = '0.01';
%IRSSI = (
	authors         => 'Justin Lee',
	contact         => 'kool.name@gmail.com',
	name            => 'googlebot',
	description     => 'Google script... Just for fun',
	license         => 'GPL',
);

# Start of man page

=head1 DESCRIPTION

This simple script sux, but hey, I wrote it! lol. Anyway, it's a google bot just like the title says, it'll change over time.

Right now this script is only trying to work, it will take a C<< !google <query> >>, but I went and broke the only working part.

=head2 Methods

=over 5

=item C<< !google <query> >>

Used to respond to a private message and channel message, still will if someone fixes it. ;)

=back

=head1 AUTHOR

Dean <kool.name@gmail.com> - L<http://dean.dnsalias.com/>

=cut

# Start of script

settings_add_str($IRSSI{'name'}, 'google_active_channels', '');

my %google_active_channels;
load_globals();

signal_add('setup changed', 'load_globals');
signal_add('event privmsg', 'irc_privmsg');

sub load_globals {
	map { $google_active_channels{uc($_)} = 1 }
		quotewords(',', 0, settings_get_str('google_active_channels'));
}

sub find_target {
	my ($to, $from) = @_;

	if($to =~ /^#/){
		return $to; # from a channel
	}
	return $from; # from a private message
}

sub doGoogleQuery {
	my ($query) = @_;

	my $google = Net::Google->new(key=>LOCAL_GOOGLE_KEY);
	my $search = $google->search();

	$search->query($query);

	$search->lr(qw(en fr));
	$search->starts_at(5);
	$search->max_results(15);

	return $search;
}

sub sayGoogleResults {
	my ($server, $target, $to, $from, $address, $results) = @_;
	
	#       $server->command("msg $target You queried: $result->{'searchQuery'}");
			
			#~~WRONG~~
	$server->command("msg $target There were $results->{'estimatedTotalResultsCount'} results.");
}

sub irc_privmsg {
	my ($server, $data, $from, $address) = @_;
	my ($to, $message) = split(/\s+:/, $data, 2);
	my ($me, $target) = ($server->{'nick'}, find_target($to, $from));

	# Check if it was for the channel we joined...
	if(uc($to) eq uc($me) or $google_active_channels{uc($to)}){
		if($message =~ /^\s*!google\s(\S+)/i){

		# $result will be set to 0 or 1
			my $result = sayGoogleResults(  $server, $target, $to, $from,
							$address, doGoogleQuery($1)
			);      # must doGoogleQuery to get final var
		}
	}
	return 1;
}