13e0732be7e0f81794f832af2e6a3f205fe9353e
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 ] [ -d ] <message-id>\n" .
27 " or: nntpid [ -v ] [ -d ] <newsgroup> <article-number>\n" .
28 "where: -v verbose (print interaction with news server)\n" .
29 " -d direct output (don't consider using PAGER)\n" .
30 " also: nntpid --version report version number\n" .
31 " nntpid --help display this help text\n" .
32 " nntpid --licence display (MIT) licence text\n";
35 "nntpid is copyright 2000,2004 Simon Tatham.\n" .
37 "Permission is hereby granted, free of charge, to any person\n" .
38 "obtaining a copy of this software and associated documentation files\n" .
39 "(the \"Software\"), to deal in the Software without restriction,\n" .
40 "including without limitation the rights to use, copy, modify, merge,\n" .
41 "publish, distribute, sublicense, and/or sell copies of the Software,\n" .
42 "and to permit persons to whom the Software is furnished to do so,\n" .
43 "subject to the following conditions:\n" .
45 "The above copyright notice and this permission notice shall be\n" .
46 "included in all copies or substantial portions of the Software.\n" .
48 "THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND,\n" .
49 "EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF\n" .
50 "MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND\n" .
51 "NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS\n" .
52 "BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN\n" .
53 "ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\n" .
54 "CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n" .
60 while ($ARGV[0] =~ /^-(.+)$/) {
62 $verbose = 1, next if $1 eq "v";
63 $pager = 0, next if $1 eq "d";
67 } elsif ($1 eq "-version") {
68 if ('$Revision$' =~ /Revision:\s+(\d+)/) {
69 print "nntpid revision $1\n";
71 print "nntpid: unknown revision\n";
74 } elsif ($1 eq "-licence" or $1 eq "-license") {
80 die $usage if !defined $ARGV[0];
82 if (defined $ARGV[1]) {
93 $ns=$ENV{'NNTPSERVER'};
94 if (!defined $ns or !length $ns) {
95 $ns = `cat /etc/nntpserver`;
97 $port = (getservbyname("nntp", "tcp"))[2];
99 $proto = getprotobyname("tcp");
100 $paddr = sockaddr_in
($port, $ns);
102 socket(S
,PF_INET
,SOCK_STREAM
,$proto) or die "socket: $!";
103 connect(S
,$paddr) or die "connect: $!";
108 $code =~ /^2\d\d/ or die "no initial greeting from server\n";
110 &docmd
("MODE READER");
111 # some servers require a GROUP before an ARTICLE command
112 &docmd
("GROUP $group");
113 &docmd
("ARTICLE $mid");
125 if ($pager and -t STDOUT
) {
126 $pagername = $ENV{"PAGER"};
127 $pagername = "more" unless defined $pagername;
128 open PAGER
, "| $pagername";
137 print STDERR
">>> $line\n" if $verbose;
144 $code = substr($_,0,3);
145 print STDERR
"<<< $_\n" if $verbose;
153 if ($code eq "480") { &auth
; } else { last; }
155 $code =~ /^2\d\d/ or die "failed on `$cmd':\n$_\n";
160 if ($ENV{"NNTPAUTH"}) {
161 $auth = $ENV{"NNTPAUTH"};
162 &putline
("AUTHINFO GENERIC $auth");
163 pipe AUTHSTDIN
, TOAUTH
or die "unable to create pipes";
164 pipe FROMAUTH
, AUTHSTDOUT
or die "unable to create pipes";
167 die "unable to fork for authentication helper";
168 } elsif ($pid == 0) {
170 $ENV{"NNTP_AUTH_FDS"} = "0.1";
171 open STDIN
, "<&AUTHSTDIN";
172 open STDOUT
, ">&AUTHSTDOUT";
180 &getline
; print TOAUTH
"$_\n";
187 die "failed authentication\n" unless $?
== 0;