X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ian/git?a=blobdiff_plain;f=scripts%2Fnntpid;h=0b2919a559a80ef40f10e2152d7176ee9024746f;hb=30cbd6a4e31c37e9726f1d6f1eaab7a60ba7aa76;hp=be961eee6431ca79d979c7750b0a5f4a538341a3;hpb=7fabdc93b382e25ce1d8cc355781e4a56880f335;p=chiark-utils.git diff --git a/scripts/nntpid b/scripts/nntpid index be961ee..0b2919a 100755 --- a/scripts/nntpid +++ b/scripts/nntpid @@ -2,34 +2,42 @@ # Originally by Simon Tatham # Modified by Richard Kettlewell, Colin Watson, Ian Jackson +# +# Copyright -2011 Simon Tatham +# Copyright 2011 Richard Kettlewell +# Copyright 2011 Colin Watson +# Copyright 2011 Ian Jackson +# +# Permission is hereby granted, free of charge, to any person obtaining a +# copy of this software and associated documentation files (the "Software"), +# to deal in the Software without restriction, including without limitation +# the rights to use, copy, modify, merge, publish, distribute, sublicense, +# and/or sell copies of the Software, and to permit persons to whom the +# Software is furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in +# all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +# SOFTWARE IN THE PUBLIC INTEREST, INC. BE LIABLE FOR ANY CLAIM, DAMAGES OR +# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, +# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +# DEALINGS IN THE SOFTWARE. + +use Chiark::NNTP; + +our $verbose; +($verbose='STDERR', shift @ARGV) if $ARGV[0] eq "-v"; + +my $c = cnntp_connect($verbose); +$c->banner_reader(); + +our $code; -require 5.002; -use Socket; -use FileHandle; - -$verbose=1, shift @ARGV if $ARGV[0] eq "-v"; - -$ns=$ENV{'NNTPSERVER'}; -if (!defined $ns or !length $ns) { - $ns = `cat /etc/nntpserver`; - chomp($ns); -} -$port = (getservbyname("nntp", "tcp"))[2]; -$ns = inet_aton($ns); -$proto = getprotobyname("tcp"); -$paddr = sockaddr_in($port, $ns); - -socket(S,PF_INET,SOCK_STREAM,$proto) or die "socket: $!"; -connect(S,$paddr) or die "connect: $!"; - -S->autoflush(1); - -&getline; -$code =~ /^2\d\d/ or die "no initial greeting from server\n"; - -&docmd("MODE READER"); # some servers require a GROUP before an ARTICLE command -&docmd("GROUP misc.misc"); +$c->docmd("GROUP misc.misc"); if(@ARGV == 0) { while(<>) { @@ -49,7 +57,7 @@ if(@ARGV == 0) { } } -&docmd("QUIT"); +$c->docmd("QUIT"); close S; sub lookup { @@ -57,12 +65,12 @@ sub lookup { if($mid !~ /\@/ and $mid =~ /^(.*)[: ](\d+)$/) { my ($g, $n) = ($1, $2); - &docmd("GROUP $g"); - &docmd("ARTICLE $n"); + $c->docmd("GROUP $g"); + $c->docmd("ARTICLE $n"); } else { $mid =~ s/.*\.*//; - &docmd("ARTICLE <$mid>"); + $c->docmd("ARTICLE <$mid>"); } my $fh= 'STDOUT'; @@ -74,7 +82,7 @@ sub lookup { } while (1) { - &getline; + ($code,$_) = $c->getline(); s/[\r\n]//g; last if /^\.$/; s/^\.//; @@ -85,59 +93,3 @@ sub lookup { close $fh or die "$? $!"; } } - -sub putline { - my ($line) = @_; - print STDERR ">>> $line\n" if $verbose; - print S "$line\r\n"; -} - -sub getline { - $_ = ; - s/[\r\n]*$//s; - $code = substr($_,0,3); - print STDERR "<<< $_\n" if $verbose; -} - -sub docmd { - my ($cmd) = @_; - for my $n (0,1) { - &putline($cmd); - &getline; - if ($code eq "480") { &auth; } else { last; } - } - $code =~ /^2\d\d/ or die "failed on `$cmd':\n$_\n"; -} - -sub auth { - # Authentication. - if ($ENV{"NNTPAUTH"}) { - $auth = $ENV{"NNTPAUTH"}; - &putline("AUTHINFO GENERIC $auth"); - pipe AUTHSTDIN, TOAUTH or die "unable to create pipes"; - pipe FROMAUTH, AUTHSTDOUT or die "unable to create pipes"; - $pid = fork; - if (!defined $pid) { - die "unable to fork for authentication helper"; - } elsif ($pid == 0) { - # we are child - $ENV{"NNTP_AUTH_FDS"} = "0.1"; - open STDIN, "<&AUTHSTDIN"; - open STDOUT, ">&AUTHSTDOUT"; - close S; - exec $auth; - } - # we are parent - close AUTHSTDIN; - close AUTHSTDOUT; - autoflush TOAUTH 1; - &getline; print TOAUTH "$_\n"; - while () { - s/[\r\n]*$//s; - &putline($_); - &getline; - print TOAUTH "$_\n"; - } - die "failed authentication\n" unless $? == 0; - } -}