<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;"># 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&lt;LICENCE&gt; 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-&gt;VERSION(2.007);
} &amp;&amp; 'IO::Socket::SSL';

my $nossl_warn = !$ssl_class &amp;&amp;
  'To use SSL please install IO::Socket::SSL with version&gt;=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-&gt;VERSION(0.25) || die;
  $family_key = 'Family';
} &amp;&amp; 'IO::Socket::IP' || eval {
  require IO::Socket::INET6;
  no warnings 'numeric';
  IO::Socket::INET6-&gt;VERSION(2.62);
} &amp;&amp; '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 =&gt; '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-&gt;SUPER::new(%connect) or next;
    ${*$obj}{'net_nntp_host'} = $h;
    ${*$obj}{'net_nntp_arg'} = \%arg;
    if ($arg{SSL}) {
      Net::NNTP::_SSL-&gt;start_SSL($obj,%arg) or next;
    }
  }

  return
    unless defined $obj;

  $obj-&gt;autoflush(1);
  $obj-&gt;debug(exists $arg{Debug} ? $arg{Debug} : undef);

  unless ($obj-&gt;response() == CMD_OK) {
    $obj-&gt;close;
    return;
  }

  my $c = $obj-&gt;code;
  my @m = $obj-&gt;message;

  unless (exists $arg{Reader} &amp;&amp; $arg{Reader} == 0) {

    # if server is INN and we have transfer rights the we are currently
    # talking to innd not nnrpd
    if ($obj-&gt;reader) {

      # If reader succeeds the we need to consider this code to determine postok
      $c = $obj-&gt;code;
    }
    else {

      # I want to ignore this failure, so restore the previous status.
      $obj-&gt;set_status($c, \@m);
    }
  }

  ${*$obj}{'net_nntp_post'} = $c == 200 ? 1 : 0;

  $obj;
}


sub host {
  my $me = shift;
  ${*$me}{'net_nntp_host'};
}


sub debug_text {
  my $nntp  = shift;
  my $inout = shift;
  my $text  = shift;

  if ( (ref($nntp) and $nntp-&gt;code == 350 and $text =~ /^(\S+)/)
    || ($text =~ /^(authinfo\s+pass)/io))
  {
    $text = "$1 ....\n";
  }

  $text;
}


sub postok {
  @_ == 1 or croak 'usage: $nntp-&gt;postok()';
  my $nntp = shift;
  ${*$nntp}{'net_nntp_post'} || 0;
}


sub starttls {
  my $self = shift;
  $ssl_class or die $nossl_warn;
  $self-&gt;_STARTTLS or return;
  Net::NNTP::_SSL-&gt;start_SSL($self,
    %{ ${*$self}{'net_nntp_arg'} }, # (ssl) args given in new
    @_   # more (ssl) args
  ) or return;
  return 1;
}


sub article {
  @_ &gt;= 1 &amp;&amp; @_ &lt;= 3 or croak 'usage: $nntp-&gt;article([{$msgid|$msgnum}[, $fh]])';
  my $nntp = shift;
  my @fh;

  @fh = (pop) if @_ == 2 || (@_ &amp;&amp; (ref($_[0]) || ref(\$_[0]) eq 'GLOB'));

  $nntp-&gt;_ARTICLE(@_)
    ? $nntp-&gt;read_until_dot(@fh)
    : undef;
}


sub articlefh {
  @_ &gt;= 1 &amp;&amp; @_ &lt;= 2 or croak 'usage: $nntp-&gt;articlefh([{$msgid|$msgnum}])';
  my $nntp = shift;

  return unless $nntp-&gt;_ARTICLE(@_);
  return $nntp-&gt;tied_fh;
}


sub authinfo {
  @_ == 3 or croak 'usage: $nntp-&gt;authinfo($user, $pass)';
  my ($nntp, $user, $pass) = @_;

  $nntp-&gt;_AUTHINFO("USER",      $user) == CMD_MORE
    &amp;&amp; $nntp-&gt;_AUTHINFO("PASS", $pass) == CMD_OK;
}


sub authinfo_simple {
  @_ == 3 or croak 'usage: $nntp-&gt;authinfo_simple($user, $pass)';
  my ($nntp, $user, $pass) = @_;

  $nntp-&gt;_AUTHINFO('SIMPLE') == CMD_MORE
    &amp;&amp; $nntp-&gt;command($user, $pass)-&gt;response == CMD_OK;
}


sub body {
  @_ &gt;= 1 &amp;&amp; @_ &lt;= 3 or croak 'usage: $nntp-&gt;body([{$msgid|$msgnum}[, $fh]])';
  my $nntp = shift;
  my @fh;

  @fh = (pop) if @_ == 2 || (@_ &amp;&amp; ref($_[0]) || ref(\$_[0]) eq 'GLOB');

  $nntp-&gt;_BODY(@_)
    ? $nntp-&gt;read_until_dot(@fh)
    : undef;
}


