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)
10 # The name of your news server is obtained from the environment variable
11 # NNTPSERVER, or from the file /etc/nntpserver if that's not set.
13 # This script supports AUTHINFO GENERIC authentication using the
14 # environment variable NNTPAUTH. It will only attempt this if it receives
15 # a 480 response from the news server; if your news server isn't paranoid
16 # then the script will never need to look at NNTPAUTH.
18 # Copyright 2000 Simon Tatham. All rights reserved.
19 # FIXME: put in a licence notice.
26 "usage: nntpid [ -v ] <message-id>\n" .
27 " or: nntpid [ -v ] <newsgroup> <article-number>\n" .
28 "where: -v verbose (print interaction with news server)\n" .
29 " also: nntpid --version report version number\n" .
30 " nntpid --help display this help text\n" .
31 " nntpid --licence display (MIT) licence text\n";
34 "nntpid is copyright 2000,2004 Simon Tatham.\n" .
36 "Permission is hereby granted, free of charge, to any person\n" .
37 "obtaining a copy of this software and associated documentation files\n" .
38 "(the \"Software\"), to deal in the Software without restriction,\n" .
39 "including without limitation the rights to use, copy, modify, merge,\n" .
40 "publish, distribute, sublicense, and/or sell copies of the Software,\n" .
41 "and to permit persons to whom the Software is furnished to do so,\n" .
42 "subject to the following conditions:\n" .
44 "The above copyright notice and this permission notice shall be\n" .
45 "included in all copies or substantial portions of the Software.\n" .
47 "THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND,\n" .
48 "EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF\n" .
49 "MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND\n" .
50 "NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS\n" .
51 "BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN\n" .
52 "ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\n" .
53 "CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n" .
56 while ($ARGV[0] =~ /^-(.+)$/) {
58 $verbose=1, next if $1 eq "v";
62 } elsif ($1 eq "-version") {
63 if ('$Revision$' =~ /Revision:\s+(\d+)/) {
64 print "nntpid revision $1\n";
66 print "nntpid: unknown revision\n";
69 } elsif ($1 eq "-licence" or $1 eq "-license") {
75 die $usage if !defined $ARGV[0];
77 if (defined $ARGV[1]) {
88 $ns=$ENV{'NNTPSERVER'};
89 if (!defined $ns or !length $ns) {
90 $ns = `cat /etc/nntpserver`;
92 $port = (getservbyname("nntp", "tcp"))[2];
94 $proto = getprotobyname("tcp");
95 $paddr = sockaddr_in
($port, $ns);
97 socket(S
,PF_INET
,SOCK_STREAM
,$proto) or die "socket: $!";
98 connect(S
,$paddr) or die "connect: $!";
103 $code =~ /^2\d\d/ or die "no initial greeting from server\n";
105 &docmd
("MODE READER");
106 # some servers require a GROUP before an ARTICLE command
107 &docmd
("GROUP $group");
108 &docmd
("ARTICLE $mid");
121 print STDERR
">>> $line\n" if $verbose;
128 $code = substr($_,0,3);
129 print STDERR
"<<< $_\n" if $verbose;
137 if ($code eq "480") { &auth
; } else { last; }
139 $code =~ /^2\d\d/ or die "failed on `$cmd':\n$_\n";
144 if ($ENV{"NNTPAUTH"}) {
145 $auth = $ENV{"NNTPAUTH"};
146 &putline
("AUTHINFO GENERIC $auth");
147 pipe AUTHSTDIN
, TOAUTH
or die "unable to create pipes";
148 pipe FROMAUTH
, AUTHSTDOUT
or die "unable to create pipes";
151 die "unable to fork for authentication helper";
152 } elsif ($pid == 0) {
154 $ENV{"NNTP_AUTH_FDS"} = "0.1";
155 open STDIN
, "<&AUTHSTDIN";
156 open STDOUT
, ">&AUTHSTDOUT";
164 &getline
; print TOAUTH
"$_\n";
171 die "failed authentication\n" unless $?
== 0;