]> git.donarmstrong.com Git - infobot.git/blob - src/Modules/zfi.pl
* Merge back with trunk to r1810
[infobot.git] / src / Modules / zfi.pl
1 package zfi;
2
3 # Search Zaurus Feeds Index (ZFI)
4 # Version 1.0
5 # Released 02 Oct 2002
6
7 # Based on ZSI package by Darien Kruss <darien@kruss.com>
8 # Modified by Jordan Wiens <jordan@d0pe.com> (numatrix on #zaurus) and
9 # Eric Lin <anselor@d0pe.com> (anselor on #zaurus) to search ZFI instead of ZSI
10
11 # This script relies on the following page returning results
12 # http://zaurii.com/zfi/zfibot.php
13 # Returns the 5 latest/newest entries
14
15 # http://zaurii.com/zfi/zfibot.php?query=XXXX
16 # Returns all matches where XXX is in the name, description, etc
17
18 # Returned matches are pipe-separated, one record per line
19 # name|URL|description
20
21 # These are the phrases we get called for:
22
23 # 'zfi'  or  'zfi <search>'
24
25 # We reply publicly or privately, depending how we were called
26
27 use strict;
28
29 my $no_zfi;
30
31 BEGIN {
32     $no_zfi = 0;
33     eval "use LWP::UserAgent";
34     $no_zfi++ if ($@);
35 }
36
37 sub queryText {
38     my ($query) = @_;
39
40     if ($no_zfi) {
41         &::status("zfi module requires LWP::UserAgent.");
42         return '';
43     }
44
45     my $res_return = 5;
46
47     my $ua = new LWP::UserAgent;
48     $ua->proxy( 'http', $::param{'httpProxy'} ) if ( &::IsParam('httpProxy') );
49
50     $ua->timeout(10);
51
52     my $searchpath;
53     if ($query) {
54         $searchpath = "http://zaurii.com/zfi/zfibot.php?query=$query";
55     }
56     else {
57         $searchpath = "http://zaurii.com/zfi/zfibot.php";
58     }
59
60     my $request = new HTTP::Request( 'GET', "$searchpath" );
61     my $response = $ua->request($request);
62
63     if ( !$response->is_success ) {
64         return
65 "Something failed in connecting to the ZFI web server. Try again later.";
66     }
67
68     my $content = $response->content;
69
70     if ( $content =~ /No entries found/im ) {
71         return "No results were found searching ZFI for '$query'.";
72     }
73
74     my $res_count   = 0;    #local counter
75     my $res_display = 0;    #results displayed
76
77     my @lines = split( /\n/, $content );
78
79     my $result = '';
80     foreach my $line (@lines) {
81         if ( length($line) > 10 ) {
82             my ( $name, $href, $desc ) = split( /\|/, $line );
83
84             if ( $res_count < $res_return ) {
85                 $result .= "$name ($desc) $href : ";
86                 $res_display++;
87             }
88             $res_count++;
89         }
90     }
91
92     if ( ($query) && ( $res_count > $res_display ) ) {
93         $result .=
94 "$res_display of $res_count shown. All at http://zaurii.com/zfi/index.phtml?p=r&r=$query";
95     }
96
97     return $result;
98 }
99
100 sub query {
101     my ($args) = @_;
102     &::performStrictReply( &queryText($args) );
103     return;
104 }
105
106 1;
107 __END__
108
109 # vim:ts=4:sw=4:expandtab:tw=80