home *** CD-ROM | disk | FTP | other *** search
/ CLIX - Fazer Clix Custa Nix / CLIX-CD.cdr / mac / lib / Net / Cmd.pm next >
Text File  |  1997-03-24  |  11KB  |  545 lines

  1. # Net::Cmd.pm
  2. #
  3. # Copyright (c) 1995-1997 Graham Barr <gbarr@ti.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.0801";
  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.    my $str =  join(" ",@_) . "\015\012";
  168.  
  169.    syswrite($cmd,$str,length $str);
  170.  
  171.    $cmd->debug_print(1,$str)
  172.     if($cmd->debug);
  173.  
  174.    ${*$cmd}{'net_cmd_resp'} = [];      # the response
  175.    ${*$cmd}{'net_cmd_code'} = "000";    # Made this one up :-)
  176.   }
  177.  
  178.  $cmd;
  179. }
  180.  
  181. sub ok
  182. {
  183.  @_ == 1 or croak 'usage: $obj->ok()';
  184.  
  185.  my $code = $_[0]->code;
  186.  0 < $code && $code < 400;
  187. }
  188.  
  189. sub unsupported
  190. {
  191.  my $cmd = shift;
  192.  
  193.  ${*$cmd}{'net_cmd_resp'} = [ 'Unsupported command' ];
  194.  ${*$cmd}{'net_cmd_code'} = 580;
  195.  0;
  196. }
  197.  
  198. sub getline
  199. {
  200.  my $cmd = shift;
  201.  
  202.  ${*$cmd}{'net_cmd_lines'} ||= [];
  203.  
  204.  return shift @{${*$cmd}{'net_cmd_lines'}}
  205.     if scalar(@{${*$cmd}{'net_cmd_lines'}});
  206.  
  207.  my $partial = ${*$cmd}{'net_cmd_partial'} || "";
  208.  
  209.  my $rin = "";
  210.  vec($rin,fileno($cmd),1) = 1;
  211.  
  212.  my $buf;
  213.  
  214.  until(scalar(@{${*$cmd}{'net_cmd_lines'}}))
  215.   {
  216.    my $timeout = $cmd->timeout || undef;
  217.    my $rout;
  218.    if (select($rout=$rin, undef, undef, $timeout))
  219.     {
  220.      unless (sysread($cmd, $buf="", 1024))
  221.       {
  222.        carp ref($cmd) . ": Unexpected EOF on command channel"
  223.         if $cmd->debug;
  224.        $cmd->close;
  225.        return undef;
  226.       } 
  227.  
  228.      substr($buf,0,0) = $partial;    ## prepend from last sysread
  229.  
  230.      my @buf = split(/\015?\012/, $buf);    ## break into lines
  231.  
  232.      $partial = length($buf) == 0 || substr($buf, -1, 1) eq "\012"
  233.         ? ''
  234.           : pop(@buf);
  235.  
  236.      map { $_ .= "\n" } @buf;
  237.  
  238.      push(@{${*$cmd}{'net_cmd_lines'}},@buf);
  239.  
  240.     }
  241.    else
  242.     {
  243.      carp "$cmd: Timeout" if($cmd->debug);
  244.      return undef;
  245.     }
  246.   }
  247.  
  248.  ${*$cmd}{'net_cmd_partial'} = $partial;
  249.  
  250.  shift @{${*$cmd}{'net_cmd_lines'}};
  251. }
  252.  
  253. sub ungetline
  254. {
  255.  my($cmd,$str) = @_;
  256.  
  257.  ${*$cmd}{'net_cmd_lines'} ||= [];
  258.  unshift(@{${*$cmd}{'net_cmd_lines'}}, $str);
  259. }
  260.  
  261. sub parse_response
  262. {
  263.  return ()
  264.     unless $_[1] =~ s/^(\d\d\d)(.?)//o;
  265.  ($1, $2 eq "-");
  266. }
  267.  
  268. sub response
  269. {
  270.  my $cmd = shift;
  271.  my($code,$more) = (undef) x 2;
  272.  
  273.  ${*$cmd}{'net_cmd_resp'} ||= [];
  274.  
  275.  while(1)
  276.   {
  277.    my $str = $cmd->getline();
  278.  
  279.    $cmd->debug_print(0,$str)
  280.      if ($cmd->debug);
  281.  
  282.    ($code,$more) = $cmd->parse_response($str);
  283.    unless(defined $code)
  284.     {
  285.      $cmd->ungetline($str);
  286.      last;
  287.     }
  288.  
  289.    ${*$cmd}{'net_cmd_code'} = $code;
  290.  
  291.    push(@{${*$cmd}{'net_cmd_resp'}},$str);
  292.  
  293.    last unless($more);
  294.   } 
  295.  
  296.  substr($code,0,1);
  297. }
  298.  
  299. sub read_until_dot
  300. {
  301.  my $cmd = shift;
  302.  my $arr = [];
  303.  
  304.  while(1)
  305.   {
  306.    my $str = $cmd->getline();
  307.  
  308.    $cmd->debug_print(0,$str)
  309.      if ($cmd->debug & 4);
  310.  
  311.    last if($str =~ /^\.\r?\n/o);
  312.  
  313.    $str =~ s/^\.\././o;
  314.  
  315.    push(@$arr,$str);
  316.   }
  317.  
  318.  $arr;
  319. }
  320.  
  321. sub datasend
  322. {
  323.  my $cmd = shift;
  324.  my $arr = @_ == 1 && ref($_[0]) ? $_[0] : \@_;
  325.  my $line = join("" ,@$arr);
  326.  
  327.  return 1
  328.     unless length($line);
  329.  
  330.  if($cmd->debug)
  331.   {
  332.    my $b = "$cmd>>> ";
  333.    print STDERR $b,join("\n$b",split(/\n/,$line)),"\n";
  334.   }
  335.  
  336.  $line =~ s/\n/\015\012/sgo;
  337.  
  338.  ${*$cmd}{'net_cmd_lastch'} ||= " ";
  339.  $line = ${*$cmd}{'net_cmd_lastch'} . $line;
  340.  
  341.  $line =~ s/(\012\.)/$1./sog;
  342.  
  343.  ${*$cmd}{'net_cmd_lastch'} = substr($line,-1,1);
  344.  
  345.  my $len = length($line) - 1;
  346.  
  347.  return $len == 0 ||
  348.     syswrite($cmd, $line, $len, 1) == $len;
  349. }
  350.  
  351. sub dataend
  352. {
  353.  my $cmd = shift;
  354.  
  355.  return 1
  356.     unless(exists ${*$cmd}{'net_cmd_lastch'});
  357.  
  358.  if(${*$cmd}{'net_cmd_lastch'} eq "\015")
  359.   {
  360.    syswrite($cmd,"\012",1);
  361.    print STDERR "\n"
  362.     if($cmd->debug);
  363.   }
  364.  elsif(${*$cmd}{'net_cmd_lastch'} ne "\012")
  365.   {
  366.    syswrite($cmd,"\015\012",2);
  367.    print STDERR "\n"
  368.     if($cmd->debug);
  369.   }
  370.  
  371.  print STDERR "$cmd>>> .\n"
  372.     if($cmd->debug);
  373.  
  374.  syswrite($cmd,".\015\012",3);
  375.  
  376.  delete ${*$cmd}{'net_cmd_lastch'};
  377.  
  378.  $cmd->response() == CMD_OK;
  379. }
  380.  
  381. 1;
  382.  
  383. __END__
  384.  
  385.  
  386. =head1 NAME
  387.  
  388. Net::Cmd - Network Command class (as used by FTP, SMTP etc)
  389.  
  390. =head1 SYNOPSIS
  391.  
  392.     use Net::Cmd;
  393.     
  394.     @ISA = qw(Net::Cmd);
  395.  
  396. =head1 DESCRIPTION
  397.  
  398. C<Net::Cmd> is a collection of methods that can be inherited by a sub class
  399. of C<IO::Handle>. These methods implement the functionality required for a
  400. command based protocol, for example FTP and SMTP.
  401.  
  402. =head1 USER METHODS
  403.  
  404. These methods provide a user interface to the C<Net::Cmd> object.
  405.  
  406. =over 4
  407.  
  408. =item debug ( VALUE )
  409.  
  410. Set the level of debug information for this object. If C<VALUE> is not given
  411. then the current state is returned. Otherwise the state is changed to 
  412. C<VALUE> and the previous state returned. 
  413.  
  414. Set the level of debug information for this object. If no argument is
  415. given then the current state is returned. Otherwise the state is
  416. changed to C<$value>and the previous state returned.  Different packages
  417. may implement different levels of debug but, a  non-zero value result in
  418. copies of all commands and responses also being sent to STDERR.
  419.  
  420. If C<VALUE> is C<undef> then the debug level will be set to the default
  421. debug level for the class.
  422.  
  423. This method can also be called as a I<static> method to set/get the default
  424. debug level for a given class.
  425.  
  426. =item message ()
  427.  
  428. Returns the text message returned from the last command
  429.  
  430. =item code ()
  431.  
  432. Returns the 3-digit code from the last command. If a command is pending
  433. then the value 0 is returned
  434.  
  435. =item ok ()
  436.  
  437. Returns non-zero if the last code value was greater than zero and
  438. less than 400. This holds true for most command servers. Servers
  439. where this does not hold may override this method.
  440.  
  441. =item status ()
  442.  
  443. Returns the most significant digit of the current status code. If a command
  444. is pending then C<CMD_PENDING> is returned.
  445.  
  446. =item datasend ( DATA )
  447.  
  448. Send data to the remote server, converting LF to CRLF. Any line starting
  449. with a '.' will be prefixed with another '.'.
  450. C<DATA> may be an array or a reference to an array.
  451.  
  452. =item dataend ()
  453.  
  454. End the sending of data to the remote server. This is done by ensuring that
  455. the data already sent ends with CRLF then sending '.CRLF' to end the
  456. transmission. Once this data has been sent C<dataend> calls C<response> and
  457. returns true if C<response> returns CMD_OK.
  458.  
  459. =back
  460.  
  461. =head1 CLASS METHODS
  462.  
  463. These methods are not intended to be called by the user, but used or 
  464. over-ridden by a sub-class of C<Net::Cmd>
  465.  
  466. =over 4
  467.  
  468. =item debug_print ( DIR, TEXT )
  469.  
  470. Print debugging information. C<DIR> denotes the direction I<true> being
  471. data being sent to the server. Calls C<debug_text> before printing to
  472. STDERR.
  473.  
  474. =item debug_text ( TEXT )
  475.  
  476. This method is called to print debugging information. TEXT is
  477. the text being sent. The method should return the text to be printed
  478.  
  479. This is primarily meant for the use of modules such as FTP where passwords
  480. are sent, but we do not want to display them in the debugging information.
  481.  
  482. =item command ( CMD [, ARGS, ... ])
  483.  
  484. Send a command to the command server. All arguments a first joined with
  485. a space character and CRLF is appended, this string is then sent to the
  486. command server.
  487.  
  488. Returns undef upon failure
  489.  
  490. =item unsupported ()
  491.  
  492. Sets the status code to 580 and the response text to 'Unsupported command'.
  493. Returns zero.
  494.  
  495. =item response ()
  496.  
  497. Obtain a response from the server. Upon success the most significant digit
  498. of the status code is returned. Upon failure, timeout etc., I<undef> is
  499. returned.
  500.  
  501. =item parse_response ( TEXT )
  502.  
  503. This method is called by C<response> as a method with one argument. It should
  504. return an array of 2 values, the 3-digit status code and a flag which is true
  505. when this is part of a multi-line response and this line is not the list.
  506.  
  507. =item getline ()
  508.  
  509. Retrieve one line, delimited by CRLF, from the remote server. Returns I<undef>
  510. upon failure.
  511.  
  512. B<NOTE>: If you do use this method for any reason, please remember to add
  513. some C<debug_print> calls into your method.
  514.  
  515. =item ungetline ( TEXT )
  516.  
  517. Unget a line of text from the server.
  518.  
  519. =item read_until_dot ()
  520.  
  521. Read data from the remote server until a line consisting of a single '.'.
  522. Any lines starting with '..' will have one of the '.'s removed.
  523.  
  524. Returns a reference to a list containing the lines, or I<undef> upon failure.
  525.  
  526. =back
  527.  
  528. =head1 EXPORTS
  529.  
  530. C<Net::Cmd> exports six subroutines, five of these, C<CMD_INFO>, C<CMD_OK>,
  531. C<CMD_MORE>, C<CMD_REJECT> and C<CMD_ERROR> ,correspond to possible results
  532. of C<response> and C<status>. The sixth is C<CMD_PENDING>.
  533.  
  534. =head1 AUTHOR
  535.  
  536. Graham Barr <gbarr@ti.com>
  537.  
  538. =head1 COPYRIGHT
  539.  
  540. Copyright (c) 1995-1997 Graham Barr. All rights reserved.
  541. This program is free software; you can redistribute it and/or modify
  542. it under the same terms as Perl itself.
  543.  
  544. =cut
  545.