📄 cmd.pm
字号:
$arr;}sub datasend { my $cmd = shift; my $arr = @_ == 1 && ref($_[0]) ? $_[0] : \@_; my $line = join("", @$arr); # encode to individual utf8 bytes if # $line is a string (in internal UTF-8) utf8::encode($line) if is_utf8($line); return 0 unless defined(fileno($cmd)); my $last_ch = ${*$cmd}{'net_cmd_last_ch'}; $last_ch = ${*$cmd}{'net_cmd_last_ch'} = "\012" unless defined $last_ch; return 1 unless length $line; if ($cmd->debug) { foreach my $b (split(/\n/, $line)) { $cmd->debug_print(1, "$b\n"); } } $line =~ tr/\r\n/\015\012/ unless "\r" eq "\015"; my $first_ch = ''; if ($last_ch eq "\015") { $first_ch = "\012" if $line =~ s/^\012//; } elsif ($last_ch eq "\012") { $first_ch = "." if $line =~ /^\./; } $line =~ s/\015?\012(\.?)/\015\012$1$1/sg; substr($line, 0, 0) = $first_ch; ${*$cmd}{'net_cmd_last_ch'} = substr($line, -1, 1); my $len = length($line); my $offset = 0; my $win = ""; vec($win, fileno($cmd), 1) = 1; my $timeout = $cmd->timeout || undef; local $SIG{PIPE} = 'IGNORE' unless $^O eq 'MacOS'; while ($len) { my $wout; my $s = select(undef, $wout = $win, undef, $timeout); if ((defined $s and $s > 0) or -f $cmd) # -f for testing on win32 { my $w = syswrite($cmd, $line, $len, $offset); unless (defined($w)) { carp("$cmd: $!") if $cmd->debug; return undef; } $len -= $w; $offset += $w; } else { carp("$cmd: Timeout") if ($cmd->debug); return undef; } } 1;}sub rawdatasend { my $cmd = shift; my $arr = @_ == 1 && ref($_[0]) ? $_[0] : \@_; my $line = join("", @$arr); return 0 unless defined(fileno($cmd)); return 1 unless length($line); if ($cmd->debug) { my $b = "$cmd>>> "; print STDERR $b, join("\n$b", split(/\n/, $line)), "\n"; } my $len = length($line); my $offset = 0; my $win = ""; vec($win, fileno($cmd), 1) = 1; my $timeout = $cmd->timeout || undef; local $SIG{PIPE} = 'IGNORE' unless $^O eq 'MacOS'; while ($len) { my $wout; if (select(undef, $wout = $win, undef, $timeout) > 0) { my $w = syswrite($cmd, $line, $len, $offset); unless (defined($w)) { carp("$cmd: $!") if $cmd->debug; return undef; } $len -= $w; $offset += $w; } else { carp("$cmd: Timeout") if ($cmd->debug); return undef; } } 1;}sub dataend { my $cmd = shift; return 0 unless defined(fileno($cmd)); my $ch = ${*$cmd}{'net_cmd_last_ch'}; my $tosend; if (!defined $ch) { return 1; } elsif ($ch ne "\012") { $tosend = "\015\012"; } $tosend .= ".\015\012"; local $SIG{PIPE} = 'IGNORE' unless $^O eq 'MacOS'; $cmd->debug_print(1, ".\n") if ($cmd->debug); syswrite($cmd, $tosend, length $tosend); delete ${*$cmd}{'net_cmd_last_ch'}; $cmd->response() == CMD_OK;}# read and write to tied filehandlesub tied_fh { my $cmd = shift; ${*$cmd}{'net_cmd_readbuf'} = ''; my $fh = gensym(); tie *$fh, ref($cmd), $cmd; return $fh;}# tie to myselfsub 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; my ($len, $offset) = @_[1, 2]; return unless exists ${*$cmd}{'net_cmd_readbuf'}; my $done = 0; while (!$done and length(${*$cmd}{'net_cmd_readbuf'}) < $len) { ${*$cmd}{'net_cmd_readbuf'} .= $cmd->getline() or return; $done++ if ${*$cmd}{'net_cmd_readbuf'} =~ s/^\.\r?\n\Z//m; } $_[0] = ''; substr($_[0], $offset + 0) = substr(${*$cmd}{'net_cmd_readbuf'}, 0, $len); substr(${*$cmd}{'net_cmd_readbuf'}, 0, $len) = ''; delete ${*$cmd}{'net_cmd_readbuf'} if $done; 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'}; my $line = $cmd->getline; return if $line =~ /^\.\r?\n/; $line;}sub PRINT { my $cmd = shift; my ($buf, $len, $offset) = @_; $len ||= length($buf); $offset += 0; return unless $cmd->datasend(substr($buf, $offset, $len)); ${*$cmd}{'net_cmd_sending'}++; # flag that we should call dataend() return $len;}sub CLOSE { my $cmd = shift; my $r = exists(${*$cmd}{'net_cmd_sending'}) ? $cmd->dataend : 1; delete ${*$cmd}{'net_cmd_readbuf'}; delete ${*$cmd}{'net_cmd_sending'}; $r;}1;__END__=head1 NAMENet::Cmd - Network Command class (as used by FTP, SMTP etc)=head1 SYNOPSIS use Net::Cmd; @ISA = qw(Net::Cmd);=head1 DESCRIPTIONC<Net::Cmd> is a collection of methods that can be inherited by a sub classof C<IO::Handle>. These methods implement the functionality required for acommand based protocol, for example FTP and SMTP.=head1 USER METHODSThese 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 giventhen the current state is returned. Otherwise the state is changed to C<VALUE> and the previous state returned. Different packagesmay 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 defaultdebug level for the class.This method can also be called as a I<static> method to set/get the defaultdebug 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 pendingthen the value 0 is returned=item ok ()Returns non-zero if the last code value was greater than zero andless than 400. This holds true for most command servers. Serverswhere this does not hold may override this method.=item status ()Returns the most significant digit of the current status code. If a commandis pending then C<CMD_PENDING> is returned.=item datasend ( DATA )Send data to the remote server, converting LF to CRLF. Any line startingwith 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 thatthe data already sent ends with CRLF then sending '.CRLF' to end thetransmission. Once this data has been sent C<dataend> calls C<response> andreturns true if C<response> returns CMD_OK.=back=head1 CLASS METHODSThese 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> beingdata being sent to the server. Calls C<debug_text> before printing toSTDERR.=item debug_text ( TEXT )This method is called to print debugging information. TEXT isthe text being sent. The method should return the text to be printedThis is primarily meant for the use of modules such as FTP where passwordsare 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 witha space character and CRLF is appended, this string is then sent to thecommand 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 digitof the status code is returned. Upon failure, timeout etc., I<undef> isreturned.=item parse_response ( TEXT )This method is called by C<response> as a method with one argument. It shouldreturn an array of 2 values, the 3-digit status code and a flag which is truewhen 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 addsome 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 acommand, you may read from this filehandle using read() or <>. Thefilehandle will return EOF when the final dot is encountered.Similarly, you may write to the filehandle in order to send data tothe server after issuing a command that expects data to be written.See the Net::POP3 and Net::SMTP modules for examples of this.=back=head1 EXPORTSC<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 resultsof C<response> and C<status>. The sixth is C<CMD_PENDING>.=head1 AUTHORGraham Barr <gbarr@pobox.com>=head1 COPYRIGHTCopyright (c) 1995-2006 Graham Barr. All rights reserved.This program is free software; you can redistribute it and/or modifyit under the same terms as Perl itself.=cut
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -