#!/usr/bin/env perl
# Copyright (C) 2005 by İsmail Dönmez ( Resistence is futile, turn on god damn Unicode! )
# Licensed under GPL v2 or later at your option
#
# A simple google search script using Google's SOAP api

use warnings;
use strict;
use Getopt::Long;

my $port=shift;
my $server=shift;
my $target=shift;
my $key;
my $googleSearch;
my $result;
my $print;
my $spell;
my $search;
my $search_escape;
my $spellcheck;
my $browser;
my $error;
my $url;
my $info;
my $query;
my $limit=5;


GetOptions('search=s' => \$search,
	   'spell=s' => \$spellcheck,
	   'browser' => \$browser,
	   'limit|max|maxresults=i' => \$limit);

if(!$browser && !$search && !$spellcheck && !@ARGV)
{
    system 'dcop', $port, 'Konversation', 'info', "\x02Google Search Script Usage:\x02";
    system 'dcop', $port, 'Konversation', 'info', '/google (--max <number>) (--search) <keyword> : Search for keyword at Google';
    system 'dcop', $port, 'Konversation', 'info', '/google --spell <word> : Spellcheck word using Google';
    exec   'dcop', $port, 'Konversation', 'info', '/google -b <keyword> : Show results in konqueror';
}
elsif($browser)
{
    $query = join(" ",@ARGV);
    $query = CGI::escape($query);
    
    if(@ARGV)
    {
	$url="http://www.google.com/search?q=$query";
    }
    else
    {
	$url="http://www.google.com/";
    }
    exec 'kfmclient','openURL',$url;
}

eval { require SOAP::Lite; };

if($@)
{
    exec 'dcop', $port, 'Konversation', 'error', "You need the perl SOAP::Lite module installed.";
}

eval { require HTML::Entities; };

if($@)
{
    exec 'dcop', $port, 'Konversation', 'error', "You need the perl HTML::Entities module installed.";
}

use CGI qw(:standard);

if(!open(KEY, "$ENV{'HOME'}/.googlekey"))
{
    system 'dcop', $port, 'Konversation', 'error', "~/.googlekey doesn't exist!";
    system 'dcop', $port, 'Konversation', 'error', "Get a key from http://api.google.com/createkey and put the key in ~/.googlekey";
}
else
{
    while(<KEY>)
    {
	chomp;
	$key = $_;
    }

    close(KEY);
    
    if($search || !$spellcheck)
    {
	$search = join(" ",$search,@ARGV);
	system 'dcop', $port, 'Konversation', 'info',  "Searching Google for \x02$search\x02 ...";

	$googleSearch = SOAP::Lite->service("http://api.google.com/GoogleSearch.wsdl")->
	    on_fault(sub {
		my $res = $_[0]; 
		$error = $res->call->faultstring;
		exec 'dcop', $port, 'Konversation', 'error', "$error";
	    });
       
	if(utf8::valid($search))
	{
	    utf8::decode($search);
	}

	$search_escape = $search;
	# Encode "&", "<", ">", and "'" . See http://groups.yahoo.com/group/soaplite/message/3037
	HTML::Entities::encode_entities($search_escape, "&<>'");
	if($limit > 10) { $limit=10; }
	$result = $googleSearch->doGoogleSearch($key, $search_escape, 0, $limit, "false", "", "false", "", "UTF-8", "UTF-8"); 
	
	if($result->{estimatedTotalResultsCount} > 0)
	{
	    system 'dcop', $port, 'Konversation', 'info', "Results \x021-$limit\x02 of about \x02$result->{estimatedTotalResultsCount}\x02 for \x02$search\x02 (\x02$result->{searchTime}\x02 seconds)";
	}
	else
	{
	    exec 'dcop', $port, 'Konversation', 'error', 'Google search returned zero results';
	}
	
	foreach $result (@{$result->{resultElements}})
	{
	    $print = $result->{URL}." (".$result->{title}.")";
	    $print =~ s/\<b\>/\x02/g;
	    $print =~ s/\<\/b\>/\x02/g;
	    HTML::Entities::decode_entities($print);
	    system 'dcop', $port, 'Konversation', 'info', $print;
	}
    }
    elsif($spellcheck)
    {
	$spellcheck = join(" ",$spellcheck,@ARGV);

	system 'dcop', $port, 'Konversation', 'info', "Spellchecking \x02$spellcheck\x02 ...";
	$googleSearch = SOAP::Lite->service("http://api.google.com/GoogleSearch.wsdl"); 
	$spell = $googleSearch->doSpellingSuggestion($key, $spellcheck);

	if($spell)
	{
	    $spell = "Spelling suggestion: \x02$spell\x02";
	}
	else
	{
	    exec 'dcop', $port, 'Konversation', 'error','No alternative spelling found';
	}
	
	system 'dcop', $port, 'Konversation', 'info', $spell;
    }
}
