blib/lib/Circle/GlobalRules.pm

filter: Statementstmt 17.8% 35 / 196 Branchbran 0.0% 0 / 108 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 38.4% 10 / 26 Podpod 0.0% 0 / 17 totaltotal 12.7% 45 / 352 SCAR 80.4
CC
66
Cov
11%
parse_action_rewrite
110.0
parse_action_format
90.0
eval_action_rewrite
72.0
CRAP 3095.6
« blib/lib/Circle/Net/IRC.pm ↑ Summary blib/lib/Circle/Net/IRC/Channel.pm »
1 # You may distribute under the terms of the GNU General Public License
2 #
3 # (C) Paul Evans, 2008-2012 -- leonerd@leonerd.org.uk
4  
5 package Circle::GlobalRules;
6  
7 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 4 ▶use Text::Balanced qw( extract_delimited extract_quotelike );
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 4 ▶use base qw( Circle::Rule::Store ); # for the attributes
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 sub unquote_qr
15 {
16 0 ▶ my $re = shift;
Subroutine unquote_qr: not called CC=3 SCAR=24.8
Pod undocumented
17  
18 0 $re = "$re";
19  
20 # Perl tries to put (?-xism:RE) around our pattern. Lets attempt to remove
21 # it if we can
22 # Recent perls use (?^:RE) instead
23 0 $re =~ s/^\(\?-xism:(.*)\)$/$1/;
24 0 $re =~ s/^\(\?\^:(.*)\)$/$1/;
25  
26 0 ▶ return ( $2, $1 ) if $re =~ m/^\(\?([ixsm]*)-?[xism]*:(.*)\)$/;
Branch: 1 branch
BranchTrueFalseTotal
if $re =~ /^\(\?([ixsm]*)-?[xism]*:(.*)\)$/ 0 0 0
27 0 ▶ return ( $2, $1 ) if $re =~ m/^\(\?\^([ixsm]*):(.*)\)$/;
Branch: 1 branch
BranchTrueFalseTotal
if $re =~ /^\(\?\^([ixsm]*):(.*)\)$/ 0 0 0
28  
29 # Failed. Lets just be safe then
30 0 return ( $re, "" );
31 }
32  
33 # Not an object class. Instead, just a store of rule subs
34  
35 sub register
36 {
37 3 ▶ my ( $rulestore ) = @_;
Subroutine register: called CC=1 SCAR=0.0
Pod undocumented
38  
39 3 $rulestore->register_cond( matches => __PACKAGE__ );
40  
41 3 $rulestore->register_action( rewrite => __PACKAGE__ );
42 3 $rulestore->register_action( format => __PACKAGE__ );
43 3 $rulestore->register_action( unformat => __PACKAGE__ );
44 3 $rulestore->register_action( level => __PACKAGE__ );
45 }
46  
47 ###### CONDITIONS
48  
49 ### MATCHES
50  
51 sub parse_cond_matches
52 : Rule_description("Look for regexp or substring matches in the text")
53 : Rule_format('/regexp/ or "literal"')
54 {
55 0 ▶ shift; # class
Subroutine parse_cond_matches: not called CC=6 SCAR=37.4
Pod undocumented
56 0 my ( $spec ) = @_;
57  
58 0 ▶ if( $spec =~ m{^/} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($spec =~ m[^/]) { } 0 0 0
elsif ($spec =~ /^"/) { } 0 0 0
59 # Try to pull the flags
60 0 ▶ my ( $content, $flags ) = $spec =~ m{^/(.*)/([i]*)$} or die "Unrecognised regexp string $spec\n";
Branch: 1 branch
BranchTrueFalseTotal
() = $spec =~ m[^/(.*)/([i]*)$] or die "Unrecognised regexp string $spec\n" 0 0 0
61  
62 0 ▶ return qr/$content/i if $flags eq "i";
Branch: 1 branch
BranchTrueFalseTotal
if $flags eq "i" 0 0 0
63 0 return qr/$content/;
64 }
65 elsif( $spec =~ m{^"} ) {
66 0 ▶ my ( $content ) = $spec =~ m{^"(.*)"$} or die "Unrecognised literal string $spec\n";
Branch: 1 branch
BranchTrueFalseTotal
my($content) = $spec =~ /^"(.*)"$/ or die "Unrecognised literal string $spec\n" 0 0 0
67  
68 0 return qr/\Q$content/;
69 }
70 else {
71 0 die "Unrecognised string type $spec";
72 }
73 4 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
74  
75 sub deparse_cond_matches
76 {
77 0 ▶ shift; # class
Subroutine deparse_cond_matches: not called CC=1 SCAR=6.9
Pod undocumented
78 0 my ( $re ) = @_;
79  
80 0 my ( $pattern, $flags ) = unquote_qr( $re );
81 0 return "/$pattern/$flags";
82 }
83  
84 sub eval_cond_matches
85 {
86 0 ▶ shift; # class
Subroutine eval_cond_matches: not called CC=3 SCAR=24.8
Pod undocumented
87 0 my ( $event, $results, $re ) = @_;
88  
89 0 my $text = $event->{text}->str;
90  
91 0 pos( $text ) = 0;
92  
93 0 my $matched;
94  
95 0 ▶ while( $text =~ m/$re/g ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ /$re/g) 0 0 0
96 0 my @matchgroups;
97 0 for ( 0 .. $#+ ) {
98 0 my ( $start, $end ) = ( $-[$_], $+[$_] );
99 0 my $len = $end - $start;
100  
101 0 push @matchgroups, [ $start, $len ];
102 }
103  
104 0 $results->push_result( "matchgroups", \@matchgroups );
105 0 $matched = 1;
106 }
107  
108 0 return $matched;
109 }
110  
111 ###### ACTIONS
112  
113 ### REWRITE
114  
115 sub parse_action_rewrite
116 : Rule_description("Rewrite text of the line or matched parts")
117 : Rule_format('line|matches|match(number) "string"|s/pattern/replacement/')
118 {
119 0 ▶ shift; # class
Subroutine parse_action_rewrite: not called CC=10 SCAR=47.0
Pod undocumented
120 0 my ( $spec ) = @_;
121  
122 0 ▶ $spec =~ s/^(\w+)\s*// or die "Expected type as first argument\n";
Branch: 1 branch
BranchTrueFalseTotal
$spec =~ s/^(\w+)\s*// or die "Expected type as first argument\n" 0 0 0
123 0 my $type = $1;
124  
125 0 my $groupnum;
126  
127 0 ▶ if( $type eq "line" ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($type eq 'line') { } 0 0 0
elsif ($type eq 'matches') { } 0 0 0
elsif ($type eq 'match') { } 0 0 0
128 0 $groupnum = -1;
129 }
130 elsif( $type eq "matches" ) {
131 0 $groupnum = 0;
132 }
133 elsif( $type eq "match" ) {
134 0 ▶ $spec =~ s/^\((\d+)\)\s*// or die "Expected match group number\n";
Branch: 1 branch
BranchTrueFalseTotal
$spec =~ s/^\((\d+)\)\s*// or die "Expected match group number\n" 0 0 0
135 0 $groupnum = $1;
136 }
137 else {
138 0 die "Unrecognised format type $type\n";
139 }
140  
141 0 ▶ my ( undef, $remains, undef, $op, $delim, $lhs, undef, undef, $rhs, undef, $mods ) = extract_quotelike( $spec )
Branch: 1 branch
BranchTrueFalseTotal
my(undef, $remains, undef, $op, $delim, $lhs, undef, undef, $rhs, undef, $mods) = &extract_quotelike($spec) or die "Expected \"string\" or s/pattern/replacement/" 0 0 0
142 or die 'Expected "string" or s/pattern/replacement/';
143 0 $spec = $remains;
144 0 ▶ $op = $delim if $op eq "";
Branch: 1 branch
BranchTrueFalseTotal
if $op eq "" 0 0 0
145  
146 0 ▶ if( $op eq '"' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($op eq '"') { } 0 0 0
elsif ($op eq 's') { } 0 0 0
147 # Literal
148 0 return ( $groupnum, literal => $lhs );
149 }
150 elsif( $op eq "s" ) {
151 # s/foo/bar/
152 0 my $global = $mods =~ s/g//;
153 # TODO: Range check that mods contains only /ism
154 0 return ( $groupnum, subst => qr/(?$mods:$lhs)/, $rhs, $global );
155 }
156 else {
157 0 die 'Expected "string" or s/pattern/replacement/';
158 }
159 4 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
160  
161 sub deparse_action_rewrite
162 {
163 0 ▶ shift; # class
Subroutine deparse_action_rewrite: not called CC=6 SCAR=37.4
Pod undocumented
164 0 my ( $groupnum, $kind, $lhs, $rhs, $global ) = @_;
165  
166 0 ▶ my $type = $groupnum == -1 ? "line" :
Branch: 2 branches
BranchTrueFalseTotal
$groupnum == -1 ? : 0 0 0
$groupnum == 0 ? : 0 0 0
167 $groupnum == 0 ? "matches" :
168 "match($groupnum)";
169  
170 0 ▶ if( $kind eq "literal" ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($kind eq 'literal') { } 0 0 0
elsif ($kind eq 'subst') { } 0 0 0
171 0 return "$type \"$lhs\"";
172 }
173 elsif( $kind eq "subst" ) {
174 0 my ( $pattern, $flags ) = unquote_qr( $lhs );
175 0 ▶ return "$type s/$pattern/$rhs/$flags" . ( $global ? "g" : "" );
Branch: 1 branch
BranchTrueFalseTotal
$global ? : 0 0 0
176 }
177 }
178  
179 sub eval_action_rewrite
180 {
181 0 ▶ shift; # class
Subroutine eval_action_rewrite: not called CC=8 SCAR=42.8
Pod undocumented
182 0 my ( $event, $results, $groupnum, $kind, $lhs, $rhs, $global ) = @_;
183  
184 0 my @location;
185 0 ▶ if( $groupnum == -1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($groupnum == -1) { } 0 0 0
186 0 @location = ( 0, -1 );
187 }
188 else {
189 0 foreach my $groups ( @{ $results->get_result( "matchgroups" ) } ) {
190 0 ▶ my $group = $groups->[$groupnum] or next;
Branch: 1 branch
BranchTrueFalseTotal
my $group = $groups->[$groupnum] or next 0 0 0
191 0 @location = @$group;
192 0 last; # can only do the first one
193 }
194 }
195  
196 0 my $text = $event->{text}->substr( $location[0], $location[1] );
197  
198 0 ▶ if( $kind eq "literal" ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($kind eq 'literal') { } 0 0 0
elsif ($kind eq 'subst') { } 0 0 0
199 0 $text = $lhs;
200 }
201 elsif( $kind eq "subst" ) {
202 0 ▶ $text =~ s/$lhs/$rhs/ if !$global;
Branch: 1 branch
BranchTrueFalseTotal
unless $global 0 0 0
203 0 ▶ $text =~ s/$lhs/$rhs/g if $global;
Branch: 1 branch
BranchTrueFalseTotal
if $global 0 0 0
204 }
205  
206 0 $event->{text}->set_substr( $location[0], $location[1], $text );
207 }
208  
209 ### FORMAT
210  
211 sub parse_action_format
212 : Rule_description("Apply formatting to the line or matched parts")
213 : Rule_format('line|matches|match(number) key="value" [key="value" ...]')
214 {
215 0 ▶ shift; # class
Subroutine parse_action_format: not called CC=9 SCAR=45.0
Pod undocumented
216 0 my ( $spec ) = @_;
217  
218 0 ▶ $spec =~ s/^(\w+)\s*// or die "Expected type as first argument\n";
Branch: 1 branch
BranchTrueFalseTotal
$spec =~ s/^(\w+)\s*// or die "Expected type as first argument\n" 0 0 0
219 0 my $type = $1;
220  
221 0 my $groupnum;
222  
223 0 ▶ if( $type eq "line" ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($type eq 'line') { } 0 0 0
elsif ($type eq 'matches') { } 0 0 0
elsif ($type eq 'match') { } 0 0 0
224 0 $groupnum = -1;
225 }
226 elsif( $type eq "matches" ) {
227 0 $groupnum = 0;
228 }
229 elsif( $type eq "match" ) {
230 0 ▶ $spec =~ s/^\((\d+)\)\s*// or die "Expected match group number\n";
Branch: 1 branch
BranchTrueFalseTotal
$spec =~ s/^\((\d+)\)\s*// or die "Expected match group number\n" 0 0 0
231 0 $groupnum = $1;
232 }
233 else {
234 0 die "Unrecognised format type $type\n";
235 }
236  
237 0 my %format;
238 0 ▶ while( $spec =~ s/^(\w+)=// ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spec =~ s/^(\w+)=//) 0 0 0
239 0 my $name = $1;
240  
241 0 my $value = extract_delimited( $spec, q{"'} );
242 0 s/^["']//, s/["']$// for $value;
243  
244 0 $format{$name} = $value;
245  
246 0 $spec =~ s/^\s+//;
247 }
248  
249 0 ▶ if( length $spec ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $spec) 0 0 0
250 0 die "Unrecognised format spec $spec\n";
251 }
252  
253 0 return ( $groupnum, \%format );
254 4 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
255  
256 sub deparse_action_format
257 {
258 0 ▶ shift; # class
Subroutine deparse_action_format: not called CC=4 SCAR=30.0
Pod undocumented
259 0 my ( $groupnum, $formathash ) = @_;
260  
261 0 ▶ return unless %$formathash;
Branch: 1 branch
BranchTrueFalseTotal
unless %$formathash 0 0 0
262  
263 0 ▶ my $type = $groupnum == -1 ? "line" :
Branch: 2 branches
BranchTrueFalseTotal
$groupnum == -1 ? : 0 0 0
$groupnum == 0 ? : 0 0 0
264 $groupnum == 0 ? "matches" :
265 "match($groupnum)";
266  
267 0 return "$type ".join( " ", map { qq($_="$formathash->{$_}") } sort keys %$formathash );
268 }
269  
270 sub eval_action_format
271 {
272 0 ▶ shift; # class
Subroutine eval_action_format: not called CC=6 SCAR=37.4
Pod undocumented
273 0 my ( $event, $results, $groupnum, $formathash ) = @_;
274  
275 0 my $str = $event->{text};
276  
277 0 ▶ if( $groupnum == -1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($groupnum == -1) { } 0 0 0
278 0 $str->apply_tag( 0, -1, $_, $formathash->{$_} ) for keys %$formathash;
279 }
280 else {
281 0 foreach my $groups ( @{ $results->get_result( "matchgroups" ) } ) {
282 0 ▶ my $group = $groups->[$groupnum] or next;
Branch: 1 branch
BranchTrueFalseTotal
my $group = $groups->[$groupnum] or next 0 0 0
283 0 my ( $start, $len ) = @$group;
284  
285 0 $str->apply_tag( $start, $len, $_, $formathash->{$_} ) for keys %$formathash;
286 }
287 }
288 }
289  
290 ### UNFORMAT
291  
292 sub parse_action_unformat
293 : Rule_description("Remove formatting from the line or matched parts")
294 : Rule_format('line|matches|match(number) key [key ...]')
295 {
296 0 ▶ shift; # class
Subroutine parse_action_unformat: not called CC=8 SCAR=42.8
Pod undocumented
297 0 my ( $spec ) = @_;
298  
299 0 ▶ $spec =~ s/^(\w+)\s*// or die "Expected type as first argument\n";
Branch: 1 branch
BranchTrueFalseTotal
$spec =~ s/^(\w+)\s*// or die "Expected type as first argument\n" 0 0 0
300 0 my $type = $1;
301  
302 0 my $groupnum;
303  
304 0 ▶ if( $type eq "line" ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($type eq 'line') { } 0 0 0
elsif ($type eq 'matches') { } 0 0 0
elsif ($type eq 'match') { } 0 0 0
305 0 $groupnum = -1;
306 }
307 elsif( $type eq "matches" ) {
308 0 $groupnum = 0;
309 }
310 elsif( $type eq "match" ) {
311 0 ▶ $spec =~ s/^\((\d+)\)\s*// or die "Expected match group number\n";
Branch: 1 branch
BranchTrueFalseTotal
$spec =~ s/^\((\d+)\)\s*// or die "Expected match group number\n" 0 0 0
312 0 $groupnum = $1;
313 }
314 else {
315 0 die "Unrecognised format type $type\n";
316 }
317  
318 0 my @tags;
319 0 ▶ while( $spec =~ s/^(\w+)// ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spec =~ s/^(\w+)//) 0 0 0
320 0 my $name = $1;
321  
322 0 push @tags, $name;
323  
324 0 $spec =~ s/^\s+//;
325 }
326  
327 0 ▶ if( length $spec ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $spec) 0 0 0
328 0 die "Unrecognised format spec $spec\n";
329 }
330  
331 0 return ( $groupnum, \@tags );
332 4 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
333  
334 sub deparse_action_unformat
335 {
336 0 ▶ shift; # class
Subroutine deparse_action_unformat: not called CC=4 SCAR=30.0
Pod undocumented
337 0 my ( $groupnum, $taglist ) = @_;
338  
339 0 ▶ my $type = $groupnum == -1 ? "line" :
Branch: 2 branches
BranchTrueFalseTotal
$groupnum == -1 ? : 0 0 0
$groupnum == 0 ? : 0 0 0
340 $groupnum == 0 ? "matches" :
341 "match($groupnum)";
342  
343 0 my $ret = $type;
344 0 $ret .= " $_" for @$taglist;
345  
346 0 return $ret;
347 }
348  
349 4 my @alltags = qw( fg bg b u i );
350  
351 sub eval_action_unformat
352 {
353 0 ▶ shift; # class
Subroutine eval_action_unformat: not called CC=7 SCAR=40.3
Pod undocumented
354 0 my ( $event, $results, $groupnum, $taglist ) = @_;
355  
356 0 ▶ $taglist = \@alltags unless @$taglist;
Branch: 1 branch
BranchTrueFalseTotal
unless @$taglist 0 0 0
357  
358 0 my $str = $event->{text};
359  
360 0 ▶ if( $groupnum == -1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($groupnum == -1) { } 0 0 0
361 0 $str->unapply_tag( 0, -1, $_ ) for @$taglist;
362 }
363 else {
364 0 foreach my $groups ( @{ $results->get_result( "matchgroups" ) } ) {
365 0 ▶ my $group = $groups->[$groupnum] or next;
Branch: 1 branch
BranchTrueFalseTotal
my $group = $groups->[$groupnum] or next 0 0 0
366 0 my ( $start, $len ) = @$group;
367  
368 0 $str->unapply_tag( $start, $len, $_ ) for @$taglist;
369 }
370 }
371 }
372  
373 ### LEVEL
374  
375 sub parse_action_level
376 : Rule_description("Set the activity level for the targetted item")
377 : Rule_format('$level')
378 {
379 0 ▶ shift; # class
Subroutine parse_action_level: not called CC=4 SCAR=30.0
Pod undocumented
380 0 my ( $spec ) = @_;
381  
382 0 ▶ $spec =~ s/^(\d)// or die "Expected level number as first argument\n";
Branch: 1 branch
BranchTrueFalseTotal
$spec =~ s/^(\d)// or die "Expected level number as first argument\n" 0 0 0
383 0 my $level = $1;
384  
385 0 ▶ $level >= 0 and $level <= 3 or die "Expected 'level' between 0 and 3\n";
Branch: 1 branch
BranchTrueFalseTotal
$level >= 0 and $level <= 3 or die "Expected 'level' between 0 and 3\n" 0 0 0
Condition0%0/3
$level >= 0 and $level <= 3
!ll&&!rl&&r
000
MC/DC0%0/2
$level >= 0 and $level <= 3
$level >= 0 $level <= 3
Truth table0%0/3
A and B
A= $level >= 0B= $level <= 3
ABresult
0X0
100
111
386  
387 0 return ( $level );
388 4 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
389  
390 sub deparse_action_level
391 {
392 0 ▶ shift; # class
Subroutine deparse_action_level: not called CC=1 SCAR=6.9
Pod undocumented
393 0 my ( $level ) = @_;
394  
395 0 return "$level";
396 }
397  
398 sub eval_action_level
399 {
400 0 ▶ shift; # class
Subroutine eval_action_level: not called CC=1 SCAR=6.9
Pod undocumented
401 0 my ( $event, $results, $level ) = @_;
402  
403 0 $event->{level} = $level;
404 }
405  
406 4 0x55AA;
« blib/lib/Circle/Net/IRC.pm ↑ Summary blib/lib/Circle/Net/IRC/Channel.pm »