blib/lib/Bot/BasicBot/Pluggable/Module/Crontab.pm

filter: Statementstmt 100% 187 / 187 Branchbran 56.3% 71 / 126 Conditioncond 23.6% 17 / 72 MC/DCmcdc 0.0% 0 / 30 Subroutinesub 100% 30 / 30 Podpod 100% 8 / 8 totaltotal 69.0% 313 / 453 CC 109 SCAR 25.7
CC
109
Cov
71%
tick
21.3
tick
21.3
tick
21.3
CRAP 13.1
↑ Summary
1 package Bot::BasicBot::Pluggable::Module::Crontab;
2  
3 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 7 our $VERSION = '1.01';
7
8 #----------------------------------------------------------------------------
9  
10 #############################################################################
11 #Library Modules #
12 #############################################################################
13  
14 6 ▶use base qw(Bot::BasicBot::Pluggable::Module);
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 6 ▶use DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
17 6 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
18 6 ▶use Time::Crontab;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 #############################################################################
21 #Variables
22 #############################################################################
23  
24 7 my @crontab;
25 7 my $load_time = 0;
26  
27 #----------------------------------------------------------------------------
28  
29 #############################################################################
30 # Public Methods #
31 #############################################################################
32  
33 1 ▶sub init {
Subroutine init: called CC=2 SCAR=7.0
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
34 1 my $self = shift;
35  
36 1 my $file = $self->store->get( 'crontab', 'file' );
37 1 ▶ unless($file) {
Branch: 6 branches
BranchTrueFalseTotal
unless ($file) 0 0 0
unless ($file) 1 0 1
unless ($file) 0 1 1
unless ($file) 0 1 1
unless ($file) 1 0 1
unless ($file) 4 1 5
38 1 $file = $0;
39 1 $file =~ s/\.pl$/.cron/;
40 }
41  
42 1 $self->store->set( 'crontab', 'file', $file );
43 }
44
45 2 ▶sub help {
Subroutine help: called CC=1 SCAR=0.0
Pod documented
46 2 return "Posts messages to specific channels based on a crontab.";
47 }
48
49 2 ▶sub tick {
Subroutine tick: called CC=8 SCAR=30.6
Pod documented
50 2 my $self = shift;
51  
52 2 $self->_load_cron();
53 2 my $wkno = DateTime->now->week_number;
54  
55 2 for my $cron (@crontab) {
56 2 ▶ next unless($cron->{tab}->match(time));
Branch: 6 branches
BranchTrueFalseTotal
unless $cron->{"tab"}->match(time) 1 0 1
unless $cron->{"tab"}->match(time) 2 2 4
unless $cron->{"tab"}->match(time) 0 1 1
unless $cron->{"tab"}->match(time) 0 1 1
unless $cron->{"tab"}->match(time) 0 1 1
unless $cron->{"tab"}->match(time) 1 0 1
57 next unless(
58 $cron->{weekno} eq '*'
59 || ( $cron->{modulus} && $cron->{result} == ($wkno % $cron->{modulus}) )
60 || ( $cron->{weekno} =~ /^\d+$/ && $wkno == $cron->{weekno} )
61 2 ▶ );
Branch: 6 branches
BranchTrueFalseTotal
unless $cron->{"weekno"} eq "*" or $cron->{"modulus"} and $cron->{"result"} == $wkno % $cron->{"modulus"} or $cron->{"weekno"} =~ /^\d+$/ and $wkno == $cron->{"weekno"} 0 1 1
unless $cron->{"weekno"} eq "*" or $cron->{"modulus"} and $cron->{"result"} == $wkno % $cron->{"modulus"} or $cron->{"weekno"} =~ /^\d+$/ and $wkno == $cron->{"weekno"} 1 1 2
unless $cron->{"weekno"} eq "*" or $cron->{"modulus"} and $cron->{"result"} == $wkno % $cron->{"modulus"} or $cron->{"weekno"} =~ /^\d+$/ and $wkno == $cron->{"weekno"} 0 1 1
unless $cron->{"weekno"} eq "*" or $cron->{"modulus"} and $cron->{"result"} == $wkno % $cron->{"modulus"} or $cron->{"weekno"} =~ /^\d+$/ and $wkno == $cron->{"weekno"} 1 0 1
unless $cron->{"weekno"} eq "*" or $cron->{"modulus"} and $cron->{"result"} == $wkno % $cron->{"modulus"} or $cron->{"weekno"} =~ /^\d+$/ and $wkno == $cron->{"weekno"} 0 1 1
unless $cron->{"weekno"} eq "*" or $cron->{"modulus"} and $cron->{"result"} == $wkno % $cron->{"modulus"} or $cron->{"weekno"} =~ /^\d+$/ and $wkno == $cron->{"weekno"} 2 1 3
Condition23%17/72
$cron->{"weekno"} eq "*" or $cron->{"modulus"} and $cron->{"result"} == $wkno % $cron->{"modulus"} or $cron->{"weekno"} =~ /^\d+$/ and $wkno == $cron->{"weekno"}
l!l&&r!l&&!r
000
$cron->{"weekno"} eq "*" or $cron->{"modulus"} and $cron->{"result"} == $wkno % $cron->{"modulus"}
l!l&&r!l&&!r
000
$cron->{"modulus"} and $cron->{"result"} == $wkno % $cron->{"modulus"}
!ll&&!rl&&r
000
$cron->{"weekno"} =~ /^\d+$/ and $wkno == $cron->{"weekno"}
!ll&&!rl&&r
000
$cron->{"weekno"} eq "*" or $cron->{"modulus"} and $cron->{"result"} == $wkno % $cron->{"modulus"} or $cron->{"weekno"} =~ /^\d+$/ and $wkno == $cron->{"weekno"}
l!l&&r!l&&!r
100
$cron->{"weekno"} eq "*" or $cron->{"modulus"} and $cron->{"result"} == $wkno % $cron->{"modulus"}
l!l&&r!l&&!r
100
$cron->{"modulus"} and $cron->{"result"} == $wkno % $cron->{"modulus"}
!ll&&!rl&&r
000
$cron->{"weekno"} =~ /^\d+$/ and $wkno == $cron->{"weekno"}
!ll&&!rl&&r
000
$cron->{"weekno"} eq "*" or $cron->{"modulus"} and $cron->{"result"} == $wkno % $cron->{"modulus"} or $cron->{"weekno"} =~ /^\d+$/ and $wkno == $cron->{"weekno"}
l!l&&r!l&&!r
100
$cron->{"weekno"} eq "*" or $cron->{"modulus"} and $cron->{"result"} == $wkno % $cron->{"modulus"}
l!l&&r!l&&!r
010
$cron->{"modulus"} and $cron->{"result"} == $wkno % $cron->{"modulus"}
!ll&&!rl&&r
001
$cron->{"weekno"} =~ /^\d+$/ and $wkno == $cron->{"weekno"}
!ll&&!rl&&r
000
$cron->{"weekno"} eq "*" or $cron->{"modulus"} and $cron->{"result"} == $wkno % $cron->{"modulus"} or $cron->{"weekno"} =~ /^\d+$/ and $wkno == $cron->{"weekno"}
l!l&&r!l&&!r
001
$cron->{"weekno"} eq "*" or $cron->{"modulus"} and $cron->{"result"} == $wkno % $cron->{"modulus"}
l!l&&r!l&&!r
001
$cron->{"modulus"} and $cron->{"result"} == $wkno % $cron->{"modulus"}
!ll&&!rl&&r
010
$cron->{"weekno"} =~ /^\d+$/ and $wkno == $cron->{"weekno"}
!ll&&!rl&&r
100
$cron->{"weekno"} eq "*" or $cron->{"modulus"} and $cron->{"result"} == $wkno % $cron->{"modulus"} or $cron->{"weekno"} =~ /^\d+$/ and $wkno == $cron->{"weekno"}
l!l&&r!l&&!r
010
$cron->{"weekno"} eq "*" or $cron->{"modulus"} and $cron->{"result"} == $wkno % $cron->{"modulus"}
l!l&&r!l&&!r
001
$cron->{"modulus"} and $cron->{"result"} == $wkno % $cron->{"modulus"}
!ll&&!rl&&r
100
$cron->{"weekno"} =~ /^\d+$/ and $wkno == $cron->{"weekno"}
!ll&&!rl&&r
001
$cron->{"weekno"} eq "*" or $cron->{"modulus"} and $cron->{"result"} == $wkno % $cron->{"modulus"} or $cron->{"weekno"} =~ /^\d+$/ and $wkno == $cron->{"weekno"}
l!l&&r!l&&!r
001
$cron->{"weekno"} eq "*" or $cron->{"modulus"} and $cron->{"result"} == $wkno % $cron->{"modulus"}
l!l&&r!l&&!r
001
$cron->{"modulus"} and $cron->{"result"} == $wkno % $cron->{"modulus"}
!ll&&!rl&&r
100
$cron->{"weekno"} =~ /^\d+$/ and $wkno == $cron->{"weekno"}
!ll&&!rl&&r
010
MC/DC0%0/5
$cron->{"weekno"} eq "*" or $cron->{"modulus"} and $cron->{"result"} == $wkno % $cron->{"modulus"} or $cron->{"weekno"} =~ /^\d+$/ and $wkno == $cron->{"weekno"}
$cron->{"weekno"} eq "*" $cron->{"modulus"} $cron->{"result"} == $wkno % $cron->{"modulus"} $cron->{"weekno"} =~ /^\d+$/ $wkno == $cron->{"weekno"}
MC/DC0%0/5
$cron->{"weekno"} eq "*" or $cron->{"modulus"} and $cron->{"result"} == $wkno % $cron->{"modulus"} or $cron->{"weekno"} =~ /^\d+$/ and $wkno == $cron->{"weekno"}
$cron->{"weekno"} eq "*" $cron->{"modulus"} $cron->{"result"} == $wkno % $cron->{"modulus"} $cron->{"weekno"} =~ /^\d+$/ $wkno == $cron->{"weekno"}
MC/DC0%0/5
$cron->{"weekno"} eq "*" or $cron->{"modulus"} and $cron->{"result"} == $wkno % $cron->{"modulus"} or $cron->{"weekno"} =~ /^\d+$/ and $wkno == $cron->{"weekno"}
$cron->{"weekno"} eq "*" $cron->{"modulus"} $cron->{"result"} == $wkno % $cron->{"modulus"} $cron->{"weekno"} =~ /^\d+$/ $wkno == $cron->{"weekno"}
MC/DC0%0/5
$cron->{"weekno"} eq "*" or $cron->{"modulus"} and $cron->{"result"} == $wkno % $cron->{"modulus"} or $cron->{"weekno"} =~ /^\d+$/ and $wkno == $cron->{"weekno"}
$cron->{"weekno"} eq "*" $cron->{"modulus"} $cron->{"result"} == $wkno % $cron->{"modulus"} $cron->{"weekno"} =~ /^\d+$/ $wkno == $cron->{"weekno"}
MC/DC0%0/5
$cron->{"weekno"} eq "*" or $cron->{"modulus"} and $cron->{"result"} == $wkno % $cron->{"modulus"} or $cron->{"weekno"} =~ /^\d+$/ and $wkno == $cron->{"weekno"}
$cron->{"weekno"} eq "*" $cron->{"modulus"} $cron->{"result"} == $wkno % $cron->{"modulus"} $cron->{"weekno"} =~ /^\d+$/ $wkno == $cron->{"weekno"}
MC/DC0%0/5
$cron->{"weekno"} eq "*" or $cron->{"modulus"} and $cron->{"result"} == $wkno % $cron->{"modulus"} or $cron->{"weekno"} =~ /^\d+$/ and $wkno == $cron->{"weekno"}
$cron->{"weekno"} eq "*" $cron->{"modulus"} $cron->{"result"} == $wkno % $cron->{"modulus"} $cron->{"weekno"} =~ /^\d+$/ $wkno == $cron->{"weekno"}
Truth table0%0/8
A or B and C or D and E
A= $cron->{"weekno"} eq "*"B= $cron->{"modulus"}C= $cron->{"result"} == $wkno % $cron->{"modulus"}D= $cron->{"weekno"} =~ /^\d+$/E= $wkno == $cron->{"weekno"}
ABCDEresult
1XXXX1
011XX1
00X111
010111
00X0X0
00X100
0100X0
010100
Truth table12%1/8
A or B and C or D and E
A= $cron->{"weekno"} eq "*"B= $cron->{"modulus"}C= $cron->{"result"} == $wkno % $cron->{"modulus"}D= $cron->{"weekno"} =~ /^\d+$/E= $wkno == $cron->{"weekno"}
ABCDEresult
1XXXX1
011XX1
00X111
010111
00X0X0
00X100
0100X0
010100
Truth table12%1/8
A or B and C or D and E
A= $cron->{"weekno"} eq "*"B= $cron->{"modulus"}C= $cron->{"result"} == $wkno % $cron->{"modulus"}D= $cron->{"weekno"} =~ /^\d+$/E= $wkno == $cron->{"weekno"}
ABCDEresult
1XXXX1
011XX1
00X111
010111
00X0X0
00X100
0100X0
010100
Truth table12%1/8
A or B and C or D and E
A= $cron->{"weekno"} eq "*"B= $cron->{"modulus"}C= $cron->{"result"} == $wkno % $cron->{"modulus"}D= $cron->{"weekno"} =~ /^\d+$/E= $wkno == $cron->{"weekno"}
ABCDEresult
1XXXX1
011XX1
00X111
010111
00X0X0
00X100
0100X0
010100
Truth table12%1/8
A or B and C or D and E
A= $cron->{"weekno"} eq "*"B= $cron->{"modulus"}C= $cron->{"result"} == $wkno % $cron->{"modulus"}D= $cron->{"weekno"} =~ /^\d+$/E= $wkno == $cron->{"weekno"}
ABCDEresult
1XXXX1
011XX1
00X111
010111
00X0X0
00X100
0100X0
010100
Truth table12%1/8
A or B and C or D and E
A= $cron->{"weekno"} eq "*"B= $cron->{"modulus"}C= $cron->{"result"} == $wkno % $cron->{"modulus"}D= $cron->{"weekno"} =~ /^\d+$/E= $wkno == $cron->{"weekno"}
ABCDEresult
1XXXX1
011XX1
00X111
010111
00X0X0
00X100
0100X0
010100
62  
63 $self->say(
64 channel => $cron->{channel},
65 body => $cron->{message}
66 2 );
67 }
68  
69 2 return 60 - DateTime->now->second; # ensure we are running each minute
70 }
71  
72 #############################################################################
73 # Private Methods #
74 #############################################################################
75  
76 3 ▶sub _load_cron {
Subroutine _load_cron: called CC=11 SCAR=27.8
77 2 my $self = shift;
78  
79 2 ▶ my $fn = $self->store->get( 'crontab', 'file' ) or return 0;
Branch: 5 branches
BranchTrueFalseTotal
my $fn = $self->store->get("crontab", "file") or return 0 0 2 2
my $fn = $self->store->get("crontab", "file") or return 0 1 1 2
my $fn = $self->store->get("crontab", "file") or return 0 1 1 2
my $fn = $self->store->get("crontab", "file") or return 0 0 1 1
my $fn = $self->store->get("crontab", "file") or return 0 0 1 1
80 3 ▶ return 0 unless(-r $fn); # file must be readable
Branch: 5 branches
BranchTrueFalseTotal
unless -r $fn 0 2 2
unless -r $fn 0 1 1
unless -r $fn 0 1 1
unless -r $fn 0 1 1
unless -r $fn 0 1 1
81  
82 3 my $mod = (stat($fn))[9];
83 3 ▶ return 1 if($mod <= $load_time); # don't reload if not modified
Branch: 5 branches
BranchTrueFalseTotal
if $mod <= $load_time 0 2 2
if $mod <= $load_time 0 1 1
if $mod <= $load_time 0 1 1
if $mod <= $load_time 0 1 1
if $mod <= $load_time 0 1 1
84  
85 3 @crontab = ();
86  
87 2 ▶ my $fh = IO::File->new($fn,'r') or die "Cannot load file [$fn]: $!\n";
Branch: 5 branches
BranchTrueFalseTotal
my $fh = "IO::File"->new($fn, "r") or die "Cannot load file [$fn]: $!\n" 0 1 1
my $fh = "IO::File"->new($fn, "r") or die "Cannot load file [$fn]: $!\n" 0 1 1
my $fh = "IO::File"->new($fn, "r") or die "Cannot load file [$fn]: $!\n" 0 1 1
my $fh = "IO::File"->new($fn, "r") or die "Cannot load file [$fn]: $!\n" 0 1 1
my $fh = "IO::File"->new($fn, "r") or die "Cannot load file [$fn]: $!\n" 0 1 1
88 2 ▶ while(<$fh>) {
Branch: 5 branches
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 0 1 1
if (defined($_ = readline $fh)) 1 0 1
if (defined($_ = readline $fh)) 1 0 1
if (defined($_ = readline $fh)) 0 1 1
if (defined($_ = readline $fh)) 0 1 1
89 3 s/\s+$//;
90 3 my $line = $_;
91 3 ▶ next unless($line);
Branch: 4 branches
BranchTrueFalseTotal
unless $line 1 0 1
unless $line 1 0 1
unless $line 1 0 1
unless $line 1 0 1
92  
93 3 ▶ next if($line =~ /^#/); # ignore comment lines
Branch: 4 branches
BranchTrueFalseTotal
if $line =~ /^#/ 0 1 1
if $line =~ /^#/ 0 1 1
if $line =~ /^#/ 0 1 1
if $line =~ /^#/ 0 1 1
94 2 ▶ next if($line =~ /^$/); # ignore blank lines
Branch: 4 branches
BranchTrueFalseTotal
if $line =~ /^$/ 0 1 1
if $line =~ /^$/ 0 1 1
if $line =~ /^$/ 0 1 1
if $line =~ /^$/ 0 1 1
95  
96 2 my @fields = split(/ /,$line,8);
97 3 my $crontab = join(' ',(@fields)[0..4]);
98 3 my $tab;
99 3 eval { $tab = Time::Crontab->new($crontab) };
100 2 ▶ next if($@);
Branch: 4 branches
BranchTrueFalseTotal
if $@ 0 1 1
if $@ 1 0 1
if $@ 1 0 1
if $@ 1 0 1
101  
102 2 my ($modulus,$result);
103 3 ▶ ($modulus,$result) = split(/\//,$fields[5],2) if($fields[5] =~ m!^\d+/\d+!);
Branch: 4 branches
BranchTrueFalseTotal
if $fields[5] =~ m[^\d+/\d+] 1 0 1
if $fields[5] =~ m[^\d+/\d+] 2 1 3
if $fields[5] =~ m[^\d+/\d+] 1 1 2
if $fields[5] =~ m[^\d+/\d+] 1 1 2
104  
105 2 push @crontab, {
106 tab => $tab,
107 weekno => $fields[5],
108 modulus => $modulus,
109 result => $result,
110 channel => $fields[6],
111 message => $fields[7]
112 };
113  
114 1 print "added $crontab $fields[5] = $fields[6] - $fields[7]\n";
115 }
116  
117 2 $fh->close;
118 2 $load_time = $mod;
119 }
120
121
122 7 1;
123
124 __END__
↑ Summary