File: delayer.in

package info (click to toggle)
inn2 2.5.4-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 11,720 kB
  • ctags: 8,983
  • sloc: ansic: 92,499; sh: 13,509; perl: 12,921; makefile: 2,985; yacc: 842; python: 342; lex: 255
file content (69 lines) | stat: -rw-r--r-- 1,380 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/usr/bin/perl
# -*- perl -*-
#
# delay lines for N seconds.
#
# primarily meant to be used with INN to generate a delayed feed with innfeed.
#
# put it into your newsfeeds file like
#
# innfeed-delayed!\
#        :!*\
#        :Tc,Wnm*,S16384:/usr/local/news/bin/delayer 60 \
#		/usr/local/news/bin/innfeed -c innfeed-delayed.conf
#
# done by christian mock <cm@tahina.priv.at> sometime in july 1998,
# and put into the public domain.
#
$delay = shift || die "usage: $0 delay prog-n-args\n";

$timeout = $delay;
$eof = 0;

open(OUT, "|" . join(" ", @ARGV)) || die "open |prog-n-args: $!\n";

#select(OUT);
#$| = 1;
#select(STDOUT);

$rin = '';
vec($rin,fileno(STDIN),1) = 1;

while(!$eof || $#queue >= 0) {
    if(!$eof) {
	($nfound,$timeleft) =
	    select($rout=$rin, undef, undef, $timeout);
    } else {
	sleep($timeout);
    }
    $now = time(); $exp = $now + $delay;

    if(!$eof && vec($rout,fileno(STDIN),1)) {
	$line = <STDIN>;
	if(!defined $line) {	# exit NOW!
	    foreach(@queue) {
		s/^[^:]+://g;
		print OUT;
	    }
	    close(OUT);
	    sleep(1);
	    exit;
	}
	push(@queue, "$exp:$line");
    }

    if($#queue < 0) {
	undef $timeout;
	next;
    }

    ($first, $line) = split(/:/, $queue[0], 2);
    while($#queue >= 0 && $first <= $now) {
	print OUT $line;
	shift(@queue);
	($first, $line) = split(/:/, $queue[0], 2);
    }
    $timeout = $first - $now;
	
}