blib/lib/POE/Component/Client/SimpleFTP.pm

filter: Statementstmt 20.6% 97 / 469 Branchbran 0.6% 1 / 144 Conditioncond 0.0% 0 / 18 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 25.7% 17 / 66 Podpod 7.6% 1 / 13 totaltotal 16.0% 116 / 722 CC 80 SCAR 28.4
CC
80
Cov
16%
BUILD
72.0
__ANON__
42.0
__ANON__
30.0
CRAP 17.1
↑ Summary Build »
1 #
2 # This file is part of POE-Component-Client-SimpleFTP
3 #
4 # This software is copyright (c) 2011 by Apocalypse.
5 #
6 # This is free software; you can redistribute it and/or modify it under
7 # the same terms as the Perl 5 programming language system itself.
8 #
9 1 ▶use strict; use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
10 package POE::Component::Client::SimpleFTP;
11 BEGIN {
12 1 ▶ $POE::Component::Client::SimpleFTP::VERSION = '0.003';
Subroutine BEGIN: called CC=1 SCAR=0.0
13 }
14 BEGIN {
15 1 ▶ $POE::Component::Client::SimpleFTP::AUTHORITY = 'cpan:APOCAL';
Subroutine BEGIN: called CC=1 SCAR=0.0
16 }
17  
18 # ABSTRACT: A simple FTP client library for POE
19  
20 1 ▶use POE::Component::Client::SimpleFTP::Utils qw( :code EOL );
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 ▶use MooseX::POE::SweetArgs;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 1 ▶use POE;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 1 ▶use POE::Wheel::SocketFactory;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 1 ▶use POE::Wheel::ReadWrite;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 1 ▶use POE::Filter::Stream;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 1 ▶use POE::Filter::Line;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 1 ▶use POE::Driver::SysRW;
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 1 ▶use Socket qw( INADDR_ANY AF_INET SOCK_STREAM unpack_sockaddr_in inet_ntoa );
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 BEGIN {
32  
33  
34 1 ▶ if ( ! defined &DEBUG ) { *DEBUG = sub () { 0 } }
Subroutine BEGIN: called CC=2 SCAR=7.2
Branch: 1 branch
BranchTrueFalseTotal
if (not defined &POE::Component::Client::SimpleFTP::DEBUG) 1 0 1
35 }
36  
37  
38 1 has alias => (
39 isa => 'Str',
40 is => 'ro',
41 default => 'ftp',
42 );
43  
44  
45 1 has username => (
46 isa => 'Str',
47 is => 'ro',
48 required => 1,
49 );
50  
51  
52 1 has password => (
53 isa => 'Str',
54 is => 'ro',
55 required => 1,
56 );
57  
58  
59 1 has remote_addr => (
60 isa => 'Str',
61 is => 'ro',
62 required => 1,
63 );
64  
65  
66 1 has remote_port => (
67 isa => 'Int',
68 is => 'ro',
69 default => 21,
70 );
71  
72  
73 1 has local_addr => (
74 isa => 'Str',
75 is => 'ro',
76 default => INADDR_ANY,
77 );
78  
79  
80 1 has local_port => (
81 isa => 'Int',
82 is => 'ro',
83 default => 0,
84 );
85  
86  
87 1 has local_data_port => (
88 isa => 'Int',
89 is => 'ro',
90 default => 0,
91 );
92  
93  
94 1 has tls_cmd => (
95 isa => 'Bool',
96 is => 'ro',
97 writer => '_set_tls_cmd',
98 default => 0,
99 );
100  
101  
102 1 has tls_data => (
103 isa => 'Bool',
104 is => 'ro',
105 writer => '_set_tls_data',
106 default => 0,
107 );
108  
109  
110 1 has timeout => (
111 isa => 'Int',
112 is => 'ro',
113 default => 120,
114 );
115  
116  
117 {
118 1 ▶ use Moose::Util::TypeConstraints;
Subroutine BEGIN: called CC=1 SCAR=0.0
119  
120 1 has connection_mode => (
121 isa => enum( [ qw( active passive ) ] ),
122 is => 'ro',
123 default => 'passive',
124 );
125 }
126  
127 ### internal attributes
128  
129 # the socketfactory/readwrite wheels for the command connection
130 1 has cmd_sf => (
131 isa => 'Maybe[POE::Wheel::SocketFactory]',
132 is => 'rw',
133 init_arg => undef,
134 );
135  
136 1 has cmd_rw => (
137 isa => 'Maybe[POE::Wheel::ReadWrite]',
138 is => 'rw',
139 init_arg => undef,
140 );
141  
142 # the socketfactory/readwrite wheels for the data connection
143 1 has data_sf => (
144 isa => 'Maybe[POE::Wheel::SocketFactory]',
145 is => 'rw',
146 init_arg => undef,
147 );
148  
149 1 has data_rw => (
150 isa => 'Maybe[POE::Wheel::ReadWrite]',
151 is => 'rw',
152 init_arg => undef,
153 );
154  
155 {
156 1 ▶ use Moose::Util::TypeConstraints;
Subroutine BEGIN: called CC=1 SCAR=0.0
157  
158 # it is valid until the next complex command, then we check this to see
159 # what we need to do
160 # A = ascii
161 # I = image
162 1 has data_type => (
163 isa => enum( [ qw( A I ) ] ),
164 is => 'rw',
165 init_arg => undef,
166 predicate => '_has_data_type',
167 );
168 }
169  
170 # holds data for multi-line replies
171 1 has input_buffer => (
172 isa => 'Maybe[Str]',
173 is => 'rw',
174 init_arg => undef,
175 default => undef,
176 );
177  
178 1 has input_buffer_code => (
179 isa => 'Int',
180 is => 'rw',
181 init_arg => undef,
182 );
183  
184 # the session that spawned us and receives events from us
185 1 has _master => (
186 isa => 'Int',
187 is => 'rw',
188 init_arg => undef,
189 );
190  
191 # the internal state of the connection
192 has state => (
193 isa => 'Str',
194 is => 'rw',
195 default => 'connect',
196 init_arg => undef,
197 ( DEBUG ? ( trigger => sub {
198 0 ▶ my( $self, $new, $old ) = @_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
199 0 warn "switching from state($old) to state($new)\n";
200 0 return;
201 1 } ) : () ),
202 );
203  
204 # holds what "simple" command we are processing when state is 'simple_command'
205 1 has simple_command => (
206 isa => 'Str',
207 is => 'rw',
208 init_arg => undef,
209 );
210  
211 # holds whatever data the command needs
212 has command_data => (
213 isa => 'Any',
214 is => 'rw',
215 0 ▶ default => sub { {} },
Subroutine __ANON__: not called CC=1 SCAR=6.9
216 1 init_arg => undef,
217 );
218  
219 # translation from posted events to ftp commands
220 1 my %command_map = (
221 'cd' => "CWD",
222 'mkdir' => "MKD",
223 'rmdir' => "RMD",
224 'ls' => "LIST",
225 'dir' => "NLST",
226 'get' => "RETR",
227 'put' => "STOR",
228 'delete' => "DELE",
229 'quote' => "QUOT",
230 'disconnect' => "QUIT",
231 'features' => "FEAT",
232 'options' => "OPTS",
233 );
234  
235 1 my @simple_commands = ( qw(
236 cdup mdtm noop pwd site size stat syst help acct smnt
237 cd cwd
238 dele delete
239 mkd mkdir
240 rmd rmdir
241 quot quote
242 quit disconnect
243 feat features
244 opts options
245 ) );
246  
247 1 my @complex_commands = ( qw(
248 list ls
249 nlst dir
250 retr get
251 stor stou put
252 ) );
253  
254 # a simple command that forces shutdown
255 event shutdown => sub {
256 0 ▶ my $self = shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
257  
258 0 $self->_shutdown;
259 0 return;
260 1 };
261  
262 # build our "simple" command handlers
263 1 foreach my $cmd ( @simple_commands ) {
264 event $cmd => sub {
265 0 ▶ my( $self, @args ) = @_;
Subroutine __ANON__: not called CC=5 SCAR=34.0
266 0 my $command = $cmd;
267  
268 # ignore commands if we are shutting down
269 0 ▶ return if $self->state eq 'shutdown';
Branch: 1 branch
BranchTrueFalseTotal
if $self->state eq "shutdown" 0 0 0
270  
271 # are we already sending a command?
272 0 ▶ if ( $self->state ne 'idle' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->state ne "idle") 0 0 0
273 # special-case the quit/disconnect methods
274 0 ▶ if ( $cmd =~ /^(?:quit|disconnect)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cmd =~ /^(?:quit|disconnect)$/) { } 0 0 0
275 0 $self->_shutdown;
276 0 return;
277 } else {
278 0 die "Unable to send '$cmd' because we are processing " . $self->state;
279 }
280 }
281  
282 # do we need to translate the command to the actual FTP command?
283 0 ▶ if ( exists $command_map{ $cmd } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $command_map{$cmd}) 0 0 0
284 0 $command = $command_map{ $cmd };
285 }
286  
287 # store the command we are processing then send it
288 0 warn "doing simple_command($cmd) with data(" . join( ' ', @args ) . ")\n" if DEBUG;
289 0 $self->simple_command( $cmd );
290 0 $self->command_data( \@args );
291 0 $self->command( 'simple_command', $command, @args );
292 0 return;
293 27 };
294 }
295  
296 # build our "complex" command handlers ( they require a data connection )
297 1 foreach my $cmd ( @complex_commands ) {
298 event $cmd => sub {
299 0 ▶ my( $self, @args ) = @_;
Subroutine __ANON__: not called CC=5 SCAR=34.0
300  
301 # ignore commands if we are shutting down
302 0 ▶ return if $self->state eq 'shutdown';
Branch: 1 branch
BranchTrueFalseTotal
if $self->state eq "shutdown" 0 0 0
303  
304 # are we already sending a command?
305 0 ▶ if ( $self->state ne 'idle' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->state ne "idle") 0 0 0
306 0 die "Unable to send '$cmd' because we are processing " . $self->state;
307 }
308  
309 # start doing this command!
310 0 warn "doing complex command($cmd) with data(" . join( ' ', @args ) . ")\n" if DEBUG;
311 0 $self->command_data( {
312 'cmd' => $cmd,
313 'data' => \@args,
314 } );
315 0 ▶ if ( $cmd =~ /^(?:ls|dir|list|nlst)$/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($cmd =~ /^(?:ls|dir|list|nlst)$/) { } 0 0 0
elsif ($cmd =~ /^(?:get|put|retr|stor|stou)$/) { } 0 0 0
316 0 $self->prepare_listing;
317 } elsif ( $cmd =~ /^(?:get|put|retr|stor|stou)$/ ) {
318 0 $self->prepare_transfer;
319 }
320  
321 0 return;
322 9 };
323 }
324  
325 # rename support
326 1 foreach my $cmd ( qw( rename mv ) ) {
327 event $cmd => sub {
328 0 ▶ my( $self, $from, $to ) = @_;
Subroutine __ANON__: not called CC=3 SCAR=24.8
329  
330 # ignore commands if we are shutting down
331 0 ▶ return if $self->state eq 'shutdown';
Branch: 1 branch
BranchTrueFalseTotal
if $self->state eq "shutdown" 0 0 0
332  
333 # are we already sending a command?
334 0 ▶ if ( $self->state ne 'idle' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->state ne "idle") 0 0 0
335 0 die "Unable to send '$cmd' because we are processing " . $self->state;
336 }
337  
338 # Start the rename!
339 $self->command_data( {
340 0 from => $from,
341 to => $to,
342 } );
343 0 $self->command( 'rename_start', 'RNFR', $from );
344  
345 0 return;
346 2 };
347 }
348  
349 sub prepare_listing {
350 0 ▶ my $self = shift;
Subroutine prepare_listing: not called CC=3 SCAR=24.8
Pod undocumented
351  
352 # do we need to set the TYPE?
353 0 ▶ if ( ! $self->_has_data_type or $self->data_type eq 'I' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->_has_data_type or $self->data_type eq 'I') { } 0 0 0
Condition0%0/3
not $self->_has_data_type or $self->data_type eq "I"
l!l&&r!l&&!r
000
MC/DC0%0/2
not $self->_has_data_type or $self->data_type eq "I"
not $self->_has_data_type $self->data_type eq "I"
Truth table0%0/3
not(A) or B
A= not $self->_has_data_typeB= $self->data_type eq "I"
ABresult
1X1
011
000
354 0 $self->command_data->{'type'} = 'A';
355 0 $self->command( 'complex_type', 'TYPE', 'A' );
356 } else {
357 # Okay, proceed to start the data connection stuff
358 0 $self->start_data_connection;
359 }
360 }
361  
362 sub prepare_transfer {
363 0 ▶ my $self = shift;
Subroutine prepare_transfer: not called CC=3 SCAR=24.8
Pod undocumented
364  
365 # do we need to set the TYPE?
366 0 ▶ if ( ! $self->_has_data_type or $self->data_type eq 'A' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->_has_data_type or $self->data_type eq 'A') { } 0 0 0
Condition0%0/3
not $self->_has_data_type or $self->data_type eq "A"
l!l&&r!l&&!r
000
MC/DC0%0/2
not $self->_has_data_type or $self->data_type eq "A"
not $self->_has_data_type $self->data_type eq "A"
Truth table0%0/3
not(A) or B
A= not $self->_has_data_typeB= $self->data_type eq "A"
ABresult
1X1
011
000
367 0 $self->command_data->{'type'} = 'I';
368 0 $self->command( 'complex_type', 'TYPE', 'I' );
369 } else {
370 # Okay, proceed to start the data connection stuff
371 0 $self->start_data_connection;
372 }
373 }
374  
375 sub start_data_connection {
376 0 ▶ my $self = shift;
Subroutine start_data_connection: not called CC=2 SCAR=17.9
Pod undocumented
377  
378 # okay, we go ahead with the PASV/PORT command
379 0 ▶ if ( $self->connection_mode eq 'passive' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->connection_mode eq 'passive') { } 0 0 0
380 0 $self->command( 'complex_pasv', 'PASV' );
381 } else {
382 # Okay, create our listening socket
383 0 $self->create_data_connection;
384 }
385 }
386  
387 # build our data complex command handlers
388 1 foreach my $cmd ( qw( put stor stou ) ) {
389 event "${cmd}_data" => sub {
390 0 ▶ my( $self, $input ) = @_;
Subroutine __ANON__: not called CC=5 SCAR=34.0
391  
392 # don't print the input as it could be binary stuff
393 0 warn "received ${cmd}_data\n" if DEBUG;
394  
395 # ignore commands if we are shutting down
396 0 ▶ return if $self->state eq 'shutdown';
Branch: 1 branch
BranchTrueFalseTotal
if $self->state eq "shutdown" 0 0 0
397  
398 # should only happen in complex state
399 0 ▶ if ( $self->state eq 'complex_data' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->state eq 'complex_data') { } 0 0 0
400 # This should only happen for put commands
401 0 ▶ if ( $self->command_data->{'cmd'} eq $cmd ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->command_data->{'cmd'} eq $cmd) { } 0 0 0
402 # send the data to our rw wheel
403 0 ▶ if ( defined $self->data_rw ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->data_rw) { } 0 0 0
404 0 $self->data_rw->put( $input );
405 } else {
406 0 die "got ${cmd}_data when we are not connected!";
407 }
408 } else {
409 0 die "got ${cmd}_data when we are not doing a STOR";
410 }
411 } else {
412 0 die "got ${cmd}_data when we are in wrong state: " . $self->state;
413 }
414  
415 0 return;
416 3 };
417  
418 event "${cmd}_close" => sub {
419 0 ▶ my $self = shift;
Subroutine __ANON__: not called CC=5 SCAR=34.0
420  
421 0 warn "received ${cmd}_close\n" if DEBUG;
422  
423 # ignore commands if we are shutting down
424 0 ▶ return if $self->state eq 'shutdown';
Branch: 1 branch
BranchTrueFalseTotal
if $self->state eq "shutdown" 0 0 0
425  
426 # should only happen in complex state
427 0 ▶ if ( $self->state eq 'complex_data' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->state eq 'complex_data') { } 0 0 0
428 # This should only happen for put commands
429 0 ▶ if ( $self->command_data->{'cmd'} eq $cmd ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->command_data->{'cmd'} eq $cmd) { } 0 0 0
430 # kill the rw wheel, disconnecting from the server
431 0 ▶ if ( defined $self->data_rw ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->data_rw) { } 0 0 0
432 0 $self->process_complex_closed;
433 } else {
434 # maybe a timing issue, server killed the connection while this event was in the queue?
435 # then the data_rw_error event would have caught this and sent the appropriate error message
436 0 warn "unable to ${cmd}_close as wheel is gone\n" if DEBUG;
437 }
438 } else {
439 0 die "got ${cmd}_close when we are not doing a STOR";
440 }
441 } else {
442 0 die "got ${cmd}_close when we are in wrong state: " . $self->state;
443 }
444  
445 0 return;
446 3 };
447 }
448  
449 sub BUILD {
450 0 ▶ my $self = shift;
Subroutine BUILD: not called CC=8 SCAR=42.8
Pod undocumented
451  
452 # Did we enable TLS mode?
453 0 ▶ if ( $self->tls_cmd or $self->tls_data ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->tls_cmd or $self->tls_data) 0 0 0
Condition0%0/3
$self->tls_cmd or $self->tls_data
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->tls_cmd or $self->tls_data
$self->tls_cmd $self->tls_data
Truth table0%0/3
A or B
A= $self->tls_cmdB= $self->tls_data
ABresult
1X1
011
000
454 0 eval 'require POE::Component::SSLify';
455 0 ▶ if ( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
456 0 warn "Unable to use SSLify: $@\n";
457 0 $self->_set_tls_cmd( 0 );
458 0 $self->_set_tls_data( 0 );
459 }
460 }
461  
462 # Make sure that the local_port and local_data_port is different!
463 0 ▶ if ( $self->local_port == $self->local_data_port and $self->local_port != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->local_port == $self->local_data_port and $self->local_port != 0) 0 0 0
Condition0%0/3
$self->local_port == $self->local_data_port and $self->local_port != 0
!ll&&!rl&&r
000
MC/DC0%0/2
$self->local_port == $self->local_data_port and $self->local_port != 0
$self->local_port == $self->local_data_port $self->local_port != 0
Truth table0%0/3
A and B
A= $self->local_port == $self->local_data_portB= $self->local_port != 0
ABresult
0X0
100
111
464 0 die "Please specify different local_port and local_data_port settings!";
465 }
466  
467 # In order to use active mode connection, we MUST provide a local IP address to bind
468 # otherwise getsockname() on INADDR_ANY unsurprisingly returns 0.0.0.0 which is worthless for the PORT command!
469 0 ▶ if ( $self->connection_mode eq 'active' and $self->local_addr eq '0.0.0.0' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->connection_mode eq "active" and $self->local_addr eq "0.0.0.0") 0 0 0
Condition0%0/3
$self->connection_mode eq "active" and $self->local_addr eq "0.0.0.0"
!ll&&!rl&&r
000
MC/DC0%0/2
$self->connection_mode eq "active" and $self->local_addr eq "0.0.0.0"
$self->connection_mode eq "active" $self->local_addr eq "0.0.0.0"
Truth table0%0/3
A and B
A= $self->connection_mode eq "active"B= $self->local_addr eq "0.0.0.0"
ABresult
0X0
100
111
470 0 die "Please specify a local_addr address to bind to for active connections!";
471 }
472  
473 # Figure out who called us so we store it for events
474 0 $self->_master( $poe_kernel->get_active_session->ID );
475 }
476  
477 # Okay, go connect to the host!
478 sub START {
479 0 ▶ my $self = shift;
Subroutine START: not called CC=1 SCAR=6.9
Pod undocumented
480  
481 0 warn "starting\n" if DEBUG;
482  
483 0 $poe_kernel->alias_set( $self->alias );
484  
485 # set a timeout before trying to connect
486 0 $poe_kernel->delay( 'timeout_event' => $self->timeout );
487 0 $self->cmd_sf( POE::Wheel::SocketFactory->new(
488 SocketDomain => AF_INET,
489 SocketType => SOCK_STREAM,
490 SocketProtocol => 'tcp',
491 RemoteAddress => $self->remote_addr,
492 RemotePort => $self->remote_port,
493 BindAddr => $self->local_addr,
494 BindPort => $self->local_port,
495 SuccessEvent => 'cmd_sf_connected',
496 FailureEvent => 'cmd_sf_error'
497 ) );
498  
499 0 return;
500 }
501  
502 # helper sub to simplify sending events to the master
503 sub tell_master {
504 0 ▶ my( $self, $event, @args ) = @_;
Subroutine tell_master: not called CC=1 SCAR=6.9
Pod undocumented
505  
506 0 warn "telling master about event $event\n" if DEBUG;
507  
508 0 $poe_kernel->post( $self->_master, $event, @args );
509 }
510  
511 # shutdown the connection
512 sub _shutdown {
513 0 ▶ my $self = shift;
Subroutine _shutdown: not called CC=1 SCAR=6.9
514  
515 0 warn "shutdown\n" if DEBUG;
516 0 $self->state( 'shutdown' );
517  
518 # destroy our wheels
519 0 $self->cmd_sf( undef );
520 0 $self->cmd_rw( undef );
521 0 $self->data_sf( undef );
522 0 $self->data_rw( undef );
523  
524 # remove the timeout if it exists
525 0 $poe_kernel->delay( 'timeout_event' );
526  
527 0 $poe_kernel->alias_remove( $self->alias );
528 }
529  
530  
531 sub yield {
532 0 ▶ my( $self, @args ) = @_;
Subroutine yield: not called CC=1 SCAR=6.9
Pod documented
533 0 $poe_kernel->post( $self->get_session_id, @args );
534 }
535  
536 event _child => sub {
537 0 ▶ return;
Subroutine __ANON__: not called CC=1 SCAR=6.9
538 1 };
539  
540 event timeout_event => sub {
541 0 ▶ my $self = shift;
Subroutine __ANON__: not called CC=3 SCAR=24.8
542  
543 # Okay, we timed out doing something
544 0 ▶ if ( $self->state eq 'connect' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->state eq 'connect') { } 0 0 0
elsif ($self->state eq 'complex_sf') { } 0 0 0
545 # failed to connect to the server
546 0 $self->tell_master( 'connect_error', 0, 'timedout' );
547  
548 # nothing else to do...
549 0 $self->_shutdown;
550 } elsif ( $self->state eq 'complex_sf' ) {
551 # timed out waiting for the data connection
552  
553 # since this is a pre-data-connection error, the complex command is done
554 0 $self->process_complex_error( 0, 'timedout' );
555 0 $self->state( 'idle' );
556 } else {
557 0 die "unknown state in timeout_event: " . $self->state;
558 }
559  
560 0 return;
561 1 };
562  
563 event cmd_sf_connected => sub {
564 0 ▶ my( $self, $fh, $host, $port, $wheel_id ) = @_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
565  
566 0 warn "cmd_sf_connected\n" if DEBUG;
567  
568 # remove the timeout
569 0 $poe_kernel->delay( 'timeout_event' );
570  
571 # convert it into a readwrite wheel
572 0 $self->cmd_rw( POE::Wheel::ReadWrite->new(
573 Handle => $fh,
574 Filter => POE::Filter::Line->new( Literal => EOL ),
575 Driver => POE::Driver::SysRW->new,
576 InputEvent => 'cmd_rw_input',
577 ErrorEvent => 'cmd_rw_error',
578 ) );
579  
580 0 return;
581 1 };
582  
583 event cmd_sf_error => sub {
584 0 ▶ my( $self, $operation, $errnum, $errstr, $wheel_id ) = @_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
585  
586 0 warn "cmd_sf_error $operation $errnum $errstr\n" if DEBUG;
587  
588 0 $self->tell_master( 'connect_error', 0, "$operation error $errnum: $errstr" );
589  
590 # nothing else to do...
591 0 $self->_shutdown;
592  
593 0 return;
594 1 };
595  
596 event cmd_rw_input => sub {
597 0 ▶ my( $self, $input, $wheel_id ) = @_;
Subroutine __ANON__: not called CC=6 SCAR=37.4
598  
599 0 warn "cmd_rw_input(" . $self->state . "): '$input'\n" if DEBUG;
600  
601 # parse the input according to RFC 959
602 # TODO put this code in POE::Filter::FTP or something?
603 0 my( $code, $line );
604 0 ▶ if ( $input =~ /^(\d\d\d)(\-?)(.+)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($input =~ /^(\d\d\d)(\-?)(.+)$/) { } 0 0 0
605 0 $code = $1;
606 0 my( $minus, $string ) = ( $2, $3 );
607 0 $string =~ s/^\s+//;
608 0 $string =~ s/\s+$//;
609  
610 0 ▶ if ( length $minus ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $minus) { } 0 0 0
611 # begin of multi-line reply
612 0 warn "begin of multi-line($code): '$string'\n" if DEBUG;
613 0 $self->input_buffer( $string );
614 0 $self->input_buffer_code( $code );
615 0 return;
616 } else {
617 # end of multi-line reply?
618 0 ▶ if ( defined $self->input_buffer ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->input_buffer) { } 0 0 0
619 # Make sure the code matches!
620 0 ▶ if ( $self->input_buffer_code != $code ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->input_buffer_code != $code) { } 0 0 0
621 0 die "ftpd sent invalid reply: $input";
622 } else {
623 0 warn "end of multi-line: '$string'\n" if DEBUG;
624 0 $line = $self->input_buffer . "\n" . $input;
625 0 $self->input_buffer( undef );
626 }
627 } else {
628 0 $line = $string;
629 }
630 }
631 } else {
632 # If we are in a multi-line reply, just collect the input
633 0 ▶ if ( defined $self->input_buffer ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->input_buffer) { } 0 0 0
634 # per the RFC, the first character should be padded by a space if needed
635 0 $input =~ s/^\s//;
636 0 warn "got multi-line input: '$input'\n" if DEBUG;
637 0 $self->input_buffer( $self->input_buffer . "\n" . $input );
638 0 return;
639 } else {
640 0 die "ftpd sent invalid reply: $input";
641 }
642 }
643  
644 # process the input, depending on our state
645 0 my $subref = "_ftpd_" . $self->state;
646 0 warn "calling $subref to process $code:$line\n" if DEBUG;
647 0 $self->$subref( $code, $line );
648  
649 0 return;
650 1 };
651  
652 event cmd_rw_error => sub {
653 0 ▶ my( $self, $operation, $errnum, $errstr, $wheel_id) = @_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
654  
655 0 warn "cmd_rw_error $operation $errnum $errstr\n" if DEBUG;
656  
657 0 $self->tell_master( 'connect_error', 0, "$operation error $errnum: $errstr" );
658  
659 # nothing else to do...
660 0 $self->_shutdown;
661  
662 0 return;
663 1 };
664  
665 # sets the state for a command and sends it over the control connection
666 sub command {
667 0 ▶ my( $self, $state, $cmd, @args ) = @_;
Subroutine command: not called CC=3 SCAR=24.8
Pod undocumented
668  
669 # If we don't have a readwrite wheel, then we can't send anything!
670 0 ▶ if ( ! defined $self->cmd_rw ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->cmd_rw) 0 0 0
671 0 die "Unable to send '$cmd' as we aren't connected!";
672 }
673  
674 # change to the specified state, then send the args!
675 0 $self->state( $state );
676 0 ▶ if ( $cmd eq 'QUOT' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cmd eq "QUOT") 0 0 0
677 # user-defined string, send it as-is!
678 0 $cmd = shift @args;
679 }
680  
681 0 $cmd = uc $cmd; # to make sure
682 0 my $cmdstr = join( ' ', $cmd, @args );
683 0 warn "sending command '$cmdstr'\n" if DEBUG;
684 0 $self->cmd_rw->put( $cmdstr );
685 }
686  
687 sub _ftpd_idle {
688 0 ▶ my( $self, $code, $reply ) = @_;
Subroutine _ftpd_idle: not called CC=1 SCAR=6.9
689  
690 0 die "unexpected text while we are idle: $code $reply";
691 }
692  
693 # the first line of text we received from the ftpd ( the greeting )
694 sub _ftpd_connect {
695 0 ▶ my( $self, $code, $reply ) = @_;
Subroutine _ftpd_connect: not called CC=3 SCAR=24.8
696  
697 0 ▶ if ( code_success( $code ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (code_success($code)) { } 0 0 0
698 0 $self->tell_master( 'connected', $code, $reply );
699  
700 # do we want TLS?
701 0 ▶ if ( $self->tls_cmd ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->tls_cmd) { } 0 0 0
702 # begin TLS authentication procedure, as per RFC 2228 / 4217
703 0 $self->command( 'tls_cmd', 'AUTH', 'TLS' );
704 } else {
705 # send the username!
706 0 $self->command( 'user', 'USER', $self->username );
707 }
708 } else {
709 0 $self->tell_master( 'connect_error', $code, $reply );
710  
711 # nothing else to do...
712 0 $self->_shutdown;
713 }
714 }
715  
716 sub _ftpd_tls_cmd {
717 0 ▶ my( $self, $code, $reply ) = @_;
Subroutine _ftpd_tls_cmd: not called CC=3 SCAR=24.8
718  
719 0 ▶ if ( code_success( $code ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (code_success($code)) { } 0 0 0
720 # Okay, time to SSLify the connection!
721 0 my $socket = $self->cmd_rw->get_input_handle();
722 0 $self->cmd_rw( undef );
723  
724 0 eval { $socket = POE::Component::SSLify::Client_SSLify( $socket, 'tlsv1' ) };
725 0 ▶ if ( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
726 0 die "Unable to SSLify control connection: $@";
727 }
728  
729 # set up the rw wheel again
730 0 $self->cmd_rw( POE::Wheel::ReadWrite->new(
731 Handle => $socket,
732 Filter => POE::Filter::Line->new( Literal => EOL ),
733 Driver => POE::Driver::SysRW->new,
734 InputEvent => 'rw_input',
735 ErrorEvent => 'rw_error',
736 ) );
737  
738 # Now, send the username!
739 0 $self->command( 'user', 'USER', $self->username );
740 } else {
741 # server probably doesn't support AUTH TLS
742 0 $self->tell_master( 'login_error', $code, $reply );
743 0 $self->state( 'idle' );
744 }
745 }
746  
747 sub _ftpd_pbsz {
748 0 ▶ my( $self, $code, $reply ) = @_;
Subroutine _ftpd_pbsz: not called CC=2 SCAR=17.9
749  
750 0 ▶ if ( code_success( $code ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (code_success($code)) { } 0 0 0
751 0 $self->command( 'prot', 'PROT', 'P' );
752 } else {
753 0 $self->tell_master( 'login_error', $code, $reply );
754 0 $self->state( 'idle' );
755 }
756 }
757  
758 sub _ftpd_prot {
759 0 ▶ my( $self, $code, $reply ) = @_;
Subroutine _ftpd_prot: not called CC=2 SCAR=17.9
760  
761 0 ▶ if ( code_success( $code ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (code_success($code)) { } 0 0 0
762 0 $self->tell_master( 'authenticated' );
763 } else {
764 0 $self->tell_master( 'login_error', $code, $reply );
765 }
766  
767 0 $self->state( 'idle' );
768 }
769  
770 sub _ftpd_user {
771 0 ▶ my( $self, $code, $reply ) = @_;
Subroutine _ftpd_user: not called CC=3 SCAR=24.8
772  
773 0 ▶ if ( code_success( $code ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (code_success($code)) { } 0 0 0
elsif (code_intermediate($code)) { } 0 0 0
774 # no need for password ( probably anonymous account )
775 0 $self->prepare_tls_stuff;
776 } elsif ( code_intermediate( $code ) ) {
777 # send the password!
778 0 $self->command( 'password', 'PASS', $self->password );
779 } else {
780 0 $self->tell_master( 'login_error', $code, $reply );
781 0 $self->state( 'idle' );
782 }
783 }
784  
785 sub prepare_tls_stuff {
786 0 ▶ my $self = shift;
Subroutine prepare_tls_stuff: not called CC=2 SCAR=17.9
Pod undocumented
787  
788 # do we need to setup the data channel TLS stuff?
789 0 ▶ if ( $self->tls_data ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->tls_data) { } 0 0 0
790 # TODO is 0 a good default?
791 0 $self->command( 'pbsz', 'PBSZ', 0 );
792 } else {
793 0 $self->tell_master( 'authenticated' );
794 0 $self->state( 'idle' );
795 }
796 }
797  
798 sub _ftpd_password {
799 0 ▶ my( $self, $code, $reply ) = @_;
Subroutine _ftpd_password: not called CC=2 SCAR=17.9
800  
801 0 ▶ if ( code_success( $code ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (code_success($code)) { } 0 0 0
802 0 $self->prepare_tls_stuff;
803 } else {
804 0 $self->tell_master( 'login_error', $code, $reply );
805 0 $self->state( 'idle' );
806 }
807 }
808  
809 sub _ftpd_simple_command {
810 0 ▶ my( $self, $code, $reply ) = @_;
Subroutine _ftpd_simple_command: not called CC=3 SCAR=24.8
811  
812 # special-case for quit
813 0 ▶ if ( $self->simple_command =~ /^(?:quit|disconnect)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->simple_command =~ /^(?:quit|disconnect)$/) 0 0 0
814 0 $self->_shutdown;
815 0 return;
816 }
817  
818 0 my $event = $self->simple_command;
819 0 ▶ if ( ! code_success( $code ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (code_success($code)) 0 0 0
820 0 $event .= '_error';
821 }
822 0 $self->tell_master( $event, $code, $reply, @{ $self->command_data } );
823  
824 0 $self->command_data( undef );
825 0 $self->state( 'idle' );
826 }
827  
828 sub _ftpd_complex_type {
829 0 ▶ my( $self, $code, $reply ) = @_;
Subroutine _ftpd_complex_type: not called CC=2 SCAR=17.9
830  
831 0 ▶ if ( code_success( $code ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (code_success($code)) { } 0 0 0
832 0 $self->data_type( delete $self->command_data->{'type'} );
833 0 $self->start_data_connection;
834 } else {
835 # since this is a pre-data-connection error, the complex command is done
836 0 $self->process_complex_error( $code, $reply );
837 0 $self->state( 'idle' );
838 }
839 }
840  
841 sub _ftpd_complex_pasv {
842 0 ▶ my( $self, $code, $reply ) = @_;
Subroutine _ftpd_complex_pasv: not called CC=2 SCAR=17.9
843  
844 0 ▶ if ( code_success( $code ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (code_success($code)) { } 0 0 0
845 # Got the server's data!
846 # TODO the RFC is fuzzy about it, but can the port digit be negative?!?!
847 # http://cr.yp.to/ftp/retr.html
848 0 my @data = $reply =~ /(\d+),(\d+),(\d+),(\d+),(\d+),(\d+)/;
849 0 $self->command_data->{'ip'} = join '.', @data[0 .. 3];
850 0 $self->command_data->{'port'} = $data[4]*256 + $data[5];
851  
852 # Okay, create our listening socket
853 0 $self->create_data_connection;
854 } else {
855 # since this is a pre-data-connection error, the complex command is done
856 0 $self->process_complex_error( $code, $reply );
857 0 $self->state( 'idle' );
858 }
859 }
860  
861 sub _ftpd_complex_port {
862 0 ▶ my( $self, $code, $reply ) = @_;
Subroutine _ftpd_complex_port: not called CC=2 SCAR=17.9
863  
864 0 ▶ if ( code_success( $code ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (code_success($code)) { } 0 0 0
865 # now, send the actual complex command :)
866 0 $self->process_complex_command;
867 } else {
868 # since this is a pre-data-connection error, the complex command is done
869 0 $self->process_complex_error( $code, $reply );
870 0 $self->state( 'idle' );
871 }
872 }
873  
874 sub create_data_connection {
875 0 ▶ my $self = shift;
Subroutine create_data_connection: not called CC=3 SCAR=24.8
Pod undocumented
876  
877 # we now transition to the "complex" state
878 # the "real" state is kept in $self->command_data->{cmd}
879  
880 # the arguments to socketfactory depend on whether we are in active or passive mode
881 0 my %sf_args = (
882 SocketDomain => AF_INET,
883 SocketType => SOCK_STREAM,
884 SocketProtocol => 'tcp',
885 SuccessEvent => 'data_sf_connected',
886 FailureEvent => 'data_sf_error',
887  
888 BindAddr => $self->local_addr,
889 BindPort => $self->local_data_port,
890 );
891  
892 0 ▶ if ( $self->connection_mode eq 'passive' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->connection_mode eq 'passive') { } 0 0 0
893 # use the ip/port we already received
894 0 $sf_args{ RemoteAddress } = $self->command_data->{'ip'};
895 0 $sf_args{ RemotePort } = $self->command_data->{'port'};
896 } else {
897 # enable the Reuse param so we can sanely use the same local port
898 0 $sf_args{ Reuse } = 1;
899 }
900  
901 # create the socketfactory!
902 0 $poe_kernel->delay( 'timeout_event' => $self->timeout );
903 0 $self->data_sf( POE::Wheel::SocketFactory->new( %sf_args ) );
904  
905 # Now that we've created the SF, do we need to send the PORT data?
906 0 ▶ if ( $self->connection_mode eq 'active' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->connection_mode eq 'active') { } 0 0 0
907 # TODO what if SF had an error binding to the socket?
908 0 my $socket = $self->data_sf->getsockname;
909 0 my( $port, $addr ) = unpack_sockaddr_in( $socket );
910  
911 # TODO why won't getsockname give me the right ip????
912 # warn "GOT " . inet_ntoa( $addr ) . ":$port" if DEBUG;
913 # $addr = inet_ntoa( $addr ); ( always returns 0.0.0.0, but netstat shows it binding on the right ip! )
914 0 $addr = $self->local_addr;
915  
916 0 my @addr = split( /\./, $addr );
917 0 my @port = ( int( $port / 256 ), $port % 256 );
918 0 $self->command( 'complex_port', 'PORT', join( ',', @addr, @port ) );
919 } else {
920 # wait for the connection to server
921 0 $self->state( 'complex_sf' );
922 }
923 }
924  
925 event data_sf_connected => sub {
926 0 ▶ my( $self, $fh, $host, $port, $wheel_id ) = @_;
Subroutine __ANON__: not called CC=3 SCAR=24.8
927  
928 # TODO what if we get multiple connections?
929 # probably can happen if an attacker attacks us while we use active PORT connection
930  
931 0 warn "data_sf_connected\n" if DEBUG;
932  
933 # all done with the SF wheel
934 0 $self->data_sf( undef );
935  
936 # kill the timeout timer
937 0 $poe_kernel->delay( 'timeout_event' );
938  
939 # args for the RW wheel
940 0 my %rw_args = (
941 Handle => $fh,
942 Driver => POE::Driver::SysRW->new,
943 InputEvent => 'data_rw_input',
944 ErrorEvent => 'data_rw_error',
945 FlushedEvent => 'data_rw_flushed',
946 );
947 0 ▶ if ( $self->command_data->{'cmd'} =~ /^(?:ls|dir|list|nlst)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->command_data->{'cmd'} =~ /^(?:ls|dir|list|nlst)$/) { } 0 0 0
948 0 $rw_args{'Filter'} = POE::Filter::Line->new( InputLiteral => EOL );
949 } else {
950 0 $rw_args{'Filter'} = POE::Filter::Stream->new;
951 }
952  
953 # convert it into a readwrite wheel
954 0 $self->data_rw( POE::Wheel::ReadWrite->new( %rw_args ) );
955  
956 # do we need to send the actual command?
957 0 ▶ if ( $self->connection_mode eq 'passive' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->connection_mode eq "passive") 0 0 0
958 0 $self->process_complex_command;
959 }
960  
961 0 return;
962 1 };
963  
964 sub process_complex_command {
965 0 ▶ my $self = shift;
Subroutine process_complex_command: not called CC=3 SCAR=24.8
Pod undocumented
966  
967 0 my $cmd = $self->command_data->{'cmd'};
968  
969 # do we need to translate the command?
970 0 ▶ if ( exists $command_map{ $cmd } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $command_map{$cmd}) { } 0 0 0
971 0 $cmd = $command_map{ $cmd };
972 } else {
973 0 $cmd = uc( $cmd );
974 }
975  
976 # since the code in sub command doesn't like sending undef's we have to check it here
977 0 ▶ if ( defined $self->command_data->{'data'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->command_data->{'data'}) { } 0 0 0
978 0 $self->command( 'complex_start', $cmd, @{ $self->command_data->{'data'} } );
979 } else {
980 0 $self->command( 'complex_start', $cmd );
981 }
982 }
983  
984 event data_sf_error => sub {
985 0 ▶ my( $self, $operation, $errnum, $errstr, $wheel_id ) = @_;
Subroutine __ANON__: not called CC=2 SCAR=17.9
986  
987 0 warn "data_sf_error: $operation $errnum $errstr\n" if DEBUG;
988  
989 # some sort of error?
990 0 ▶ if ( $self->state eq 'complex_sf' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->state eq 'complex_sf') { } 0 0 0
991 0 $self->process_complex_error( undef, "$operation error $errnum: $errstr" );
992 } else {
993 0 die "unexpected data_sf_error in wrong state: " . $self->state;
994 }
995 1 };
996  
997 sub _ftpd_complex_start {
998 0 ▶ my( $self, $code, $reply ) = @_;
Subroutine _ftpd_complex_start: not called CC=5 SCAR=34.0
999  
1000 # actually process the "start" of the command
1001 0 ▶ if ( code_preliminary( $code ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (code_preliminary($code)) { } 0 0 0
elsif (code_success($code)) { } 0 0 0
1002 # let the master know it's ready to send/receive stuff!
1003 0 $self->tell_master( $self->command_data->{'cmd'} . '_connected', @{ $self->command_data->{'data'} } );
1004 0 $self->state( 'complex_data' );
1005  
1006 # do we have any buffered data?
1007 0 ▶ if ( exists $self->command_data->{'buffer'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->command_data->{"buffer"}) 0 0 0
1008 0 warn "sending buffered chunks\n" if DEBUG;
1009 0 foreach my $chunk ( @{ $self->command_data->{'buffer'} } ) {
1010 0 $self->tell_master( $self->command_data->{'cmd'} . '_data', $chunk, @{ $self->command_data->{'data'} } );
1011 }
1012 0 delete $self->command_data->{'buffer'};
1013 }
1014 } elsif ( code_success( $code ) ) {
1015 0 die "unexpected success for start of complex command: $code $reply";
1016 } else {
1017 0 $self->process_complex_error( $code, $reply );
1018 }
1019 }
1020  
1021 sub _ftpd_complex_error {
1022 0 ▶ my( $self, $code, $reply ) = @_;
Subroutine _ftpd_complex_error: not called CC=2 SCAR=17.9
1023  
1024 # we are supposed to get some kind of error from the ftpd
1025 # because something screwed up while doing the data connection
1026 0 ▶ if ( code_failure( $code ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (code_failure($code)) { } 0 0 0
1027 # okay, all done!
1028 0 $self->state( 'idle' );
1029 0 $self->command_data( undef );
1030 } else {
1031 0 die "unexpected input while in complex_error state: $code $reply";
1032 }
1033 }
1034  
1035 sub _ftpd_complex_done {
1036 0 ▶ my( $self, $code, $reply ) = @_;
Subroutine _ftpd_complex_done: not called CC=2 SCAR=17.9
1037  
1038 # got the final result of the complex command!
1039 0 my $event = $self->command_data->{'cmd'};
1040 0 ▶ if ( ! code_success( $code ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (code_success($code)) 0 0 0
1041 0 $event .= '_error';
1042 }
1043 0 $self->tell_master( $event, $code, $reply, @{ $self->command_data->{'data'} } );
1044  
1045 # clear all data for this complex command
1046 0 $self->state( 'idle' );
1047 0 $self->command_data( undef );
1048  
1049 # TODO maybe we got the complex reply *before* the RW is closed?
1050 }
1051  
1052 sub process_complex_error {
1053 0 ▶ my( $self, $code, $reply ) = @_;
Subroutine process_complex_error: not called CC=1 SCAR=6.9
Pod undocumented
1054  
1055 # go to the error state, so we can receive whatever the ftpd wants to send to us
1056 0 $self->state( 'complex_error' );
1057  
1058 0 $self->data_sf( undef );
1059 0 $self->data_rw( undef );
1060  
1061 0 $self->tell_master( $self->command_data->{'cmd'} . '_error', $code, $reply, @{ $self->command_data->{'data'} } );
1062  
1063 # all done processing this complex command
1064 0 $self->command_data( undef );
1065 }
1066  
1067 sub process_complex_closed {
1068 0 ▶ my $self = shift;
Subroutine process_complex_closed: not called CC=1 SCAR=6.9
Pod undocumented
1069  
1070 # Okay, we are done with this command!
1071 0 $self->state( 'complex_done' );
1072 0 $self->data_rw( undef );
1073  
1074 # TODO should we send an event_closed command? I think it's superfluous...
1075 }
1076  
1077 event data_rw_input => sub {
1078 0 ▶ my( $self, $input, $wheel_id ) = @_;
Subroutine __ANON__: not called CC=4 SCAR=30.0
1079  
1080 0 warn "data_rw_input: '$input'\n" if DEBUG;
1081  
1082 # should only happen in complex state
1083 0 ▶ if ( $self->state eq 'complex_data' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->state eq 'complex_data') { } 0 0 0
elsif ($self->state eq 'complex_start') { } 0 0 0
1084 # send it back to the master
1085 0 $self->tell_master( $self->command_data->{'cmd'} . '_data', $input, @{ $self->command_data->{'data'} } );
1086 } elsif ( $self->state eq 'complex_start' ) {
1087 # oh boy, the server immediately sent us some data while we were processing the start
1088 # that means we have to buffer it so we correctly send it *after* we send the connected event
1089 0 warn "storing input for buffer\n" if DEBUG;
1090 0 ▶ if ( ! exists $self->command_data->{'buffer'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $self->command_data->{'buffer'}) { } 0 0 0
1091 0 $self->command_data->{'buffer'} = [ $input ];
1092 } else {
1093 0 push( @{ $self->command_data->{'buffer'} }, $input );
1094 }
1095 } else {
1096 0 die "unexpected data_rw_input in wrong state: " . $self->state;
1097 }
1098  
1099 0 return;
1100 1 };
1101  
1102 event data_rw_error => sub {
1103 0 ▶ my( $self, $operation, $errnum, $errstr, $wheel_id ) = @_;
Subroutine __ANON__: not called CC=5 SCAR=34.0
1104  
1105 0 warn "data_rw_error: $operation $errnum $errstr\n" if DEBUG;
1106  
1107 # should only happen in complex state
1108 0 ▶ if ( $self->state eq 'complex_data' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->state eq 'complex_data') { } 0 0 0
1109 # Is it a normal EOF or an error?
1110 0 ▶ if ( $operation eq "read" and $errnum == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($operation eq 'read' and $errnum == 0) { } 0 0 0
Condition0%0/3
$operation eq "read" and $errnum == 0
!ll&&!rl&&r
000
MC/DC0%0/2
$operation eq "read" and $errnum == 0
$operation eq "read" $errnum == 0
Truth table0%0/3
A and B
A= $operation eq "read"B= $errnum == 0
ABresult
0X0
100
111
1111 # only in the put state is this a real error
1112 0 ▶ if ( $self->command_data->{'cmd'} =~ /^(?:put|stor|stou)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->command_data->{'cmd'} =~ /^(?:put|stor|stou)$/) { } 0 0 0
1113 0 $self->process_complex_error( undef, "$operation error $errnum: $errstr" );
1114 } else {
1115 # otherwise it was a listing/get which means the data stream is done
1116 0 $self->process_complex_closed;
1117 }
1118 } else {
1119 0 $self->process_complex_error( undef, "$operation error $errnum: $errstr" );
1120 }
1121 } else {
1122 0 die "unexpected data_rw_error in wrong state: " . $self->state;
1123 }
1124  
1125 0 return;
1126 1 };
1127  
1128 event data_rw_flushed => sub {
1129 0 ▶ my( $self, $wheel_id ) = @_;
Subroutine __ANON__: not called CC=3 SCAR=24.8
1130  
1131 0 warn "data_rw_flushed\n" if DEBUG;
1132  
1133 # should only happen in complex state
1134 0 ▶ if ( $self->state eq 'complex_data' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->state eq 'complex_data') { } 0 0 0
1135 # This should only happen for put commands
1136 0 ▶ if ( $self->command_data->{'cmd'} =~ /^(?:put|stor|stou)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->command_data->{'cmd'} =~ /^(?:put|stor|stou)$/) { } 0 0 0
1137 0 $self->tell_master( $self->command_data->{'cmd'} . '_flushed', @{ $self->command_data->{'data'} } );
1138 } else {
1139 0 die "unexpected data_rw_flushed for complex command:" . $self->command_data->{'cmd'};
1140 }
1141 } else {
1142 0 die "unexpected data_rw_flushed in wrong state: " . $self->state;
1143 }
1144  
1145 0 return;
1146 1 };
1147  
1148 sub _ftpd_rename_start {
1149 0 ▶ my( $self, $code, $reply ) = @_;
Subroutine _ftpd_rename_start: not called CC=2 SCAR=17.9
1150  
1151 0 ▶ if ( code_intermediate( $code ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (code_intermediate($code)) { } 0 0 0
1152 # TODO should we send a rename_partial event? I think it's superfluous...
1153 0 $self->command( 'rename_done', 'RNTO', $self->command_data->{'to'} );
1154 } else {
1155 0 $self->tell_master( 'rename_error', $code, $reply, $self->command_data->{'from'}, $self->command_data->{'to'} );
1156 0 $self->command_data( undef );
1157 0 $self->state( 'idle' );
1158 }
1159 }
1160  
1161 sub _ftpd_rename_done {
1162 0 ▶ my( $self, $code, $reply ) = @_;
Subroutine _ftpd_rename_done: not called CC=2 SCAR=17.9
1163  
1164 0 my $event = 'rename';
1165 0 ▶ if ( ! code_success( $code ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (code_success($code)) 0 0 0
1166 0 $event .= '_error';
1167 }
1168 0 $self->tell_master( $event, $code, $reply, $self->command_data->{'from'}, $self->command_data->{'to'} );
1169  
1170 0 $self->command_data( undef );
1171 0 $self->state( 'idle' );
1172 }
1173  
1174 1 ▶no MooseX::POE::SweetArgs;
Subroutine BEGIN: called CC=1 SCAR=0.0
1175 1 __PACKAGE__->meta->make_immutable;
1176 1 1;
1177  
1178  
1179 __END__
1180 =pod
1181  
1182 =for :stopwords Apocalypse cpan testmatrix url annocpan anno bugtracker rt cpants kwalitee
1183 diff irc mailto metadata placeholders ftp
1184  
1185 =encoding utf-8
1186  
1187 =for Pod::Coverage command EOL START BUILD tell_master create_data_connection prepare_listing prepare_tls_stuff prepare_transfer
1188 =for Pod::Coverage process_complex_closed process_complex_error start_data_connection
1189  
1190 =head1 NAME
1191  
1192 POE::Component::Client::SimpleFTP - A simple FTP client library for POE
1193  
1194 =head1 VERSION
1195  
1196 This document describes v0.003 of POE::Component::Client::SimpleFTP - released May 04, 2011 as part of POE-Component-Client-SimpleFTP.
1197  
1198 =head1 SYNOPSIS
1199  
1200 # A simple FTP client logging in to a server
1201 use POE qw( Component::Client::SimpleFTP );
1202  
1203 POE::Session->create(
1204 inline_states => {
1205 _start => sub {
1206 POE::Component::Client::SimpleFTP->new(
1207 alias => "ftp",
1208 remote_addr => "invalid.addr",
1209 username => "myuser",
1210 password => "mypassword",
1211 );
1212 return;
1213 },
1214 authenticated => sub {
1215 print "LOGGED ON!\n";
1216 $_[KERNEL]->post( "ftp", "quit" );
1217 return;
1218 }
1219 },
1220 );
1221 POE::Kernel->run;
1222  
1223 =head1 DESCRIPTION
1224  
1225 This is a simple FTP client to use in a POE application. It's a complete rewrite of the old L<POE::Component::Client::FTP> codebase and makes
1226 it easier to use. Most of the API/event flow is compatible, so you should have few problems porting your code to this module.
1227  
1228 You start by creating the ftp object and wait for it to send you events. By default the caller session will get all the events directed to it,
1229 no need to "register" for events or anything like that. Events are sent to you in the generic form of C<$command> or C<${command}_error> events.
1230 This module will parse the FTP reply codes and determine if it is an error or not, and dispatch it to the appropriate event.
1231  
1232 An important thing to keep in mind is that there is no command queueing done in this module. It is up to the user to know what state they are
1233 in and to dispatch events at the right time. If a command is sent while this module is processing one, an exception will be thrown. Fortunately,
1234 due to the way events are named, it should be easy to keep track of the event flow.
1235  
1236 =head2 Initial Connection
1237  
1238 When the object is created, it attempts to make a connection to the server specified in the attributes. It will automatically login with the
1239 provided credentials. Additionally, it will enable TLS mode if you enabled the attributes L</tls_cmd> and L</tls_data>. There is a timeout timer
1240 on the initial connection that you can tweak via setting L</timeout>.
1241  
1242 The following events may be sent to your session:
1243  
1244 =head3 authenticated
1245  
1246 This event is sent when the entire login procedure is done. At this point you can send commands to the server.
1247  
1248 No arguments.
1249  
1250 =head3 connect_error
1251  
1252 This event is sent when there's an error connecting to the server. The component will automatically destroy itself at this point, so if you
1253 want to retry the connection, you have to make a new object.
1254  
1255 The first argument is the error code, and the 2nd argument is the error string.
1256  
1257 The code "0" is used here, because we never got a reply from the server. However, it is nice to have consistency with the other
1258 event handlers, so it is supplied.
1259  
1260 Example code: 0
1261  
1262 Example reply: timedout
1263  
1264 =head3 login_error
1265  
1266 This event is sent when there's an error trying to login to the server. The component will automatically destroy itself at this point, so if you
1267 want to retry the connection, you have to make a new object.
1268  
1269 The first argument is the error code, and the 2nd argument is the error string.
1270  
1271 Example code: 530
1272  
1273 Example reply: Login incorrect.
1274  
1275 =head2 Simple Commands
1276  
1277 This is a class of commands that can be sent to the server after receiving the L</authenticated> event. They perform identically, and will send
1278 the same replies back to your session. Some commands require arguments, others don't.
1279  
1280 Normally the events will include at least 2 arguments: the FTP return code and the actual reply line from the server. If the command included
1281 arguments, it will be supplied in the event to make identifying actions easier.
1282  
1283 Some commands is an alias for the actual command ( cd vs cwd ) but the event name will follow the aliased command. If a cwd event is sent, the
1284 error event is C<cwd_error>. If a cd event is sent, the error event is C<cd_error>.
1285  
1286 # send the cd command in an event handler somewhere
1287 $ftp->yield( 'cd', '/foobar' );
1288  
1289 # handler for the resulting event received from this component
1290 sub cd {
1291 my( $code, $reply, $path ) = @_[ ARG0 .. ARG2 ];
1292  
1293 # $code probably is 250
1294 # $reply probably is "Directory successfully changed."
1295 # $path will be "/foobar"
1296 }
1297  
1298 sub cd_error {
1299 my( $code, $reply, $path ) = @_[ ARG0 .. ARG2 ];
1300  
1301 # $code probably is 550
1302 # $reply probably is "Failed to change directory."
1303 # $path will be "/foobar"
1304 }
1305  
1306 =head3 cwd
1307  
1308 Changes the working directory.
1309  
1310 Arguments: the path to change to ( required )
1311  
1312 Example code: 250
1313  
1314 Example reply: Directory successfully changed.
1315  
1316 =head3 cd
1317  
1318 An alias for L</cwd>
1319  
1320 =head3 dele
1321  
1322 Deletes a file.
1323  
1324 Arguments: the file to delete ( required )
1325  
1326 Example code: 250
1327  
1328 Example reply: Delete operation successful.
1329  
1330 =head3 delete
1331  
1332 An alias for L</dele>
1333  
1334 =head3 mkd
1335  
1336 Creates a directory.
1337  
1338 Arguments: the directory path to create ( required )
1339  
1340 You can supply an absolute path or a relative path. It is up to the server to figure out where to create the directory. It's easier to use
1341 absolute paths so you are sure that the server is creating the directory in the right place!
1342  
1343 Remember, the FTP protocol doesn't support recursive directory creation! If C</foo> exists but C</foo/bar> doesn't, then you cannot create
1344 C</foo/bar/baz>!
1345  
1346 Example code: 257
1347  
1348 Example reply: "/foo" created
1349  
1350 =head3 mkdir
1351  
1352 An alias for L</mkd>
1353  
1354 =head3 rmd
1355  
1356 Removes a directory.
1357  
1358 Arguments: the directory path to delete ( required )
1359  
1360 You can supply an absolute path or a relative path. It is up to the server to figure out where to delete the directory. It's easier to use
1361 absolute paths so you are sure that the server is deleting the right directory!
1362  
1363 Example code: 250
1364  
1365 Example reply: Remove directory operation successful.
1366  
1367 =head3 rmdir
1368  
1369 An alias for L</rmd>
1370  
1371 =head3 cdup
1372  
1373 Changes the working directory to the parent.
1374  
1375 Remember, there might be symlinks or other bizarre stuff going on behind the scenes! It's best to supply full pathnames to L</cwd> to be safe.
1376  
1377 Arguments: none
1378  
1379 Example code: 250
1380  
1381 Example reply: Directory successfully changed.
1382  
1383 =head3 pwd
1384  
1385 Prints the current working directory.
1386  
1387 Arguments: none
1388  
1389 Example code: 257
1390  
1391 Example reply: "/"
1392  
1393 =head3 rename
1394  
1395 Renames a target file to a new name.
1396  
1397 Arguments: the old filename and the new filename
1398  
1399 Remember, the pathnames must exist and is a valid target. Best to send absolute paths!
1400  
1401 Example code: 250
1402  
1403 Example reply: Rename successful.
1404  
1405 =head3 mv
1406  
1407 An alias for L</rename>
1408  
1409 =head3 quit
1410  
1411 Disconnects from the server. Behaves differently depending on the context when this command is received. After this command is sent, this
1412 module will destroy itself and not send any more events to your session.
1413  
1414 If this module isn't processing anything it will send the QUIT command and gracefully shutdown when it receives the server reply.
1415  
1416 If this module is processing a command it will disconnect immediately, killing any command processing/data transfers that is happening.
1417  
1418 If you want to force immediate shutdown, use the L</shutdown> event.
1419  
1420 Arguments: none
1421  
1422 =head3 disconnect
1423  
1424 An alias for L</quit>
1425  
1426 =head3 shutdown
1427  
1428 Forces a shutdown of the component and kills everything.
1429  
1430 Arguments: none
1431  
1432 =head3 noop
1433  
1434 Executes a no-operation command. Useful to keep the connection open or to get the round-trip latency, or whatever :)
1435  
1436 Arguments: none
1437  
1438 Example code: 200
1439  
1440 Example reply: NOOP ok.
1441  
1442 =head3 quot
1443  
1444 Sends a quoted command to the server. Useful for sending commands that this module doesn't support.
1445  
1446 Arguments: the actual command + arguments to send.
1447  
1448 $ftp->yield( 'quot', 'CRAZYCMD', @crazy_args );
1449  
1450 =head3 quote
1451  
1452 An alias for L</quot>
1453  
1454 =head3 help
1455  
1456 Gets the server's help output for a command.
1457  
1458 Arguments: optional command to ask for help
1459  
1460 Example code: 214
1461  
1462 Example reply:
1463  
1464 The following commands are recognized.
1465 ABOR ACCT ALLO APPE CDUP CWD DELE EPRT EPSV FEAT HELP LIST MDTM MKD
1466 MODE NLST NOOP OPTS PASS PASV PORT PWD QUIT REIN REST RETR RMD RNFR
1467 RNTO SITE SIZE SMNT STAT STOR STOU STRU SYST TYPE USER XCUP XCWD XMKD
1468 XPWD XRMD
1469 Help OK.
1470  
1471 =head3 site
1472  
1473 Executes a specific command that the server supports. Consult your ftp administrator or the document for the ftpd software for more information.
1474  
1475 Arguments: the command to execute + any optional arguments.
1476  
1477 Example code: 500
1478  
1479 Example reply: Unknown SITE command.
1480  
1481 =head3 stat
1482  
1483 Receives some informational text about the current status of the connection.
1484  
1485 BEWARE: While the RFC says this command can be sent while a data transfer is in progress, this is unimplemented!
1486  
1487 Arguments: none
1488  
1489 Example code: 211
1490  
1491 Example reply:
1492  
1493 FTP server status:
1494 Connected to 192.168.0.199
1495 Logged in as apoc
1496 TYPE: ASCII
1497 No session bandwidth limit
1498 Session timeout in seconds is 300
1499 Control connection is plain text
1500 Data connections will be plain text
1501 At session startup, client count was 1
1502 vsFTPd 2.2.0 - secure, fast, stable
1503 End of status
1504  
1505 =head3 syst
1506  
1507 Gets the system information of the server.
1508  
1509 Arguments: none
1510  
1511 Example code: 215
1512  
1513 Example reply: UNIX Type: L8
1514  
1515 =head3 acct
1516  
1517 Send the account information for your login. Generally not used, but if your server requires it you should send this immediately after getting the
1518 L</authenticated> event.
1519  
1520 Arguments: your account information
1521  
1522 Example code: 502
1523  
1524 Example reply: ACCT not implemented.
1525  
1526 =head3 smnt
1527  
1528 Mounts a different filesystem volume on your account. Generally not used.
1529  
1530 Arguments: a pathname to mount or system-specific string
1531  
1532 Example code: 502
1533  
1534 Example reply: SMNT not implemented.
1535  
1536 =head3 mdtm
1537  
1538 Gets the modification time of a file. Not supported by all servers! ( RFC 3659 )
1539  
1540 Arguments: the file to query
1541  
1542 Example code: 213
1543  
1544 Example reply: 20110502230157
1545  
1546 You can use the L<POE::Component::Client::SimpleFTP::Utils/mdtm_parser> function to convert it into a L<DateTime> object.
1547  
1548 =head3 size
1549  
1550 Gets the size of a file in bytes. Not supported by all servers! ( RFC 3659 )
1551  
1552 Arguments: the file to query
1553  
1554 Example code: 213
1555  
1556 Example reply: 48
1557  
1558 =head3 feat
1559  
1560 Queries the FEAT capabilities of the server. Not supported by all servers! ( RFC 2389 )
1561  
1562 Arguments: none
1563  
1564 Example code: 211
1565  
1566 Example reply:
1567  
1568 Features:
1569 EPRT
1570 EPSV
1571 MDTM
1572 PASV
1573 REST STREAM
1574 SIZE
1575 TVFS
1576 UTF8
1577 End
1578  
1579 You can use the L<POE::Component::Client::SimpleFTP::Utils/feat_parser> function to convert it into an array of features.
1580  
1581 =head3 features
1582  
1583 An alias for L</feat>
1584  
1585 =head3 opts
1586  
1587 Sets an option for the current session. Not supported by all servers! ( RFC 2389 )
1588  
1589 Arguments: the option to set
1590  
1591 Example code: 501
1592  
1593 Example reply: Option not understood.
1594  
1595 =head3 options
1596  
1597 An alias for L</opts>
1598  
1599 =head2 Complex Commands
1600  
1601 This class of commands is called complex because they require opening a new data connection to the server. The requested data is transferred
1602 over this connection, and the result is sent back to your session. All of the commands behave the same except for the "upload" types.
1603  
1604 Please look at the C<examples> directory included in this distribution for code samples.
1605  
1606 The typical flow of this command is as follows:
1607  
1608 $ftp->yield( 'get', "/myfile.txt" );
1609  
1610 # receive a "get_connected" event
1611 # args is: "/myfile.txt"
1612  
1613 # at this point you prepare to process the incoming data
1614  
1615 # receive a "get_data" event
1616 # args is: $chunk, "/myfile.txt"
1617  
1618 # at this point you should write out the data to the terminal, a file, or whatever!
1619  
1620 # ... keep receiving "get_data" until the server finish sending
1621  
1622 # receive a "get" event
1623 # args is: $code, $reply, "/myfile.txt"
1624  
1625 # at this point the transfer is complete
1626  
1627 # if at any point there is an error, a "get_error" event is sent
1628 # args is: $code, $reply, "/myfile.txt"
1629  
1630 For the "upload" events where you are sending data to the server, the flow is:
1631  
1632 $ftp->yield( 'put', '/myfile.txt' );
1633  
1634 # receive a "put_connected" event
1635 # args is: "/myfile"
1636  
1637 # at this point you should get the data to send to the server
1638 # from your local filesystem, from a database server, or whatever!
1639  
1640 # send a chunk of data to the server
1641 # the chunk size should depend on your application - a reasonable default is 10240 bytes
1642 $ftp->yield( 'put_data', $chunk );
1643  
1644 # receive a "put_flushed" event
1645 # args is: "/myfile"
1646  
1647 # at this point, you can either send another chunk or signal EOF
1648 $ftp->yield( 'put_close' );
1649  
1650 # receive a "put" event
1651 # args is: $code, $reply, "/myfile"
1652  
1653 # at this point the transfer is complete
1654  
1655 # if at any point there is an error, a "put_error" event is sent
1656 # args is: $code, $reply, "/myfile"
1657  
1658 =head3 list
1659  
1660 Receives a directory list. The data is sent in a format similar to the UNIX "ls" command, but can be anything!
1661  
1662 Arguments: the optional path to query ( defaults to current working directory )
1663  
1664 Example data:
1665  
1666 drwxr-xr-x 4 1000 1000 4096 May 02 18:24 a
1667 drwxr-xr-x 4 1000 1000 4096 May 02 18:24 b
1668 drwxr-xr-x 4 1000 1000 4096 May 02 18:24 c
1669 -rw-r--r-- 1 1000 1000 0 May 02 20:26 foo.txt
1670  
1671 =head3 ls
1672  
1673 An alias for L<list>
1674  
1675 =head3 nlst
1676  
1677 Receives a directory list. Differs from L<list> in that only the names are received.
1678  
1679 Arguments: the optional path to query ( defaults to current working directory )
1680  
1681 Example data:
1682  
1683 a
1684 b
1685 c
1686 foo.txt
1687  
1688 =head3 dir
1689  
1690 An alias for L</nlst>
1691  
1692 =head3 retr
1693  
1694 Retrieves a file from the server.
1695  
1696 Arguments: the filename to receive
1697  
1698 =head3 get
1699  
1700 An alias for L</get>
1701  
1702 =head3 stor
1703  
1704 Transmits a file to the server. This uses the "upload" command flow explained in L</Complex Commands>!
1705  
1706 Arguments: the filename to put
1707  
1708 =head3 stou
1709  
1710 Transmits a file to the server. This differs from L</stor> in that the ftp server is required to store the file in a unique way. This uses the
1711 "upload" command flow explained in L</Complex Commands>!
1712  
1713 Arguments: the filename to put
1714  
1715 =head3 put
1716  
1717 An alias for L</stor>
1718  
1719 =head1 ATTRIBUTES
1720  
1721 =head2 alias
1722  
1723 The alias this component will use. You can send commands to the ftpd in 2 ways:
1724  
1725 my $ftp = POE::Component::Client::SimpleFTP->new( alias => "ftp", ... );
1726 $poe_kernel->post( 'ftp', 'cd', 'foobar' );
1727  
1728 # Or, you can use the yield sub:
1729 $ftp->yield( 'cd', 'foobar' );
1730  
1731 The default is: ftp
1732  
1733 =head2 username
1734  
1735 The FTP username you will be sending to the server.
1736  
1737 required.
1738  
1739 =head2 password
1740  
1741 The FTP password you will be sending to the server.
1742  
1743 required.
1744  
1745 =head2 remote_addr
1746  
1747 The IP address of the FTP server to connect to. Can be a DNS hostname or IPv4/6 string.
1748  
1749 required.
1750  
1751 =head2 remote_port
1752  
1753 The port of the FTP server to connect to.
1754  
1755 The default is: 21
1756  
1757 =head2 local_addr
1758  
1759 The local IP address to bind to for all connections to the server.
1760  
1761 The default is: INADDR_ANY ( let the OS decide )
1762  
1763 =head2 local_port
1764  
1765 The local port to bind to for the control connection to the server. If you need to change the data connection's port, please
1766 change the L</local_data_port> attribute.
1767  
1768 The default is: 0 ( let the OS decide )
1769  
1770 =head2 local_data_port
1771  
1772 The local port to bind to for the data connection to the server. Must be a different port than the L</local_port> attribute!
1773  
1774 The default is: 0 ( let the OS decide )
1775  
1776 =head2 tls_cmd
1777  
1778 A boolean value to enable/disable TLS encryption of the command connection. If you want to use this,
1779 you must have L<POE::Component::SSLify> installed!
1780  
1781 The default is: false
1782  
1783 =head2 tls_data
1784  
1785 A boolean value to enable/disable TLS encryption of the data connection. If you want to use this,
1786 you must have L<POE::Component::SSLify> installed!
1787  
1788 The default is: false
1789  
1790 =head2 timeout
1791  
1792 A value specifying the timeout in seconds for the initial connection to the FTP server.
1793  
1794 The default is: 120
1795  
1796 =head2 connection_mode
1797  
1798 Determine what connection mode we will be using when opening the data connection to the server. In "active" mode,
1799 the server will be connecting to us. In "passive" mode we will be connecting to the server. You usually need "passive" mode
1800 if you are behind a firewall.
1801  
1802 The default is: passive
1803  
1804 =head1 FUNCTIONS
1805  
1806 =head2 DEBUG
1807  
1808 Enable this if you want to get debugging output. Do it like this:
1809  
1810 sub POE::Component::Client::SimpleFTP::DEBUG () { 1 }
1811 use POE::Component::Client::SimpleFTP;
1812  
1813 The default is: false
1814  
1815 =head2 yield
1816  
1817 This method provides an alternative object based means of posting events to the component.
1818 First argument is the event to post, following arguments are sent as arguments to the resultant post.
1819  
1820 my $ftp = POE::Component::Client::SimpleFTP->new( ... );
1821 $ftp->yield( 'cd', 'foobar' );
1822  
1823 # equivalent to:
1824 $poe_kernel->post( $ftp->alias, 'cd', 'foobar' );
1825  
1826 =head1 TLS support
1827  
1828 TLS encryption is available if you want. You would need to enable the L</tls_cmd> and L</tls_data> attributes and have L<POE::Component::SSLify>
1829 installed in order to use it. It should work with a lot of servers and commands. However, not the entire specification is implemented!
1830 If you encounter problems when using TLS on a server, please let me know by filing a bug report!
1831  
1832 =head1 Unimplemented Commands/Actions/Features
1833  
1834 Those are the ideas that probably will be implemented in a future version. Some of them require core changes to this module, while others
1835 can be done in user-space but should be implemented here to make it "simpler" :)
1836  
1837 * full TLS support - check the RFCs
1838 * FXP ( server<->server ) transfers
1839 * intelligent NAT detection
1840 * full ipv6 compatibility
1841 * restart/abort/append a transfer
1842 * bandwidth throttling for data connection
1843 * support for "mkdir -p" where this module automatically creates all directories needed
1844 * passing a filename/filehandle/whatever to put/get so this module automatically does the reading/writing
1845 * directory mirroring ( ala rsync )
1846 * use POE::Filter::Ls for parsing ( need to improve it first hah )
1847 * encoded pathnames ( translate \012 in filename to \000 as per RFC 959 )
1848 * security stuff - http://cr.yp.to/ftp/security.html
1849 * event prefix ( so you get ftp_cd events instead of cd ) for easier event management
1850 * strict command validation ( we don't check if the command requires 0, 1, or N args )
1851  
1852 =head2 RFC 959 "FILE TRANSFER PROTOCOL (FTP)"
1853  
1854 * REIN ( not allowed, as it generally screws up - just reconnect! )
1855 * STRU ( default file type is always a good idea )
1856 * MODE ( default stream type is always a good idea )
1857 * APPE ( should be easy to implement, but im lazy )
1858 * ALLO ( it is generally unused and obsolete? )
1859 * REST ( a bit tricky to implement, maybe later )
1860 * ABOR ( not allowed, as it generally screws up - just disconnect! )
1861 * PASV ( this module automatically does it )
1862 * PORT ( this module automatically does it )
1863 * TYPE ( this module automatically does it )
1864 * STAT ( implemented, but not allowed while a transfer is in progress as it generally screws things up )
1865  
1866 =head2 RFC 2228 "FTP Security Extensions"
1867  
1868 * AUTH ( only AUTH TLS is supported now )
1869 * PROT/PBSZ is supported with the default options if you enable tls_cmd/tls_data
1870 * ADAT ( not needed for AUTH TLS? )
1871 * CCC ( not needed with TLS? )
1872 * MIC ( not needed with TLS? )
1873 * CONF ( not needed with TLS? )
1874 * ENC ( not needed with TLS? )
1875  
1876 =head2 RFC 2389 "Feature negotiation mechanism for the File Transfer Protocol"
1877  
1878 * The entire RFC is implemented
1879  
1880 =head2 RFC 2428 "FTP Extensions for IPv6 and NATs"
1881  
1882 * EPRT
1883 * EPSV
1884  
1885 =head2 RFC 2577 "FTP Security Considerations"
1886  
1887 * unimplemented
1888  
1889 =head2 RFC 2640 "Internationalization of the File Transfer Protocol"
1890  
1891 * unimplemented
1892  
1893 =head2 RFC 3659 "Extensions to FTP"
1894  
1895 * REST ( same reason as the RFC 959 one )
1896 * MLST
1897 * MLSD
1898  
1899 =head2 RFC 4217 "Securing FTP with TLS"
1900  
1901 * unimplemented except for what is implemented in 2228 :)
1902  
1903 =head2 RFC 5796 "FTP Command and Extension Registry"
1904  
1905 * No need to implement this, it is for servers only
1906  
1907 =head1 SUPPORT
1908  
1909 =head2 Perldoc
1910  
1911 You can find documentation for this module with the perldoc command.
1912  
1913 perldoc POE::Component::Client::SimpleFTP
1914  
1915 =head2 Websites
1916  
1917 The following websites have more information about this module, and may be of help to you. As always,
1918 in addition to those websites please use your favorite search engine to discover more resources.
1919  
1920 =over 4
1921  
1922 =item *
1923  
1924 Search CPAN
1925  
1926 The default CPAN search engine, useful to view POD in HTML format.
1927  
1928 L<http://search.cpan.org/dist/POE-Component-Client-SimpleFTP>
1929  
1930 =item *
1931  
1932 RT: CPAN's Bug Tracker
1933  
1934 The RT ( Request Tracker ) website is the default bug/issue tracking system for CPAN.
1935  
1936 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=POE-Component-Client-SimpleFTP>
1937  
1938 =item *
1939  
1940 AnnoCPAN
1941  
1942 The AnnoCPAN is a website that allows community annonations of Perl module documentation.
1943  
1944 L<http://annocpan.org/dist/POE-Component-Client-SimpleFTP>
1945  
1946 =item *
1947  
1948 CPAN Ratings
1949  
1950 The CPAN Ratings is a website that allows community ratings and reviews of Perl modules.
1951  
1952 L<http://cpanratings.perl.org/d/POE-Component-Client-SimpleFTP>
1953  
1954 =item *
1955  
1956 CPAN Forum
1957  
1958 The CPAN Forum is a web forum for discussing Perl modules.
1959  
1960 L<http://cpanforum.com/dist/POE-Component-Client-SimpleFTP>
1961  
1962 =item *
1963  
1964 CPANTS
1965  
1966 The CPANTS is a website that analyzes the Kwalitee ( code metrics ) of a distribution.
1967  
1968 L<http://cpants.perl.org/dist/overview/POE-Component-Client-SimpleFTP>
1969  
1970 =item *
1971  
1972 CPAN Testers
1973  
1974 The CPAN Testers is a network of smokers who run automated tests on uploaded CPAN distributions.
1975  
1976 L<http://www.cpantesters.org/distro/P/POE-Component-Client-SimpleFTP>
1977  
1978 =item *
1979  
1980 CPAN Testers Matrix
1981  
1982 The CPAN Testers Matrix is a website that provides a visual way to determine what Perls/platforms PASSed for a distribution.
1983  
1984 L<http://matrix.cpantesters.org/?dist=POE-Component-Client-SimpleFTP>
1985  
1986 =item *
1987  
1988 CPAN Testers Dependencies
1989  
1990 The CPAN Testers Dependencies is a website that shows a chart of the test results of all dependencies for a distribution.
1991  
1992 L<http://deps.cpantesters.org/?module=POE::Component::Client::SimpleFTP>
1993  
1994 =back
1995  
1996 =head2 Email
1997  
1998 You can email the author of this module at C<APOCAL at cpan.org> asking for help with any problems you have.
1999  
2000 =head2 Internet Relay Chat
2001  
2002 You can get live help by using IRC ( Internet Relay Chat ). If you don't know what IRC is,
2003 please read this excellent guide: L<http://en.wikipedia.org/wiki/Internet_Relay_Chat>. Please
2004 be courteous and patient when talking to us, as we might be busy or sleeping! You can join
2005 those networks/channels and get help:
2006  
2007 =over 4
2008  
2009 =item *
2010  
2011 irc.perl.org
2012  
2013 You can connect to the server at 'irc.perl.org' and join this channel: #perl-help then talk to this person for help: Apocalypse.
2014  
2015 =item *
2016  
2017 irc.freenode.net
2018  
2019 You can connect to the server at 'irc.freenode.net' and join this channel: #perl then talk to this person for help: Apocal.
2020  
2021 =item *
2022  
2023 irc.efnet.org
2024  
2025 You can connect to the server at 'irc.efnet.org' and join this channel: #perl then talk to this person for help: Ap0cal.
2026  
2027 =back
2028  
2029 =head2 Bugs / Feature Requests
2030  
2031 Please report any bugs or feature requests by email to C<bug-poe-component-client-simpleftp at rt.cpan.org>, or through
2032 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=POE-Component-Client-SimpleFTP>. You will be automatically notified of any
2033 progress on the request by the system.
2034  
2035 =head2 Source Code
2036  
2037 The code is open to the world, and available for you to hack on. Please feel free to browse it and play
2038 with it, or whatever. If you want to contribute patches, please send me a diff or prod me to pull
2039 from your repository :)
2040  
2041 L<http://github.com/apocalypse/perl-poe-component-simpleftp>
2042  
2043 git clone git://github.com/apocalypse/perl-poe-component-simpleftp.git
2044  
2045 =head1 AUTHOR
2046  
2047 Apocalypse <APOCAL@cpan.org>
2048  
2049 =head1 COPYRIGHT AND LICENSE
2050  
2051 This software is copyright (c) 2011 by Apocalypse.
2052  
2053 This is free software; you can redistribute it and/or modify it under
2054 the same terms as the Perl 5 programming language system itself.
2055  
2056 The full text of the license can be found in the LICENSE file included with this distribution.
2057  
2058 =head1 DISCLAIMER OF WARRANTY
2059  
2060 BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
2061 FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT
2062 WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER
2063 PARTIES PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND,
2064 EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE
2065 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2066 PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE
2067 SOFTWARE IS WITH YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME
2068 THE COST OF ALL NECESSARY SERVICING, REPAIR, OR CORRECTION.
2069  
2070 IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
2071 WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
2072 REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE LIABLE
2073 TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL, OR
2074 CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE
2075 SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING
2076 RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A
2077 FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF
2078 SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
2079 DAMAGES.
2080  
2081 =cut
2082  
↑ Summary Build »