3 # Copyright (c) 1995 Graham Barr <Graham.Barr@tiuk.ti.com>. All rights
4 # reserved. This program is free software; you can redistribute it and/or
5 # modify it under the same terms as Perl itself.
11 Net::Cmd - Network Command class (as used by FTP, SMTP etc)
21 C<Net::Cmd> is a collection of methods that can be inherited by a sub class
22 of C<IO::Handle>. These methods implement the functionality required for a
23 command based protocol, for example FTP and SMTP.
27 These methods provide a user interface to the C<Net::Cmd> object.
33 Set the level of debug information for this object. If C<VALUE> is not given
34 then the current state is returned. Otherwise the state is changed to
35 C<VALUE> and the previous state returned. If C<VALUE> is C<undef> then
36 the debug level will be set to the default debug level for the class.
38 This method can also be called as a I<static> method to set/get the default
39 debug level for a given class.
43 Returns the text message returned from the last command
47 Returns the 3-digit code from the last command. If a command is pending
48 then the value 0 is returned
52 Returns non-zero if the last code value was greater than zero and
53 less than 400. This holds true for most command servers. Servers
54 where this does not hold may override this method.
58 Returns the most significant digit of the current status code. If a command
59 is pending then C<CMD_PENDING> is returned.
61 =item datasend ( DATA )
63 Send data to the remote server, delimiting lines with CRLF. Any lin starting
64 with a '.' will be prefixed with another '.'.
68 End the sending of data to the remote server. This is done by ensureing that
69 the data already sent ends with CRLF then sending '.CRLF' to end the
70 transmission. Once this data has been sent C<dataend> calls C<response> and
71 returns true if C<response> returns CMD_OK.
77 These methods are not intended to be called by the user, but used or
78 over-ridden by a sub-class of C<Net::Cmd>
82 =item debug_print ( DIR, TEXT )
84 Print debugging information. C<DIR> denotes the direction I<true> being
85 data being sent to the server. Calls C<debug_text> before printing to
88 =item debug_text ( TEXT )
90 This method is called to print debugging information. TEXT is
91 the text being sent. The method should return the text to be printed
93 This is primarily meant for the use of modules such as FTP where passwords
94 are sent, but we do not want to display them in the debugging information.
96 =item command ( CMD [, ARGS, ... ])
98 Send a command to the command server. All arguments a first joined with
99 a space character and CRLF is appended, this string is then sent to the
102 Returns undef upon failure
106 Sets the status code to 580 and the response text to 'Unsupported command'.
111 Obtain a responce from the server. Upon success the most significant digit
112 of the status code is returned. Upon failure, timeout etc., I<undef> is
115 =item parse_response ( TEXT )
117 This method is called by C<response> as a method with one argument. It should
118 return an array of 2 values, the 3-digit status code and a flag which is true
119 when this is part of a multi-line response and this line is not the list.
123 Retreive one line, delimited by CRLF, from the remote server. Returns I<undef>
126 B<NOTE>: If you do use this method for any reason, please remember to add
127 some C<debug_print> calls into your method.
129 =item ungetline ( TEXT )
131 Unget a line of text from the server.
133 =item read_until_dot ()
135 Read data from the remote server until a line consisting of a single '.'.
136 Any lines starting with '..' will have one of the '.'s removed.
138 Returns a reference to a list containing the lines, or I<undef> upon failure.
144 C<Net::Cmd> exports six subroutines, five of these, C<CMD_INFO>, C<CMD_OK>,
145 C<CMD_MORE>, C<CMD_REJECT> and C<CMD_ERROR> ,correspond to possible results
146 of C<response> and C<status>. The sixth is C<CMD_PENDING>.
150 Graham Barr <Graham.Barr@tiuk.ti.com>
158 Copyright (c) 1995 Graham Barr. All rights reserved. This program is free
159 software; you can redistribute it and/or modify it under the same terms
168 use vars qw(@ISA @EXPORT $VERSION);
171 $VERSION = sprintf("%d.%02d", q$Revision: 2.2 $ =~ /(\d+)\.(\d+)/);
173 @EXPORT = qw(CMD_INFO CMD_OK CMD_MORE CMD_REJECT CMD_ERROR CMD_PENDING);
180 sub CMD_PENDING { 0 }
195 my %spc = ( $pkg , "");
198 while ($pkg = shift @do)
200 next if defined $done{$pkg};
204 my $v = defined ${"${pkg}::VERSION"}
205 ? "(" . ${"${pkg}::VERSION"} . ")"
208 my $spc = $spc{$pkg};
209 print STDERR "$cmd: ${spc}${pkg}${v}\n";
211 if(defined @{"${pkg}::ISA"})
213 @spc{@{"${pkg}::ISA"}} = (" " . $spc{$pkg}) x @{"${pkg}::ISA"};
214 unshift(@do, @{"${pkg}::ISA"});
223 @_ == 1 or @_ == 2 or croak 'usage: $obj->debug([LEVEL])';
225 my($cmd,$level) = @_;
226 my $pkg = ref($cmd) || $cmd;
231 $oldval = ${*$cmd}{'net_cmd_debug'} || 0;
235 $oldval = $debug{$pkg} || 0;
241 $level = $debug{$pkg} || 0
242 unless defined $level;
245 if($level && !exists $debug{$pkg});
249 ${*$cmd}{'net_cmd_debug'} = $level;
253 $debug{$pkg} = $level;
261 @_ == 1 or croak 'usage: $obj->message()';
265 wantarray ? @{${*$cmd}{'net_cmd_resp'}}
266 : join("", @{${*$cmd}{'net_cmd_resp'}});
269 sub debug_text { $_[2] }
273 my($cmd,$out,$text) = @_;
274 print STDERR $cmd,($out ? '>>> ' : '<<< '), $cmd->debug_text($out,$text);
279 @_ == 1 or croak 'usage: $obj->code()';
283 ${*$cmd}{'net_cmd_code'};
288 @_ == 1 or croak 'usage: $obj->code()';
292 substr(${*$cmd}{'net_cmd_code'},0,1);
297 @_ == 3 or croak 'usage: $obj->set_status( CODE, MESSAGE)';
301 (${*$cmd}{'net_cmd_code'},${*$cmd}{'net_cmd_resp'}) = @_;
311 if(exists ${*$cmd}{'net_cmd_lastch'});
315 my $str = join(" ", @_) . "\015\012";
317 syswrite($cmd,$str,length $str);
319 $cmd->debug_print(1,$str)
322 ${*$cmd}{'net_cmd_resp'} = []; # the responce
323 ${*$cmd}{'net_cmd_code'} = "000"; # Made this one up :-)
331 @_ == 1 or croak 'usage: $obj->ok()';
333 my $code = $_[0]->code;
334 0 < $code && $code < 400;
341 ${*$cmd}{'net_cmd_resp'} = [ 'Unsupported command' ];
342 ${*$cmd}{'net_cmd_code'} = 580;
350 ${*$cmd}{'net_cmd_lines'} ||= [];
352 return shift @{${*$cmd}{'net_cmd_lines'}}
353 if scalar(@{${*$cmd}{'net_cmd_lines'}});
355 my $partial = ${*$cmd}{'net_cmd_partial'} || "";
358 vec($rin,fileno($cmd),1) = 1;
362 until(scalar(@{${*$cmd}{'net_cmd_lines'}}))
364 my $timeout = $cmd->timeout || undef;
366 if (select($rout=$rin, undef, undef, $timeout))
368 unless (sysread($cmd, $buf="", 1024))
370 carp ref($cmd) . ": Unexpected EOF on command channel";
374 substr($buf,0,0) = $partial; ## prepend from last sysread
376 my @buf = split(/\015?\012/, $buf); ## break into lines
378 $partial = length($buf) == 0 || substr($buf, -1, 1) eq "\012"
382 map { $_ .= "\n" } @buf;
384 push(@{${*$cmd}{'net_cmd_lines'}},@buf);
389 carp "$cmd: Timeout" if($cmd->debug);
394 ${*$cmd}{'net_cmd_partial'} = $partial;
396 shift @{${*$cmd}{'net_cmd_lines'}};
403 ${*$cmd}{'net_cmd_lines'} ||= [];
404 unshift(@{${*$cmd}{'net_cmd_lines'}}, $str);
410 unless $_[1] =~ s/^(\d\d\d)(.)//o;
417 my($code,$more) = (undef) x 2;
419 ${*$cmd}{'net_cmd_resp'} ||= [];
423 my $str = $cmd->getline();
425 $cmd->debug_print(0,$str)
428 if($str =~ s/^(\d\d\d)(.?)//o)
430 ($code,$more) = ($1,$2 && $2 eq "-");
434 $cmd->ungetline($str);
438 push(@{${*$cmd}{'net_cmd_resp'}},$str);
443 ${*$cmd}{'net_cmd_code'} = $code;
455 my $str = $cmd->getline();
457 $cmd->debug_print(0,$str)
458 if ($cmd->debug & 4);
460 last if($str =~ /^\.\n/o);
473 my $lch = exists ${*$cmd}{'net_cmd_lastch'} ? ${*$cmd}{'net_cmd_lastch'}
475 my $arr = @_ == 1 && ref($_[0]) ? $_[0] : \@_;
476 my $line = $lch . join("" ,@$arr);
478 ${*$cmd}{'net_cmd_lastch'} = substr($line,-1,1);
481 unless length($line) > 1;
485 my $ln = substr($line,1);
487 print STDERR $b,join("\n$b",split(/\n/,$ln)),"\n";
490 $line =~ s/\n/\015\012/sgo;
491 $line =~ s/(?=\012\.)/./sgo;
493 my $len = length($line) - 1;
496 syswrite($cmd, $line, $len, 1) == $len;
504 unless(exists ${*$cmd}{'net_cmd_lastch'});
506 if(${*$cmd}{'net_cmd_lastch'} eq "\015")
508 syswrite($cmd,"\012",1);
512 elsif(${*$cmd}{'net_cmd_lastch'} ne "\012")
514 syswrite($cmd,"\015\012",2);
519 print STDERR "$cmd>>> .\n"
522 syswrite($cmd,".\015\012",3);
524 delete ${*$cmd}{'net_cmd_lastch'};
526 $cmd->response() == CMD_OK;