| 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 | # or: nntpid -a news.group.name (download all available articles) |
| 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,2004 Simon Tatham. All rights reserved. |
| 20 | |
| 21 | require 5.002; |
| 22 | use Socket; |
| 23 | use FileHandle; |
| 24 | |
| 25 | $usage = |
| 26 | "usage: nntpid [ -v ] [ -d ] <message-id>\n" . |
| 27 | " or: nntpid [ -v ] [ -d ] <newsgroup> <article-number>\n" . |
| 28 | " or: nntpid [ -v ] -a <newsgroup>\n" . |
| 29 | "where: -v verbose (print interaction with news server)\n" . |
| 30 | " -d direct output (don't consider using PAGER)\n" . |
| 31 | " -a dump all articles in group to stdout as mbox\n" . |
| 32 | " also: nntpid --version report version number\n" . |
| 33 | " nntpid --help display this help text\n" . |
| 34 | " nntpid --licence display (MIT) licence text\n"; |
| 35 | |
| 36 | $licence = |
| 37 | "nntpid is copyright 2000,2004 Simon Tatham.\n" . |
| 38 | "\n" . |
| 39 | "Permission is hereby granted, free of charge, to any person\n" . |
| 40 | "obtaining a copy of this software and associated documentation files\n" . |
| 41 | "(the \"Software\"), to deal in the Software without restriction,\n" . |
| 42 | "including without limitation the rights to use, copy, modify, merge,\n" . |
| 43 | "publish, distribute, sublicense, and/or sell copies of the Software,\n" . |
| 44 | "and to permit persons to whom the Software is furnished to do so,\n" . |
| 45 | "subject to the following conditions:\n" . |
| 46 | "\n" . |
| 47 | "The above copyright notice and this permission notice shall be\n" . |
| 48 | "included in all copies or substantial portions of the Software.\n" . |
| 49 | "\n" . |
| 50 | "THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND,\n" . |
| 51 | "EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF\n" . |
| 52 | "MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND\n" . |
| 53 | "NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS\n" . |
| 54 | "BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN\n" . |
| 55 | "ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\n" . |
| 56 | "CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n" . |
| 57 | "SOFTWARE.\n"; |
| 58 | |
| 59 | $pager = 1; |
| 60 | $verbose = 0; |
| 61 | $all = 0; |
| 62 | |
| 63 | while ($ARGV[0] =~ /^-(.+)$/) { |
| 64 | shift @ARGV; |
| 65 | $verbose = 1, next if $1 eq "v"; |
| 66 | $pager = 0, next if $1 eq "d"; |
| 67 | $all = 1, next if $1 eq "a"; |
| 68 | if ($1 eq "-help") { |
| 69 | print STDERR $usage; |
| 70 | exit 0; |
| 71 | } elsif ($1 eq "-version") { |
| 72 | if ('$Revision$' =~ /Revision:\s+(\d+)/) { |
| 73 | print "nntpid revision $1\n"; |
| 74 | } else { |
| 75 | print "nntpid: unknown revision\n"; |
| 76 | } |
| 77 | exit 0; |
| 78 | } elsif ($1 eq "-licence" or $1 eq "-license") { |
| 79 | print $licence; |
| 80 | exit 0; |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | die $usage if !defined $ARGV[0]; |
| 85 | |
| 86 | if ($all) { |
| 87 | $group = $ARGV[0]; |
| 88 | } elsif (defined $ARGV[1]) { |
| 89 | $group = $ARGV[0]; |
| 90 | $mid = $ARGV[1]; |
| 91 | } else { |
| 92 | $group = "misc.misc"; |
| 93 | $mid = $ARGV[0]; |
| 94 | $mid =~ s/^<//; |
| 95 | $mid =~ s/>$//; |
| 96 | $mid = "<$mid>"; |
| 97 | } |
| 98 | |
| 99 | $ns=$ENV{'NNTPSERVER'}; |
| 100 | if (!defined $ns or !length $ns) { |
| 101 | $ns = `cat /etc/nntpserver`; |
| 102 | chomp $ns; |
| 103 | } |
| 104 | $port = (getservbyname("nntp", "tcp"))[2]; |
| 105 | $ns = inet_aton($ns); |
| 106 | $proto = getprotobyname("tcp"); |
| 107 | $paddr = sockaddr_in($port, $ns); |
| 108 | |
| 109 | socket(S,PF_INET,SOCK_STREAM,$proto) or die "socket: $!"; |
| 110 | connect(S,$paddr) or die "connect: $!"; |
| 111 | |
| 112 | S->autoflush(1); |
| 113 | |
| 114 | $fatal = 1; # most errors need to be fatal |
| 115 | |
| 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 |
| 121 | $numbers = &docmd("GROUP $group"); |
| 122 | if ($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 | } |
| 130 | } else { |
| 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 | |
| 146 | sub 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; |
| 159 | } |
| 160 | |
| 161 | sub putline { |
| 162 | my ($line) = @_; |
| 163 | print STDERR ">>> $line\n" if $verbose; |
| 164 | print S "$line\r\n"; |
| 165 | } |
| 166 | |
| 167 | sub getline { |
| 168 | $_ = <S>; |
| 169 | s/[\r\n]*$//s; |
| 170 | $code = substr($_,0,3); |
| 171 | print STDERR "<<< $_\n" if $verbose; |
| 172 | return substr($_,4); |
| 173 | } |
| 174 | |
| 175 | sub docmd { |
| 176 | my ($cmd) = @_; |
| 177 | # We go at most twice round the following loop. If the first attempt |
| 178 | # to fetch the article fails with a 480 response, we try again |
| 179 | # having authenticated first; but if the second attempt also fails |
| 180 | # with 480, then the authentication didn't work, so we should give |
| 181 | # up rather than try it pointlessly again. |
| 182 | for my $n (0,1) { |
| 183 | &putline($cmd); |
| 184 | $line = &getline; |
| 185 | if ($code eq "480") { &auth; } else { last; } |
| 186 | } |
| 187 | if ($code !~ /^2\d\d/) { |
| 188 | die "failed on `$cmd':\n$_\n" if $fatal; |
| 189 | return undef; |
| 190 | } |
| 191 | return $line; |
| 192 | } |
| 193 | |
| 194 | sub auth { |
| 195 | # Authentication. |
| 196 | if ($ENV{"NNTPAUTH"}) { |
| 197 | $auth = $ENV{"NNTPAUTH"}; |
| 198 | &putline("AUTHINFO GENERIC $auth"); |
| 199 | pipe AUTHSTDIN, TOAUTH or die "unable to create pipes"; |
| 200 | pipe FROMAUTH, AUTHSTDOUT or die "unable to create pipes"; |
| 201 | $pid = fork; |
| 202 | if (!defined $pid) { |
| 203 | die "unable to fork for authentication helper"; |
| 204 | } elsif ($pid == 0) { |
| 205 | # we are child |
| 206 | $ENV{"NNTP_AUTH_FDS"} = "0.1"; |
| 207 | open STDIN, "<&AUTHSTDIN"; |
| 208 | open STDOUT, ">&AUTHSTDOUT"; |
| 209 | close S; |
| 210 | exec $auth; |
| 211 | } |
| 212 | # we are parent |
| 213 | close AUTHSTDIN; |
| 214 | close AUTHSTDOUT; |
| 215 | autoflush TOAUTH 1; |
| 216 | &getline; print TOAUTH "$_\n"; |
| 217 | while (<FROMAUTH>) { |
| 218 | s/[\r\n]*$//s; |
| 219 | &putline($_); |
| 220 | &getline; |
| 221 | print TOAUTH "$_\n"; |
| 222 | } |
| 223 | die "failed authentication\n" unless $? == 0; |
| 224 | } |
| 225 | } |