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.
13 use vars qw(@ISA @EXPORT $VERSION);
18 @EXPORT = qw(CMD_INFO CMD_OK CMD_MORE CMD_REJECT CMD_ERROR CMD_PENDING);
40 my %spc = ( $pkg , "");
43 while ($pkg = shift @do)
45 next if defined $done{$pkg};
49 my $v = defined ${"${pkg}::VERSION"}
50 ? "(" . ${"${pkg}::VERSION"} . ")"
54 print STDERR "$cmd: ${spc}${pkg}${v}\n";
58 @spc{@{"${pkg}::ISA"}} = (" " . $spc{$pkg}) x @{"${pkg}::ISA"};
59 unshift(@do, @{"${pkg}::ISA"});
68 @_ == 1 or @_ == 2 or croak 'usage: $obj->debug([LEVEL])';
71 my $pkg = ref($cmd) || $cmd;
76 $oldval = ${*$cmd}{'net_cmd_debug'} || 0;
80 $oldval = $debug{$pkg} || 0;
86 $level = $debug{$pkg} || 0
87 unless defined $level;
90 if($level && !exists $debug{$pkg});
94 ${*$cmd}{'net_cmd_debug'} = $level;
98 $debug{$pkg} = $level;
106 @_ == 1 or croak 'usage: $obj->message()';
110 wantarray ? @{${*$cmd}{'net_cmd_resp'}}
111 : join("", @{${*$cmd}{'net_cmd_resp'}});
114 sub debug_text { $_[2] }
118 my($cmd,$out,$text) = @_;
119 print STDERR $cmd,($out ? '>>> ' : '<<< '), $cmd->debug_text($out,$text);
124 @_ == 1 or croak 'usage: $obj->code()';
128 ${*$cmd}{'net_cmd_code'} = "000"
129 unless exists ${*$cmd}{'net_cmd_code'};
131 ${*$cmd}{'net_cmd_code'};
136 @_ == 1 or croak 'usage: $obj->status()';
140 substr(${*$cmd}{'net_cmd_code'},0,1);
145 @_ == 3 or croak 'usage: $obj->set_status(CODE, MESSAGE)';
148 my($code,$resp) = @_;
153 (${*$cmd}{'net_cmd_code'},${*$cmd}{'net_cmd_resp'}) = ($code, $resp);
162 return $cmd unless defined fileno($cmd);
165 if(exists ${*$cmd}{'net_cmd_lastch'});
169 local $SIG{PIPE} = 'IGNORE';
171 my $str = join(" ", map { /\n/ ? do { my $n = $_; $n =~ tr/\n/ /; $n } : $_; } @_) . "\015\012";
172 my $len = length $str;
176 unless (defined($swlen = syswrite($cmd,$str,$len)) && $swlen == $len);
178 $cmd->debug_print(1,$str)
181 ${*$cmd}{'net_cmd_resp'} = []; # the response
182 ${*$cmd}{'net_cmd_code'} = "000"; # Made this one up :-)
190 @_ == 1 or croak 'usage: $obj->ok()';
192 my $code = $_[0]->code;
193 0 < $code && $code < 400;
200 ${*$cmd}{'net_cmd_resp'} = [ 'Unsupported command' ];
201 ${*$cmd}{'net_cmd_code'} = 580;
209 ${*$cmd}{'net_cmd_lines'} ||= [];
211 return shift @{${*$cmd}{'net_cmd_lines'}}
212 if scalar(@{${*$cmd}{'net_cmd_lines'}});
214 my $partial = defined(${*$cmd}{'net_cmd_partial'})
215 ? ${*$cmd}{'net_cmd_partial'} : "";
216 my $fd = fileno($cmd);
226 until(scalar(@{${*$cmd}{'net_cmd_lines'}}))
228 my $timeout = $cmd->timeout || undef;
230 if (select($rout=$rin, undef, undef, $timeout))
232 unless (sysread($cmd, $buf="", 1024))
234 carp(ref($cmd) . ": Unexpected EOF on command channel")
240 substr($buf,0,0) = $partial; ## prepend from last sysread
242 my @buf = split(/\015?\012/, $buf, -1); ## break into lines
246 push(@{${*$cmd}{'net_cmd_lines'}}, map { "$_\n" } @buf);
251 carp("$cmd: Timeout") if($cmd->debug);
256 ${*$cmd}{'net_cmd_partial'} = $partial;
258 shift @{${*$cmd}{'net_cmd_lines'}};
265 ${*$cmd}{'net_cmd_lines'} ||= [];
266 unshift(@{${*$cmd}{'net_cmd_lines'}}, $str);
272 unless $_[1] =~ s/^(\d\d\d)(.?)//o;
279 my($code,$more) = (undef) x 2;
281 ${*$cmd}{'net_cmd_resp'} ||= [];
285 my $str = $cmd->getline();
288 unless defined($str);
290 $cmd->debug_print(0,$str)
293 ($code,$more) = $cmd->parse_response($str);
294 unless(defined $code)
296 $cmd->ungetline($str);
300 ${*$cmd}{'net_cmd_code'} = $code;
302 push(@{${*$cmd}{'net_cmd_resp'}},$str);
318 my $str = $cmd->getline() or return undef;
320 $cmd->debug_print(0,$str)
321 if ($cmd->debug & 4);
323 last if($str =~ /^\.\r?\n/o);
343 my $arr = @_ == 1 && ref($_[0]) ? $_[0] : \@_;
344 my $line = join("" ,@$arr);
346 return 0 unless defined(fileno($cmd));
349 unless length($line);
354 print STDERR $b,join("\n$b",split(/\n/,$line)),"\n";
357 $line =~ s/\n/\015\012/sgo;
359 ${*$cmd}{'net_cmd_lastch'} ||= " ";
360 $line = ${*$cmd}{'net_cmd_lastch'} . $line;
362 $line =~ s/(\012\.)/$1./sog;
364 ${*$cmd}{'net_cmd_lastch'} = substr($line,-1,1);
366 my $len = length($line) - 1;
369 vec($win,fileno($cmd),1) = 1;
370 my $timeout = $cmd->timeout || undef;
375 if (select(undef,$wout=$win, undef, $timeout) > 0)
377 my $w = syswrite($cmd, $line, $len, $offset);
380 carp("$cmd: $!") if $cmd->debug;
388 carp("$cmd: Timeout") if($cmd->debug);
400 return 0 unless defined(fileno($cmd));
403 unless(exists ${*$cmd}{'net_cmd_lastch'});
405 if(${*$cmd}{'net_cmd_lastch'} eq "\015")
407 syswrite($cmd,"\012",1);
411 elsif(${*$cmd}{'net_cmd_lastch'} ne "\012")
413 syswrite($cmd,"\015\012",2);
418 print STDERR "$cmd>>> .\n"
421 syswrite($cmd,".\015\012",3);
423 delete ${*$cmd}{'net_cmd_lastch'};
425 $cmd->response() == CMD_OK;
435 Net::Cmd - Network Command class (as used by FTP, SMTP etc)
445 C<Net::Cmd> is a collection of methods that can be inherited by a sub class
446 of C<IO::Handle>. These methods implement the functionality required for a
447 command based protocol, for example FTP and SMTP.
451 These methods provide a user interface to the C<Net::Cmd> object.
455 =item debug ( VALUE )
457 Set the level of debug information for this object. If C<VALUE> is not given
458 then the current state is returned. Otherwise the state is changed to
459 C<VALUE> and the previous state returned.
461 Set the level of debug information for this object. If no argument is
462 given then the current state is returned. Otherwise the state is
463 changed to C<$value>and the previous state returned. Different packages
464 may implement different levels of debug but, a non-zero value result in
465 copies of all commands and responses also being sent to STDERR.
467 If C<VALUE> is C<undef> then the debug level will be set to the default
468 debug level for the class.
470 This method can also be called as a I<static> method to set/get the default
471 debug level for a given class.
475 Returns the text message returned from the last command
479 Returns the 3-digit code from the last command. If a command is pending
480 then the value 0 is returned
484 Returns non-zero if the last code value was greater than zero and
485 less than 400. This holds true for most command servers. Servers
486 where this does not hold may override this method.
490 Returns the most significant digit of the current status code. If a command
491 is pending then C<CMD_PENDING> is returned.
493 =item datasend ( DATA )
495 Send data to the remote server, converting LF to CRLF. Any line starting
496 with a '.' will be prefixed with another '.'.
497 C<DATA> may be an array or a reference to an array.
501 End the sending of data to the remote server. This is done by ensuring that
502 the data already sent ends with CRLF then sending '.CRLF' to end the
503 transmission. Once this data has been sent C<dataend> calls C<response> and
504 returns true if C<response> returns CMD_OK.
510 These methods are not intended to be called by the user, but used or
511 over-ridden by a sub-class of C<Net::Cmd>
515 =item debug_print ( DIR, TEXT )
517 Print debugging information. C<DIR> denotes the direction I<true> being
518 data being sent to the server. Calls C<debug_text> before printing to
521 =item debug_text ( TEXT )
523 This method is called to print debugging information. TEXT is
524 the text being sent. The method should return the text to be printed
526 This is primarily meant for the use of modules such as FTP where passwords
527 are sent, but we do not want to display them in the debugging information.
529 =item command ( CMD [, ARGS, ... ])
531 Send a command to the command server. All arguments a first joined with
532 a space character and CRLF is appended, this string is then sent to the
535 Returns undef upon failure
539 Sets the status code to 580 and the response text to 'Unsupported command'.
544 Obtain a response from the server. Upon success the most significant digit
545 of the status code is returned. Upon failure, timeout etc., I<undef> is
548 =item parse_response ( TEXT )
550 This method is called by C<response> as a method with one argument. It should
551 return an array of 2 values, the 3-digit status code and a flag which is true
552 when this is part of a multi-line response and this line is not the list.
556 Retrieve one line, delimited by CRLF, from the remote server. Returns I<undef>
559 B<NOTE>: If you do use this method for any reason, please remember to add
560 some C<debug_print> calls into your method.
562 =item ungetline ( TEXT )
564 Unget a line of text from the server.
566 =item read_until_dot ()
568 Read data from the remote server until a line consisting of a single '.'.
569 Any lines starting with '..' will have one of the '.'s removed.
571 Returns a reference to a list containing the lines, or I<undef> upon failure.
577 C<Net::Cmd> exports six subroutines, five of these, C<CMD_INFO>, C<CMD_OK>,
578 C<CMD_MORE>, C<CMD_REJECT> and C<CMD_ERROR> ,correspond to possible results
579 of C<response> and C<status>. The sixth is C<CMD_PENDING>.
583 Graham Barr <gbarr@pobox.com>
587 Copyright (c) 1995-1997 Graham Barr. All rights reserved.
588 This program is free software; you can redistribute it and/or modify
589 it under the same terms as Perl itself.