Cmd.pm revision 7c478bd95313f5f23a4c958a745db2134aa03244
#
# Copyright (c) 1995-1997 Graham Barr <gbarr@pobox.com>. All rights reserved.
# modify it under the same terms as Perl itself.
require 5.001;
require Exporter;
use strict;
use Carp;
use Symbol 'gensym';
BEGIN {
if ($^O eq 'os390') {
# Convert::EBCDIC->import;
}
}
$VERSION = "2.24";
sub CMD_INFO { 1 }
sub CMD_OK { 2 }
sub CMD_MORE { 3 }
sub CMD_REJECT { 4 }
sub CMD_ERROR { 5 }
sub CMD_PENDING { 0 }
my %debug = ();
sub toebcdic
{
my $cmd = shift;
unless (exists ${*$cmd}{'net_cmd_asciipeer'})
{
my $string = $_[0];
}
${*$cmd}{'net_cmd_asciipeer'}
: $_[0];
}
sub toascii
{
my $cmd = shift;
${*$cmd}{'net_cmd_asciipeer'}
: $_[0];
}
sub _print_isa
{
no strict qw(refs);
my $pkg = shift;
my %done = ();
{
my $v = defined ${"${pkg}::VERSION"}
? "(" . ${"${pkg}::VERSION"} . ")"
: "";
if(@{"${pkg}::ISA"})
{
unshift(@do, @{"${pkg}::ISA"});
}
}
}
sub debug
{
@_ == 1 or @_ == 2 or croak 'usage: $obj->debug([LEVEL])';
my $oldval = 0;
if(ref($cmd))
{
}
else
{
}
return $oldval
unless @_ == 2;
unless defined $level;
if(ref($cmd))
{
}
else
{
}
$oldval;
}
sub message
{
@_ == 1 or croak 'usage: $obj->message()';
my $cmd = shift;
wantarray ? @{${*$cmd}{'net_cmd_resp'}}
}
sub debug_text { $_[2] }
sub debug_print
{
}
sub code
{
@_ == 1 or croak 'usage: $obj->code()';
my $cmd = shift;
unless exists ${*$cmd}{'net_cmd_code'};
${*$cmd}{'net_cmd_code'};
}
sub status
{
@_ == 1 or croak 'usage: $obj->status()';
my $cmd = shift;
}
sub set_status
{
@_ == 3 or croak 'usage: $obj->set_status(CODE, MESSAGE)';
my $cmd = shift;
unless ref($resp);
1;
}
sub command
{
my $cmd = shift;
unless (defined fileno($cmd))
{
return $cmd;
}
if(exists ${*$cmd}{'net_cmd_need_crlf'});
if (scalar(@_))
{
my $str = join(" ", map { /\n/ ? do { my $n = $_; $n =~ tr/\n/ /; $n } : $_; } @_);
$str .= "\015\012";
my $swlen;
$cmd->close
}
$cmd;
}
sub ok
{
@_ == 1 or croak 'usage: $obj->ok()';
}
sub unsupported
{
my $cmd = shift;
0;
}
sub getline
{
my $cmd = shift;
${*$cmd}{'net_cmd_lines'} ||= [];
return shift @{${*$cmd}{'net_cmd_lines'}}
if scalar(@{${*$cmd}{'net_cmd_lines'}});
return undef
unless defined $fd;
my $rin = "";
my $buf;
until(scalar(@{${*$cmd}{'net_cmd_lines'}}))
{
my $rout;
{
{
carp(ref($cmd) . ": Unexpected EOF on command channel")
$cmd->close;
return undef;
}
}
else
{
return undef;
}
}
if ($tr)
{
{
}
}
shift @{${*$cmd}{'net_cmd_lines'}};
}
sub ungetline
{
${*$cmd}{'net_cmd_lines'} ||= [];
}
sub parse_response
{
return ()
unless $_[1] =~ s/^(\d\d\d)(.?)//o;
($1, $2 eq "-");
}
sub response
{
my $cmd = shift;
${*$cmd}{'net_cmd_resp'} ||= [];
while(1)
{
return CMD_ERROR
unless defined($str);
unless(defined $code)
{
last;
}
last unless($more);
}
}
sub read_until_dot
{
my $cmd = shift;
my $fh = shift;
my $arr = [];
while(1)
{
last if($str =~ /^\.\r?\n/o);
$str =~ s/^\.\././o;
if (defined $fh)
{
}
else
{
}
}
$arr;
}
sub datasend
{
my $cmd = shift;
return 0 unless defined(fileno($cmd));
unless (length $line) {
# Even though we are not sending anything, the fact we were
# called means that dataend needs to be called before the next
# command, which happens of net_cmd_need_crlf exists
return 1;
}
foreach my $b (split(/\n/,$line)) {
}
}
my $offset = 0;
my $win = "";
while($len)
{
my $wout;
{
unless (defined($w))
{
return undef;
}
$len -= $w;
$offset += $w;
}
else
{
return undef;
}
}
1;
}
sub rawdatasend
{
my $cmd = shift;
return 0 unless defined(fileno($cmd));
return 1
unless length($line);
{
my $b = "$cmd>>> ";
}
my $offset = 0;
my $win = "";
while($len)
{
my $wout;
{
unless (defined($w))
{
return undef;
}
$len -= $w;
$offset += $w;
}
else
{
return undef;
}
}
1;
}
sub dataend
{
my $cmd = shift;
return 0 unless defined(fileno($cmd));
return 1
unless(exists ${*$cmd}{'net_cmd_need_crlf'});
if ${*$cmd}{'net_cmd_need_crlf'};
delete ${*$cmd}{'net_cmd_need_crlf'};
}
# read and write to tied filehandle
sub tied_fh {
my $cmd = shift;
return $fh;
}
# tie to myself
sub TIEHANDLE {
my $class = shift;
my $cmd = shift;
return $cmd;
}
# Tied filehandle read. Reads requested data length, returning
# end-of-file when the dot is encountered.
sub READ {
my $cmd = shift;
return unless exists ${*$cmd}{'net_cmd_readbuf'};
my $done = 0;
}
$_[0] = '';
return length $_[0];
}
sub READLINE {
my $cmd = shift;
# 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'};
return if $line =~ /^\.\r?\n/;
$line;
}
sub PRINT {
my $cmd = shift;
$offset += 0;
return $len;
}
sub CLOSE {
my $cmd = shift;
delete ${*$cmd}{'net_cmd_readbuf'};
delete ${*$cmd}{'net_cmd_sending'};
$r;
}
1;
=head1 NAME
Net::Cmd - Network Command class (as used by FTP, SMTP etc)
=head1 SYNOPSIS
use Net::Cmd;
@ISA = qw(Net::Cmd);
=head1 DESCRIPTION
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.
=head1 USER METHODS
These methods provide a user interface to the C<Net::Cmd> object.
=over 4
=item debug ( VALUE )
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.
Different packages
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.
debug level for a given class.
=item message ()
Returns the text message returned from the last command
=item code ()
Returns the 3-digit code from the last command. If a command is pending
then the value 0 is returned
=item ok ()
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.
=item status ()
Returns the most significant digit of the current status code. If a command
is pending then C<CMD_PENDING> is returned.
=item datasend ( DATA )
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.
=item dataend ()
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.
=back
=head1 CLASS METHODS
These methods are not intended to be called by the user, but used or
over-ridden by a sub-class of C<Net::Cmd>
=over 4
=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
STDERR.
=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
command server.
Returns undef upon failure
=item unsupported ()
Sets the status code to 580 and the response text to 'Unsupported command'.
Returns zero.
=item response ()
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
returned.
=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.
=item getline ()
Retrieve one line, delimited by CRLF, from the remote server. Returns I<undef>
upon failure.
B<NOTE>: If you do use this method for any reason, please remember to add
some C<debug_print> calls into your method.
=item ungetline ( TEXT )
Unget a line of text from the server.
=item rawdatasend ( DATA )
Send data to the remote server without performing any conversions. C<DATA>
is a scalar.
=item read_until_dot ()
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.
=item tied_fh ()
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.
=back
=head1 EXPORTS
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>.
=head1 AUTHOR
Graham Barr <gbarr@pobox.com>
=head1 COPYRIGHT
Copyright (c) 1995-1997 Graham Barr. All rights reserved.
it under the same terms as Perl itself.
=for html <hr>
=cut