blib/lib/Circle/Ruleable.pm

filter: Statementstmt 30.8% 37 / 120 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 13 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 52.1% 12 / 23 Podpod 0.0% 0 / 10 totaltotal 24.2% 49 / 202 SCAR 58.9
CC
27
Cov
23%
command_rules_describe
182.0
command_rules_list
30.0
__ANON__
30.0
CRAP 360.1
« blib/lib/Circle/Net/IRC/Target.pm ↑ Summary blib/lib/Circle/Command.pm »
1 # You may distribute under the terms of the GNU General Public License
2 #
3 # (C) Paul Evans, 2008-2014 -- leonerd@leonerd.org.uk
4  
5 package Circle::Ruleable;
6  
7 4 ▶use base qw( Circle::Commandable );
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 sub init_rulestore
13 {
14 0 ▶ my $self = shift;
Subroutine init_rulestore: not called CC=1 SCAR=6.9
Pod undocumented
15 0 my %args = @_;
16  
17 0 $self->{rulestore} = Circle::Rule::Store->new( %args );
18 }
19  
20 sub run_rulechain
21 {
22 0 ▶ my $self = shift;
Subroutine run_rulechain: not called CC=2 SCAR=17.9
Pod undocumented
23 0 my ( $chainname, $event ) = @_;
24  
25 0 ▶ return if eval { $self->{rulestore}->run( $chainname, $event ); 1 };
Branch: 1 branch
BranchTrueFalseTotal
if eval { do { $self->{"rulestore"}->run($chainname, $event); 1 } } 0 0 0
26  
27 0 my $err = $@; chomp $err;
28 0 $self->responderr( "Exception during processing of rulechain '$chainname': $err" );
29 }
30  
31 sub command_rules
32 : Command_description("Display or manipulate action rules")
33 ▶{
Subroutine command_rules: not called CC=1 SCAR=0.0
Pod undocumented
34 # The body doesn't matter as it never gets run
35 4 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
36  
37 sub command_rules_list
38 : Command_description("List the action rules")
39 : Command_subof('rules')
40 : Command_default()
41 : Command_arg('chain?')
42 {
43 0 ▶ my $self = shift;
Subroutine command_rules_list: not called CC=5 SCAR=34.0
Pod undocumented
44 0 my ( $chain, $cinv ) = @_;
45  
46 0 my $rulestore = $self->{rulestore};
47  
48 0 my @chains = $rulestore->chains;
49  
50 0 ▶ if( defined $chain ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $chain) 0 0 0
51 0 ▶ grep { $chain eq $_ } @chains or
Branch: 1 branch
BranchTrueFalseTotal
grep {$chain eq $_;} @chains or return $cinv->responderr("No such rule chain '${chain}'") 0 0 0
52 return $cinv->responderr( "No such rule chain '$chain'" );
53 }
54  
55 0 foreach my $chain ( sort @chains ) {
56 0 $cinv->respond( "Chain '$chain':" );
57 0 my @rules = $rulestore->get_chain( $chain )->deparse_rules();
58 0 $cinv->respond( "$_: $rules[$_]" ) for 0 .. $#rules;
59 }
60  
61 0 return;
62 4 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
63  
64 sub command_rules_add
65 : Command_description("Add a new rule")
66 : Command_subof('rules')
67 : Command_arg('chain')
68 : Command_arg('spec', eatall => 1)
69 {
70 0 ▶ my $self = shift;
Subroutine command_rules_add: not called CC=1 SCAR=6.9
Pod undocumented
71 0 my ( $chain, $spec, $cinv ) = @_;
72  
73 0 my $rulestore = $self->{rulestore};
74  
75 0 $rulestore->get_chain( $chain )->append_rule( $spec );
76  
77 0 $cinv->respond( "Added to chain $chain" );
78 4 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
79  
80 sub command_rules_insert
81 : Command_description("Insert a rule before another rule")
82 : Command_subof('rules')
83 : Command_arg('chain')
84 : Command_arg('index')
85 : Command_arg('spec', eatall => 1)
86 {
87 0 ▶ my $self = shift;
Subroutine command_rules_insert: not called CC=2 SCAR=17.9
Pod undocumented
88 0 my ( $chain, $index, $spec, $cinv ) = @_;
89  
90 0 ▶ $index =~ m/^\d+$/ or
Branch: 1 branch
BranchTrueFalseTotal
$index =~ /^\d+$/ or return $cinv->responderr("Bad index: $index") 0 0 0
91 return $cinv->responderr( "Bad index: $index" );
92  
93 0 my $rulestore = $self->{rulestore};
94  
95 0 $rulestore->get_chain( $chain )->insert_rule( $index, $spec );
96  
97 0 $cinv->respond( "Inserted in $chain before rule $index" );
98 4 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
99  
100 sub command_rules_replace
101 : Command_description("Replace an existing rule with a new one")
102 : Command_subof('rules')
103 : Command_arg('chain')
104 : Command_arg('index')
105 : Command_arg('spec', eatall => 1)
106 {
107 0 ▶ my $self = shift;
Subroutine command_rules_replace: not called CC=1 SCAR=6.9
Pod undocumented
108 0 my ( $chain, $index, $spec, $cinv ) = @_;
109  
110 # We'll do this by inserting our new rule before the one we want to
111 # replace. If it works, delete the old one, which will now be one further
112 # down.
113  
114 0 my $rulestore = $self->{rulestore};
115 0 my $rulechain = $rulestore->get_chain( $chain );
116  
117 0 $rulechain->insert_rule( $index, $spec );
118 0 $rulechain->delete_rule( $index + 1 );
119  
120 0 $cinv->respond( "Replaced $chain rule $index" );
121 4 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
122  
123 sub command_rules_delete
124 : Command_description("Delete a rule")
125 : Command_subof('rules')
126 : Command_arg('chain')
127 : Command_arg('index')
128 {
129 0 ▶ my $self = shift;
Subroutine command_rules_delete: not called CC=2 SCAR=17.9
Pod undocumented
130 0 my ( $chain, $index, $cinv ) = @_;
131  
132 0 ▶ $index =~ m/^\d+$/ or
Branch: 1 branch
BranchTrueFalseTotal
$index =~ /^\d+$/ or return $cinv->responderr("Bad index: $index") 0 0 0
133 return $cinv->responderr( "Bad index: $index" );
134  
135 0 my $rulestore = $self->{rulestore};
136  
137 0 $rulestore->get_chain( $chain )->delete_rule( $index );
138  
139 0 $cinv->respond( "Deleted $chain rule $index" );
140 4 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
141  
142 sub command_rules_describe
143 : Command_description("Describe rule conditions or actions")
144 : Command_subof('rules')
145 : Command_arg('name?')
146 : Command_opt('conds=+', desc => "List conditions")
147 : Command_opt('actions=+', desc => "List actions")
148 {
149 0 ▶ my $self = shift;
Subroutine command_rules_describe: not called CC=13 SCAR=52.0
Pod undocumented
150 0 my ( $name, $opts, $cinv ) = @_;
151  
152 0 my $rulestore = $self->{rulestore};
153  
154 0 my @names;
155
156 0 ▶ if( defined $name ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $name) { } 0 0 0
157 0 @names = ( $name );
158 }
159 else {
160 # List both if neither or both options specified
161 0 ▶ push @names, sort $rulestore->list_conds if !$opts->{actions} or $opts->{conds};
Branch: 1 branch
BranchTrueFalseTotal
if not $opts->{"actions"} or $opts->{"conds"} 0 0 0
Condition0%0/3
not $opts->{"actions"} or $opts->{"conds"}
l!l&&r!l&&!r
000
MC/DC0%0/2
not $opts->{"actions"} or $opts->{"conds"}
not $opts->{"actions"} $opts->{"conds"}
Truth table0%0/3
not(A) or B
A= not $opts->{"actions"}B= $opts->{"conds"}
ABresult
1X1
011
000
162 0 ▶ push @names, sort $rulestore->list_actions if !$opts->{conds} or $opts->{actions};
Branch: 1 branch
BranchTrueFalseTotal
if not $opts->{"conds"} or $opts->{"actions"} 0 0 0
Condition0%0/3
not $opts->{"conds"} or $opts->{"actions"}
l!l&&r!l&&!r
000
MC/DC0%0/2
not $opts->{"conds"} or $opts->{"actions"}
not $opts->{"conds"} $opts->{"actions"}
Truth table0%0/3
not(A) or B
A= not $opts->{"conds"}B= $opts->{"actions"}
ABresult
1X1
011
000
163 }
164  
165 0 for my $name ( @names ) {
166 0 ▶ if( my $attrs = eval { $rulestore->describe_cond( $name ) } ) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $attrs = eval { do { $rulestore->describe_cond($name) } }) { } 0 0 0
elsif ($attrs = eval { do { $rulestore->describe_action($name) } }) { } 0 0 0
167 0 ▶ my $description = $attrs->{desc} || "[has no description]";
Condition0%0/2
$attrs->{'desc'} || '[has no description]'
l!l
00
MC/DC0%0/1
$attrs->{'desc'} || '[has no description]'
$attrs->{'desc'}
Truth table0%0/2
A
A= $attrs->{'desc'}
Aresult
11
00
168 0 $cinv->respond( "Condition '$name': $description" );
169 0 ▶ $cinv->respond( " $name($attrs->{format})" ) if defined $attrs->{format};
Branch: 1 branch
BranchTrueFalseTotal
if defined $attrs->{"format"} 0 0 0
170 }
171 0 elsif( $attrs = eval { $rulestore->describe_action( $name ) } ) {
172 0 ▶ my $description = $attrs->{desc} || "[has no description]";
Condition0%0/2
$attrs->{'desc'} || '[has no description]'
l!l
00
MC/DC0%0/1
$attrs->{'desc'} || '[has no description]'
$attrs->{'desc'}
Truth table0%0/2
A
A= $attrs->{'desc'}
Aresult
11
00
173 0 $cinv->respond( "Action '$name': $description" );
174 0 ▶ $cinv->respond( " $name($attrs->{format})" ) if defined $attrs->{format};
Branch: 1 branch
BranchTrueFalseTotal
if defined $attrs->{"format"} 0 0 0
175 }
176 else {
177 0 $cinv->responderr( "No such condition or action '$name'" );
178 }
179 }
180  
181 0 return;
182 4 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
183  
184 4 ▶use Class::Method::Modifiers qw( install_modifier );
Subroutine BEGIN: called CC=1 SCAR=0.0
185 sub APPLY_Ruleable
186 {
187 8 ▶ my $caller = caller;
Subroutine APPLY_Ruleable: called CC=1 SCAR=5.3
Pod undocumented
188  
189 install_modifier $caller, after => load_configuration => sub {
190 0 ▶ my $self = shift;
Subroutine __ANON__: not called CC=4 SCAR=30.0
191 0 my ( $ynode ) = @_;
192  
193 0 ▶ return unless my $rules_ynode = $ynode->{rules};
Branch: 1 branch
BranchTrueFalseTotal
unless my $rules_ynode = $ynode->{"rules"} 0 0 0
194 0 my $rulestore = $self->{rulestore};
195  
196 0 foreach my $chain ( keys %$rules_ynode ) {
197 0 my $chain_ynode = $rules_ynode->{$chain};
198 0 my $chain = $rulestore->new_chain( $chain ); # or fetch the existing one
199 0 $chain->clear;
200 0 $chain->append_rule( $_ ) for @$chain_ynode;
201 }
202 8 };
203  
204 install_modifier $caller, after => store_configuration => sub {
205 0 ▶ my $self = shift;
Subroutine __ANON__: not called CC=5 SCAR=34.0
206 0 my ( $ynode ) = @_;
207  
208 0 my $rulestore = $self->{rulestore};
209 0 ▶ my $rules_ynode = $ynode->{rules} ||= YAML::Node->new({});
Condition0%0/3
$ynode->{"rules"} ||= "YAML::Node"->new({})
l!l&&r!l&&!r
000
MC/DC0%0/2
$ynode->{"rules"} ||= "YAML::Node"->new({})
$ynode->{"rules"} "YAML::Node"->new({})
Truth table0%0/3
A ||= B
A= $ynode->{"rules"}B= "YAML::Node"->new({})
ABresult
1X1
011
000
210  
211 0 foreach my $chain ( $rulestore->chains ) {
212 0 my $chain_ynode = $rules_ynode->{$chain} = [
213 $rulestore->get_chain( $chain )->deparse_rules(),
214 ];
215 }
216  
217 # Delete any of the old ones
218 0 ▶ $rulestore->get_chain( $_ ) or delete $rules_ynode->{$_} for keys %$rules_ynode;
Branch: 1 branch
BranchTrueFalseTotal
$rulestore->get_chain($_) or delete $rules_ynode->{$_} 0 0 0
219 8 };
220 }
221  
222 4 0x55AA;
« blib/lib/Circle/Net/IRC/Target.pm ↑ Summary blib/lib/Circle/Command.pm »