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