chiark / gitweb /
fishdescriptor: build system (nugatory)
[chiark-utils.git] / scripts / nntpid
1 #!/usr/bin/perl
2
3 # Originally by Simon Tatham
4 # Modified by Richard Kettlewell, Colin Watson, Ian Jackson
5 #
6 # Copyright -2011 Simon Tatham
7 # Copyright 2011 Richard Kettlewell
8 # Copyright 2011 Colin Watson
9 # Copyright 2011 Ian Jackson
10 #
11 # Permission is hereby granted, free of charge, to any person obtaining a
12 # copy of this software and associated documentation files (the "Software"),
13 # to deal in the Software without restriction, including without limitation
14 # the rights to use, copy, modify, merge, publish, distribute, sublicense,
15 # and/or sell copies of the Software, and to permit persons to whom the
16 # Software is furnished to do so, subject to the following conditions:
17 #
18 # The above copyright notice and this permission notice shall be included in
19 # all copies or substantial portions of the Software.
20
21 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
24 # SOFTWARE IN THE PUBLIC INTEREST, INC. BE LIABLE FOR ANY CLAIM, DAMAGES OR
25 # OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
26 # ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27 # DEALINGS IN THE SOFTWARE.
28
29 use Chiark::NNTP;
30
31 our $verbose;
32 ($verbose='STDERR', shift @ARGV) if $ARGV[0] eq "-v";
33
34 my $c = cnntp_connect($verbose);
35 $c->banner_reader();
36
37 our $code;
38
39 # some servers require a GROUP before an ARTICLE command
40 $c->docmd("GROUP misc.misc");
41
42 if(@ARGV == 0) {
43   while(<>) {
44     s/(^\s+|\s+$)//gs;
45     lookup($_);
46   }
47 } else {
48   while (@ARGV) {
49     my $item = shift @ARGV;
50     if($item !~ /[\@:]/ and not defined $group) {
51       # maybe a bare group followed by an article number
52       die unless @ARGV;
53       my $number = shift @ARGV;
54       $item = "$item $number";
55     }
56     lookup($item);
57   }
58 }
59
60 $c->docmd("QUIT");
61 close S;
62
63 sub lookup {
64   my $mid = shift;
65
66   if($mid !~ /\@/ and $mid =~ /^(.*)[: ](\d+)$/) {
67       my ($g, $n) = ($1, $2);
68       $c->docmd("GROUP $g");
69       $c->docmd("ARTICLE $n");
70   } else {
71       $mid =~ s/.*\<//;
72       $mid =~ s/\>.*//;
73       $c->docmd("ARTICLE <$mid>");
74   }
75
76   my $fh= 'STDOUT';
77   if (-t $fh) {
78     my $lesscmd= $ENV{'NNTPID_PAGER'};
79     $lesscmd= 'less' unless defined $lesscmd;
80     open LESS, "|-", 'sh','-c',$lesscmd or die $!;
81     $fh= 'LESS';
82   }
83   
84   while (1) {
85     ($code,$_) = $c->getline();
86     s/[\r\n]//g;
87     last if /^\.$/;
88     s/^\.//;
89     print $fh "$_\n";
90   }
91
92   if ($fh ne 'STDOUT') {
93     close $fh or die "$? $!";
94   }
95 }