chiark / gitweb /
fixes
[inn-innduct.git] / scripts / simpleftp.in
1 #! /usr/bin/perl -w
2
3 # simpleftp - Rudimentary FTP client.
4 #
5 # Author: David Lawrence <tale@isc.org>.
6 #         Rewritten by Julien Elie <julien@trigofacile.com> to use Net::FTP.
7 #
8 # Fetch files to the local machine based on URLs on the command line.
9 # INN's configure searches for ncftp, wget, linx, et caetera,
10 # but they're all system add-ons, so this is provided.
11 #
12 # Perl 5 is already required by other parts of INN; it only took a half hour
13 # to write this, so this was the easiest way to go for a backup plan.
14 #
15 # This script is nowhere near as flexible as libwww.  If you really need
16 # that kind of power, get libwww and use it.  This is just sufficient for what
17 # INN needed.
18
19 use strict;
20 use Net::FTP;
21 use Sys::Hostname;
22
23 $0 =~ s(.*/)();
24
25 my $usage = "Usage: $0 ftp-URL ...\n";
26
27 @ARGV
28   or die $usage;
29
30 for (@ARGV) {
31   m(^ftp://)
32     or die $usage;
33 }
34
35 my ($lasthost, $ftp);
36
37 # This will keep track of how many _failed_.
38 my $exit = @ARGV;
39
40 for (@ARGV) {
41   my ($host, $path) = m%^ftp://([^/]+)(/.+)%;
42   my $user = 'anonymous';
43   my $pass = (getpwuid($<))[0] . '@' . hostname;
44   my $port = 21;
45
46   unless (defined $host && defined $path) {
47     warn "$0: bad URL: $_\n";
48     next;
49   }
50
51   if ($host =~ /(.*):(.*)\@(.*)/) {
52     $user = $1;
53     $pass = $2;
54     $host = $3;
55   }
56
57   if ($host =~ /(.*):(.*)/) {
58     $port = $1;
59     $host = $2;
60   }
61
62   if (defined $lasthost && $host ne $lasthost) {
63     $ftp->quit;
64     $lasthost = undef;
65   }
66
67   unless (defined $lasthost) {
68     $ftp = Net::FTP->new($host, Port => $port)
69       or next;
70     $ftp->login($user, $pass)
71       or next;
72   }
73
74   my $localfile = $path;
75   $path =~ s([^/]+$)();
76   $localfile =~ s(.*/)();
77
78   $ftp->cwd($path)
79     or next;
80   $ftp->get($localfile)
81     or next;
82
83   $exit--;
84   $lasthost = $host;
85 }
86
87 $ftp->quit
88   if defined $lasthost;
89
90 exit $exit;