sub bodyfh {
  @_ &gt;= 1 &amp;&amp; @_ &lt;= 2 or croak 'usage: $nntp-&gt;bodyfh([{$msgid|$msgnum}])';
  my $nntp = shift;
  return unless $nntp-&gt;_BODY(@_);
  return $nntp-&gt;tied_fh;
}


sub head {
  @_ &gt;= 1 &amp;&amp; @_ &lt;= 3 or croak 'usage: $nntp-&gt;head([{$msgid|$msgnum}[, $fh]])';
  my $nntp = shift;
  my @fh;

  @fh = (pop) if @_ == 2 || (@_ &amp;&amp; ref($_[0]) || ref(\$_[0]) eq 'GLOB');

  $nntp-&gt;_HEAD(@_)
    ? $nntp-&gt;read_until_dot(@fh)
    : undef;
}


sub headfh {
  @_ &gt;= 1 &amp;&amp; @_ &lt;= 2 or croak 'usage: $nntp-&gt;headfh([{$msgid|$msgnum}])';
  my $nntp = shift;
  return unless $nntp-&gt;_HEAD(@_);
  return $nntp-&gt;tied_fh;
}


sub nntpstat {
  @_ == 1 || @_ == 2 or croak 'usage: $nntp-&gt;nntpstat([{$msgid|$msgnum}])';
  my $nntp = shift;

  $nntp-&gt;_STAT(@_) &amp;&amp; $nntp-&gt;message =~ /(&lt;[^&gt;]+&gt;)/o
    ? $1
    : undef;
}


