New mode for nntpid: `nntpid -a news.group.name' downloads all
authorsimon <simon@cda61777-01e9-0310-a592-d414129be87e>
Wed, 15 Feb 2006 18:40:24 +0000 (18:40 +0000)
committersimon <simon@cda61777-01e9-0310-a592-d414129be87e>
Wed, 15 Feb 2006 18:40:24 +0000 (18:40 +0000)
available articles in a group and writes them to an mbox. Handy if
you want to search a whole group in ways beyond those supported by
your newsreader.

git-svn-id: svn://svn.tartarus.org/sgt/utils@6562 cda61777-01e9-0310-a592-d414129be87e

nntpid/nntpid
nntpid/nntpid.but

index 13e0732..e444c73 100755 (executable)
@@ -6,6 +6,7 @@
 # Usage:     nntpid <messageid>
 #    or:     nntpid messageid               (angle brackets optional)
 #    or:     nntpid news.group.name 1234    (group+number form)
+#    or:     nntpid -a news.group.name      (download all available articles)
 #
 # The name of your news server is obtained from the environment variable
 # NNTPSERVER, or from the file /etc/nntpserver if that's not set.
@@ -25,8 +26,10 @@ use FileHandle;
 $usage =
   "usage: nntpid [ -v ] [ -d ] <message-id>\n" .
   "   or: nntpid [ -v ] [ -d ] <newsgroup> <article-number>\n" .
+  "   or: nntpid [ -v ] -a <newsgroup>\n" .
   "where: -v                  verbose (print interaction with news server)\n" .
   "       -d                  direct output (don't consider using PAGER)\n" .
+  "       -a                  dump all articles in group to stdout as mbox\n" .
   " also: nntpid --version    report version number\n" .
   "       nntpid --help       display this help text\n" .
   "       nntpid --licence    display (MIT) licence text\n";
@@ -56,11 +59,13 @@ $licence =
 
 $pager = 1;
 $verbose = 0;
+$all = 0;
 
 while ($ARGV[0] =~ /^-(.+)$/) {
   shift @ARGV;
   $verbose = 1, next if $1 eq "v";
   $pager = 0, next if $1 eq "d";
+  $all = 1, next if $1 eq "a";
   if ($1 eq "-help") {
     print STDERR $usage;
     exit 0;
@@ -79,7 +84,9 @@ while ($ARGV[0] =~ /^-(.+)$/) {
 
 die $usage if !defined $ARGV[0];
 
-if (defined $ARGV[1]) {
+if ($all) {
+  $group = $ARGV[0];
+} elsif (defined $ARGV[1]) {
   $group = $ARGV[0];
   $mid = $ARGV[1];
 } else {
@@ -104,32 +111,51 @@ connect(S,$paddr) or die "connect: $!";
 
 S->autoflush(1);
 
+$fatal = 1; # most errors need to be fatal
+
 &getline;
 $code =~ /^2\d\d/ or die "no initial greeting from server\n";
 
 &docmd("MODE READER");
 # some servers require a GROUP before an ARTICLE command
-&docmd("GROUP $group");
-&docmd("ARTICLE $mid");
-$art = "";
-while (1) {
-  &getline;
-  s/[\r\n]//g;
-  last if /^\.$/;
-  s/^\.//;
-  $art .= "$_\n";
-}
-&docmd("QUIT");
-close S;
-
-if ($pager and -t STDOUT) {
-    $pagername = $ENV{"PAGER"};
-    $pagername = "more" unless defined $pagername;
-    open PAGER, "| $pagername";
-    print PAGER $art;
-    close PAGER;
+$numbers = &docmd("GROUP $group");
+if ($all) {
+    @numbers = split / /, $numbers;
+    $fatal = 0; # ignore failure to retrieve any given article
+    for ($mid = $numbers[1]; $mid <= $numbers[2]; $mid++) {
+       $art = &getart($mid);
+       $art =~ s/\n(>*From )/\n>$1/gs;
+       print "From nntpid ".(localtime)."\n".$art."\n";
+    }
 } else {
-    print $art;
+    $art = &getart($mid);
+    &docmd("QUIT");
+    close S;
+
+    if ($pager and -t STDOUT) {
+       $pagername = $ENV{"PAGER"};
+       $pagername = "more" unless defined $pagername;
+       open PAGER, "| $pagername";
+       print PAGER $art;
+       close PAGER;
+    } else {
+       print $art;
+    }
+}
+
+sub getart {
+  my ($mid) = @_;
+  $ret = &docmd("ARTICLE $mid");
+  return undef if !defined $ret;
+  $art = "";
+  while (1) {
+    &getline;
+    s/[\r\n]//g;
+    last if /^\.$/;
+    s/^\.//;
+    $art .= "$_\n";
+  }
+  return $art;
 }
 
 sub putline {
@@ -143,16 +169,21 @@ sub getline {
   s/[\r\n]*$//s;
   $code = substr($_,0,3);
   print STDERR "<<< $_\n" if $verbose;
+  return substr($_,4);
 }
 
 sub docmd {
   my ($cmd) = @_;
   while (1) {
     &putline($cmd);
-    &getline;
+    $line = &getline;
     if ($code eq "480") { &auth; } else { last; }
   }
-  $code =~ /^2\d\d/ or die "failed on `$cmd':\n$_\n";
+  if ($code !~ /^2\d\d/) {
+    die "failed on `$cmd':\n$_\n" if $fatal;
+    return undef;
+  }
+  return $line;
 }
 
 sub auth {
index 7f21512..3444a99 100644 (file)
 \e bbbbbb   bb     bb   iiiiiiiiii
 \c nntpid [ -v ] [ -d ] newsgroup-name article-number
 \e bbbbbb   bb     bb   iiiiiiiiiiiiii iiiiiiiiiiiiii
+\c nntpid [ -v ] -a newsgroup-name
+\e bbbbbb   bb   bb iiiiiiiiiiiiii
 
 \U DESCRIPTION
 
-\cw{nntpid} makes a connection to a news server, retrieves a single
-article, and displays it.
+\cw{nntpid} makes a connection to a news server, retrieves one or
+more articles, and displays it.
 
 You can specify the article you want by either:
 
@@ -38,6 +40,10 @@ that cause unexpected behaviour in your terminal. If \cw{nntpid}
 detects that its standard output is not a terminal, however, it will
 bypass the pager and just write out the article directly.
 
+There is a third mode of operation, enabled by the \cw{-a} option,
+in which \cw{nntpid} retrieves \e{all} available articles in the
+group and writes them to standard output in \cw{mbox} format.
+
 \U ARGUMENTS
 
 If you specify one argument, \cw{nntpid} assumes it is a Message-ID.
@@ -60,6 +66,12 @@ conversation with the news server on standard error.
 \dd Direct output. In this mode, \cw{nntpid} will write the article
 straight to standard output without bothering to try using a pager.
 
+\dt \cw{-a}
+
+\dd Retrieve all articles from the given newsgroup. In this mode,
+\cw{nntpid} will always write straight to standard output (so the
+\cw{-d} option is unnecessary).
+
 \U AUTHENTICATION
 
 Currently, the only form of authentication supported by \cw{nntpid}