chiark / gitweb /
Rerun autoconf2.13 on etch and consequently refresh configure-hostname patch
[inn-innduct.git] / contrib / delayer.in
1 #!/usr/bin/perl
2 # -*- perl -*-
3 #
4 # delay lines for N seconds.
5 #
6 # primarily meant to be used with INN to generate a delayed feed with innfeed.
7 #
8 # put it into your newsfeeds file like
9 #
10 # innfeed-delayed!\
11 #        :!*\
12 #        :Tc,Wnm*,S16384:/usr/local/news/bin/delayer 60 \
13 #               /usr/local/news/bin/startinnfeed -c innfeed-delayed.conf
14 #
15 #
16 #
17 # done by christian mock <cm@tahina.priv.at> sometime in july 1998,
18 # and put into the public domain.
19 #
20 $delay = shift || die "usage: $0 delay prog-n-args\n";
21
22 $timeout = $delay;
23 $eof = 0;
24
25 open(OUT, "|" . join(" ", @ARGV)) || die "open |prog-n-args: $!\n";
26
27 #select(OUT);
28 #$| = 1;
29 #select(STDOUT);
30
31 $rin = '';
32 vec($rin,fileno(STDIN),1) = 1;
33
34 while(!$eof || $#queue >= 0) {
35     if(!$eof) {
36         ($nfound,$timeleft) =
37             select($rout=$rin, undef, undef, $timeout);
38     } else {
39         sleep($timeout);
40     }
41     $now = time(); $exp = $now + $delay;
42
43     if(!$eof && vec($rout,fileno(STDIN),1)) {
44         $line = <STDIN>;
45         if(!defined $line) {    # exit NOW!
46             foreach(@queue) {
47                 s/^[^:]+://g;
48                 print OUT;
49             }
50             close(OUT);
51             sleep(1);
52             exit;
53         }
54         push(@queue, "$exp:$line");
55     }
56
57     if($#queue < 0) {
58         undef $timeout;
59         next;
60     }
61
62     ($first, $line) = split(/:/, $queue[0], 2);
63     while($#queue >= 0 && $first <= $now) {
64         print OUT $line;
65         shift(@queue);
66         ($first, $line) = split(/:/, $queue[0], 2);
67     }
68     $timeout = $first - $now;
69         
70 }
71