| 1 | # | ||||||||||
| 2 | # This file is part of POE-Component-Client-SimpleFTP | ||||||||||
| 3 | # | ||||||||||
| 4 | # This software is copyright (c) 2014 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 | 3 | ▶ | 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::Utils; | ||||||||||
| 11 | 3 | $POE::Component::Client::SimpleFTP::Utils::VERSION = '0.004'; | |||||||||
| 12 | 3 | our $AUTHORITY = 'cpan:APOCAL'; | |||||||||
| 13 | |||||||||||
| 14 | # ABSTRACT: Miscellaneous FTP utility functions | ||||||||||
| 15 | |||||||||||
| 16 | 3 | ▶ | use parent 'Exporter'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 17 | 3 | our @EXPORT_OK = qw( | |||||||||
| 18 | code_preliminary code_success code_intermediate code_failure code_tls | ||||||||||
| 19 | EOL | ||||||||||
| 20 | mdtm_parser feat_parser | ||||||||||
| 21 | ); | ||||||||||
| 22 | 3 | our %EXPORT_TAGS = ( | |||||||||
| 23 | 'code' => [ | ||||||||||
| 24 | qw( code_preliminary code_success code_intermediate code_failure code_tls ), | ||||||||||
| 25 | ], | ||||||||||
| 26 | ); | ||||||||||
| 27 | |||||||||||
| 28 | #pod =func code_preliminary | ||||||||||
| 29 | #pod | ||||||||||
| 30 | #pod Tests whether the code is a 1yz code ( Positive Preliminary reply ) and returns a boolean value. | ||||||||||
| 31 | #pod | ||||||||||
| 32 | #pod =cut | ||||||||||
| 33 | |||||||||||
| 34 | #pod =func code_success | ||||||||||
| 35 | #pod | ||||||||||
| 36 | #pod Tests whether the code is a 2yz code ( Positive Completion reply ) and returns a boolean value. | ||||||||||
| 37 | #pod | ||||||||||
| 38 | #pod =cut | ||||||||||
| 39 | |||||||||||
| 40 | #pod =func code_intermediate | ||||||||||
| 41 | #pod | ||||||||||
| 42 | #pod Tests whether the code is a 3yz code ( Positive Intermediate reply ) and returns a boolean value. | ||||||||||
| 43 | #pod | ||||||||||
| 44 | #pod =cut | ||||||||||
| 45 | |||||||||||
| 46 | #pod =func code_failure | ||||||||||
| 47 | #pod | ||||||||||
| 48 | #pod Tests whether the code is a 4yz or 5yz code ( Transient/Permanent Negative Completion reply ) and returns a boolean value. | ||||||||||
| 49 | #pod | ||||||||||
| 50 | #pod =cut | ||||||||||
| 51 | |||||||||||
| 52 | #pod =func code_tls | ||||||||||
| 53 | #pod | ||||||||||
| 54 | #pod Tests whether the code is a 6yz code ( Protected reply ) and returns a boolean value. | ||||||||||
| 55 | #pod | ||||||||||
| 56 | #pod =cut | ||||||||||
| 57 | |||||||||||
| 58 | # helper sub to validate a code before doing the actual comparison | ||||||||||
| 59 | 31 | ▶ | sub _check_code { | ||||||||
|
Subroutine
_check_code: called CC=4 SCAR=13.9
| |||||||||||
| 60 | 31 | ▶ | return if ! defined $_[0]; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 61 | 26 | ▶ | return if length( $_[0] ) != 3; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 62 | 11 | ▶ | return if $_[0] !~ /^\d+$/; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 63 | 11 | return 1; | |||||||||
| 64 | } | ||||||||||
| 65 | |||||||||||
| 66 | # helper subs to figure out what a code is | ||||||||||
| 67 | 6 | ▶ | sub code_preliminary { return if ! _check_code( $_[0] ); return substr( $_[0], 0, 1 ) == 1 } | ||||||||
|
Subroutine
code_preliminary: called CC=2 SCAR=6.9
Pod
documented
Branch: 1 branch
| |||||||||||
| 68 | 6 | ▶ | sub code_success { return if ! _check_code( $_[0] ); return substr( $_[0], 0, 1 ) == 2 } | ||||||||
|
Subroutine
code_success: called CC=2 SCAR=6.9
Pod
documented
Branch: 1 branch
| |||||||||||
| 69 | 6 | ▶ | sub code_intermediate { return if ! _check_code( $_[0] ); return substr( $_[0], 0, 1 ) == 3 } | ||||||||
|
Subroutine
code_intermediate: called CC=2 SCAR=6.9
Pod
documented
Branch: 1 branch
| |||||||||||
| 70 | 7 | ▶ | sub code_failure { return if ! _check_code( $_[0] ); return $_[0] =~ /^[45]/ } | ||||||||
|
Subroutine
code_failure: called CC=2 SCAR=6.9
Pod
documented
Branch: 1 branch
| |||||||||||
| 71 | 6 | ▶ | sub code_tls { return if ! _check_code( $_[0] ); return substr( $_[0], 0, 1 ) == 6 } | ||||||||
|
Subroutine
code_tls: called CC=2 SCAR=6.9
Pod
documented
Branch: 1 branch
| |||||||||||
| 72 | |||||||||||
| 73 | #pod =func EOL | ||||||||||
| 74 | #pod | ||||||||||
| 75 | #pod Returns the end-of-line terminator as specified in RFC 959 | ||||||||||
| 76 | #pod | ||||||||||
| 77 | #pod =cut | ||||||||||
| 78 | |||||||||||
| 79 | sub EOL () { "\015\012" } | ||||||||||
| 80 | |||||||||||
| 81 | #pod =func mdtm_parser | ||||||||||
| 82 | #pod | ||||||||||
| 83 | #pod Returns a L<DateTime> object representing the modification timestamp of a file. Useful for parsing L<POE::Component::Client::SimpleFTP/mdtm> replies! | ||||||||||
| 84 | #pod | ||||||||||
| 85 | #pod NOTE: The MDTM format does not supply a timezone, you have to process that yourself! | ||||||||||
| 86 | #pod | ||||||||||
| 87 | #pod On an error returns undef. | ||||||||||
| 88 | #pod | ||||||||||
| 89 | #pod =cut | ||||||||||
| 90 | |||||||||||
| 91 | 8 | ▶ | sub mdtm_parser { | ||||||||
|
Subroutine
mdtm_parser: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||
| 92 | 8 | my $mdtm = shift; | |||||||||
| 93 | |||||||||||
| 94 | # check to see if we received microseconds | ||||||||||
| 95 | 8 | my $microseconds; | |||||||||
| 96 | 8 | ▶ | if ( $mdtm =~ /^(\d+)\.(\d+)$/ ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 97 | 2 | $mdtm = $1; | |||||||||
| 98 | 2 | $microseconds = $2; | |||||||||
| 99 | } | ||||||||||
| 100 | |||||||||||
| 101 | 8 | require DateTime::Format::Strptime; | |||||||||
| 102 | 8 | my $strp = DateTime::Format::Strptime->new( | |||||||||
| 103 | # RFC 3659 pattern: YYYYMMDDHHMMSS.sss | ||||||||||
| 104 | pattern => "%Y%m%d%H%M%S", | ||||||||||
| 105 | on_error => 'undef', | ||||||||||
| 106 | ); | ||||||||||
| 107 | 8 | my $dt = $strp->parse_datetime( $mdtm ); | |||||||||
| 108 | 8 | ▶ | if ( defined $dt ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 109 | 3 | ▶ | if ( defined $microseconds ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 110 | # add it to the object! | ||||||||||
| 111 | 2 | $dt->set_nanosecond( $microseconds * 1000 ); | |||||||||
| 112 | } | ||||||||||
| 113 | 3 | return $dt; | |||||||||
| 114 | } else { | ||||||||||
| 115 | 5 | return; | |||||||||
| 116 | } | ||||||||||
| 117 | } | ||||||||||
| 118 | |||||||||||
| 119 | #pod =func feat_parser | ||||||||||
| 120 | #pod | ||||||||||
| 121 | #pod Returns an array of FEAT capabilities present on the server. Useful for parsing L<POE::Component::Client::SimpleFTP/feat> replies! | ||||||||||
| 122 | #pod | ||||||||||
| 123 | #pod On an error returns an empty array. | ||||||||||
| 124 | #pod | ||||||||||
| 125 | #pod =cut | ||||||||||
| 126 | |||||||||||
| 127 | 8 | ▶ | sub feat_parser { | ||||||||
|
Subroutine
feat_parser: called CC=5 SCAR=16.2
Pod
documented
| |||||||||||
| 128 | 8 | my $feat = shift; | |||||||||
| 129 | |||||||||||
| 130 | # validation | ||||||||||
| 131 | 8 | ▶ | return () if ! defined $feat; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 132 | 8 | ▶ | return () if ! length( $feat ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 133 | |||||||||||
| 134 | # it should be a string with newlines in it separating the FEAT replies | ||||||||||
| 135 | 8 | my @data = split( "\n", $feat ); | |||||||||
| 136 | 8 | ▶ | return () if scalar @data <= 1; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 137 | |||||||||||
| 138 | # remove the first/last elements as they are informational text | ||||||||||
| 139 | 4 | shift @data; | |||||||||
| 140 | 4 | pop @data; | |||||||||
| 141 | |||||||||||
| 142 | # remove any whitespace | ||||||||||
| 143 | 4 | foreach my $f ( @data ) { | |||||||||
| 144 | 7 | $f =~ s/^\s+//; | |||||||||
| 145 | 7 | $f =~ s/\s+$//; | |||||||||
| 146 | } | ||||||||||
| 147 | |||||||||||
| 148 | # all done! | ||||||||||
| 149 | 4 | return @data; | |||||||||
| 150 | } | ||||||||||
| 151 | |||||||||||
| 152 | 3 | 1; | |||||||||
| 153 | |||||||||||
| 154 | __END__ | ||||||||||