AnyEvent-FTP

 view release on metacpan or  search on metacpan

bin/aeftpd  view on Meta::CPAN

  $cred = {
    user => (join '', map { chr(ord('a') + int rand(26)) } (1..10)),
    pass => (join '', map { chr(ord('a') + int rand(26)) } (1..10)),
  };
}
elsif(defined $cred)
{
  my($user,$pass) = split /:/, $cred;
  unless(defined $pass)
  {
    say STDERR "password not provided for --cred option";
    exit 2;
  }
  $cred = {
    user => $user,
    pass => $pass,
  };
}

$default_context = "AnyEvent::FTP::Server::Context::$default_context"
  unless $default_context =~ /::/;

bin/aeftpd  view on Meta::CPAN

Redirect stderr from the daemon to the file specified by the given path

=head2 --chroot

Use C<chroot> to restrict the user to only his home directory once he has logged
in.  This option requires the C<chroot> function, which is supported by Perl on
most UNIX and UNIX like operating systems.

=head2 --cred [ I<user>:I<pass> | random ]

Allow authentication with the given username and password.  If you specify C<random>
then a randomly generated username and password will be used.

=head2 --auth I<class>

Specify a L<Authen::Simple> class to use for authentication.  You should NOT
include the Authen::Simple prefix when specifying the class (that is use
C<PAM> instead of C<Authen::Simple::PAM>).

=head2 --auth I<key>=I<val>

Specify an argument to pass into the chosen L<Authen::Simple> object.

example/fget.pl  view on Meta::CPAN

}

$remote = URI->new($remote);

unless($remote->scheme eq 'ftp')
{
  say STDERR "only FTP URLs are supported";
  exit 2;
}

unless(defined $remote->password)
{
  $remote->password(prompt('p', 'Password: ', '', ''));
  say '';
}

do {
  my $from = $remote->clone;
  $from->password(undef);

  say "SRC: ", $from;
};

