3 # Script to retrieve a single article from a news server, either by
4 # message-id or by group name and article number.
6 # Usage: nntpid <messageid>
7 # or: nntpid messageid (angle brackets optional)
8 # or: nntpid news.group.name 1234 (group+number form)
9 # or: nntpid -a news.group.name (download all available articles)
11 # The name of your news server is obtained from the environment variable
12 # NNTPSERVER, or from the file /etc/nntpserver if that's not set.
14 # This script supports AUTHINFO GENERIC authentication using the
15 # environment variable NNTPAUTH. It will only attempt this if it receives
16 # a 480 response from the news server; if your news server isn't paranoid
17 # then the script will never need to look at NNTPAUTH.
19 # Copyright 2000,2004,2011 Simon Tatham. All rights reserved.
26 "usage: nntpid [ -v ] [ -d ] <article> [<article>...] display articles\n" .
27 " or: nntpid [ -v ] [ -d ] display articles read from standard input\n" .
28 " or: nntpid [ -v ] -a <newsgroup> dump a newsgroup in mbox format\n" .
29 "where: <article> a news article specified in one of several ways:\n" .
30 " - Message-Id in angle brackets\n" .
31 " - bare Message-Id without angle brackets\n" .
32 " - newsgroup and article number in separate words\n" .
33 " - newsgroup and article number separated by :\n" .
34 " -v verbose (print interaction with news server)\n" .
35 " -d direct output (don't consider using PAGER)\n" .
36 " -a dump all articles in group to stdout as mbox\n" .
37 " -b return current bounds on group's article numbers\n" .
38 " also: nntpid --version report version number\n" .
39 " nntpid --help display this help text\n" .
40 " nntpid --licence display (MIT) licence text\n";
43 "nntpid is copyright 2000,2004,2011 Simon Tatham.\n" .
45 "Permission is hereby granted, free of charge, to any person\n" .
46 "obtaining a copy of this software and associated documentation files\n" .
47 "(the \"Software\"), to deal in the Software without restriction,\n" .
48 "including without limitation the rights to use, copy, modify, merge,\n" .
49 "publish, distribute, sublicense, and/or sell copies of the Software,\n" .
50 "and to permit persons to whom the Software is furnished to do so,\n" .
51 "subject to the following conditions:\n" .
53 "The above copyright notice and this permission notice shall be\n" .
54 "included in all copies or substantial portions of the Software.\n" .
56 "THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND,\n" .
57 "EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF\n" .
58 "MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND\n" .
59 "NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS\n" .
60 "BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN\n" .
61 "ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\n" .
62 "CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n" .
69 while ($ARGV[0] =~ /^-(.+)$/) {
71 $verbose = 1, next if $1 eq "v";
72 $pager = 0, next if $1 eq "d";
73 $mode = 'all', next if $1 eq "a";
74 $mode = 'bounds', next if $1 eq "b";
78 } elsif ($1 eq "-version") {
79 if ('$Revision$' =~ /Revision:\s+(\d+)/) {
80 print "nntpid revision $1\n";
82 print "nntpid: unknown revision\n";
85 } elsif ($1 eq "-licence" or $1 eq "-license") {
92 # -a uses completely different command-line semantics from the
94 die "nntpid: -a expected exactly one argument\n" if @ARGV != 1;
96 } elsif ($mode eq 'bounds') {
97 # -b has similar command-line semantics to -a.
98 die "nntpid: -b expected exactly one argument\n" if @ARGV != 1;
101 # We will read article ids from standard input once we've connected
102 # to the NNTP server.
106 while (defined ($arg = shift @ARGV)) {
107 # See if this argument makes sense on its own.
108 ($group, $mid) = &parsearticle
($arg);
112 # If it doesn't, try concatenating it with a space to the next
113 # argument (so you can provide a group and article number in two
114 # successive command-line arguments).
115 $args = $arg . " " . $ARGV[0];
116 ($group, $mid) = &parsearticle
($args);
119 shift @ARGV; # and eat the second argument
121 # If all else fails, die in panic.
122 die "nntpid: argument '$arg': unable to parse\n";
128 $ns=$ENV{'NNTPSERVER'};
129 if (!defined $ns or !length $ns) {
130 $ns = `cat /etc/nntpserver`;
133 $port = (getservbyname("nntp", "tcp"))[2];
134 $ns = inet_aton
($ns);
135 $proto = getprotobyname("tcp");
136 $paddr = sockaddr_in
($port, $ns);
139 if ($mode eq 'all') {
140 # Write out the entire contents of a newsgroup in mbox format.
141 $numbers = &docmd
("GROUP $group");
142 @numbers = split / /, $numbers;
143 $fatal = 0; # ignore failure to retrieve any given article
144 for ($mid = $numbers[1]; $mid <= $numbers[2]; $mid++) {
145 $art = &getart
("$group:$mid");
147 $art =~ s/\n(>*From )/\n>$1/gs;
148 print "From nntpid ".(localtime)."\n".$art."\n";
151 } elsif ($mode eq 'bounds') {
152 # Write out the bounds of the group's article numbers.
153 $numbers = &docmd
("GROUP $group");
154 @numbers = split / /, $numbers;
155 print "$numbers[1] $numbers[2]\n";
156 } elsif ($mode eq 'stdin') {
159 s/^\s+//; s/\s+$//; # trim whitespace
162 } elsif ($mode eq 'list') {
164 &displayarticle
($item);
169 # Article identifiers used as input to this program can be in a
170 # variety of formats. This function untangles one into a standard
171 # format, which is either (undef, message-id) or (group, article
172 # number). In case of parse failure, it returns (undef, undef).
174 if ($art =~ /^(.*<)?([^<>]*\@[^<>]*)(>.*)?$/) {
175 # Anything with an @ sign is treated as a Message-ID. We trim
176 # angle brackets and anything outside them.
178 } elsif ($art =~ /^(\S+)(\s+|:)(\d+)$/) {
179 # A group name and article number separated by whitespace or a
184 return (undef, undef);
193 my $art = &getart
($mid);
194 return unless defined $art;
196 if ($pager and -t STDOUT
) {
197 # Close the NNTP connection before invoking the pager, in case the
198 # user spends so long looking at the article that the server times
202 $pagername = $ENV{"PAGER"};
203 $pagername = "more" unless defined $pagername;
204 open PAGER
, "| $pagername";
217 ($group, $mid) = &parsearticle
($art);
219 warn "unable to parse '$art'\n";
221 } elsif (defined $group) {
222 # This is a (group, article number) pair.
223 &docmd
("GROUP $group");
224 $ret = &docmd
("ARTICLE $mid");
226 # This is a Message-Id. Some NNTP servers will insist on having
227 # seen a GROUP command before 'ARTICLE <some.random@message.id>',
228 # so ensure we've sent one.
229 &docmd
("GROUP misc.misc") unless $in_a_group;
230 $ret = &docmd
("ARTICLE <$mid>");
233 return undef if !defined $ret;
249 print STDERR
">>> $line\n" if $verbose;
256 $code = substr($_,0,3);
257 print STDERR
"<<< $_\n" if $verbose;
262 return if $connected;
263 socket(S
,PF_INET
,SOCK_STREAM
,$proto) or die "socket: $!";
264 connect(S
,$paddr) or die "connect: $!";
268 $fatal = 1; # most errors need to be fatal
271 $code =~ /^2\d\d/ or die "no initial greeting from server\n";
273 &docmd
("MODE READER");
287 # We go at most twice round the following loop. If the first attempt
288 # to fetch the article fails with a 480 response, we try again
289 # having authenticated first; but if the second attempt also fails
290 # with 480, then the authentication didn't work, so we should give
291 # up rather than try it pointlessly again.
295 if ($code eq "480") { &auth
; } else { last; }
297 if ($code !~ /^2\d\d/) {
298 die "failed on `$cmd':\n$_\n" if $fatal;
306 if ($ENV{"NNTPAUTH"}) {
307 $auth = $ENV{"NNTPAUTH"};
308 &putline
("AUTHINFO GENERIC $auth");
309 pipe AUTHSTDIN
, TOAUTH
or die "unable to create pipes";
310 pipe FROMAUTH
, AUTHSTDOUT
or die "unable to create pipes";
313 die "unable to fork for authentication helper";
314 } elsif ($pid == 0) {
316 $ENV{"NNTP_AUTH_FDS"} = "0.1";
317 open STDIN
, "<&AUTHSTDIN";
318 open STDOUT
, ">&AUTHSTDOUT";
326 &getline
; print TOAUTH
"$_\n";
333 die "failed authentication\n" unless $?
== 0;