1#######################################################################
2# mygoogle.pl
3#
4# Author: Tim Van Wassenhove <timvw@users.sourceforge.net>
5#
6# Redistribution and use in source and binary forms, with or without
7# modification, are permitted provided that the following conditions
8# are met:
9# 1. Redistributions of source code must retain the above copyright
10#    notice, this list of conditions and the following disclaimer.
11# 2. Redistributions in binary form must reproduce the above copyright
12#    notice, this list of conditions and the following disclaimer in the
13#    documentation and/or other materials provided with the distribution.
14# 3. The name of the author may not be used to endorse or promote products
15#    derived from this software without specific prior written permission.
16#
17# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27#
28#######################################################################
29
30use strict;
31use Irssi;
32use LWP::UserAgent;
33use vars qw($VERSION %IRSSI);
34
35$VERSION = '1.01';
36%IRSSI = (
37	authors     => 'Tim Van Wassenhove',
38	contact     => 'timvw@users.sourceforge.net',
39	name        => 'mygoogle',
40  description => 'Query Google',
41	license     => 'BSD',
42	url         => 'http://home.mysth.be/~timvw',
43	changed     => '13-03-04 01:35',
44);
45
46# Perform the query and return the results
47sub google_query {
48
49	my $query = shift;
50
51	my $ua = LWP::UserAgent->new;
52	$ua->agent("irssi/0.1");
53
54  # Request the page
55	my $request = HTTP::Request->new(GET => "http://www.google.com/search?hl=en&q=$query");
56	my $result = $ua->request($request);
57	my $content = $result->content;
58
59  # Parse the returned page
60  my @lines = split("<br>",$content);
61	my @results = ();
62	push(@results,"Results for $query on Google:");
63	my $counter = 0;
64	foreach(@lines) {
65		if ($counter < 3 && $_ =~ /^\<font color=#008000\>(.*?)\s+(.*?)\s+\-\s+\<\/font\>(.*)$/) {
66			push(@results,"http://$1");
67			++$counter;
68		}
69	}
70	push(@results,"--");
71
72	return @results;
73}
74
75# Output the results
76sub results_write {
77	my ($server,$target,@lines) = @_;
78	foreach(@lines) {
79		$server->command("MSG $target $_");
80	}
81}
82
83# Handle what others say in public
84sub message_public {
85	my ($server,$msg,$nick,$address,$target) = @_;
86	if ($msg =~ /^\!google\s+(.+)$/) {
87		my @lines = google_query($1);
88		results_write($server,$target,@lines);
89	}
90}
91
92# Handle what we say in public
93sub message_own_public {
94	my ($server,$msg,$target) = @_;
95	message_public($server,$msg,$server->{nick},0,$target);
96}
97
98# Handle what others say in private
99sub message_private {
100	my ($server,$msg,$nick,$address) = @_;
101	message_public($server,$msg,$nick,$address,$nick);
102}
103
104# Handle what we say in private
105sub message_own_private {
106	my ($server,$msg,$target,$otarget) = @_;
107	message_public($server,$msg,$server->{nick},0,$target);
108}
109
110# Connect the signals with the functions
111Irssi::signal_add('message public','message_public');
112Irssi::signal_add('message own_public','message_own_public');
113Irssi::signal_add('message private','message_private');
114Irssi::signal_add('message own_private','message_own_private');