It's about time I brought my `nntpid' utility under version control,
[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)
9#
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.
12#
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.
17
18# Copyright 2000 Simon Tatham. All rights reserved.
19# FIXME: put in a licence notice.
20
21require 5.002;
22use Socket;
23use FileHandle;
24
25$usage =
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";
32
33$licence =
34 "nntpid is copyright 2000,2004 Simon Tatham.\n" .
35 "\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" .
43 "\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" .
46 "\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" .
54 "SOFTWARE.\n";
55
56while ($ARGV[0] =~ /^-(.+)$/) {
57 shift @ARGV;
58 $verbose=1, next if $1 eq "v";
59 if ($1 eq "-help") {
60 print STDERR $usage;
61 exit 0;
62 } elsif ($1 eq "-version") {
63 if ('$Revision$' =~ /Revision:\s+(\d+)/) {
64 print "nntpid revision $1\n";
65 } else {
66 print "nntpid: unknown revision\n";
67 }
68 exit 0;
69 } elsif ($1 eq "-licence" or $1 eq "-license") {
70 print $licence;
71 exit 0;
72 }
73}
74
75die $usage if !defined $ARGV[0];
76
77if (defined $ARGV[1]) {
78 $group = $ARGV[0];
79 $mid = $ARGV[1];
80} else {
81 $group = "misc.misc";
82 $mid = $ARGV[0];
83 $mid =~ s/^<//;
84 $mid =~ s/>$//;
85 $mid = "<$mid>";
86}
87
88$ns=$ENV{'NNTPSERVER'};
89if (!defined $ns or !length $ns) {
90 $ns = `cat /etc/nntpserver`;
91}
92$port = (getservbyname("nntp", "tcp"))[2];
93$ns = inet_aton($ns);
94$proto = getprotobyname("tcp");
95$paddr = sockaddr_in($port, $ns);
96
97socket(S,PF_INET,SOCK_STREAM,$proto) or die "socket: $!";
98connect(S,$paddr) or die "connect: $!";
99
100S->autoflush(1);
101
102&getline;
103$code =~ /^2\d\d/ or die "no initial greeting from server\n";
104
105&docmd("MODE READER");
106# some servers require a GROUP before an ARTICLE command
107&docmd("GROUP $group");
108&docmd("ARTICLE $mid");
109while (1) {
110 &getline;
111 s/[\r\n]//g;
112 last if /^\.$/;
113 s/^\.//;
114 print STDOUT "$_\n";
115}
116&docmd("QUIT");
117close S;
118
119sub putline {
120 my ($line) = @_;
121 print STDERR ">>> $line\n" if $verbose;
122 print S "$line\r\n";
123}
124
125sub getline {
126 $_ = <S>;
127 s/[\r\n]*$//s;
128 $code = substr($_,0,3);
129 print STDERR "<<< $_\n" if $verbose;
130}
131
132sub docmd {
133 my ($cmd) = @_;
134 while (1) {
135 &putline($cmd);
136 &getline;
137 if ($code eq "480") { &auth; } else { last; }
138 }
139 $code =~ /^2\d\d/ or die "failed on `$cmd':\n$_\n";
140}
141
142sub auth {
143 # Authentication.
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";
149 $pid = fork;
150 if (!defined $pid) {
151 die "unable to fork for authentication helper";
152 } elsif ($pid == 0) {
153 # we are child
154 $ENV{"NNTP_AUTH_FDS"} = "0.1";
155 open STDIN, "<&AUTHSTDIN";
156 open STDOUT, ">&AUTHSTDOUT";
157 close S;
158 exec $auth;
159 }
160 # we are parent
161 close AUTHSTDIN;
162 close AUTHSTDOUT;
163 autoflush TOAUTH 1;
164 &getline; print TOAUTH "$_\n";
165 while (<FROMAUTH>) {
166 s/[\r\n]*$//s;
167 &putline($_);
168 &getline;
169 print TOAUTH "$_\n";
170 }
171 die "failed authentication\n" unless $? == 0;
172 }
173}