File Coverage

blib/lib/CPAN/WAIT.pm
Criterion Covered Total %
statement 7 86 8.1
branch 1 40 2.5
condition 0 51 0.0
subroutine 3 9 33.3
pod 5 5 100.0
total 16 191 8.3


line stmt bran cond sub pod time code
1             # -*- Mode: Perl -*-
2             # $Basename: WAIT.pm $
3             # $Revision: 1.6 $
4             # Author : Ulrich Pfeifer
5             # Created On : Fri Jan 31 11:30:46 1997
6             # Last Modified By: Ulrich Pfeifer
7             # Last Modified On: Thu Mar 23 21:19:20 2000
8             # Language : CPerl
9             # Update Count : 145
10             # Status : Unknown, Use with caution!
11             #
12             # (C) Copyright 1997, Ulrich Pfeifer, all rights reserved.
13             #
14             #
15              
16             package CPAN::WAIT;
17 1     1   2919 use ExtUtils::MakeMaker; # MM->catfile
  1         96558  
  1         240  
18              
19             # try to avoid 'use'ing CPAN.pm
20             # code stolen from CPAN::Config::load ;
21              
22             eval {require CPAN::Config;}; # We eval because of some
23             # MakeMaker problems
24             unless ($CPAN::dot_cpan++){
25             unshift @INC, MM->catdir($ENV{HOME},".cpan");
26             eval {require CPAN::MyConfig;}; # where you can override
27             # system wide settings
28             shift @INC;
29             }
30            
31              
32              
33             require WAIT::Client;
34             require FileHandle;
35 1     1   9 use vars qw($VERSION $DEBUG $TIMEOUT);
  1         3  
  1         1253  