my @path = split /\//, $remote->path;
my $fn = pop @path;
if(-e $fn)
{
  say STDERR "local file already exists";
  exit 2;

example/fget.pl  view on Meta::CPAN


$ftp->on_each_response(sub {
  my $res = shift;
  if($debug)
  {
    say sprintf "SERVER: [ %d ] %s", $res->code, $_ for @{ $res->message };
  }
});

$ftp->connect($remote->host, $remote->port)->recv;
$ftp->login($remote->user, $remote->password)->recv;
$ftp->type('I')->recv;

$ftp->cwd(join '/', '', @path)->recv;

my $remote_size;

if($progress)
{
  my $listing = $ftp->list($fn)->recv;
  foreach my $class (qw( File::Listing File::Listing::Ftpcopy ))

example/fls.pl  view on Meta::CPAN

}

$uri = URI->new($uri);

unless($uri->scheme eq 'ftp')
{
  say STDERR "only FTP URL accpeted";
  exit 2;
}

unless(defined $uri->password)
{
  $uri->password(prompt('p', 'Password: ', '', ''));
  say '';
}

my $path = $uri->path;
$uri->path('');

$ftp->connect($uri);

say $_ for @{ $ftp->$method($path)->recv };

example/fput.pl  view on Meta::CPAN


$local  = file($local);
$remote = URI->new($remote);

unless($remote->scheme eq 'ftp')
{
  say STDERR "only FTP URLs are supported";
  exit 2;
}

unless(defined $remote->password)
{
  $remote->password(prompt('p', 'Password: ', '', ''));
  say '';
}

do {
  my $from = URI::file->new_abs($local);
  my $to = $remote->clone;
  $to->password(undef);

  say "SRC: ", $from;
  say "DST: ", $to;
};

my $ftp = AnyEvent::FTP::Client->new( passive => $active ? 0 : 1 );

$ftp->on_send(sub {
  my($cmd, $arguments) = @_;
  $arguments //= '';

example/fput.pl  view on Meta::CPAN

$ftp->on_each_response(sub {
  my $res = shift;
  if($debug)
  {
    say sprintf "SERVER: [ %d ] %s", $res->code, $_ for @{ $res->message };
  }
});


$ftp->connect($remote->host, $remote->port)->recv;
$ftp->login($remote->user, $remote->password)->recv;
$ftp->type('I')->recv;

if(defined $remote->path)
{
  $ftp->cwd($remote->path)->recv;
}

open my $fh, '<', $local;
binmode $fh;

lib/AnyEvent/FTP/Client.pm  view on Meta::CPAN

    );

    $self->on_next_response(sub {
      my $res = shift;
      return $cv->croak($res) unless $res->is_success;
      $self->emit(greeting => $res);
      if(defined $uri)
      {
        my @start_commands = (
          [USER => $uri->user],
          [PASS => $uri->password],
        );
        push @start_commands, [CWD => $uri->path] if $uri->path ne '';
        $self->unshift_command(@start_commands, $cv);
      }
      else
      {
        $cv->send($res);
        $self->pop_command;
      }
    });

lib/AnyEvent/FTP/Client.pm  view on Meta::CPAN

=head2 send

This event gets fired on every command sent to the remote server.  Keep
in mind that some methods of L<AnyEvent::FTP> may make multiple FTP commands
in order to implement their functionality (for example, C<recv>, C<stor>, etc).
One use of this event is to print out commands as they are sent for debugging:

 $client->on_send(sub {
   my($cmd, $arguments) = @_;
   $arguments //= '';
   # hide passwords
   $arguments = 'XXXX' if $cmd =~ /^pass$/i;
   say "CLIENT: $cmd $arguments";
 });

=head2 error

This event is emitted when there is a network error with the remote server.
It passes in a string which describes in human readable description of what
went wrong.

lib/AnyEvent/FTP/Client.pm  view on Meta::CPAN

=item $client-E<gt>connect($host, [ $port ])

The host and port of the remote server.  If not specified, the default FTP port will be used (21).

=item $client-E<gt>connect($uri)

The URI of the remote FTP server.  C<$uri> must be either an instance of L<URI> with the C<ftp>
scheme, or a string with an FTP URL.

If you use this method to connect to the FTP server, connect will also attempt to login with
the username and password specified in the URL (or anonymous FTP if no credentials are
specified).

If there is a path included in the URL, then connect will also do a C<CWD> so that you start
in that directory.

=back

=head2 login

 $client->login($user, $pass);

lib/AnyEvent/FTP/Client.pm  view on Meta::CPAN

=head2 user

 $client->user( $username );

Specify the user to login as.  See C<connect> or C<login> methods for a shortcut.

=head2 pass

 $client->pass( $pass );

Specify the password to use for login.  See C<connect> or C<login> methods for a shortcut.

=head2 acct

 $client->acct( $acct );

Specify user's account.  This is sometimes used for authentication and authorization when you login
to some servers, but is seldom used today in practice.  See RFC959 for details.

=head2 size

lib/AnyEvent/FTP/Client.pm  view on Meta::CPAN

 }
 
 $remote = URI->new($remote);
 
 unless($remote->scheme eq 'ftp')
 {
   say STDERR "only FTP URLs are supported";
   exit 2;
 }
 
 unless(defined $remote->password)
 {
   $remote->password(prompt('p', 'Password: ', '', ''));
   say '';
 }
 
 do {
   my $from = $remote->clone;
   $from->password(undef);
 
   say "SRC: ", $from;
 };
 
 my @path = split /\//, $remote->path;
 my $fn = pop @path;
 if(-e $fn)
 {
   say STDERR "local file already exists";
   exit 2;

lib/AnyEvent/FTP/Client.pm  view on Meta::CPAN

 
 $ftp->on_each_response(sub {
   my $res = shift;
   if($debug)
   {
     say sprintf "SERVER: [ %d ] %s", $res->code, $_ for @{ $res->message };
   }
 });
 
 $ftp->connect($remote->host, $remote->port)->recv;
 $ftp->login($remote->user, $remote->password)->recv;
 $ftp->type('I')->recv;
 
 $ftp->cwd(join '/', '', @path)->recv;
 
 my $remote_size;
 
 if($progress)
 {
   my $listing = $ftp->list($fn)->recv;
   foreach my $class (qw( File::Listing File::Listing::Ftpcopy ))

lib/AnyEvent/FTP/Client.pm  view on Meta::CPAN

 }
 
 $uri = URI->new($uri);
 
 unless($uri->scheme eq 'ftp')
 {
   say STDERR "only FTP URL accpeted";
   exit 2;
 }
 
 unless(defined $uri->password)
 {
   $uri->password(prompt('p', 'Password: ', '', ''));
   say '';
 }
 
 my $path = $uri->path;
 $uri->path('');
 
 $ftp->connect($uri);
 
 say $_ for @{ $ftp->$method($path)->recv };

lib/AnyEvent/FTP/Client.pm  view on Meta::CPAN

 
 $local  = file($local);
 $remote = URI->new($remote);
 
 unless($remote->scheme eq 'ftp')
 {
   say STDERR "only FTP URLs are supported";
   exit 2;
 }
 
 unless(defined $remote->password)
 {
   $remote->password(prompt('p', 'Password: ', '', ''));
   say '';
 }
 
 do {
   my $from = URI::file->new_abs($local);
   my $to = $remote->clone;
   $to->password(undef);
 
   say "SRC: ", $from;
   say "DST: ", $to;
 };
 
 my $ftp = AnyEvent::FTP::Client->new( passive => $active ? 0 : 1 );
 
 $ftp->on_send(sub {
   my($cmd, $arguments) = @_;
   $arguments //= '';

lib/AnyEvent/FTP/Client.pm  view on Meta::CPAN

 $ftp->on_each_response(sub {
   my $res = shift;
   if($debug)
   {
     say sprintf "SERVER: [ %d ] %s", $res->code, $_ for @{ $res->message };
   }
 });
 
 
 $ftp->connect($remote->host, $remote->port)->recv;
 $ftp->login($remote->user, $remote->password)->recv;
 $ftp->type('I')->recv;
 
 if(defined $remote->path)
 {
   $ftp->cwd($remote->path)->recv;
 }
 
 open my $fh, '<', $local;
 binmode $fh;
 

lib/AnyEvent/FTP/Server/Role/Auth.pm  view on Meta::CPAN

  }
  else
  {
    $con->send_response(530 => "USER requires a parameter");
  }

  $self->done;
}


sub help_pass { 'PASS <sp> password' }

sub cmd_pass
{
  my($self, $con, $req) = @_;

  my $user = $self->user;
  my $pass = $req->args;

  unless(defined $user)
  {

lib/AnyEvent/FTP/Server/Role/Auth.pm  view on Meta::CPAN


 use AnyEvent:FTP::Server;
 
 my $server = AnyEvent::FTP::Server->new;
 $server->on_connect(sub {
   # $con isa AnyEvent::FTP::Server::Connection
   my $con = shift;
   # $context isa AnyEvent::FTP::Server::Context::MyContext
   my $context = $con->context;
 
   # allow login from user 'user' with password 'secret'
   $context->authenticator(sub {
     my($user, $pass) = @_;
     return $user eq 'user' && $pass eq 'secret';
   });
 
   # make the client wait 5 seconds if they enter a
   # bad username / password
   $context->bad_authentication_delay(5);
 });

=head1 DESCRIPTION

This role provides an authentication interface for your L<AnyEvent::FTP::Server>
context.

=head1 ATTRIBUTES

=head2 user

The user specified by the last FTP C<USER> command.

=head2 authenticated

True if the user has successfully logged in.

=head2 authenticator

Sub ref used to check username password combinations.
By default all authentication requests are refused.

=head2 bad_authentication_delay

Number of seconds to wait after a bad login attempt.

=head2 unauthenticated_safe_commands

List of the commands that are safe to execute before the user
has authenticated.  The default is USER, PASS, HELP and QUIT

lib/Test/AnyEventFTPServer.pm  view on Meta::CPAN

blocks then you will need to do it in a separate process.
L<AnyEvent::FTP::Client> is a client that doesn't block and so
is safe to use in testing against the server.

=head1 ATTRIBUTES

=head2 test_uri

 my $uri = $test_server->test_uri

The full URL (including host, port, username and password) of the
test ftp server.  This is returned as L<URI>.

=head2 res

 my $res = $test_server->res

The last L<AnyEvent::FTP::Client::Response> object returned from the
server after calling the C<command_ok> method.

=head2 content

lib/Test/AnyEventFTPServer.pm  view on Meta::CPAN

then you want to set this to false.

=head1 METHODS

=head2 create_ftpserver_ok

 my $test_server = create_ftpserver_ok;
 my $test_server = create_ftpserver_ok($default_context);
 my $test_server = create_ftpserver_ok($default_context, $test_name);

Create the FTP server with a random username and password
for logging in.  You can get the username/password from the
C<test_uri> attribute, or connect to the server using
L<AnyEvent::FTP::Client> automatically with the C<connect_ftpclient_ok>
method below.

=head2 connect_ftpclient_ok

 my $client = $test_server->connect_ftpclient_ok;
 my $client = $test_server->connect_ftpclient_ok($test_name);

Connect to the FTP server, return the L<AnyEvent::FTP::Client>

t/anyevent_ftp_client.t  view on Meta::CPAN

  our $detect;

  local $config->{dir} = $CWD;

  prep_client( $client );

  my $uri = URI->new('ftp:');
  $uri->host($config->{host});
  $uri->port($config->{port});
  $uri->user($config->{user});
  $uri->password($config->{pass});
  $uri->path(do {
    my $dir = $config->{dir};
    if($^O eq 'MSWin32')
    {
      (undef,$dir,undef) = File::Spec->splitpath($dir,1);
      $dir =~ s{\\}{/}g;
    }
    $dir;
  });
  isa_ok $uri, 'URI';

t/anyevent_ftp_client.t  view on Meta::CPAN

      eval { $client->quit->recv };
      return;
    }
    isa_ok $res, 'AnyEvent::FTP::Response';
    is $res->code, 250, 'code = 250';
    is $client->pwd->recv, net_pwd($config->{dir}), "dir = " . net_pwd($config->{dir});
    $client->quit->recv;
  };

  $uri->user('bogus');
  $uri->password('bogus');

  SKIP: {
    skip 'bftp quit broken', 2 if $detect->{xb};
    eval { $client->connect($uri->as_string)->recv };
    my $error = $@;
    isa_ok $error, 'AnyEvent::FTP::Response';
    is $error->code, 530, 'code = 530';
    $client->quit->recv;
  };

  $uri->user($config->{user});
  $uri->password($config->{pass});
  $uri->path('/bogus/bogus/bogus');

  SKIP: {
    skip 'bftp quit broken', 2 if $detect->{xb};
    eval { $client->connect($uri->as_string)->recv };
    my $error = $@;
    isa_ok $error, 'AnyEvent::FTP::Response';
    is $error->code, 550, 'code = 550';
    $client->quit->recv;
  };



( run in 0.859 second using v1.01-cache-2.11-cpan-49f99fa48dc )