sub group {
  @_ == 1 || @_ == 2 or croak 'usage: $nntp-&gt;group([$group])';
  my $nntp = shift;
  my $grp  = ${*$nntp}{'net_nntp_group'};

  return $grp
    unless (@_ || wantarray);

  my $newgrp = shift;

  $newgrp = (defined($grp) and length($grp)) ? $grp : ""
    unless defined($newgrp) and length($newgrp);

  return 
    unless $nntp-&gt;_GROUP($newgrp) and $nntp-&gt;message =~ /(\d+)\s+(\d+)\s+(\d+)\s+(\S+)/;

  my ($count, $first, $last, $group) = ($1, $2, $3, $4);

  # group may be replied as '(current group)'
  $group = ${*$nntp}{'net_nntp_group'}
    if $group =~ /\(/;

  ${*$nntp}{'net_nntp_group'} = $group;

  wantarray
    ? ($count, $first, $last, $group)
    : $group;
}


sub help {
  @_ == 1 or croak 'usage: $nntp-&gt;help()';
  my $nntp = shift;

  $nntp-&gt;_HELP
    ? $nntp-&gt;read_until_dot
    : undef;
}


sub ihave {
  @_ &gt;= 2 or croak 'usage: $nntp-&gt;ihave($msgid[, $message])';
  my $nntp  = shift;
  my $msgid = shift;

  $nntp-&gt;_IHAVE($msgid) &amp;&amp; $nntp-&gt;datasend(@_)
    ? @_ == 0 || $nntp-&gt;dataend
    : undef;
}


sub last {
  @_ == 1 or croak 'usage: $nntp-&gt;last()';
  my $nntp = shift;

  $nntp-&gt;_LAST &amp;&amp; $nntp-&gt;message =~ /(&lt;[^&gt;]+&gt;)/o
    ? $1
    : undef;
}


sub list {
  @_ == 1 or croak 'usage: $nntp-&gt;list()';
  my $nntp = shift;

  $nntp-&gt;_LIST
    ? $nntp-&gt;_grouplist
    : undef;
}


sub newgroups {
  @_ &gt;= 2 or croak 'usage: $nntp-&gt;newgroups($since[, $distributions])';
  my $nntp = shift;
  my $since = _timestr(shift);
  my $distributions = shift || "";

  $distributions = join(",", @{$distributions})
    if ref($distributions);

  $nntp-&gt;_NEWGROUPS($since, $distributions)
    ? $nntp-&gt;_grouplist
    : undef;
}


sub newnews {
  @_ &gt;= 2 &amp;&amp; @_ &lt;= 4
    or croak 'usage: $nntp-&gt;newnews($since[, $groups[, $distributions]])';
  my $nntp = shift;
  my $since = _timestr(shift);
  my $groups = @_ ? shift : $nntp-&gt;group;
  my $distributions = shift || "";

  $groups ||= "*";
  $groups = join(",", @{$groups})
    if ref($groups);

  $distributions = join(",", @{$distributions})
    if ref($distributions);

  $nntp-&gt;_NEWNEWS($groups, $since, $distributions)
    ? $nntp-&gt;_articlelist
    : undef;
}


sub next {
  @_ == 1 or croak 'usage: $nntp-&gt;next()';
  my $nntp = shift;

  $nntp-&gt;_NEXT &amp;&amp; $nntp-&gt;message =~ /(&lt;[^&gt;]+&gt;)/o
    ? $1
    : undef;
}


sub post {
  @_ &gt;= 1 or croak 'usage: $nntp-&gt;post([$message])';
  my $nntp = shift;

  $nntp-&gt;_POST() &amp;&amp; $nntp-&gt;datasend(@_)
    ? @_ == 0 || $nntp-&gt;dataend
    : undef;
}


sub postfh {
  my $nntp = shift;
  return unless $nntp-&gt;_POST();
  return $nntp-&gt;tied_fh;
}


sub quit {
  @_ == 1 or croak 'usage: $nntp-&gt;quit()';
  my $nntp = shift;

  $nntp-&gt;_QUIT;
  $nntp-&gt;close;
}


sub slave {
  @_ == 1 or croak 'usage: $nntp-&gt;slave()';
  my $nntp = shift;

  $nntp-&gt;_SLAVE;
}

##
## The following methods are not implemented by all servers
##


sub active {
  @_ == 1 || @_ == 2 or croak 'usage: $nntp-&gt;active([$pattern])';
  my $nntp = shift;

  $nntp-&gt;_LIST('ACTIVE', @_)
    ? $nntp-&gt;_grouplist
    : undef;
}


sub active_times {
  @_ == 1 or croak 'usage: $nntp-&gt;active_times()';
  my $nntp = shift;

  $nntp-&gt;_LIST('ACTIVE.TIMES')
    ? $nntp-&gt;_grouplist
    : undef;
}


sub distributions {
  @_ == 1 or croak 'usage: $nntp-&gt;distributions()';
  my $nntp = shift;

  $nntp-&gt;_LIST('DISTRIBUTIONS')
    ? $nntp-&gt;_description
    : undef;
}


sub distribution_patterns {
  @_ == 1 or croak 'usage: $nntp-&gt;distribution_patterns()';
  my $nntp = shift;

  my $arr;
  local $_;

  ## no critic (ControlStructures::ProhibitMutatingListFunctions)
  $nntp-&gt;_LIST('DISTRIB.PATS')
    &amp;&amp; ($arr = $nntp-&gt;read_until_dot)
    ? [grep { /^\d/ &amp;&amp; (chomp, $_ = [split /:/]) } @$arr]
    : undef;
}


sub newsgroups {
  @_ == 1 || @_ == 2 or croak 'usage: $nntp-&gt;newsgroups([$pattern])';
  my $nntp = shift;

  $nntp-&gt;_LIST('NEWSGROUPS', @_)
    ? $nntp-&gt;_description
    : undef;
}


sub overview_fmt {
  @_ == 1 or croak 'usage: $nntp-&gt;overview_fmt()';
  my $nntp = shift;

  $nntp-&gt;_LIST('OVERVIEW.FMT')
    ? $nntp-&gt;_articlelist
    : undef;
}


sub subscriptions {
  @_ == 1 or croak 'usage: $nntp-&gt;subscriptions()';
  my $nntp = shift;

  $nntp-&gt;_LIST('SUBSCRIPTIONS')
    ? $nntp-&gt;_articlelist
    : undef;
}


sub listgroup {
  @_ == 1 || @_ == 2 or croak 'usage: $nntp-&gt;listgroup([$group])';
  my $nntp = shift;

  $nntp-&gt;_LISTGROUP(@_)
    ? $nntp-&gt;_articlelist
    : undef;
}


sub reader {
  @_ == 1 or croak 'usage: $nntp-&gt;reader()';
  my $nntp = shift;

  $nntp-&gt;_MODE('READER');
}


sub xgtitle {
  @_ == 1 || @_ == 2 or croak 'usage: $nntp-&gt;xgtitle([$pattern])';
  my $nntp = shift;

  $nntp-&gt;_XGTITLE(@_)
    ? $nntp-&gt;_description
    : undef;
}


sub xhdr {
  @_ &gt;= 2 &amp;&amp; @_ &lt;= 4 or croak 'usage: $nntp-&gt;xhdr($header[, $message_spec])';
  my $nntp = shift;
  my $header = shift;
  my $arg = _msg_arg(@_);

  $nntp-&gt;_XHDR($header, $arg)
    ? $nntp-&gt;_description
    : undef;
}


sub xover {
  @_ == 2 || @_ == 3 or croak 'usage: $nntp-&gt;xover($message_spec)';
  my $nntp = shift;
  my $arg  = _msg_arg(@_);

  $nntp-&gt;_XOVER($arg)
    ? $nntp-&gt;_fieldlist
    : undef;
}


sub xpat {
  @_ == 4 || @_ == 5 or croak 'usage: $nntp-&gt;xpat($header, $pattern, $message_spec )';
  my $nntp = shift;
  my $header = shift;
  my $pattern = shift;
  my $arg = _msg_arg(@_);

  $pattern = join(" ", @$pattern)
    if ref($pattern);

  $nntp-&gt;_XPAT($header, $arg, $pattern)
    ? $nntp-&gt;_description
    : undef;
}


sub xpath {
  @_ == 2 or croak 'usage: $nntp-&gt;xpath($message_id)';
  my ($nntp, $message_id) = @_;

  return
    unless $nntp-&gt;_XPATH($message_id);

  my $m;
  ($m = $nntp-&gt;message) =~ s/^\d+\s+//o;
  my @p = split /\s+/, $m;

  wantarray ? @p : $p[0];
}


sub xrover {
  @_ == 2 || @_ == 3 or croak 'usage: $nntp-&gt;xrover($message_spec)';
  my $nntp = shift;
  my $arg  = _msg_arg(@_);

  $nntp-&gt;_XROVER($arg)
    ? $nntp-&gt;_description
    : undef;
}


sub date {
  @_ == 1 or croak 'usage: $nntp-&gt;date()';
  my $nntp = shift;

  $nntp-&gt;_DATE
    &amp;&amp; $nntp-&gt;message =~ /(\d{4})(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)/
    ? timegm($6, $5, $4, $3, $2 - 1, $1)
    : undef;
}


##
## Private subroutines
##


sub _msg_arg {
  my $spec = shift;
  my $arg  = "";

  if (@_) {
    carp "Depriciated passing of two message numbers, " . "pass a reference"
      if $^W;
    $spec = [$spec, $_[0]];
  }

  if (defined $spec) {
    if (ref($spec)) {
      $arg = $spec-&gt;[0];
      if (defined $spec-&gt;[1]) {
        $arg .= "-"
          if $spec-&gt;[1] != $spec-&gt;[0];
        $arg .= $spec-&gt;[1]
          if $spec-&gt;[1] &gt; $spec-&gt;[0];
      }
    }
    else {
      $arg = $spec;
    }
  }

  $arg;
}


sub _timestr {
  my $time = shift;
  my @g    = reverse((gmtime($time))[0 .. 5]);
  $g[1] += 1;
  $g[0] %= 100;
  sprintf "%02d%02d%02d %02d%02d%02d GMT", @g;
}


sub _grouplist {
  my $nntp = shift;
  my $arr  = $nntp-&gt;read_until_dot
    or return;

  my $hash = {};

  foreach my $ln (@$arr) {
    my @a = split(/[\s\n]+/, $ln);
    $hash-&gt;{$a[0]} = [@a[1, 2, 3]];
  }

  $hash;
}


sub _fieldlist {
  my $nntp = shift;
  my $arr  = $nntp-&gt;read_until_dot
    or return;

  my $hash = {};

  foreach my $ln (@$arr) {
    my @a = split(/[\t\n]/, $ln);
    my $m = shift @a;
    $hash-&gt;{$m} = [@a];
  }

  $hash;
}


sub _articlelist {
  my $nntp = shift;
  my $arr  = $nntp-&gt;read_until_dot;

  chomp(@$arr)
    if $arr;

  $arr;
}


sub _description {
  my $nntp = shift;
  my $arr  = $nntp-&gt;read_until_dot
    or return;

  my $hash = {};

  foreach my $ln (@$arr) {
    chomp($ln);

    $hash-&gt;{$1} = $ln
      if $ln =~ s/^\s*(\S+)\s*//o;
  }

  $hash;

}

##
## The commands
##


sub _ARTICLE  { shift-&gt;command('ARTICLE',  @_)-&gt;response == CMD_OK }
sub _AUTHINFO { shift-&gt;command('AUTHINFO', @_)-&gt;response }
sub _BODY     { shift-&gt;command('BODY',     @_)-&gt;response == CMD_OK }
sub _DATE      { shift-&gt;command('DATE')-&gt;response == CMD_INFO }
sub _GROUP     { shift-&gt;command('GROUP', @_)-&gt;response == CMD_OK }
sub _HEAD      { shift-&gt;command('HEAD', @_)-&gt;response == CMD_OK }
sub _HELP      { shift-&gt;command('HELP', @_)-&gt;response == CMD_INFO }
sub _IHAVE     { shift-&gt;command('IHAVE', @_)-&gt;response == CMD_MORE }
sub _LAST      { shift-&gt;command('LAST')-&gt;response == CMD_OK }
sub _LIST      { shift-&gt;command('LIST', @_)-&gt;response == CMD_OK }
sub _LISTGROUP { shift-&gt;command('LISTGROUP', @_)-&gt;response == CMD_OK }
sub _NEWGROUPS { shift-&gt;command('NEWGROUPS', @_)-&gt;response == CMD_OK }
sub _NEWNEWS   { shift-&gt;command('NEWNEWS', @_)-&gt;response == CMD_OK }
sub _NEXT      { shift-&gt;command('NEXT')-&gt;response == CMD_OK }
sub _POST      { shift-&gt;command('POST', @_)-&gt;response == CMD_MORE }
sub _QUIT      { shift-&gt;command('QUIT', @_)-&gt;response == CMD_OK }
sub _SLAVE     { shift-&gt;command('SLAVE', @_)-&gt;response == CMD_OK }
sub _STARTTLS  { shift-&gt;command("STARTTLS")-&gt;response() == CMD_MORE }
sub _STAT      { shift-&gt;command('STAT', @_)-&gt;response == CMD_OK }
sub _MODE      { shift-&gt;command('MODE', @_)-&gt;response == CMD_OK }
sub _XGTITLE   { shift-&gt;command('XGTITLE', @_)-&gt;response == CMD_OK }
sub _XHDR      { shift-&gt;command('XHDR', @_)-&gt;response == CMD_OK }
sub _XPAT      { shift-&gt;command('XPAT', @_)-&gt;response == CMD_OK }
sub _XPATH     { shift-&gt;command('XPATH', @_)-&gt;response == CMD_OK }
sub _XOVER     { shift-&gt;command('XOVER', @_)-&gt;response == CMD_OK }
sub _XROVER    { shift-&gt;command('XROVER', @_)-&gt;response == CMD_OK }
sub _XTHREAD   { shift-&gt;unsupported }
sub _XSEARCH   { shift-&gt;unsupported }
sub _XINDEX    { shift-&gt;unsupported }

##
## IO/perl methods
##


sub DESTROY {
  my $nntp = shift;
  defined(fileno($nntp)) &amp;&amp; $nntp-&gt;quit;
}

{
  package Net::NNTP::_SSL;
  our @ISA = ( $ssl_class ? ($ssl_class):(), 'Net::NNTP' );
  sub starttls { die "NNTP connection is already in SSL mode" }
  sub start_SSL {
    my ($class,$nntp,%arg) = @_;
    delete @arg{ grep { !m{^SSL_} } keys %arg };
    ( $arg{SSL_verifycn_name} ||= $nntp-&gt;host )
        =~s{(?&lt;!:):[\w()]+$}{}; # strip port
    $arg{SSL_hostname} = $arg{SSL_verifycn_name}
        if ! defined $arg{SSL_hostname} &amp;&amp; $class-&gt;can_client_sni;
    my $ok = $class-&gt;SUPER::start_SSL($nntp,
      SSL_verifycn_scheme =&gt; 'nntp',
      %arg
    );
    $@ = $ssl_class-&gt;errstr if !$ok;
    return $ok;
  }
}




1;

__END__

=head1 NAME

Net::NNTP - NNTP Client class

=head1 SYNOPSIS

    use Net::NNTP;

    $nntp = Net::NNTP-&gt;new("some.host.name");
    $nntp-&gt;quit;

    # start with SSL, e.g. nntps
    $nntp = Net::NNTP-&gt;new("some.host.name", SSL =&gt; 1);

    # start with plain and upgrade to SSL
    $nntp = Net::NNTP-&gt;new("some.host.name");
    $nntp-&gt;starttls;


=head1 DESCRIPTION

C&lt;Net::NNTP&gt; is a class implementing a simple NNTP client in Perl as described
in RFC977 and RFC4642.
With L&lt;IO::Socket::SSL&gt; installed it also provides support for implicit and
explicit TLS encryption, i.e. NNTPS or NNTP+STARTTLS.

The Net::NNTP class is a subclass of Net::Cmd and (depending on avaibility) of
IO::Socket::IP, IO::Socket::INET6 or IO::Socket::INET.

=head2 Class Methods

=over 4

=item C&lt;new([$host][, %options])&gt;

This is the constructor for a new Net::NNTP object. C&lt;$host&gt; is the
name of the remote host to which a NNTP connection is required. If not
given then it may be passed as the C&lt;Host&gt; option described below. If no host is passed
then two environment variables are checked, first C&lt;NNTPSERVER&gt; then
C&lt;NEWSHOST&gt;, then C&lt;Net::Config&gt; is checked, and if a host is not found
then C&lt;news&gt; is used.

C&lt;%options&gt; are passed in a hash like fashion, using key and value pairs.
Possible options are:

B&lt;Host&gt; - NNTP host to connect to. It may be a single scalar, as defined for
the C&lt;PeerAddr&gt; option in L&lt;IO::Socket::INET&gt;, or a reference to
an array with hosts to try in turn. The L&lt;/host&gt; method will return the value
which was used to connect to the host.

B&lt;Port&gt; - port to connect to.
Default - 119 for plain NNTP and 563 for immediate SSL (nntps).

B&lt;SSL&gt; - If the connection should be done from start with SSL, contrary to later
upgrade with C&lt;starttls&gt;.
You can use SSL arguments as documented in L&lt;IO::Socket::SSL&gt;, but it will
usually use the right arguments already.

B&lt;Timeout&gt; - Maximum time, in seconds, to wait for a response from the
NNTP server, a value of zero will cause all IO operations to block.
(default: 120)

B&lt;Debug&gt; - Enable the printing of debugging information to STDERR

B&lt;Reader&gt; - If the remote server is INN then initially the connection
will be to innd, by default C&lt;Net::NNTP&gt; will issue a C&lt;MODE READER&gt; command
so that the remote server becomes nnrpd. If the C&lt;Reader&gt; option is given
with a value of zero, then this command will not be sent and the
connection will be left talking to innd.

B&lt;LocalAddr&gt; and B&lt;LocalPort&gt; - These parameters are passed directly
to IO::Socket to allow binding the socket to a specific local address and port.

B&lt;Domain&gt; - This parameter is passed directly to IO::Socket and makes it
possible to enforce IPv4 connections even if L&lt;IO::Socket::IP&gt; is used as super
class. Alternatively B&lt;Family&gt; can be used.

=back

=head2 Object Methods

Unless otherwise stated all methods return either a I&lt;true&gt; or I&lt;false&gt;
value, with I&lt;true&gt; meaning that the operation was a success. When a method
states that it returns a value, failure will be returned as I&lt;undef&gt; or an
empty list.

C&lt;Net::NNTP&gt; inherits from C&lt;Net::Cmd&gt; so methods defined in C&lt;Net::Cmd&gt; may
be used to send commands to the remote NNTP server in addition to the methods
documented here.

=over 4

=item C&lt;host()&gt;

Returns the value used by the constructor, and passed to IO::Socket::INET,
to connect to the host.

=item C&lt;starttls()&gt;

Upgrade existing plain connection to SSL.
Any arguments necessary for SSL must be given in C&lt;new&gt; already.

=item C&lt;article([{$msgid|$msgnum}[, $fh]])&gt;

Retrieve the header, a blank line, then the body (text) of the
specified article. 

If C&lt;$fh&gt; is specified then it is expected to be a valid filehandle
and the result will be printed to it, on success a true value will be
returned. If C&lt;$fh&gt; is not specified then the return value, on success,
will be a reference to an array containing the article requested, each
entry in the array will contain one line of the article.

If no arguments are passed then the current article in the currently
selected newsgroup is fetched.

C&lt;$msgnum&gt; is a numeric id of an article in the current newsgroup, and
will change the current article pointer.  C&lt;$msgid&gt; is the message id of
an article as shown in that article's header.  It is anticipated that the
client will obtain the C&lt;$msgid&gt; from a list provided by the C&lt;newnews&gt;
command, from references contained within another article, or from the
message-id provided in the response to some other commands.

If there is an error then C&lt;undef&gt; will be returned.

=item C&lt;body([{$msgid|$msgnum}[, [$fh]])&gt;

Like C&lt;article&gt; but only fetches the body of the article.

=item C&lt;head([{$msgid|$msgnum}[, [$fh]])&gt;

Like C&lt;article&gt; but only fetches the headers for the article.

=item C&lt;articlefh([{$msgid|$msgnum}])&gt;

=item C&lt;bodyfh([{$msgid|$msgnum}])&gt;

=item C&lt;headfh([{$msgid|$msgnum}])&gt;

These are similar to article(), body() and head(), but rather than
returning the requested data directly, they return a tied filehandle
from which to read the article.

=item C&lt;nntpstat([{$msgid|$msgnum}])&gt;

The C&lt;nntpstat&gt; command is similar to the C&lt;article&gt; command except that no
text is returned.  When selecting by message number within a group,
the C&lt;nntpstat&gt; command serves to set the "current article pointer" without
sending text.

Using the C&lt;nntpstat&gt; command to
select by message-id is valid but of questionable value, since a
selection by message-id does B&lt;not&gt; alter the "current article pointer".

Returns the message-id of the "current article".

=item C&lt;group([$group])&gt;

Set and/or get the current group. If C&lt;$group&gt; is not given then information
is returned on the current group.

In a scalar context it returns the group name.

In an array context the return value is a list containing, the number
of articles in the group, the number of the first article, the number
of the last article and the group name.

=item C&lt;help()&gt;

Request help text (a short summary of commands that are understood by this
implementation) from the server. Returns the text or undef upon failure.

=item C&lt;ihave($msgid[, $message])&gt;

The C&lt;ihave&gt; command informs the server that the client has an article
whose id is C&lt;$msgid&gt;.  If the server desires a copy of that
article and C&lt;$message&gt; has been given then it will be sent.

Returns I&lt;true&gt; if the server desires the article and C&lt;$message&gt; was
successfully sent, if specified.

If C&lt;$message&gt; is not specified then the message must be sent using the
C&lt;datasend&gt; and C&lt;dataend&gt; methods from L&lt;Net::Cmd&gt;

C&lt;$message&gt; 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&lt;encode()&gt; function.

=item C&lt;last()&gt;

Set the "current article pointer" to the previous article in the current
newsgroup.

Returns the message-id of the article.

=item C&lt;date()&gt;

Returns the date on the remote server. This date will be in a UNIX time
format (seconds since 1970)

=item C&lt;postok()&gt;

C&lt;postok&gt; will return I&lt;true&gt; if the servers initial response indicated
that it will allow posting.

=item C&lt;authinfo($user, $pass)&gt;

Authenticates to the server (using the original AUTHINFO USER / AUTHINFO PASS
form, defined in RFC2980) using the supplied username and password.  Please
note that the password is sent in clear text to the server.  This command
should not be used with valuable passwords unless the connection to the server
is somehow protected.

=item C&lt;authinfo_simple($user, $pass)&gt;

Authenticates to the server (using the proposed NNTP V2 AUTHINFO SIMPLE form,
defined and deprecated in RFC2980) using the supplied username and password.
As with L&lt;/authinfo&gt; the password is sent in clear text.

=item C&lt;list()&gt;

Obtain information about all the active newsgroups. The results is a reference
to a hash where the key is a group name and each value is a reference to an
array. The elements in this array are:- the last article number in the group,
the first article number in the group and any information flags about the group.

=item C&lt;newgroups($since[, $distributions])&gt;

C&lt;$since&gt; is a time value and C&lt;$distributions&gt; is either a distribution
pattern or a reference to a list of distribution patterns.
The result is the same as C&lt;list&gt;, but the
groups return will be limited to those created after C&lt;$since&gt; and, if
specified, in one of the distribution areas in C&lt;$distributions&gt;. 

=item C&lt;newnews($since[, $groups[, $distributions]])&gt;

C&lt;$since&gt; is a time value. C&lt;$groups&gt; is either a group pattern or a reference
to a list of group patterns. C&lt;$distributions&gt; 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&lt;$since&gt;, that are in a groups which matched C&lt;$groups&gt; and a
distribution which matches C&lt;$distributions&gt;.

=item C&lt;next()&gt;

Set the "current article pointer" to the next article in the current
newsgroup.

Returns the message-id of the article.

=item C&lt;post([$message])&gt;

Post a new article to the news server. If C&lt;$message&gt; is specified and posting
is allowed then the message will be sent.

If C&lt;$message&gt; is not specified then the message must be sent using the
C&lt;datasend&gt; and C&lt;dataend&gt; methods from L&lt;Net::Cmd&gt;

C&lt;$message&gt; 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&lt;encode()&gt; function.

The message, either sent via C&lt;datasend&gt; or as the C&lt;$message&gt;
parameter, must be in the format as described by RFC822 and must
contain From:, Newsgroups: and Subject: headers.

=item C&lt;postfh()&gt;

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&lt;slave()&gt;

Tell the remote server that I am not a user client, but probably another
news server.

=item C&lt;quit()&gt;

Quit the remote server and close the socket connection.

=item C&lt;can_inet6()&gt;

Returns whether we can use IPv6.

=item C&lt;can_ssl()&gt;

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&lt;newsgroups([$pattern])&gt;

Returns a reference to a hash where the keys are all the group names which
match C&lt;$pattern&gt;, or all of the groups if no pattern is specified, and
each value contains the description text for the group.

=item C&lt;distributions()&gt;

Returns a reference to a hash where the keys are all the possible
distribution names and the values are the distribution descriptions.

=item C&lt;distribution_patterns()&gt;

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&lt;subscriptions()&gt;

Returns a reference to a list which contains a list of groups which
are recommended for a new user to subscribe to.

=item C&lt;overview_fmt()&gt;

Returns a reference to an array which contain the names of the fields returned
by C&lt;xover&gt;.

=item C&lt;active_times()&gt;

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&lt;active([$pattern])&gt;

Similar to C&lt;list&gt; but only active groups that match the pattern are returned.
C&lt;$pattern&gt; can be a group pattern.

=item C&lt;xgtitle($pattern)&gt;

Returns a reference to a hash where the keys are all the group names which
match C&lt;$pattern&gt; and each value is the description text for the group.

=item C&lt;xhdr($header, $message_spec)&gt;

Obtain the header field C&lt;$header&gt; for all the messages specified. 

The return value will be a reference
to a hash where the keys are the message numbers and each value contains
the text of the requested header for that message.

=item C&lt;xover($message_spec)&gt;

The return value will be a reference
to a hash where the keys are the message numbers and each value contains
a reference to an array which contains the overview fields for that
message.

The names of the fields can be obtained by calling C&lt;overview_fmt&gt;.

=item C&lt;xpath($message_id)&gt;

Returns the path name to the file on the server which contains the specified
message.

=item C&lt;xpat($header, $pattern, $message_spec)&gt;

The result is the same as C&lt;xhdr&gt; except the is will be restricted to
headers where the text of the header matches C&lt;$pattern&gt;

=item C&lt;xrover($message_spec)&gt;

The XROVER command returns reference information for the article(s)
specified.

Returns a reference to a HASH where the keys are the message numbers and the
values are the References: lines from the articles

=item C&lt;listgroup([$group])&gt;

Returns a reference to a list of all the active messages in C&lt;$group&gt;, or
the current group if C&lt;$group&gt; is not specified.

=item C&lt;reader()&gt;

Tell the server that you are a reader and not another server.

This is required by some servers. For example if you are connecting to
an INN server and you have transfer permission your connection will
be connected to the transfer daemon, not the NNTP daemon. Issuing
this command will cause the transfer daemon to hand over control
to the NNTP daemon.

Some servers do not understand this command, but issuing it and ignoring
the response is harmless.

=back

=head2 Unsupported

The following NNTP command are unsupported by the package, and there are
no plans to do so.

    AUTHINFO GENERIC
    XTHREAD
    XSEARCH
    XINDEX

=head2 Definitions

=over 4

=item $message_spec

C&lt;$message_spec&gt; is either a single message-id, a single message number, or
a reference to a list of two message numbers.

If C&lt;$message_spec&gt; is a reference to a list of two message numbers and the
second number in a range is less than or equal to the first then the range
represents all messages in the group after the first message number.

B&lt;NOTE&gt; For compatibility reasons only with earlier versions of Net::NNTP
a message spec can be passed as a list of two numbers, this is deprecated
and a reference to the list should now be passed

=item $pattern

The C&lt;NNTP&gt; protocol uses the C&lt;WILDMAT&gt; format for patterns.
The WILDMAT format was first developed by Rich Salz based on
the format used in the UNIX "find" command to articulate
file names. It was developed to provide a uniform mechanism
for matching patterns in the same manner that the UNIX shell
matches filenames.

Patterns are implicitly anchored at the
beginning and end of each string when testing for a match.

There are five pattern matching operations other than a strict
one-to-one match between the pattern and the source to be
checked for a match.

The first is an asterisk C&lt;*&gt; to match any sequence of zero or more
characters.

The second is a question mark C&lt;?&gt; to match any single character. The
third specifies a specific set of characters.

The set is specified as a list of characters, or as a range of characters
where the beginning and end of the range are separated by a minus (or dash)
character, or as any combination of lists and ranges. The dash can
also be included in the set as a character it if is the beginning
or end of the set. This set is enclosed in square brackets. The
close square bracket C&lt;]&gt; may be used in a set if it is the first
character in the set.

The fourth operation is the same as the
logical not of the third operation and is specified the same
way as the third with the addition of a caret character C&lt;^&gt; at
the beginning of the test string just inside the open square
bracket.

The final operation uses the backslash character to
invalidate the special meaning of an open square bracket C&lt;[&gt;,
the asterisk, backslash or the question mark. Two backslashes in
sequence will result in the evaluation of the backslash as a
character with no special meaning.

=over 4

=item Examples

=item C&lt;[^]-]&gt;

matches any single character other than a close square
bracket or a minus sign/dash.

=item C&lt;*bdc&gt;

matches any string that ends with the string "bdc"
including the string "bdc" (without quotes).

=item C&lt;[0-9a-zA-Z]&gt;

matches any single printable alphanumeric ASCII character.

=item C&lt;a??d&gt;

matches any four character string which begins
with a and ends with d.

=back

=back

=head1 EXPORTS

I&lt;None&gt;.

=head1 KNOWN BUGS

See L&lt;https://rt.cpan.org/Dist/Display.html?Status=Active&amp;Queue=libnet&gt;.

=head1 SEE ALSO

L&lt;Net::Cmd&gt;,
L&lt;IO::Socket::SSL&gt;.

=head1 AUTHOR

Graham Barr E&lt;lt&gt;L&lt;gbarr@pobox.com|mailto:gbarr@pobox.com&gt;E&lt;gt&gt;.

Steve Hay E&lt;lt&gt;L&lt;shay@cpan.org|mailto:shay@cpan.org&gt;E&lt;gt&gt; is now maintaining
libnet as of version 1.22_02.

=head1 COPYRIGHT

Copyright (C) 1995-1997 Graham Barr.  All rights reserved.

Copyright (C) 2013-2016, 2020 Steve Hay.  All rights reserved.

=head1 LICENCE

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&lt;LICENCE&gt; file.

=head1 VERSION

Version 3.15

=head1 DATE

20 March 2023

=head1 HISTORY

See the F&lt;Changes&gt; file.

=cut
</pre></body></html>