]> git.donarmstrong.com Git - infobot.git/blob - src/Modules/W3Search.pl
- Lycos.pm not installed on some machines - can cause "crash". Fixed.
[infobot.git] / src / Modules / W3Search.pl
1 # WWWSearch backend, with queries updating the is-db (optionally)
2 # Uses WWW::Search::Google and WWW::Search
3 # originally Google.pl, drastically altered.
4
5 package W3Search;
6
7 use strict;
8 use vars qw(@W3Search_engines $W3Search_regex);
9 @W3Search_engines = qw(AltaVista Dejanews Excite Gopher HotBot Infoseek
10                 Lycos Magellan PLweb SFgate Simple Verity Google z);
11 $W3Search_regex = join '|', @W3Search_engines;
12
13 my $maxshow     = 3;
14
15 sub W3Search {
16     my ($where, $what, $type) = @_;
17     my $retval = "$where can't find \002$what\002";
18     my $Search;
19
20     my @matches = grep { lc($_) eq lc($where) ? $_ : undef } @W3Search_engines;
21     if (@matches) {
22         $where = shift @matches;
23     } else {
24         &::msg($::who, "i don't know how to check '$where'");
25         return;
26     }
27
28     return unless &::loadPerlModule("WWW::Search");
29
30     eval {
31         $Search = new WWW::Search($where);
32     };
33
34     if (!defined $Search) {
35         &::msg($::who, "$where is invalid search.");
36         return;
37     }
38
39     my $Query   = WWW::Search::escape_query($what);
40     $Search->native_query($Query,
41 #       {
42 #               search_debug => 2,
43 #               search_parse_debug => 2,
44 #       }
45     );
46     $Search->http_proxy($::param{'httpProxy'}) if (&::IsParam("httpProxy"));
47     my $max = $Search->maximum_to_retrieve(10); # DOES NOT WORK.
48
49     my (%results, $count, $r);
50     while ($r = $Search->next_result()) {
51         my $url = $r->url();
52
53         ### TODO: fix regex.
54         ### TODO: use array to preserve order.
55         if ($url =~ /^http:\/\/([\w\.]*)/) {
56             my $hostname = $1;
57             next if (exists $results{$hostname});
58             $results{$hostname} = $url;
59         } else {
60             &::DEBUG("W3S: url isn't good? ($url).");
61         }
62
63         last if ++$count >= $maxshow;
64     }
65
66     if (scalar keys %results) {
67         $retval = "$where says \002$what\002 is at ".
68                 join(' or ', map { $results{$_} } sort keys %results);
69     }
70
71     &::performStrictReply($retval);
72 }
73
74 1;