36              
37             # $Format: "\$\V\E\R\S\I\O\N = '$ModuleVersion$';"$ MM_Unix bug
38             $VERSION = '0.27';
39             $TIMEOUT = 20; # Set this to some larger value if you
40             # have a slow connection.
41              
42             sub _open_connection () {
43 0     0     my ($host, $port, $con);
44            
45             # Make sure that there is a wait server to try
46 0 0         unless ($CPAN::Config->{'wait_list'}) {
47 0           $CPAN::Config->{'wait_list'} = ['wait://ls6.informatik.uni-dortmund.de'];
48             }
49            
50             # Try direct connection
51 0           my $server;
52 0           SERVER:
53 0           for $server (@{$CPAN::Config->{'wait_list'}}) {
54 0 0         warn "CPAN::WAIT $VERSION checking $server\n" if $DEBUG;
55 0 0         if ($server =~ m(^wait://([^:]+)(?::(\d+))?)) {
56 0   0       ($host, $port) = ($1, $2 || 1404);
57             # Constructor is inherited from Net::NNTP
58 0 0 0       $con = WAIT::Client->new($host, Port => $port, Timeout => $TIMEOUT)
59             unless $DEBUG and $DEBUG =~ /force proxy/;
60 0 0         last SERVER if $con;
61             }
62             }
63            
64             # Try connection via an http proxy
65 0 0         unless ($con) {
66 0 0 0       warn "Could not connect to the WAIT server at $host port $port\n"
67             unless $DEBUG and $DEBUG =~ /force proxy/;
68            
69 0 0         if ($CPAN::Config->{'http_proxy'}) {
70 0           print "Trying your http proxy $CPAN::Config->{'http_proxy'}\n";
71 0           SERVER:
72 0           for $server (@{$CPAN::Config->{'wait_list'}}) {
73 0 0         if ($server =~ m(^wait://([^:]+)(?::(\d+))?)) {
74 0   0       ($host, $port) = ($1, $2 || 1404);
75 0           $con = WAIT::Client::HTTP->new($host,
76             Port => $port,
77             Proxy => $CPAN::Config->{'http_proxy'},
78             Timeout => $TIMEOUT);
79 0 0         last SERVER if $con;
80             }
81             }
82 0 0         warn "No luck with your proxy either. Giving up\n"
83             unless $con;
84             } else {
85 0           warn "You did not tell the CPAN module about an http proxy.\n" .
86             "I could use such a beast instead of a direct connection.\n";
87             }
88             }
89            
90             # We had no luck.
91 0 0         warn "No searching available!\n" unless $con;
92            
93 0           return $con;
94             }
95              
96             my $con;
97             # Temporary file for retrieved documents
98             my $tmp = MM->catfile($CPAN::Config->{'cpan_home'}, 'w4c.pod');
99              
100             # run a search
101             sub wq {
102 0     0 1   my $self = shift;
103 0           my $result;
104 0           local ($") = ' ';
105              
106 0   0       $con ||= _open_connection || return;
      0        
107 0           print "Searching for '@_'\n";
108 0 0         unless ($result = $con->search(@_)) {
109 0           print "Your query contains a syntax error.\n";
110 0           $self->wh('wq');
111             } else {
112 0           print $con->message;
113 0           print @{$result};
  0            
114 0           print "Type 'wr ' or 'wd ' to examine the results\n";
115             }
116 0           $result;
117             }
118              
119             # display hit record
120             sub wr {
121 0     0 1   my $self = shift;
122 0           my $hit = shift;
123 0           my $result;
124              
125 0 0 0       if (@_ or !$hit) {
126 0           print "USAGE: wr \n";
127             } else {
128 0   0       $con ||= _open_connection || return;
      0        
129 0           print "fetching info on hit number $hit\n";
130 0           $result = $con->info($hit);
131 0           print @$result;
132             }
133 0           $result;
134             }
135              
136             # display hit document
137             sub wd {
138 0     0 1   my $self = shift;
139 0           my $hit = shift;
140 0           my $result;
141              
142 0 0 0       if (@_ or !$hit) {
143 0           print "USAGE: wd \n";
144 0           return;
145             }
146 0   0       $con ||= _open_connection || return;
      0        
147 0           print "Get hit number $hit ...";
148 0           my $text = $con->get($hit);
149 0 0         my $lines = ($text)?@$text:'no';
150 0           print " done\nGot $lines lines\nRunning perldoc on it ...\n";
151            
152             # perldoc does not read STDIN; so we need a temp file
153             {
154 0           my $fh = new FileHandle ">$tmp";
  0            
155 0           $fh->print(@{$text});
  0            
156             }
157              
158             # is system available every were ??
159 0 0 0       system $^X, '-S', 'perldoc', $tmp
      0        
      0        
160             and warn "Could not run '$^X -S perldoc $tmp': $?\n"
161             and system $Config{'pager'}, $tmp
162             and warn "Could not run '$Config{'pager'} $tmp': $?\n"
163             and print @$text;
164 0           $text;
165             }
166              
167             sub wl {
168 0     0 1   my $self = shift;
169 0           my $hits = shift;
170            
171 0 0         if (@_) {
172 0           print "USAGE: wl \n";
173 0           return;
174             }
175 0   0       $con ||= _open_connection || return;
      0        
176 0           print "Setting maximum hit count to $hits\n";
177 0           $con->hits($hits);
178             }
179              
180             my %HELP =
181             (
182             'h' => q{
183             'wh' displays a short summary of commands available via the WAIT
184             plugin.
185             'wh ' displays information about a the command given as argument
186             },
187             'q' => q{
188             Here are some query examples:
189              
190             information retrieval free text query
191             information or retrieval same as above
192             des=information retrieval `information' must be in the description
193             des=(information retrieval) one of them in description
194             des=(information or retrieval) same as above
195             des=(information and retrieval) both of them in description
196             des=(information not retrieval) `information' in description and
197             `retrieval' not in description
198             des=(information system*) wild-card search
199             au=ilia author names may be misspelled
200              
201             You can build arbitary boolean combination of the above examples.
202             The following fields are known:
203              
204             'synopsis', 'name', 'bugs', 'author', 'example', 'description',
205             'environment'
206              
207             Field names may be abbreviated.
208             },
209             'r' => q{
210             'wr ' displays the record of the selected hit. Records look
211             like this:
212              
213             source authors/id/CHIPS/perl5.003_24.tar.gz
214             headline perl - Practical Extraction and Report Language
215             size 12786
216             docid data/perl/pod/perl.pod
217              
218             'source' is the patch relative to http://www.perl.org/CPAN/.
219             'headline' is the contents of the 'NAME' section of the POD document
220             'size' is the size of the POD document (not the size of the tar archive)!
221             'docid' is the path the POS document is stored in. It should be the
222             path in the tar archive minus the version number + a 'data'
223             prefix.
224             },
225             'l' => q{
226             Since answers to queries are sorted by decreasing probability of relevance,
227             you will probably be interested only in the first few hits. To limit the amout
228             of network traffic, the WAIT server only returns the best 10 hits per default.
229             You can change this limit with 'wl '.
230             },
231             'd' => q{
232             The 'wd ' command retrieves the POD document form the
233             server and stores it in the file 'w2c' in your CPAN directory. Then it
234             runs 'perlpod' on it. If you have problems, check if you local
235             'perlpod' works with absolute path names. Older versions are know to
236             fail. Also try to avoid fetching of large documents like 'perlfunc.pod'.
237             Use 'wr ' to see how large the documents are before fetching
238             the actually if you have a slow connection.
239             },
240              
241             );
242              
243             sub wh {
244 0     0 1   my $self = shift;
245 0           my $cmd = shift;
246              
247 0 0 0       if ($cmd and $cmd =~ /(\w)$/) {
248 0   0       print $HELP{$1} || "No help for 'w$1' yet.\n";
249             } else {
250 0           print qq[
251             Available commands:
252             wq query search the WAIT4CPAN server
253             wr hit-number display search result record
254             wd hit-number fetch the document and run perldoc on it
255             wl count limit search to hits
256             wh command displays help on command if available
257             ];
258             }
259 0           1;
260             }
261              
262             END {
263 1 50   1   241 unlink $tmp if -e $tmp;
264             }
265              
266             1;
267              
268             __DATA__