| 1 | package File::Feed::Source::ftp; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use vars qw(@ISA); | ||
| 7 | |||
| 8 | @ISA = qw(File::Feed::Source); | ||
| 9 | |||
| 10 | use File::Feed::Source; | ||
| 11 | use File::Feed::File; | ||
| 12 | use Net::FTP; | ||
| 13 | use Net::Netrc; | ||
| 14 | |||
| 15 | sub protocol { 'ftp' } | ||
| 16 | |||
| 17 | sub feed { $_[0]->{'_feed'} } | ||
| 18 | |||
| 19 | sub begin { | ||
| 20 | my ($self, $feed) = @_; | ||
| 21 | my $host = $self->host; | ||
| 22 | my $user = $self->{'user'} ||= $self->{'uri'}->user; | ||
| 23 | my $pass = $self->{'password'} ||= $self->{'uri'}->password; | ||
| 24 | if (!defined $pass) { | ||
| 25 | my $mach = Net::Netrc->lookup($host) || die "Can't determine password for $host"; | ||
| 26 | my $login; | ||
| 27 | ($login, $pass) = $mach->lpa; | ||
| 28 | die "Looking for $user at $host, found $login" if defined $user && $login ne $user; | ||
| 29 | } | ||
| 30 | my $root = $self->root; | ||
| 31 | my $client = Net::FTP->new($host) or die "Can't connect"; | ||
| 32 | $client->login($user, $pass) or die "Can't login: ", $client->message; | ||
| 33 | $client->cwd($self->root) or die "Can't cwd $root: ", $client->message; | ||
| 34 | @$self{qw(_client _feed _list)} = ($client, $feed, {}); | ||
| 35 | return $self; | ||
| 36 | } | ||
| 37 | |||
| 38 | sub end { | ||
| 39 | my ($self) = @_; | ||
| 40 | delete @$self{qw(_client _feed _list)}; | ||
| 41 | return $self; | ||
| 42 | } | ||
| 43 | |||
| 44 | sub list { | ||
| 45 | my ($self, $path, $recursive) = @_; | ||
| 46 | my $root = $self->root; | ||
| 47 | my $abspath = defined $path ? "$root/$path" : $root; | ||
| 48 | my $ofs = length($abspath) + 1; | ||
| 49 | my $client = $self->{'_client'}; | ||
| 50 | my $dir = $client->dir($abspath) or return; | ||
| 51 | my @files; | ||
| 52 | foreach my $line (@$dir) { | ||
| 53 | next if $line !~ /^([-d])[-a-z]{9}\s.+\s(\S+)$/; | ||
| 54 | my ($type, $file) = ($1, "$abspath/$2"); | ||
| 55 | if ($type eq 'd') { | ||
| 56 | ; | ||
| 57 | } | ||
| 58 | else { | ||
| 59 | push @files, $file; | ||
| 60 | } | ||
| 61 | } | ||
| 62 | return map { substr($_, $ofs) } @files; | ||
| 63 | } | ||
| 64 | |||
| 65 | sub oldlist { | ||
| 66 | my ($self, $path, $recursive) = @_; | ||
| 67 | goto &rlist if $recursive; | ||
| 68 | $path = '.' if !defined $path; | ||
| 69 | my $client = $self->{'_client'}; | ||
| 70 | my $list = $client->ls($path) | ||
| 71 | or die "Can't list $path: ", $client->message; | ||
| 72 | return @$list; | ||
| 73 | } | ||
| 74 | |||
| 75 | sub rlist { | ||
| 76 | my ($self, $path) = @_; | ||
| 77 | my $client = $self->{'_client'}; | ||
| 78 | my @list; | ||
| 79 | _crawl($client, $path, \@list); | ||
| 80 | return @list; | ||
| 81 | } | ||
| 82 | |||
| 83 | sub _crawl { | ||
| 84 | my ($client, $path, $list) = @_; | ||
| 85 | my $dir = $client->dir($path) or return; | ||
| 86 | foreach my $line (@$dir) { | ||
| 87 | next if $line !~ /^([-d])[-a-z]{9}\s.+\s(\S+)$/; | ||
| 88 | my ($type, $file) = ($1, "$path/$2"); | ||
| 89 | if ($type eq 'd') { | ||
| 90 | _crawl($client, $file, $list); | ||
| 91 | } | ||
| 92 | else { | ||
| 93 | push @$list, $file; | ||
| 94 | } | ||
| 95 | } | ||
| 96 | } | ||
| 97 | |||
| 98 | sub fetch_file { | ||
| 99 | my ($self, $from, $to) = @_; | ||
| 100 | my $client = $self->{'_client'}; | ||
| 101 | $client->get($from, $to) | ||
| 102 | or die "Can't fetch $from: ", $client->message; | ||
| 103 | } | ||
| 104 | |||
| 105 | sub basename { | ||
| 106 | (my $path = shift) =~ s{^.+/}{}; | ||
| 107 | return $path; | ||
| 108 | } | ||
| 109 | |||
| 110 | 1; | ||
| 111 | |||
| 112 | =pod | ||
| 113 | |||
| 114 | =head1 NAME | ||
| 115 | |||
| 116 | File::Feed::Source - fetch files from an FTP server | ||
| 117 | |||
| 118 | =cut | ||
| 119 |