Ticket #22: sendinpaths.pl

File sendinpaths.pl, 2.7 KB (added by eagle, 14 years ago)

Perl version of sendinpaths

Line 
1#!/usr/bin/perl
2#
3# replacement for the sendinpaths.sh shell script.
4# assumes you already have ninpaths installed in $inn::pathbin
5#
6
7require '/news/lib/innshellvars.pl';
8
9use Getopt::Std;
10
11$pathhost    = "$inn::pathhost";
12$ninpaths    = "$inn::pathbin/ninpaths";
13$ninpathsdir = "$inn::pathlog/path";
14$reportdays  = 30;
15$keepdays    = 14;
16$emailto     = "top1000\@anthologeek.net";
17$ccto        = "usenet";
18$sendmail    = "/usr/sbin/sendmail";
19$debug       = 0;
20
21sub usage {
22        print <<_end_;
23Usage:
24        $0 [-dnh] [email]
25
26        If called without any arguments, reports are generated and auto
27        submitted to the respective inpaths accumulation site.
28
29        -d:     enable debug messages
30        -n:     gathers stats, but does not auto-submit emails
31        -h:     this help message
32
33        current default submit address: [$emailto]
34        the optional argument [email] may be used to override this default.
35_end_
36        exit(1);
37}
38
39sub main {
40        my (@files, @validfiles, @oldfiles);
41        my ($file, $path, $appendargs, $sendout, $cmd);
42
43        getopts('dnh') || &usage;
44        &usage if ( $opt_h );
45
46        # if we took an email argument
47        $emailto = $ARGV[0] if ( $ARGV[0] );
48
49        # set debug
50        $debug = 1 if ( $opt_d );
51
52        # make it simple
53        $path = $ninpathsdir;
54
55        # we'll need to scan the ninpaths directory
56        opendir(DIR, $path) || die "can't open $path, $!\n";
57        @files = readdir(DIR);
58        closedir(DIR);
59
60        foreach $file (@files) {
61                next if ( ! -f "$path/$file" );
62
63                # get a listing of all the valid files to process
64                if ( -s _ && int(-M _) < $reportdays ) {
65                        push @validfiles, $file;
66                }
67
68                # now get the list of all files that will be removed
69                if ( int(-M _) > $keepdays ) {
70                        push @oldfiles, $file;
71                }
72        }
73
74        chdir($ninpathsdir);
75
76        # process each dump
77        foreach $file (@validfiles) {
78                $cmd = "$ninpaths -u $file -r $pathhost";
79
80                printf(STDERR "execing %s\n", $cmd) if ( $debug );
81
82                open(NINPATHS, "$cmd |") || next;
83                while (<NINPATHS>) {
84                        ;
85                }
86                close(NINPATHS);
87
88                if ( $? == 0 ) {
89                        $appendargs .= " -u $file";
90                }
91        }
92
93        # prepare to send reports, and purge old entries from disk
94        $cmd = "$ninpaths $appendargs -r $pathhost";
95
96        printf(STDERR "execing %s\n", $cmd) if ( $debug );
97
98        open(NINPATHS, "$cmd |");
99        while (<NINPATHS>) {
100                $sendout .= $_;
101        }
102        close(NINPATHS);
103
104        if ( $opt_n ) {
105                # we are not sending this report anywhere, but to stdout
106                print $sendout;
107        } else {
108                open(SENDMAIL, "| $sendmail -oi -t");
109                print SENDMAIL "To: ", $emailto, "\n";
110                print SENDMAIL "Cc: ", $ccto, "\n";
111                print SENDMAIL "Subject: inpaths ", $pathhost, "\n";
112                print SENDMAIL "\n";
113                print SENDMAIL $sendout;
114                print SENDMAIL "\n";
115                close(SENDMAIL);
116
117                # now remove old dumps
118                foreach $file (@oldfiles) {
119                        printf(STDERR "unlinking %s\n", $file) if ( $debug );
120                        unlink($file);
121                }
122        }
123
124        return 0;
125}
126
127&main;
128
1290;