New mode for nntpid: `nntpid -a news.group.name' downloads all
[sgt/utils] / nntpid / nntpid
CommitLineData
3096ed75 1#!/usr/bin/perl
2#
3# Script to retrieve a single article from a news server, either by
4# message-id or by group name and article number.
5#
6# Usage: nntpid <messageid>
7# or: nntpid messageid (angle brackets optional)
8# or: nntpid news.group.name 1234 (group+number form)
52f9a468 9# or: nntpid -a news.group.name (download all available articles)
3096ed75 10#
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.
13#
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.
18
19# Copyright 2000 Simon Tatham. All rights reserved.
20# FIXME: put in a licence notice.
21
22require 5.002;
23use Socket;
24use FileHandle;
25
26$usage =
a5d9e758 27 "usage: nntpid [ -v ] [ -d ] <message-id>\n" .
28 " or: nntpid [ -v ] [ -d ] <newsgroup> <article-number>\n" .
52f9a468 29 " or: nntpid [ -v ] -a <newsgroup>\n" .
3096ed75 30 "where: -v verbose (print interaction with news server)\n" .
a5d9e758 31 " -d direct output (don't consider using PAGER)\n" .
52f9a468 32 " -a dump all articles in group to stdout as mbox\n" .
3096ed75 33 " also: nntpid --version report version number\n" .
34 " nntpid --help display this help text\n" .
35 " nntpid --licence display (MIT) licence text\n";
36
37$licence =
38 "nntpid is copyright 2000,2004 Simon Tatham.\n" .
39 "\n" .
40 "Permission is hereby granted, free of charge, to any person\n" .
41 "obtaining a copy of this software and associated documentation files\n" .
42 "(the \"Software\"), to deal in the Software without restriction,\n" .
43 "including without limitation the rights to use, copy, modify, merge,\n" .
44 "publish, distribute, sublicense, and/or sell copies of the Software,\n" .
45 "and to permit persons to whom the Software is furnished to do so,\n" .
46 "subject to the following conditions:\n" .
47 "\n" .
48 "The above copyright notice and this permission notice shall be\n" .
49 "included in all copies or substantial portions of the Software.\n" .
50 "\n" .
51 "THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND,\n" .
52 "EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF\n" .
53 "MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND\n" .
54 "NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS\n" .
55 "BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN\n" .
56 "ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\n" .
57 "CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n" .
58 "SOFTWARE.\n";
59
a5d9e758 60$pager = 1;
61$verbose = 0;
52f9a468 62$all = 0;
a5d9e758 63
3096ed75 64while ($ARGV[0] =~ /^-(.+)$/) {
65 shift @ARGV;
a5d9e758 66 $verbose = 1, next if $1 eq "v";
67 $pager = 0, next if $1 eq "d";
52f9a468 68 $all = 1, next if $1 eq "a";
3096ed75 69 if ($1 eq "-help") {
70 print STDERR $usage;
71 exit 0;
72 } elsif ($1 eq "-version") {
73 if ('$Revision$' =~ /Revision:\s+(\d+)/) {
74 print "nntpid revision $1\n";
75 } else {
76 print "nntpid: unknown revision\n";
77 }
78 exit 0;
79 } elsif ($1 eq "-licence" or $1 eq "-license") {
80 print $licence;
81 exit 0;
82 }
83}
84
85die $usage if !defined $ARGV[0];
86
52f9a468 87if ($all) {
88 $group = $ARGV[0];
89} elsif (defined $ARGV[1]) {
3096ed75 90 $group = $ARGV[0];
91 $mid = $ARGV[1];
92} else {
93 $group = "misc.misc";
94 $mid = $ARGV[0];
95 $mid =~ s/^<//;
96 $mid =~ s/>$//;
97 $mid = "<$mid>";
98}
99
100$ns=$ENV{'NNTPSERVER'};
101if (!defined $ns or !length $ns) {
102 $ns = `cat /etc/nntpserver`;
103}
104$port = (getservbyname("nntp", "tcp"))[2];
105$ns = inet_aton($ns);
106$proto = getprotobyname("tcp");
107$paddr = sockaddr_in($port, $ns);
108
109socket(S,PF_INET,SOCK_STREAM,$proto) or die "socket: $!";
110connect(S,$paddr) or die "connect: $!";
111
112S->autoflush(1);
113
52f9a468 114$fatal = 1; # most errors need to be fatal
115
3096ed75 116&getline;
117$code =~ /^2\d\d/ or die "no initial greeting from server\n";
118
119&docmd("MODE READER");
120# some servers require a GROUP before an ARTICLE command
52f9a468 121$numbers = &docmd("GROUP $group");
122if ($all) {
123 @numbers = split / /, $numbers;
124 $fatal = 0; # ignore failure to retrieve any given article
125 for ($mid = $numbers[1]; $mid <= $numbers[2]; $mid++) {
126 $art = &getart($mid);
127 $art =~ s/\n(>*From )/\n>$1/gs;
128 print "From nntpid ".(localtime)."\n".$art."\n";
129 }
a5d9e758 130} else {
52f9a468 131 $art = &getart($mid);
132 &docmd("QUIT");
133 close S;
134
135 if ($pager and -t STDOUT) {
136 $pagername = $ENV{"PAGER"};
137 $pagername = "more" unless defined $pagername;
138 open PAGER, "| $pagername";
139 print PAGER $art;
140 close PAGER;
141 } else {
142 print $art;
143 }
144}
145
146sub getart {
147 my ($mid) = @_;
148 $ret = &docmd("ARTICLE $mid");
149 return undef if !defined $ret;
150 $art = "";
151 while (1) {
152 &getline;
153 s/[\r\n]//g;
154 last if /^\.$/;
155 s/^\.//;
156 $art .= "$_\n";
157 }
158 return $art;
a5d9e758 159}
160
3096ed75 161sub putline {
162 my ($line) = @_;
163 print STDERR ">>> $line\n" if $verbose;
164 print S "$line\r\n";
165}
166
167sub getline {
168 $_ = <S>;
169 s/[\r\n]*$//s;
170 $code = substr($_,0,3);
171 print STDERR "<<< $_\n" if $verbose;
52f9a468 172 return substr($_,4);
3096ed75 173}
174
175sub docmd {
176 my ($cmd) = @_;
177 while (1) {
178 &putline($cmd);
52f9a468 179 $line = &getline;
3096ed75 180 if ($code eq "480") { &auth; } else { last; }
181 }
52f9a468 182 if ($code !~ /^2\d\d/) {
183 die "failed on `$cmd':\n$_\n" if $fatal;
184 return undef;
185 }
186 return $line;
3096ed75 187}
188
189sub auth {
190 # Authentication.
191 if ($ENV{"NNTPAUTH"}) {
192 $auth = $ENV{"NNTPAUTH"};
193 &putline("AUTHINFO GENERIC $auth");
194 pipe AUTHSTDIN, TOAUTH or die "unable to create pipes";
195 pipe FROMAUTH, AUTHSTDOUT or die "unable to create pipes";
196 $pid = fork;
197 if (!defined $pid) {
198 die "unable to fork for authentication helper";
199 } elsif ($pid == 0) {
200 # we are child
201 $ENV{"NNTP_AUTH_FDS"} = "0.1";
202 open STDIN, "<&AUTHSTDIN";
203 open STDOUT, ">&AUTHSTDOUT";
204 close S;
205 exec $auth;
206 }
207 # we are parent
208 close AUTHSTDIN;
209 close AUTHSTDOUT;
210 autoflush TOAUTH 1;
211 &getline; print TOAUTH "$_\n";
212 while (<FROMAUTH>) {
213 s/[\r\n]*$//s;
214 &putline($_);
215 &getline;
216 print TOAUTH "$_\n";
217 }
218 die "failed authentication\n" unless $? == 0;
219 }
220}