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"); |
1a88a03e |
140 | if (defined $art) { |
141 | $art =~ s/\n(>*From )/\n>$1/gs; |
142 | print "From nntpid ".(localtime)."\n".$art."\n"; |
143 | } |
67784f1f |
144 | } |
145 | } elsif ($mode eq 'stdin') { |
146 | while (<>) { |
147 | chomp; |
148 | s/^\s+//; s/\s+$//; # trim whitespace |
149 | &displayarticle($_); |
150 | } |
151 | } elsif ($mode eq 'list') { |
152 | for $item (@list) { |
153 | &displayarticle($item); |
154 | } |
155 | } |
156 | |
157 | sub parsearticle { |
158 | # Article identifiers used as input to this program can be in a |
159 | # variety of formats. This function untangles one into a standard |
160 | # format, which is either (undef, message-id) or (group, article |
161 | # number). In case of parse failure, it returns (undef, undef). |
162 | my $art = shift @_; |
163 | if ($art =~ /^(.*<)?([^<>]*\@[^<>]*)(>.*)?$/) { |
164 | # Anything with an @ sign is treated as a Message-ID. We trim |
165 | # angle brackets and anything outside them. |
166 | return (undef, $2); |
167 | } elsif ($art =~ /^(\S+)(\s+|:)(\d+)$/) { |
168 | # A group name and article number separated by whitespace or a |
169 | # colon. |
170 | return ($1, $3); |
171 | } else { |
172 | # Unable to parse. |
173 | return (undef, undef); |
174 | } |
175 | } |
3096ed75 |
176 | |
67784f1f |
177 | sub displayarticle { |
178 | my $mid = shift @_; |
3096ed75 |
179 | |
67784f1f |
180 | &connect; |
52f9a468 |
181 | |
67784f1f |
182 | my $art = &getart($mid); |
1a88a03e |
183 | return unless defined $art; |
3096ed75 |
184 | |
67784f1f |
185 | if ($pager and -t STDOUT) { |
186 | # Close the NNTP connection before invoking the pager, in case the |
187 | # user spends so long looking at the article that the server times |
188 | # us out. |
189 | &disconnect; |
190 | |
191 | $pagername = $ENV{"PAGER"}; |
192 | $pagername = "more" unless defined $pagername; |
193 | open PAGER, "| $pagername"; |
194 | print PAGER $art; |
195 | close PAGER; |
196 | } else { |
197 | print $art; |
198 | } |
52f9a468 |
199 | } |
200 | |
201 | sub getart { |
67784f1f |
202 | my $art = shift @_; |
203 | my $group; |
204 | my $mid; |
205 | |
206 | ($group, $mid) = &parsearticle($art); |
207 | if (!defined $mid) { |
208 | warn "unable to parse '$art'\n"; |
209 | return undef; |
210 | } elsif (defined $group) { |
211 | # This is a (group, article number) pair. |
212 | &docmd("GROUP $group"); |
213 | $ret = &docmd("ARTICLE $mid"); |
214 | } else { |
215 | # This is a Message-Id. Some NNTP servers will insist on having |
216 | # seen a GROUP command before 'ARTICLE <some.random@message.id>', |
217 | # so ensure we've sent one. |
218 | &docmd("GROUP misc.misc") unless $in_a_group; |
219 | $ret = &docmd("ARTICLE <$mid>"); |
220 | } |
221 | |
52f9a468 |
222 | return undef if !defined $ret; |
67784f1f |
223 | $in_a_group = 1; |
224 | |
52f9a468 |
225 | $art = ""; |
226 | while (1) { |
227 | &getline; |
228 | s/[\r\n]//g; |
229 | last if /^\.$/; |
230 | s/^\.//; |
231 | $art .= "$_\n"; |
232 | } |
233 | return $art; |
a5d9e758 |
234 | } |
235 | |
3096ed75 |
236 | sub putline { |
237 | my ($line) = @_; |
238 | print STDERR ">>> $line\n" if $verbose; |
239 | print S "$line\r\n"; |
240 | } |
241 | |
242 | sub getline { |
243 | $_ = <S>; |
244 | s/[\r\n]*$//s; |
245 | $code = substr($_,0,3); |
246 | print STDERR "<<< $_\n" if $verbose; |
52f9a468 |
247 | return substr($_,4); |
3096ed75 |
248 | } |
249 | |
67784f1f |
250 | sub connect { |
251 | return if $connected; |
252 | socket(S,PF_INET,SOCK_STREAM,$proto) or die "socket: $!"; |
253 | connect(S,$paddr) or die "connect: $!"; |
254 | |
255 | S->autoflush(1); |
256 | |
257 | $fatal = 1; # most errors need to be fatal |
258 | |
259 | &getline; |
260 | $code =~ /^2\d\d/ or die "no initial greeting from server\n"; |
261 | |
262 | &docmd("MODE READER"); |
263 | |
264 | $connected = 1; |
265 | $in_a_group = 0; |
266 | } |
267 | |
268 | sub disconnect { |
269 | &docmd("QUIT"); |
270 | close S; |
271 | $connected = 0; |
272 | } |
273 | |
3096ed75 |
274 | sub docmd { |
275 | my ($cmd) = @_; |
f42a91ed |
276 | # We go at most twice round the following loop. If the first attempt |
277 | # to fetch the article fails with a 480 response, we try again |
278 | # having authenticated first; but if the second attempt also fails |
279 | # with 480, then the authentication didn't work, so we should give |
280 | # up rather than try it pointlessly again. |
281 | for my $n (0,1) { |
3096ed75 |
282 | &putline($cmd); |
52f9a468 |
283 | $line = &getline; |
3096ed75 |
284 | if ($code eq "480") { &auth; } else { last; } |
285 | } |
52f9a468 |
286 | if ($code !~ /^2\d\d/) { |
287 | die "failed on `$cmd':\n$_\n" if $fatal; |
288 | return undef; |
289 | } |
290 | return $line; |
3096ed75 |
291 | } |
292 | |
293 | sub auth { |
294 | # Authentication. |
295 | if ($ENV{"NNTPAUTH"}) { |
296 | $auth = $ENV{"NNTPAUTH"}; |
297 | &putline("AUTHINFO GENERIC $auth"); |
298 | pipe AUTHSTDIN, TOAUTH or die "unable to create pipes"; |
299 | pipe FROMAUTH, AUTHSTDOUT or die "unable to create pipes"; |
300 | $pid = fork; |
301 | if (!defined $pid) { |
302 | die "unable to fork for authentication helper"; |
303 | } elsif ($pid == 0) { |
304 | # we are child |
305 | $ENV{"NNTP_AUTH_FDS"} = "0.1"; |
306 | open STDIN, "<&AUTHSTDIN"; |
307 | open STDOUT, ">&AUTHSTDOUT"; |
308 | close S; |
309 | exec $auth; |
310 | } |
311 | # we are parent |
312 | close AUTHSTDIN; |
313 | close AUTHSTDOUT; |
314 | autoflush TOAUTH 1; |
315 | &getline; print TOAUTH "$_\n"; |
316 | while (<FROMAUTH>) { |
317 | s/[\r\n]*$//s; |
318 | &putline($_); |
319 | &getline; |
320 | print TOAUTH "$_\n"; |
321 | } |
322 | die "failed authentication\n" unless $? == 0; |
323 | } |
324 | } |