# Net::Cmd.pm $Id: //depot/libnet/Net/Cmd.pm#34 $
# Copyright (c) 1995-1997 Graham Barr <gbarr@pobox.com>. All rights reserved.
# This program is free software; you can redistribute it and/or
# modify it under the same terms as Perl itself.
use vars
qw(@ISA @EXPORT $VERSION);
# Convert::EBCDIC->import;
@EXPORT = qw(CMD_INFO CMD_OK CMD_MORE CMD_REJECT CMD_ERROR CMD_PENDING);
my $tr = $^O
eq 'os390' ? Convert
::EBCDIC
->new() : undef;
unless (exists ${*$cmd}{'net_cmd_asciipeer'})
my $ebcdicstr = $tr->toebcdic($string);
${*$cmd}{'net_cmd_asciipeer'} = $string !~ /^\d+/ && $ebcdicstr =~ /^\d+/;
${*$cmd}{'net_cmd_asciipeer'}
${*$cmd}{'net_cmd_asciipeer'}
next if defined $done{$pkg};
my $v = defined ${"${pkg}::VERSION"}
?
"(" . ${"${pkg}::VERSION"} . ")"
$cmd->debug_print(1,"${spc}${pkg}${v}\n");
@spc{@
{"${pkg}::ISA"}} = (" " . $spc{$pkg}) x @
{"${pkg}::ISA"};
unshift(@do, @
{"${pkg}::ISA"});
@_ == 1 or @_ == 2 or croak
'usage: $obj->debug([LEVEL])';
my $pkg = ref($cmd) || $cmd;
$oldval = ${*$cmd}{'net_cmd_debug'} || 0;
$oldval = $debug{$pkg} || 0;
$level = $debug{$pkg} || 0
if($level && !exists $debug{$pkg});
${*$cmd}{'net_cmd_debug'} = $level;
@_ == 1 or croak
'usage: $obj->message()';
wantarray ? @
{${*$cmd}{'net_cmd_resp'}}
: join("", @
{${*$cmd}{'net_cmd_resp'}});
my($cmd,$out,$text) = @_;
print STDERR
$cmd,($out ?
'>>> ' : '<<< '), $cmd->debug_text($out,$text);
@_ == 1 or croak
'usage: $obj->code()';
${*$cmd}{'net_cmd_code'} = "000"
unless exists ${*$cmd}{'net_cmd_code'};
${*$cmd}{'net_cmd_code'};
@_ == 1 or croak
'usage: $obj->status()';
substr(${*$cmd}{'net_cmd_code'},0,1);
@_ == 3 or croak
'usage: $obj->set_status(CODE, MESSAGE)';
(${*$cmd}{'net_cmd_code'},${*$cmd}{'net_cmd_resp'}) = ($code, $resp);
unless (defined fileno($cmd))
$cmd->set_status("599", "Connection closed");
if(exists ${*$cmd}{'net_cmd_last_ch'});
local $SIG{PIPE
} = 'IGNORE' unless $^O
eq 'MacOS';
my $str = join(" ", map { /\n/ ?
do { my $n = $_; $n =~ tr/\n/ /; $n } : $_; } @_);
$str = $cmd->toascii($str) if $tr;
unless (defined($swlen = syswrite($cmd,$str,$len)) && $swlen == $len);
$cmd->debug_print(1,$str)
${*$cmd}{'net_cmd_resp'} = []; # the response
${*$cmd}{'net_cmd_code'} = "000"; # Made this one up :-)
@_ == 1 or croak
'usage: $obj->ok()';
0 < $code && $code < 400;
${*$cmd}{'net_cmd_resp'} = [ 'Unsupported command' ];
${*$cmd}{'net_cmd_code'} = 580;
${*$cmd}{'net_cmd_lines'} ||= [];
return shift @
{${*$cmd}{'net_cmd_lines'}}
if scalar(@
{${*$cmd}{'net_cmd_lines'}});
my $partial = defined(${*$cmd}{'net_cmd_partial'})
?
${*$cmd}{'net_cmd_partial'} : "";
until(scalar(@
{${*$cmd}{'net_cmd_lines'}}))
my $timeout = $cmd->timeout || undef;
if (select($rout=$rin, undef, undef, $timeout))
unless (sysread($cmd, $buf="", 1024))
carp
(ref($cmd) . ": Unexpected EOF on command channel")
substr($buf,0,0) = $partial; ## prepend from last sysread
my @buf = split(/\015?\012/, $buf, -1); ## break into lines
push(@
{${*$cmd}{'net_cmd_lines'}}, map { "$_\n" } @buf);
carp
("$cmd: Timeout") if($cmd->debug);
${*$cmd}{'net_cmd_partial'} = $partial;
foreach my $ln (@
{${*$cmd}{'net_cmd_lines'}})
$ln = $cmd->toebcdic($ln);
shift @
{${*$cmd}{'net_cmd_lines'}};
${*$cmd}{'net_cmd_lines'} ||= [];
unshift(@
{${*$cmd}{'net_cmd_lines'}}, $str);
unless $_[1] =~ s/^(\d\d\d)(.?)//o;
my($code,$more) = (undef) x
2;
${*$cmd}{'net_cmd_resp'} ||= [];
my $str = $cmd->getline();
$cmd->debug_print(0,$str)
($code,$more) = $cmd->parse_response($str);
${*$cmd}{'net_cmd_code'} = $code;
push(@
{${*$cmd}{'net_cmd_resp'}},$str);
my $str = $cmd->getline() or return undef;
$cmd->debug_print(0,$str)
last if($str =~ /^\.\r?\n/o);
my $arr = @_ == 1 && ref($_[0]) ?
$_[0] : \
@_;
my $line = join("" ,@
$arr);
return 0 unless defined(fileno($cmd));
my $last_ch = ${*$cmd}{'net_cmd_last_ch'};
$last_ch = ${*$cmd}{'net_cmd_last_ch'} = "\012" unless defined $last_ch;
return 1 unless length $line;
foreach my $b (split(/\n/,$line)) {
$cmd->debug_print(1, "$b\n");
$line =~ tr/\r\n/\015\012/ unless "\r" eq "\015";
if ($last_ch eq "\015") {
$first_ch = "\012" if $line =~ s/^\012//;
elsif ($last_ch eq "\012") {
$first_ch = "." if $line =~ /^\./;
$line =~ s/\015?\012(\.?)/\015\012$1$1/sg;
substr($line,0,0) = $first_ch;
${*$cmd}{'net_cmd_last_ch'} = substr($line,-1,1);
vec($win,fileno($cmd),1) = 1;
my $timeout = $cmd->timeout || undef;
local $SIG{PIPE
} = 'IGNORE' unless $^O
eq 'MacOS';
if (select(undef,$wout=$win, undef, $timeout) > 0 or -f
$cmd) # -f for testing on win32
my $w = syswrite($cmd, $line, $len, $offset);
carp
("$cmd: $!") if $cmd->debug;
carp
("$cmd: Timeout") if($cmd->debug);
my $arr = @_ == 1 && ref($_[0]) ?
$_[0] : \
@_;
my $line = join("" ,@
$arr);
return 0 unless defined(fileno($cmd));
print STDERR
$b,join("\n$b",split(/\n/,$line)),"\n";
vec($win,fileno($cmd),1) = 1;
my $timeout = $cmd->timeout || undef;
local $SIG{PIPE
} = 'IGNORE' unless $^O
eq 'MacOS';
if (select(undef,$wout=$win, undef, $timeout) > 0)
my $w = syswrite($cmd, $line, $len, $offset);
carp
("$cmd: $!") if $cmd->debug;
carp
("$cmd: Timeout") if($cmd->debug);
return 0 unless defined(fileno($cmd));
my $ch = ${*$cmd}{'net_cmd_last_ch'};
local $SIG{PIPE
} = 'IGNORE' unless $^O
eq 'MacOS';
$cmd->debug_print(1, ".\n")
syswrite($cmd,$tosend, length $tosend);
delete ${*$cmd}{'net_cmd_last_ch'};
$cmd->response() == CMD_OK
;
# read and write to tied filehandle
${*$cmd}{'net_cmd_readbuf'} = '';
# Tied filehandle read. Reads requested data length, returning
# end-of-file when the dot is encountered.
my ($len,$offset) = @_[1,2];
return unless exists ${*$cmd}{'net_cmd_readbuf'};
while (!$done and length(${*$cmd}{'net_cmd_readbuf'}) < $len) {
${*$cmd}{'net_cmd_readbuf'} .= $cmd->getline() or return;
$done++ if ${*$cmd}{'net_cmd_readbuf'} =~ s/^\.\r?\n\Z//m;
substr($_[0],$offset+0) = substr(${*$cmd}{'net_cmd_readbuf'},0,$len);
substr(${*$cmd}{'net_cmd_readbuf'},0,$len) = '';
delete ${*$cmd}{'net_cmd_readbuf'} if $done;
# in this context, we use the presence of readbuf to
# indicate that we have not yet reached the eof
return unless exists ${*$cmd}{'net_cmd_readbuf'};
my $line = $cmd->getline;
return if $line =~ /^\.\r?\n/;
my ($buf,$len,$offset) = @_;
return unless $cmd->datasend(substr($buf,$offset,$len));
${*$cmd}{'net_cmd_sending'}++; # flag that we should call dataend()
my $r = exists(${*$cmd}{'net_cmd_sending'}) ?
$cmd->dataend : 1;
delete ${*$cmd}{'net_cmd_readbuf'};
delete ${*$cmd}{'net_cmd_sending'};
Net::Cmd - Network Command class (as used by FTP, SMTP etc)
C<Net::Cmd> is a collection of methods that can be inherited by a sub class
of C<IO::Handle>. These methods implement the functionality required for a
command based protocol, for example FTP and SMTP.
These methods provide a user interface to the C<Net::Cmd> object.
Set the level of debug information for this object. If C<VALUE> is not given
then the current state is returned. Otherwise the state is changed to
C<VALUE> and the previous state returned.
may implement different levels of debug but a non-zero value results in
copies of all commands and responses also being sent to STDERR.
If C<VALUE> is C<undef> then the debug level will be set to the default
debug level for the class.
This method can also be called as a I<static> method to set/get the default
debug level for a given class.
Returns the text message returned from the last command
Returns the 3-digit code from the last command. If a command is pending
then the value 0 is returned
Returns non-zero if the last code value was greater than zero and
less than 400. This holds true for most command servers. Servers
where this does not hold may override this method.
Returns the most significant digit of the current status code. If a command
is pending then C<CMD_PENDING> is returned.
Send data to the remote server, converting LF to CRLF. Any line starting
with a '.' will be prefixed with another '.'.
C<DATA> may be an array or a reference to an array.
End the sending of data to the remote server. This is done by ensuring that
the data already sent ends with CRLF then sending '.CRLF' to end the
transmission. Once this data has been sent C<dataend> calls C<response> and
returns true if C<response> returns CMD_OK.
These methods are not intended to be called by the user, but used or
over-ridden by a sub-class of C<Net::Cmd>
=item debug_print ( DIR, TEXT )
Print debugging information. C<DIR> denotes the direction I<true> being
data being sent to the server. Calls C<debug_text> before printing to
=item debug_text ( TEXT )
This method is called to print debugging information. TEXT is
the text being sent. The method should return the text to be printed
This is primarily meant for the use of modules such as FTP where passwords
are sent, but we do not want to display them in the debugging information.
=item command ( CMD [, ARGS, ... ])
Send a command to the command server. All arguments a first joined with
a space character and CRLF is appended, this string is then sent to the
Returns undef upon failure
Sets the status code to 580 and the response text to 'Unsupported command'.
Obtain a response from the server. Upon success the most significant digit
of the status code is returned. Upon failure, timeout etc., I<undef> is
=item parse_response ( TEXT )
This method is called by C<response> as a method with one argument. It should
return an array of 2 values, the 3-digit status code and a flag which is true
when this is part of a multi-line response and this line is not the list.
Retrieve one line, delimited by CRLF, from the remote server. Returns I<undef>
B<NOTE>: If you do use this method for any reason, please remember to add
some C<debug_print> calls into your method.
Unget a line of text from the server.
=item rawdatasend ( DATA )
Send data to the remote server without performing any conversions. C<DATA>
Read data from the remote server until a line consisting of a single '.'.
Any lines starting with '..' will have one of the '.'s removed.
Returns a reference to a list containing the lines, or I<undef> upon failure.
Returns a filehandle tied to the Net::Cmd object. After issuing a
command, you may read from this filehandle using read() or <>. The
filehandle will return EOF when the final dot is encountered.
Similarly, you may write to the filehandle in order to send data to
the server after issuing a commmand that expects data to be written.
See the Net::POP3 and Net::SMTP modules for examples of this.
C<Net::Cmd> exports six subroutines, five of these, C<CMD_INFO>, C<CMD_OK>,
C<CMD_MORE>, C<CMD_REJECT> and C<CMD_ERROR>, correspond to possible results
of C<response> and C<status>. The sixth is C<CMD_PENDING>.
Graham Barr <gbarr@pobox.com>
Copyright (c) 1995-1997 Graham Barr. All rights reserved.
This program is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.
I<$Id: //depot/libnet/Net/Cmd.pm#34 $>