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 | |
67784f1f |
19 | # Copyright 2000,2004,2011 Simon Tatham. All rights reserved. |
3096ed75 |
20 | |
21 | require 5.002; |
22 | use Socket; |
23 | use FileHandle; |
24 | |
25 | $usage = |
67784f1f |
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" . |
a5d9e758 |
35 | " -d direct output (don't consider using PAGER)\n" . |
52f9a468 |
36 | " -a dump all articles in group to stdout as mbox\n" . |
3096ed75 |
37 | " also: nntpid --version report version number\n" . |
38 | " nntpid --help display this help text\n" . |
39 | " nntpid --licence display (MIT) licence text\n"; |
40 | |
41 | $licence = |
67784f1f |
42 | "nntpid is copyright 2000,2004,2011 Simon Tatham.\n" . |
3096ed75 |
43 | "\n" . |
44 | "Permission is hereby granted, free of charge, to any person\n" . |
45 | "obtaining a copy of this software and associated documentation files\n" . |
46 | "(the \"Software\"), to deal in the Software without restriction,\n" . |
47 | "including without limitation the rights to use, copy, modify, merge,\n" . |
48 | "publish, distribute, sublicense, and/or sell copies of the Software,\n" . |
49 | "and to permit persons to whom the Software is furnished to do so,\n" . |
50 | "subject to the following conditions:\n" . |
51 | "\n" . |
52 | "The above copyright notice and this permission notice shall be\n" . |
53 | "included in all copies or substantial portions of the Software.\n" . |
54 | "\n" . |
55 | "THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND,\n" . |
56 | "EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF\n" . |
57 | "MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND\n" . |
58 | "NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS\n" . |
59 | "BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN\n" . |
60 | "ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\n" . |
61 | "CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n" . |
62 | "SOFTWARE.\n"; |
63 | |
a5d9e758 |
64 | $pager = 1; |
65 | $verbose = 0; |
67784f1f |
66 | $mode = 'list'; |
a5d9e758 |
67 | |
3096ed75 |
68 | while ($ARGV[0] =~ /^-(.+)$/) { |
69 | shift @ARGV; |
a5d9e758 |
70 | $verbose = 1, next if $1 eq "v"; |
71 | $pager = 0, next if $1 eq "d"; |
67784f1f |
72 | $mode = 'all', next if $1 eq "a"; |
3096ed75 |
73 | if ($1 eq "-help") { |
74 | print STDERR $usage; |
75 | exit 0; |
76 | } elsif ($1 eq "-version") { |
77 | if ('$Revision$' =~ /Revision:\s+(\d+)/) { |
78 | print "nntpid revision $1\n"; |
79 | } else { |
80 | print "nntpid: unknown revision\n"; |
81 | } |
82 | exit 0; |
83 | } elsif ($1 eq "-licence" or $1 eq "-license") { |
84 | print $licence; |
85 | exit 0; |
86 | } |
87 | } |
88 | |
67784f1f |
89 | if ($mode eq 'all') { |
90 | # -a uses completely different command-line semantics from the |
91 | # normal ones.. |
92 | die "nntpid: -a expected exactly one argument\n" if @ARGV != 1; |
3096ed75 |
93 | $group = $ARGV[0]; |
67784f1f |
94 | } elsif (!@ARGV) { |
95 | # We will read article ids from standard input once we've connected |
96 | # to the NNTP server. |
97 | $mode = 'stdin'; |
3096ed75 |
98 | } else { |
67784f1f |
99 | @list = (); |
100 | while (defined ($arg = shift @ARGV)) { |
101 | # See if this argument makes sense on its own. |
102 | ($group, $mid) = &parsearticle($arg); |
103 | if (defined $mid) { |
104 | push @list, $arg; |
105 | } else { |
106 | # If it doesn't, try concatenating it with a space to the next |
107 | # argument (so you can provide a group and article number in two |
108 | # successive command-line arguments). |
109 | $args = $arg . " " . $ARGV[0]; |
110 | ($group, $mid) = &parsearticle($args); |
111 | if (defined $mid) { |
112 | push @list, $args; |
113 | shift @ARGV; # and eat the second argument |
114 | } else { |
115 | # If all else fails, die in panic. |
116 | die "nntpid: argument '$arg': unable to parse\n"; |
117 | } |
118 | } |
119 | } |
3096ed75 |
120 | } |
121 | |
122 | $ns=$ENV{'NNTPSERVER'}; |
123 | if (!defined $ns or !length $ns) { |
124 | $ns = `cat /etc/nntpserver`; |
b25fe76c |
125 | chomp $ns; |
3096ed75 |
126 | } |
127 | $port = (getservbyname("nntp", "tcp"))[2]; |
128 | $ns = inet_aton($ns); |
129 | $proto = getprotobyname("tcp"); |
130 | $paddr = sockaddr_in($port, $ns); |
131 | |
67784f1f |
132 | &connect; |
133 | if ($mode eq 'all') { |
134 | # Write out the entire contents of a newsgroup in mbox format. |
135 | $numbers = &docmd("GROUP $group"); |
136 | @numbers = split / /, $numbers; |
137 | $fatal = 0; # ignore failure to retrieve any given article |
138 | for ($mid = $numbers[1]; $mid <= $numbers[2]; $mid++) { |
139 | $art = &getart("$group:$mid"); |
140 | $art =~ s/\n(>*From )/\n>$1/gs; |
141 | print "From nntpid ".(localtime)."\n".$art."\n"; |
142 | } |
143 | } elsif ($mode eq 'stdin') { |
144 | while (<>) { |
145 | chomp; |
146 | s/^\s+//; s/\s+$//; # trim whitespace |
147 | &displayarticle($_); |
148 | } |
149 | } elsif ($mode eq 'list') { |
150 | for $item (@list) { |
151 | &displayarticle($item); |
152 | } |
153 | } |
154 | |
155 | sub parsearticle { |
156 | # Article identifiers used as input to this program can be in a |
157 | # variety of formats. This function untangles one into a standard |
158 | # format, which is either (undef, message-id) or (group, article |
159 | # number). In case of parse failure, it returns (undef, undef). |
160 | my $art = shift @_; |
161 | if ($art =~ /^(.*<)?([^<>]*\@[^<>]*)(>.*)?$/) { |
162 | # Anything with an @ sign is treated as a Message-ID. We trim |
163 | # angle brackets and anything outside them. |
164 | return (undef, $2); |
165 | } elsif ($art =~ /^(\S+)(\s+|:)(\d+)$/) { |
166 | # A group name and article number separated by whitespace or a |
167 | # colon. |
168 | return ($1, $3); |
169 | } else { |
170 | # Unable to parse. |
171 | return (undef, undef); |
172 | } |
173 | } |
3096ed75 |
174 | |
67784f1f |
175 | sub displayarticle { |
176 | my $mid = shift @_; |
3096ed75 |
177 | |
67784f1f |
178 | &connect; |
52f9a468 |
179 | |
67784f1f |
180 | my $art = &getart($mid); |
3096ed75 |
181 | |
67784f1f |
182 | if ($pager and -t STDOUT) { |
183 | # Close the NNTP connection before invoking the pager, in case the |
184 | # user spends so long looking at the article that the server times |
185 | # us out. |
186 | &disconnect; |
187 | |
188 | $pagername = $ENV{"PAGER"}; |
189 | $pagername = "more" unless defined $pagername; |
190 | open PAGER, "| $pagername"; |
191 | print PAGER $art; |
192 | close PAGER; |
193 | } else { |
194 | print $art; |
195 | } |
52f9a468 |
196 | } |
197 | |
198 | sub getart { |
67784f1f |
199 | my $art = shift @_; |
200 | my $group; |
201 | my $mid; |
202 | |
203 | ($group, $mid) = &parsearticle($art); |
204 | if (!defined $mid) { |
205 | warn "unable to parse '$art'\n"; |
206 | return undef; |
207 | } elsif (defined $group) { |
208 | # This is a (group, article number) pair. |
209 | &docmd("GROUP $group"); |
210 | $ret = &docmd("ARTICLE $mid"); |
211 | } else { |
212 | # This is a Message-Id. Some NNTP servers will insist on having |
213 | # seen a GROUP command before 'ARTICLE <some.random@message.id>', |
214 | # so ensure we've sent one. |
215 | &docmd("GROUP misc.misc") unless $in_a_group; |
216 | $ret = &docmd("ARTICLE <$mid>"); |
217 | } |
218 | |
52f9a468 |
219 | return undef if !defined $ret; |
67784f1f |
220 | $in_a_group = 1; |
221 | |
52f9a468 |
222 | $art = ""; |
223 | while (1) { |
224 | &getline; |
225 | s/[\r\n]//g; |
226 | last if /^\.$/; |
227 | s/^\.//; |
228 | $art .= "$_\n"; |
229 | } |
230 | return $art; |
a5d9e758 |
231 | } |
232 | |
3096ed75 |
233 | sub putline { |
234 | my ($line) = @_; |
235 | print STDERR ">>> $line\n" if $verbose; |
236 | print S "$line\r\n"; |
237 | } |
238 | |
239 | sub getline { |
240 | $_ = <S>; |
241 | s/[\r\n]*$//s; |
242 | $code = substr($_,0,3); |
243 | print STDERR "<<< $_\n" if $verbose; |
52f9a468 |
244 | return substr($_,4); |
3096ed75 |
245 | } |
246 | |
67784f1f |
247 | sub connect { |
248 | return if $connected; |
249 | socket(S,PF_INET,SOCK_STREAM,$proto) or die "socket: $!"; |
250 | connect(S,$paddr) or die "connect: $!"; |
251 | |
252 | S->autoflush(1); |
253 | |
254 | $fatal = 1; # most errors need to be fatal |
255 | |
256 | &getline; |
257 | $code =~ /^2\d\d/ or die "no initial greeting from server\n"; |
258 | |
259 | &docmd("MODE READER"); |
260 | |
261 | $connected = 1; |
262 | $in_a_group = 0; |
263 | } |
264 | |
265 | sub disconnect { |
266 | &docmd("QUIT"); |
267 | close S; |
268 | $connected = 0; |
269 | } |
270 | |
3096ed75 |
271 | sub docmd { |
272 | my ($cmd) = @_; |
f42a91ed |
273 | # We go at most twice round the following loop. If the first attempt |
274 | # to fetch the article fails with a 480 response, we try again |
275 | # having authenticated first; but if the second attempt also fails |
276 | # with 480, then the authentication didn't work, so we should give |
277 | # up rather than try it pointlessly again. |
278 | for my $n (0,1) { |
3096ed75 |
279 | &putline($cmd); |
52f9a468 |
280 | $line = &getline; |
3096ed75 |
281 | if ($code eq "480") { &auth; } else { last; } |
282 | } |
52f9a468 |
283 | if ($code !~ /^2\d\d/) { |
284 | die "failed on `$cmd':\n$_\n" if $fatal; |
285 | return undef; |
286 | } |
287 | return $line; |
3096ed75 |
288 | } |
289 | |
290 | sub auth { |
291 | # Authentication. |
292 | if ($ENV{"NNTPAUTH"}) { |
293 | $auth = $ENV{"NNTPAUTH"}; |
294 | &putline("AUTHINFO GENERIC $auth"); |
295 | pipe AUTHSTDIN, TOAUTH or die "unable to create pipes"; |
296 | pipe FROMAUTH, AUTHSTDOUT or die "unable to create pipes"; |
297 | $pid = fork; |
298 | if (!defined $pid) { |
299 | die "unable to fork for authentication helper"; |
300 | } elsif ($pid == 0) { |
301 | # we are child |
302 | $ENV{"NNTP_AUTH_FDS"} = "0.1"; |
303 | open STDIN, "<&AUTHSTDIN"; |
304 | open STDOUT, ">&AUTHSTDOUT"; |
305 | close S; |
306 | exec $auth; |
307 | } |
308 | # we are parent |
309 | close AUTHSTDIN; |
310 | close AUTHSTDOUT; |
311 | autoflush TOAUTH 1; |
312 | &getline; print TOAUTH "$_\n"; |
313 | while (<FROMAUTH>) { |
314 | s/[\r\n]*$//s; |
315 | &putline($_); |
316 | &getline; |
317 | print TOAUTH "$_\n"; |
318 | } |
319 | die "failed authentication\n" unless $? == 0; |
320 | } |
321 | } |