home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / perl_ste.zip / CPAN / WAIT.pm
Text File  |  1997-08-12  |  11KB  |  365 lines

  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: Tue Aug 12 11:54:22 1997
  8. # Language        : CPerl
  9. # Update Count    : 141
  10. # Status          : Unknown, Use with caution!
  11. # (C) Copyright 1997, Ulrich Pfeifer, all rights reserved.
  12.  
  13. package CPAN::WAIT;
  14. use ExtUtils::MakeMaker; # MM->catfile
  15. use CPAN ();
  16. require WAIT::Client;
  17. require FileHandle;
  18. use vars qw($VERSION $DEBUG $TIMEOUT);
  19.  
  20. # $Format: "\$\V\E\R\S\I\O\N = '$ModuleVersion$';"$ MM_Unix bug
  21. $VERSION = '0.24';
  22. $TIMEOUT = 20;                  # Set this to some larger value if you
  23.                                 # have a slow connection.
  24.  
  25. sub _open_connection () {
  26.   my ($host, $port, $con);
  27.   
  28.   # Make sure that there is a wait server to try
  29.   unless ($CPAN::Config->{'wait_list'}) {
  30.     $CPAN::Config->{'wait_list'} = ['wait://ls6.informatik.uni-dortmund.de'];
  31.   }
  32.   
  33.   # Try direct connection
  34.   my $server;
  35.  SERVER:
  36.   for $server (@{$CPAN::Config->{'wait_list'}}) {
  37.     warn "CPAN::WAIT $VERSION checking $server\n" if $DEBUG;
  38.     if ($server =~ m(^wait://([^:]+)(?::(\d+))?)) {
  39.       ($host, $port) = ($1, $2 || 1404);
  40.       # Constructor is inherited from Net::NNTP
  41.       $con = WAIT::Client->new($host, Port => $port, Timeout => $TIMEOUT)
  42.         unless $DEBUG and $DEBUG =~ /force proxy/;
  43.       last SERVER if $con;
  44.     }
  45.   }
  46.   
  47.   # Try connection via an http proxy
  48.   unless ($con) {
  49.     warn "Could not connect to the WAIT server at $host port $port\n"
  50.       unless $DEBUG and $DEBUG =~ /force proxy/;
  51.     
  52.     if ($CPAN::Config->{'http_proxy'}) {
  53.       print "Trying your http proxy $CPAN::Config->{'http_proxy'}\n";
  54.     SERVER:
  55.       for $server (@{$CPAN::Config->{'wait_list'}}) {
  56.         if ($server =~ m(^wait://([^:]+)(?::(\d+))?)) {
  57.           ($host, $port) = ($1, $2 || 1404);
  58.           $con = WAIT::Client::HTTP->new($host,
  59.                                          Port  => $port,
  60.                                          Proxy => $CPAN::Config->{'http_proxy'},
  61.                                          Timeout => $TIMEOUT);
  62.           last SERVER if $con;
  63.         }
  64.       }
  65.       warn "No luck with your proxy either. Giving up\n"
  66.         unless $con;
  67.     } else {
  68.       warn "You did not tell the CPAN module about an http proxy.\n" .
  69.         "I could use such a beast instead of a direct connection.\n";
  70.     }
  71.   }
  72.   
  73.   # We had no luck.
  74.   warn "No searching available!\n" unless $con;
  75.   
  76.   return $con;
  77. }
  78.  
  79. my $con;
  80. # Temporary file for retrieved documents
  81. my $tmp = MM->catfile($CPAN::Config->{'cpan_home'}, 'w4c.pod');
  82.  
  83. # run a search
  84. sub wq {
  85.   my $self = shift;
  86.   my $result;
  87.   local ($") = ' ';
  88.  
  89.   $con ||= _open_connection || return;
  90.   print "Searching for '@_'\n";
  91.   unless ($result = $con->search(@_)) {
  92.     print "Your query contains a syntax error.\n";
  93.     $self->wh('wq');
  94.   } else {
  95.     print $con->message;
  96.     print @{$result};
  97.     print "Type 'wr <number>' or 'wd <number>' to examine the results\n";
  98.   }
  99.   $result;
  100. }
  101.  
  102. # display hit record
  103. sub wr {
  104.   my $self = shift;
  105.   my $hit  = shift;
  106.   my $result;
  107.  
  108.   if (@_ or !$hit) {
  109.     print "USAGE: wr <hit-number>\n";
  110.   } else {
  111.     $con ||= _open_connection || return;
  112.     print "fetching info on hit number $hit\n";
  113.     $result = $con->info($hit);
  114.     print @$result;
  115.   }
  116.   $result;
  117. }
  118.  
  119. # display hit document
  120. sub wd {
  121.   my $self = shift;
  122.   my $hit  = shift;
  123.   my $result;
  124.  
  125.   if (@_ or !$hit) {
  126.     print "USAGE: wd <hit-number>\n";
  127.     return;
  128.   } 
  129.   $con ||= _open_connection || return;
  130.   print "Get hit number $hit ...";
  131.   my $text  = $con->get($hit);
  132.   my $lines = ($text)?@$text:'no';
  133.   print " done\nGot $lines lines\nRunning perldoc on it ...\n";
  134.   
  135.   # perldoc does not read STDIN; so we need a temp file
  136.   {
  137.     my $fh = new FileHandle ">$tmp";
  138.     $fh->print(@{$text});
  139.   }
  140.  
  141.   # is system available every were ??
  142.   system $^X, '-S', 'perldoc', $tmp
  143.     and warn "Could not run '$^X -S perldoc $tmp': $?\n"
  144.       and system $Config{'pager'}, $tmp
  145.         and warn "Could not run '$Config{'pager'} $tmp': $?\n"
  146.           and print @$text;
  147.   $text;
  148. }
  149.  
  150. sub wl {
  151.   my $self = shift;
  152.   my $hits = shift;
  153.   
  154.   if (@_) {
  155.     print "USAGE: wl <maximum-hit-count>\n";
  156.     return;
  157.   }
  158.   $con ||= _open_connection || return;
  159.   print "Setting maximum hit count to $hits\n";
  160.   $con->hits($hits);
  161. }
  162.  
  163. my %HELP =
  164.   (
  165.    'h' => q{
  166. 'wh'           displays a short summary of commands available via the WAIT
  167.                plugin.
  168. 'wh <command>' displays information about a the command given as argument
  169.    },
  170.    'q' => q{
  171. Here are some query examples:
  172.  
  173. information retrieval               free text query 
  174. information or retrieval            same as above 
  175. des=information retrieval           `information' must be in the description 
  176. des=(information retrieval)         one of them in description 
  177. des=(information or retrieval)      same as above 
  178. des=(information and retrieval)     both of them in description 
  179. des=(information not retrieval)     `information' in description and
  180.                                     `retrieval' not in description 
  181. des=(information system*)           wild-card search
  182. au=ilia                             author names may be misspelled
  183.  
  184. You can build arbitary boolean combination of the above examples.
  185. The following fields are known: 
  186.  
  187.   'synopsis', 'name', 'bugs', 'author', 'example', 'description',
  188.   'environment'
  189.  
  190. Field names may be abbreviated.
  191. },
  192.    'r' => q{
  193. 'wr <hit-number>'  displays the record of the selected hit. Records look
  194.                    like this:
  195.  
  196.    source          authors/id/CHIPS/perl5.003_24.tar.gz
  197.    headline        perl - Practical Extraction and Report Language 
  198.    size            12786
  199.    docid           data/perl/pod/perl.pod
  200.  
  201. 'source'   is the patch relative to http://www.perl.org/CPAN/.
  202. 'headline' is the contents of the 'NAME' section of the POD document
  203. 'size'     is the size of the POD document (not the size of the tar archive)!
  204. 'docid'    is the path the POS document is stored in. It should be the
  205.            path in the tar archive minus the version number + a 'data'
  206.            prefix.
  207.    },
  208.    'l' => q{
  209. Since answers to queries are sorted by decreasing probability of relevance,
  210. you will probably be interested only in the first few hits. To limit the amout
  211. of network traffic, the WAIT server only returns the best 10 hits per default.
  212. You can change this limit with 'wl <number>'.
  213.    },
  214.    'd' => q{
  215. The 'wd <hit-number>' command retrieves the POD document form the
  216. server and stores it in the file 'w2c' in your CPAN directory. Then it
  217. runs 'perlpod' on it. If you have problems, check if you local
  218. 'perlpod' works with absolute path names. Older versions are know to
  219. fail. Also try to avoid fetching of large documents like 'perlfunc.pod'.
  220. Use 'wr <hit-number>' to see how large the documents are before fetching
  221. the actually if you have a slow connection.
  222. },
  223.  
  224.   );
  225.  
  226. sub wh {
  227.   my $self = shift;
  228.   my $cmd  = shift;
  229.  
  230.   if ($cmd and $cmd =~ /(\w)$/) {
  231.     print $HELP{$1} || "No help for 'w$1' yet.\n";
  232.   } else {
  233.     print qq[
  234. Available commands:
  235. wq        query           search the WAIT4CPAN server
  236. wr        hit-number      display search result record
  237. wd        hit-number      fetch the document and run perldoc on it
  238. wl        count           limit search to <count> hits
  239. wh        command         displays help on command if available
  240. ];
  241.   }
  242.   1;
  243. }
  244.  
  245. END {
  246.   unlink $tmp if -e $tmp;
  247. }
  248.  
  249. 1;
  250.  
  251. __DATA__
  252.  
  253. =head1 NAME
  254.  
  255. CPAN::WAIT - adds commands to search a WAIT4CPAN server to the CPAN C<shell()>
  256.  
  257. =head1 SYNOPSIS
  258.  
  259.   perl -MCPAN -e shell
  260.   > wq au=wall
  261.   > wr 3
  262.   > wd 3
  263.   > wl 20
  264.   > wh
  265.   > wh wq
  266.  
  267. =head1 DESCRIPTION
  268.  
  269. B<CPAN::WAIT> adds some comands to the CPAN C<shell()> to perform
  270. searches on a WAIT server. It connects to a WAIT server using a simple
  271. protocoll resembling NNTP as described in RFC977. It uses the
  272. B<WAIT::Client> module to handle this connection. This in turn
  273. inherits from B<Net::NNTP> from the F<libnet> package. So you need
  274. B<Net::NNTP> to use this module.
  275.  
  276. If no direct connection to the WAIT server is possible, the modules
  277. tries to connect via your HTTP proxy (as given by the CPAN
  278. configuration). Be warned though that the emulation of the stateful
  279. protocol via HTTP is slow.
  280.  
  281. The variable C<CPAN::WAIT::TIMEOUT> specifies the number of seconds to
  282. wait for an answer from the server. The default is 20. You may want to
  283. set it to some larger value if you have a slow connection.
  284.  
  285. The commands available are:
  286.  
  287. =over 
  288.  
  289. =item B<wh> [B<command>]
  290.  
  291. Displays a short help message if called without arguments. If you
  292. provide the name of another command you will get more information on
  293. this command if available. Currently only B<wq> will be explained.
  294.  
  295. =item B<wl> I<count>
  296.  
  297. Limit the number of hits returned in a search to I<count>. The limit
  298. usually is set ot 10 of you don't set it.
  299.  
  300. =item B<wq> I<query>
  301.  
  302. Send a query to the server. 
  303.  
  304. Here are some query examples:
  305.  
  306.   information retrieval               free text query 
  307.   information or retrieval            same as above 
  308.   des=information retrieval           `information' must be in the description 
  309.   des=(information retrieval)         one of them in description 
  310.   des=(information or retrieval)      same as above 
  311.   des=(information and retrieval)     both of them in description 
  312.   des=(information not retrieval)     `information' in description and
  313.                                       `retrieval' not in description 
  314.   des=(information system*)           wild-card search
  315.   au=ilia                             author names may be misspelled
  316.  
  317. You can build arbitary boolean combination of the above examples.
  318. Field names may be abbreviated. For further information see
  319. F<http://ls6-www.informatik.uni-dortmund.de/CPAN>
  320.  
  321. The result should look like this:
  322.  
  323.   wq au=wall
  324.  
  325.    1 8.039 a2p - Awk to Perl translator 
  326.    2 8.039 s2p - Sed to Perl translator 
  327.    3 8.039 perlipc - Perl interprocess communication (signals, fifos, pipes, safe subprocesses, sockets, and semaphores) 
  328.    4 8.039 ExtUtils::DynaGlue - Methods for generating Perl extension files 
  329.    5 8.039 h2xs - convert .h C header files to Perl extensions 
  330.    6 8.039 Sys::Syslog, openlog, closelog, setlogmask, syslog - Perl interface to the UNIX syslog(3) calls 
  331.    7 8.039 h2ph - convert .h C header files to .ph Perl header files 
  332.    8 8.039 Shell - run shell commands transparently within perl 
  333.    9 8.039 pl2pm - Rough tool to translate Perl4 .pl files to Perl5 .pm modules. 
  334.   10 8.039 perlpod - plain old documentation 
  335.  
  336. =item B<wr> I<hit-number>
  337.  
  338. Display the Record of hit number I<hit-number>:
  339.  
  340.   wr 1
  341.   
  342.   source          authors/id/CHIPS/perl5.003_24.tar.gz
  343.   headline        a2p - Awk to Perl translator 
  344.   size            5643
  345.   docid           data/perl/x2p/a2p.pod
  346.  
  347.  
  348. =item B<wd> I<hit-number>
  349.  
  350. Fetches the full text from the server and runs B<perlpod> on it. Make
  351. sure that you have B<perlpod> in your path. Also check if your
  352. B<perlpod> version can handle absolute pathes. Some older versions
  353. ironically do not find a document if the full patch is given on the
  354. command line.
  355.  
  356. =back
  357.  
  358. =head1 AUTHOR
  359.  
  360. Ulrich Pfeifer E<lt>F<pfeifer@ls6.informatik.uni-dortmund.de>E<gt>
  361.  
  362.