libnet
view release on metacpan or search on metacpan
lib/Net/NNTP.pm view on Meta::CPAN
# Net::NNTP.pm
#
# Copyright (C) 1995-1997 Graham Barr. All rights reserved.
# Copyright (C) 2013-2016, 2020 Steve Hay. All rights reserved.
# This module is free software; you can redistribute it and/or modify it under
# the same terms as Perl itself, i.e. under the terms of either the GNU General
# Public License or the Artistic License, as specified in the F<LICENCE> file.
package Net::NNTP;
use 5.008001;
use strict;
use warnings;
use Carp;
use IO::Socket;
use Net::Cmd;
use Net::Config;
use Time::Local;
our $VERSION = "3.15";
# Code for detecting if we can use SSL
my $ssl_class = eval {
require IO::Socket::SSL;
# first version with default CA on most platforms
no warnings 'numeric';
IO::Socket::SSL->VERSION(2.007);
} && 'IO::Socket::SSL';
my $nossl_warn = !$ssl_class &&
'To use SSL please install IO::Socket::SSL with version>=2.007';
# Code for detecting if we can use IPv6
my $family_key = 'Domain';
my $inet6_class = eval {
require IO::Socket::IP;
no warnings 'numeric';
IO::Socket::IP->VERSION(0.25) || die;
$family_key = 'Family';
} && 'IO::Socket::IP' || eval {
require IO::Socket::INET6;
no warnings 'numeric';
IO::Socket::INET6->VERSION(2.62);
} && 'IO::Socket::INET6';
sub can_ssl { $ssl_class };
sub can_inet6 { $inet6_class };
our @ISA = ('Net::Cmd', $inet6_class || 'IO::Socket::INET');
sub new {
my $self = shift;
my $type = ref($self) || $self;
my ($host, %arg);
if (@_ % 2) {
$host = shift;
%arg = @_;
}
else {
%arg = @_;
$host = delete $arg{Host};
}
my $obj;
$host ||= $ENV{NNTPSERVER} || $ENV{NEWSHOST};
my $hosts = defined $host ? [$host] : $NetConfig{nntp_hosts};
@{$hosts} = qw(news)
unless @{$hosts};
my %connect = ( Proto => 'tcp');
if ($arg{SSL}) {
# SSL from start
die $nossl_warn if ! $ssl_class;
$arg{Port} ||= 563;
$connect{$_} = $arg{$_} for(grep { m{^SSL_} } keys %arg);
}
foreach my $o (qw(LocalAddr LocalPort Timeout)) {
$connect{$o} = $arg{$o} if exists $arg{$o};
}
$connect{$family_key} = $arg{Domain} || $arg{Family};
$connect{Timeout} = 120 unless defined $connect{Timeout};
$connect{PeerPort} = $arg{Port} || 'nntp(119)';
foreach my $h (@{$hosts}) {
$connect{PeerAddr} = $h;
$obj = $type->SUPER::new(%connect) or next;
${*$obj}{'net_nntp_host'} = $h;
${*$obj}{'net_nntp_arg'} = \%arg;
if ($arg{SSL}) {
Net::NNTP::_SSL->start_SSL($obj,%arg) or next;
}
}
return
unless defined $obj;
$obj->autoflush(1);
$obj->debug(exists $arg{Debug} ? $arg{Debug} : undef);
unless ($obj->response() == CMD_OK) {
$obj->close;
return;
}
my $c = $obj->code;
lib/Net/NNTP.pm view on Meta::CPAN
=item C<newgroups($since[, $distributions])>
C<$since> is a time value and C<$distributions> is either a distribution
pattern or a reference to a list of distribution patterns.
The result is the same as C<list>, but the
groups return will be limited to those created after C<$since> and, if
specified, in one of the distribution areas in C<$distributions>.
=item C<newnews($since[, $groups[, $distributions]])>
C<$since> is a time value. C<$groups> is either a group pattern or a reference
to a list of group patterns. C<$distributions> is either a distribution
pattern or a reference to a list of distribution patterns.
Returns a reference to a list which contains the message-ids of all news posted
after C<$since>, that are in a groups which matched C<$groups> and a
distribution which matches C<$distributions>.
=item C<next()>
Set the "current article pointer" to the next article in the current
newsgroup.
Returns the message-id of the article.
=item C<post([$message])>
Post a new article to the news server. If C<$message> is specified and posting
is allowed then the message will be sent.
If C<$message> is not specified then the message must be sent using the
C<datasend> and C<dataend> methods from L<Net::Cmd>
C<$message> can be either an array of lines or a reference to an array
and must be encoded by the caller to octets of whatever encoding is required,
e.g. by using the Encode module's C<encode()> function.
The message, either sent via C<datasend> or as the C<$message>
parameter, must be in the format as described by RFC822 and must
contain From:, Newsgroups: and Subject: headers.
=item C<postfh()>
Post a new article to the news server using a tied filehandle. If
posting is allowed, this method will return a tied filehandle that you
can print() the contents of the article to be posted. You must
explicitly close() the filehandle when you are finished posting the
article, and the return value from the close() call will indicate
whether the message was successfully posted.
=item C<slave()>
Tell the remote server that I am not a user client, but probably another
news server.
=item C<quit()>
Quit the remote server and close the socket connection.
=item C<can_inet6()>
Returns whether we can use IPv6.
=item C<can_ssl()>
Returns whether we can use SSL.
=back
=head2 Extension Methods
These methods use commands that are not part of the RFC977 documentation. Some
servers may not support all of them.
=over 4
=item C<newsgroups([$pattern])>
Returns a reference to a hash where the keys are all the group names which
match C<$pattern>, or all of the groups if no pattern is specified, and
each value contains the description text for the group.
=item C<distributions()>
Returns a reference to a hash where the keys are all the possible
distribution names and the values are the distribution descriptions.
=item C<distribution_patterns()>
Returns a reference to an array where each element, itself an array
reference, consists of the three fields of a line of the distrib.pats list
maintained by some NNTP servers, namely: a weight, a wildmat and a value
which the client may use to construct a Distribution header.
=item C<subscriptions()>
Returns a reference to a list which contains a list of groups which
are recommended for a new user to subscribe to.
=item C<overview_fmt()>
Returns a reference to an array which contain the names of the fields returned
by C<xover>.
=item C<active_times()>
Returns a reference to a hash where the keys are the group names and each
value is a reference to an array containing the time the groups was created
and an identifier, possibly an Email address, of the creator.
=item C<active([$pattern])>
Similar to C<list> but only active groups that match the pattern are returned.
C<$pattern> can be a group pattern.
=item C<xgtitle($pattern)>
Returns a reference to a hash where the keys are all the group names which
match C<$pattern> and each value is the description text for the group.
( run in 0.768 second using v1.01-cache-2.11-cpan-39bf76dae61 )