home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / perl_ste.zip / Net / Cmd.pm next >
Text File  |  1997-11-17  |  11KB  |  556 lines

  1. # Net::Cmd.pm
  2. #
  3. # Copyright (c) 1995-1997 Graham Barr <gbarr@pobox.com>. All rights reserved.
  4. # This program is free software; you can redistribute it and/or
  5. # modify it under the same terms as Perl itself.
  6.  
  7. package Net::Cmd;
  8.  
  9. require 5.001;
  10. require Exporter;
  11.  
  12. use strict;
  13. use vars qw(@ISA @EXPORT $VERSION);
  14. use Carp;
  15.  
  16. $VERSION = "2.10";
  17. @ISA     = qw(Exporter);
  18. @EXPORT  = qw(CMD_INFO CMD_OK CMD_MORE CMD_REJECT CMD_ERROR CMD_PENDING);
  19.  
  20. sub CMD_INFO    { 1 }
  21. sub CMD_OK    { 2 }
  22. sub CMD_MORE    { 3 }
  23. sub CMD_REJECT    { 4 }
  24. sub CMD_ERROR    { 5 }
  25. sub CMD_PENDING { 0 }
  26.  
  27. my %debug = ();
  28.  
  29. sub _print_isa
  30. {
  31.  no strict qw(refs);
  32.  
  33.  my $pkg = shift;
  34.  my $cmd = $pkg;
  35.  
  36.  $debug{$pkg} ||= 0;
  37.  
  38.  my %done = ();
  39.  my @do   = ($pkg);
  40.  my %spc = ( $pkg , "");
  41.  
  42.  print STDERR "\n";
  43.  while ($pkg = shift @do)
  44.   {
  45.    next if defined $done{$pkg};
  46.  
  47.    $done{$pkg} = 1;
  48.  
  49.    my $v = defined ${"${pkg}::VERSION"}
  50.                 ? "(" . ${"${pkg}::VERSION"} . ")"
  51.                 : "";
  52.  
  53.    my $spc = $spc{$pkg};
  54.    print STDERR "$cmd: ${spc}${pkg}${v}\n";
  55.  
  56.    if(defined @{"${pkg}::ISA"})
  57.     {
  58.      @spc{@{"${pkg}::ISA"}} = ("  " . $spc{$pkg}) x @{"${pkg}::ISA"};
  59.      unshift(@do, @{"${pkg}::ISA"});
  60.     }
  61.   }
  62.  
  63.  print STDERR "\n";
  64. }
  65.  
  66. sub debug
  67. {
  68.  @_ == 1 or @_ == 2 or croak 'usage: $obj->debug([LEVEL])';
  69.  
  70.  my($cmd,$level) = @_;
  71.  my $pkg = ref($cmd) || $cmd;
  72.  my $oldval = 0;
  73.  
  74.  if(ref($cmd))
  75.   {
  76.    $oldval = ${*$cmd}{'net_cmd_debug'} || 0;
  77.   }
  78.  else
  79.   {
  80.    $oldval = $debug{$pkg} || 0;
  81.   }
  82.  
  83.  return $oldval
  84.     unless @_ == 2;
  85.  
  86.  $level = $debug{$pkg} || 0
  87.     unless defined $level;
  88.  
  89.  _print_isa($pkg)
  90.     if($level && !exists $debug{$pkg});
  91.  
  92.  if(ref($cmd))
  93.   {
  94.    ${*$cmd}{'net_cmd_debug'} = $level;
  95.   }
  96.  else
  97.   {
  98.    $debug{$pkg} = $level;
  99.   }
  100.  
  101.  $oldval;
  102. }
  103.  
  104. sub message
  105. {
  106.  @_ == 1 or croak 'usage: $obj->message()';
  107.  
  108.  my $cmd = shift;
  109.  
  110.  wantarray ? @{${*$cmd}{'net_cmd_resp'}}
  111.            : join("", @{${*$cmd}{'net_cmd_resp'}});
  112. }
  113.  
  114. sub debug_text { $_[2] }
  115.  
  116. sub debug_print
  117. {
  118.  my($cmd,$out,$text) = @_;
  119.  print STDERR $cmd,($out ? '>>> ' : '<<< '), $cmd->debug_text($out,$text);
  120. }
  121.  
  122. sub code
  123. {
  124.  @_ == 1 or croak 'usage: $obj->code()';
  125.  
  126.  my $cmd = shift;
  127.  
  128.  ${*$cmd}{'net_cmd_code'} = "000"
  129.     unless exists ${*$cmd}{'net_cmd_code'};
  130.  
  131.  ${*$cmd}{'net_cmd_code'};
  132. }
  133.  
  134. sub status
  135. {
  136.  @_ == 1 or croak 'usage: $obj->status()';
  137.  
  138.  my $cmd = shift;
  139.  
  140.  substr(${*$cmd}{'net_cmd_code'},0,1);
  141. }
  142.  
  143. sub set_status
  144. {
  145.  @_ == 3 or croak 'usage: $obj->set_status(CODE, MESSAGE)';
  146.  
  147.  my $cmd = shift;
  148.  my($code,$resp) = @_;
  149.  
  150.  $resp = [ $resp ]
  151.     unless ref($resp);
  152.  
  153.  (${*$cmd}{'net_cmd_code'},${*$cmd}{'net_cmd_resp'}) = ($code, $resp);
  154.  
  155.  1;
  156. }
  157.  
  158. sub command
  159. {
  160.  my $cmd = shift;
  161.  
  162.  $cmd->dataend()
  163.     if(exists ${*$cmd}{'net_cmd_lastch'});
  164.  
  165.  if (scalar(@_))
  166.   {
  167.    local $SIG{PIPE} = 'IGNORE';
  168.  
  169.    my $str =  join(" ",@_) . "\015\012";
  170.    my $len = length $str;
  171.  
  172.    $cmd->close
  173.     unless syswrite($cmd,$str,$len) == $len;
  174.  
  175.    $cmd->debug_print(1,$str)
  176.     if($cmd->debug);
  177.  
  178.    ${*$cmd}{'net_cmd_resp'} = [];      # the response
  179.    ${*$cmd}{'net_cmd_code'} = "000";    # Made this one up :-)
  180.   }
  181.  
  182.  $cmd;
  183. }
  184.  
  185. sub ok
  186. {
  187.  @_ == 1 or croak 'usage: $obj->ok()';
  188.  
  189.  my $code = $_[0]->code;
  190.  0 < $code && $code < 400;
  191. }
  192.  
  193. sub unsupported
  194. {
  195.  my $cmd = shift;
  196.  
  197.  ${*$cmd}{'net_cmd_resp'} = [ 'Unsupported command' ];
  198.  ${*$cmd}{'net_cmd_code'} = 580;
  199.  0;
  200. }
  201.  
  202. sub getline
  203. {
  204.  my $cmd = shift;
  205.  
  206.  ${*$cmd}{'net_cmd_lines'} ||= [];
  207.  
  208.  return shift @{${*$cmd}{'net_cmd_lines'}}
  209.     if scalar(@{${*$cmd}{'net_cmd_lines'}});
  210.  
  211.  my $partial = ${*$cmd}{'net_cmd_partial'} || "";
  212.  my $fd = fileno($cmd);
  213.  
  214.  return undef
  215.     unless defined $fd;
  216.  
  217.  my $rin = "";
  218.  vec($rin,$fd,1) = 1;
  219.  
  220.  my $buf;
  221.  
  222.  until(scalar(@{${*$cmd}{'net_cmd_lines'}}))
  223.   {
  224.    my $timeout = $cmd->timeout || undef;
  225.    my $rout;
  226.    if (select($rout=$rin, undef, undef, $timeout))
  227.     {
  228.      unless (sysread($cmd, $buf="", 1024))
  229.       {
  230.        carp ref($cmd) . ": Unexpected EOF on command channel"
  231.         if $cmd->debug;
  232.        $cmd->close;
  233.        return undef;
  234.       } 
  235.  
  236.      substr($buf,0,0) = $partial;    ## prepend from last sysread
  237.  
  238.      my @buf = split(/\015?\012/, $buf);    ## break into lines
  239.  
  240.      $partial = length($buf) == 0 || substr($buf, -1, 1) eq "\012"
  241.         ? ''
  242.           : pop(@buf);
  243.  
  244.      map { $_ .= "\n" } @buf;
  245.  
  246.      push(@{${*$cmd}{'net_cmd_lines'}},@buf);
  247.  
  248.     }
  249.    else
  250.     {
  251.      carp "$cmd: Timeout" if($cmd->debug);
  252.      return undef;
  253.     }
  254.   }
  255.  
  256.  ${*$cmd}{'net_cmd_partial'} = $partial;
  257.  
  258.  shift @{${*$cmd}{'net_cmd_lines'}};
  259. }
  260.  
  261. sub ungetline
  262. {
  263.  my($cmd,$str) = @_;
  264.  
  265.  ${*$cmd}{'net_cmd_lines'} ||= [];
  266.  unshift(@{${*$cmd}{'net_cmd_lines'}}, $str);
  267. }
  268.  
  269. sub parse_response
  270. {
  271.  return ()
  272.     unless $_[1] =~ s/^(\d\d\d)(.?)//o;
  273.  ($1, $2 eq "-");
  274. }
  275.  
  276. sub response
  277. {
  278.  my $cmd = shift;
  279.  my($code,$more) = (undef) x 2;
  280.  
  281.  ${*$cmd}{'net_cmd_resp'} ||= [];
  282.  
  283.  while(1)
  284.   {
  285.    my $str = $cmd->getline();
  286.  
  287.    return CMD_ERROR
  288.     unless defined($str);
  289.  
  290.    $cmd->debug_print(0,$str)
  291.      if ($cmd->debug);
  292.  
  293.    ($code,$more) = $cmd->parse_response($str);
  294.    unless(defined $code)
  295.     {
  296.      $cmd->ungetline($str);
  297.      last;
  298.     }
  299.  
  300.    ${*$cmd}{'net_cmd_code'} = $code;
  301.  
  302.    push(@{${*$cmd}{'net_cmd_resp'}},$str);
  303.  
  304.    last unless($more);
  305.   } 
  306.  
  307.  substr($code,0,1);
  308. }
  309.  
  310. sub read_until_dot
  311. {
  312.  my $cmd = shift;
  313.  my $arr = [];
  314.  
  315.  while(1)
  316.   {
  317.    my $str = $cmd->getline();
  318.  
  319.    $cmd->debug_print(0,$str)
  320.      if ($cmd->debug & 4);
  321.  
  322.    last if($str =~ /^\.\r?\n/o);
  323.  
  324.    $str =~ s/^\.\././o;
  325.  
  326.    push(@$arr,$str);
  327.   }
  328.  
  329.  $arr;
  330. }
  331.  
  332. sub datasend
  333. {
  334.  my $cmd = shift;
  335.  my $arr = @_ == 1 && ref($_[0]) ? $_[0] : \@_;
  336.  my $line = join("" ,@$arr);
  337.  
  338.  return 1
  339.     unless length($line);
  340.  
  341.  if($cmd->debug)
  342.   {
  343.    my $b = "$cmd>>> ";
  344.    print STDERR $b,join("\n$b",split(/\n/,$line)),"\n";
  345.   }
  346.  
  347.  $line =~ s/\n/\015\012/sgo;
  348.  
  349.  ${*$cmd}{'net_cmd_lastch'} ||= " ";
  350.  $line = ${*$cmd}{'net_cmd_lastch'} . $line;
  351.  
  352.  $line =~ s/(\012\.)/$1./sog;
  353.  
  354.  ${*$cmd}{'net_cmd_lastch'} = substr($line,-1,1);
  355.  
  356.  my $len = length($line) - 1;
  357.  
  358.  return $len == 0 ||
  359.     syswrite($cmd, $line, $len, 1) == $len;
  360. }
  361.  
  362. sub dataend
  363. {
  364.  my $cmd = shift;
  365.  
  366.  return 1
  367.     unless(exists ${*$cmd}{'net_cmd_lastch'});
  368.  
  369.  if(${*$cmd}{'net_cmd_lastch'} eq "\015")
  370.   {
  371.    syswrite($cmd,"\012",1);
  372.    print STDERR "\n"
  373.     if($cmd->debug);
  374.   }
  375.  elsif(${*$cmd}{'net_cmd_lastch'} ne "\012")
  376.   {
  377.    syswrite($cmd,"\015\012",2);
  378.    print STDERR "\n"
  379.     if($cmd->debug);
  380.   }
  381.  
  382.  print STDERR "$cmd>>> .\n"
  383.     if($cmd->debug);
  384.  
  385.  syswrite($cmd,".\015\012",3);
  386.  
  387.  delete ${*$cmd}{'net_cmd_lastch'};
  388.  
  389.  $cmd->response() == CMD_OK;
  390. }
  391.  
  392. 1;
  393.  
  394. __END__
  395.  
  396.  
  397. =head1 NAME
  398.  
  399. Net::Cmd - Network Command class (as used by FTP, SMTP etc)
  400.  
  401. =head1 SYNOPSIS
  402.  
  403.     use Net::Cmd;
  404.     
  405.     @ISA = qw(Net::Cmd);
  406.  
  407. =head1 DESCRIPTION
  408.  
  409. C<Net::Cmd> is a collection of methods that can be inherited by a sub class
  410. of C<IO::Handle>. These methods implement the functionality required for a
  411. command based protocol, for example FTP and SMTP.
  412.  
  413. =head1 USER METHODS
  414.  
  415. These methods provide a user interface to the C<Net::Cmd> object.
  416.  
  417. =over 4
  418.  
  419. =item debug ( VALUE )
  420.  
  421. Set the level of debug information for this object. If C<VALUE> is not given
  422. then the current state is returned. Otherwise the state is changed to 
  423. C<VALUE> and the previous state returned. 
  424.  
  425. Set the level of debug information for this object. If no argument is
  426. given then the current state is returned. Otherwise the state is
  427. changed to C<$value>and the previous state returned.  Different packages
  428. may implement different levels of debug but, a  non-zero value result in
  429. copies of all commands and responses also being sent to STDERR.
  430.  
  431. If C<VALUE> is C<undef> then the debug level will be set to the default
  432. debug level for the class.
  433.  
  434. This method can also be called as a I<static> method to set/get the default
  435. debug level for a given class.
  436.  
  437. =item message ()
  438.  
  439. Returns the text message returned from the last command
  440.  
  441. =item code ()
  442.  
  443. Returns the 3-digit code from the last command. If a command is pending
  444. then the value 0 is returned
  445.  
  446. =item ok ()
  447.  
  448. Returns non-zero if the last code value was greater than zero and
  449. less than 400. This holds true for most command servers. Servers
  450. where this does not hold may override this method.
  451.  
  452. =item status ()
  453.  
  454. Returns the most significant digit of the current status code. If a command
  455. is pending then C<CMD_PENDING> is returned.
  456.  
  457. =item datasend ( DATA )
  458.  
  459. Send data to the remote server, converting LF to CRLF. Any line starting
  460. with a '.' will be prefixed with another '.'.
  461. C<DATA> may be an array or a reference to an array.
  462.  
  463. =item dataend ()
  464.  
  465. End the sending of data to the remote server. This is done by ensuring that
  466. the data already sent ends with CRLF then sending '.CRLF' to end the
  467. transmission. Once this data has been sent C<dataend> calls C<response> and
  468. returns true if C<response> returns CMD_OK.
  469.  
  470. =back
  471.  
  472. =head1 CLASS METHODS
  473.  
  474. These methods are not intended to be called by the user, but used or 
  475. over-ridden by a sub-class of C<Net::Cmd>
  476.  
  477. =over 4
  478.  
  479. =item debug_print ( DIR, TEXT )
  480.  
  481. Print debugging information. C<DIR> denotes the direction I<true> being
  482. data being sent to the server. Calls C<debug_text> before printing to
  483. STDERR.
  484.  
  485. =item debug_text ( TEXT )
  486.  
  487. This method is called to print debugging information. TEXT is
  488. the text being sent. The method should return the text to be printed
  489.  
  490. This is primarily meant for the use of modules such as FTP where passwords
  491. are sent, but we do not want to display them in the debugging information.
  492.  
  493. =item command ( CMD [, ARGS, ... ])
  494.  
  495. Send a command to the command server. All arguments a first joined with
  496. a space character and CRLF is appended, this string is then sent to the
  497. command server.
  498.  
  499. Returns undef upon failure
  500.  
  501. =item unsupported ()
  502.  
  503. Sets the status code to 580 and the response text to 'Unsupported command'.
  504. Returns zero.
  505.  
  506. =item response ()
  507.  
  508. Obtain a response from the server. Upon success the most significant digit
  509. of the status code is returned. Upon failure, timeout etc., I<undef> is
  510. returned.
  511.  
  512. =item parse_response ( TEXT )
  513.  
  514. This method is called by C<response> as a method with one argument. It should
  515. return an array of 2 values, the 3-digit status code and a flag which is true
  516. when this is part of a multi-line response and this line is not the list.
  517.  
  518. =item getline ()
  519.  
  520. Retrieve one line, delimited by CRLF, from the remote server. Returns I<undef>
  521. upon failure.
  522.  
  523. B<NOTE>: If you do use this method for any reason, please remember to add
  524. some C<debug_print> calls into your method.
  525.  
  526. =item ungetline ( TEXT )
  527.  
  528. Unget a line of text from the server.
  529.  
  530. =item read_until_dot ()
  531.  
  532. Read data from the remote server until a line consisting of a single '.'.
  533. Any lines starting with '..' will have one of the '.'s removed.
  534.  
  535. Returns a reference to a list containing the lines, or I<undef> upon failure.
  536.  
  537. =back
  538.  
  539. =head1 EXPORTS
  540.  
  541. C<Net::Cmd> exports six subroutines, five of these, C<CMD_INFO>, C<CMD_OK>,
  542. C<CMD_MORE>, C<CMD_REJECT> and C<CMD_ERROR> ,correspond to possible results
  543. of C<response> and C<status>. The sixth is C<CMD_PENDING>.
  544.  
  545. =head1 AUTHOR
  546.  
  547. Graham Barr <gbarr@pobox.com>
  548.  
  549. =head1 COPYRIGHT
  550.  
  551. Copyright (c) 1995-1997 Graham Barr. All rights reserved.
  552. This program is free software; you can redistribute it and/or modify
  553. it under the same terms as Perl itself.
  554.  
  555. =cut
  556.