#!/usr/bin/perl
-# By Simon Tatham
+# Originally by Simon Tatham
+# Modified by Richard Kettlewell, Colin Watson, Ian Jackson
-require 5.002;
-use Socket;
-use FileHandle;
+use ChiarkNNTP;
-$verbose=1, shift @ARGV if $ARGV[0] eq "-v";
+our $verbose;
+($verbose='STDERR', shift @ARGV) if $ARGV[0] eq "-v";
-die "usage: nntpid messageid\n" if !defined $ARGV[0];
-$mid = $ARGV[0];
-$mid =~ s/^<//;
-$mid =~ s/>$//;
-$mid = "<$mid>";
+my $c = cnntp_connect($verbose);
+$c->banner_reader();
-$ns=$ENV{'NNTPSERVER'};
-if (!defined $ns or !length $ns) {
- $ns = `cat /etc/nntpserver`;
- chomp($ns);
-}
-$port = (getservbyname("nntp", "tcp"))[2];
-$ns = inet_aton($ns);
-$proto = getprotobyname("tcp");
-$paddr = sockaddr_in($port, $ns);
-
-socket(S,PF_INET,SOCK_STREAM,$proto) or die "socket: $!";
-connect(S,$paddr) or die "connect: $!";
-
-S->autoflush(1);
-
-&getline;
-$code =~ /^2\d\d/ or die "no initial greeting from server\n";
+our $code;
-&docmd("MODE READER");
# some servers require a GROUP before an ARTICLE command
-&docmd("GROUP misc.misc");
-&docmd("ARTICLE $mid");
-while (1) {
- &getline;
- s/[\r\n]//g;
- last if /^\.$/;
- s/^\.//;
- print STDOUT "$_\n";
+$c->docmd("GROUP misc.misc");
+
+if(@ARGV == 0) {
+ while(<>) {
+ s/(^\s+|\s+$)//gs;
+ lookup($_);
+ }
+} else {
+ while (@ARGV) {
+ my $item = shift @ARGV;
+ if($item !~ /[\@:]/ and not defined $group) {
+ # maybe a bare group followed by an article number
+ die unless @ARGV;
+ my $number = shift @ARGV;
+ $item = "$item $number";
+ }
+ lookup($item);
+ }
}
-&docmd("QUIT");
+
+$c->docmd("QUIT");
close S;
-sub putline {
- my ($line) = @_;
- print STDERR ">>> $line\n" if $verbose;
- print S "$line\r\n";
-}
+sub lookup {
+ my $mid = shift;
-sub getline {
- $_ = <S>;
- s/[\r\n]*$//s;
- $code = substr($_,0,3);
- print STDERR "<<< $_\n" if $verbose;
-}
+ if($mid !~ /\@/ and $mid =~ /^(.*)[: ](\d+)$/) {
+ my ($g, $n) = ($1, $2);
+ $c->docmd("GROUP $g");
+ $c->docmd("ARTICLE $n");
+ } else {
+ $mid =~ s/.*\<//;
+ $mid =~ s/\>.*//;
+ $c->docmd("ARTICLE <$mid>");
+ }
-sub docmd {
- my ($cmd) = @_;
+ my $fh= 'STDOUT';
+ if (-t $fh) {
+ my $lesscmd= $ENV{'NNTPID_PAGER'};
+ $lesscmd= 'less' unless defined $lesscmd;
+ open LESS, "|-", 'sh','-c',$lesscmd or die $!;
+ $fh= 'LESS';
+ }
+
while (1) {
- &putline($cmd);
- &getline;
- if ($code eq "480") { &auth; } else { last; }
+ ($code,$_) = $c->getline();
+ s/[\r\n]//g;
+ last if /^\.$/;
+ s/^\.//;
+ print $fh "$_\n";
}
- $code =~ /^2\d\d/ or die "failed on `$cmd':\n$_\n";
-}
-sub auth {
- # Authentication.
- if ($ENV{"NNTPAUTH"}) {
- $auth = $ENV{"NNTPAUTH"};
- &putline("AUTHINFO GENERIC $auth");
- pipe AUTHSTDIN, TOAUTH or die "unable to create pipes";
- pipe FROMAUTH, AUTHSTDOUT or die "unable to create pipes";
- $pid = fork;
- if (!defined $pid) {
- die "unable to fork for authentication helper";
- } elsif ($pid == 0) {
- # we are child
- $ENV{"NNTP_AUTH_FDS"} = "0.1";
- open STDIN, "<&AUTHSTDIN";
- open STDOUT, ">&AUTHSTDOUT";
- close S;
- exec $auth;
- }
- # we are parent
- close AUTHSTDIN;
- close AUTHSTDOUT;
- autoflush TOAUTH 1;
- &getline; print TOAUTH "$_\n";
- while (<FROMAUTH>) {
- s/[\r\n]*$//s;
- &putline($_);
- &getline;
- print TOAUTH "$_\n";
- }
- die "failed authentication\n" unless $? == 0;
+ if ($fh ne 'STDOUT') {
+ close $fh or die "$? $!";
}
}