blib/lib/Bot/Cobalt/Utils.pm

filter: Statementstmt 91.0% 142 / 156 Branchbran 69.3% 61 / 88 Conditioncond 62.5% 5 / 8 MC/DCmcdc 40.0% 2 / 5 Subroutinesub 100% 18 / 18 Podpod 100% 11 / 11 totaltotal 83.5% 239 / 286 CC 49 SCAR 24.8
CC
49
Cov
83%
glob_to_re_str
29.7
timestr_to_secs
7.1
secs_to_timestr
6.3
CRAP 11.9
« blib/lib/Bot/Cobalt/IRC/Message/Public.pm ↑ Summary blib/lib/Bot/Cobalt/Frontend/Utils.pm »
1 package Bot::Cobalt::Utils;
2 16 $Bot::Cobalt::Utils::VERSION = '0.021003';
3 16 ▶use strictures 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 16 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 16 ▶use Scalar::Util 'reftype';
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 16 ▶use App::bmkpasswd ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 16 ▶use parent 'Exporter::Tiny';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 16 our @EXPORT_OK = qw/
12 secs_to_str
13 secs_to_str_y
14 secs_to_timestr
15 timestr_to_secs
16  
17 mkpasswd
18 passwdcmp
19  
20 color
21  
22 glob_grep
23 glob_to_re
24 glob_to_re_str
25 rplprintf
26 /;
27  
28 16 our %EXPORT_TAGS = (
29 ALL => [ @EXPORT_OK ],
30 );
31  
32  
33 ## codes mostly borrowed from IRC::Utils
34 16 our %COLORS = (
35 NORMAL => "\x0f",
36  
37 BOLD => "\x02",
38 UNDERLINE => "\x1f",
39 REVERSE => "\x16",
40 ITALIC => "\x1d",
41  
42 WHITE => "\x0300",
43 BLACK => "\x0301",
44 BLUE => "\x0302",
45 GREEN => "\x0303",
46 RED => "\x0304",
47 BROWN => "\x0305",
48 PURPLE => "\x0306",
49 ORANGE => "\x0307",
50 YELLOW => "\x0308",
51 TEAL => "\x0310",
52 PINK => "\x0313",
53 GREY => "\x0314",
54 GRAY => "\x0314",
55  
56 LIGHT_BLUE => "\x0312",
57 LIGHT_CYAN => "\x0311",
58 LIGHT_GREEN => "\x0309",
59 LIGHT_GRAY => "\x0315",
60 LIGHT_GREY => "\x0315",
61 );
62  
63 16 my %default_fmt_vars;
64 16 for my $color (keys %COLORS) {
65 368 my $fmtvar = 'C_'.$color;
66 368 $default_fmt_vars{$fmtvar} = $COLORS{$color};
67 }
68  
69 ## String formatting, usually for langsets:
70 5 ▶sub rplprintf {
Subroutine rplprintf: called CC=4 SCAR=13.9
Pod documented
71 5 my $string = shift;
72 5 ▶ return '' unless $string;
Branch: 1 branch
BranchTrueFalseTotal
unless $string 0 5 5
73  
74 ## rplprintf( $string, $vars )
75 ## returns empty string if no string is specified.
76 ##
77 ## variables can be terminated with % or a space:
78 ## rplprintf( "Error for %user%: %err")
79 ##
80 ## used for formatting lang RPLs
81 ## $vars should be a hash keyed by variable, f.ex:
82 ## 'user' => $username,
83 ## 'err' => $error,
84  
85 5 my %vars;
86  
87 5 ▶ if (@_ > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) { } 1 4 5
88 1 my %args = @_;
89 1 %vars = ( %default_fmt_vars, %args );
90 } else {
91 4 ▶ if (reftype $_[0] eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::reftype($_[0]) eq 'HASH') { } 4 0 4
92 4 %vars = ( %default_fmt_vars, %{$_[0]} );
93 } else {
94 0 confess "rplprintf() expects a hash"
95 }
96 }
97  
98 18 ▶ my $repl = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
99 ## _repl($1, $2, $vars)
100 18 my ($orig, $match) = @_;
101 18 ▶ defined $vars{$match} ? $vars{$match} : $orig
Branch: 1 branch
BranchTrueFalseTotal
defined $vars{$match} ? : 16 2 18
102 5 };
103  
104 5 my $regex = qr/(%([^\s%]+)%?)/;
105  
106 5 $string =~ s/$regex/$repl->($1, $2)/ge;
107  
108 5 $string
109 }
110  
111  
112 ## Glob -> regex functions:
113  
114 4 ▶sub glob_grep ($;@) {
Subroutine glob_grep: called CC=3 SCAR=11.0
Pod documented
115 4 my $glob = shift;
116 4 ▶ confess "glob_grep called with no arguments!"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $glob 0 4 4
117 unless defined $glob;
118  
119 4 ▶ my @array = ref $_[0] eq 'ARRAY' ? @{$_[0]} : @_ ;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'ARRAY' ? : 2 2 4
120  
121 4 my $re = glob_to_re($glob);
122  
123 4 grep { m/$re/ } @array
124 }
125  
126 6 ▶sub glob_to_re ($) {
Subroutine glob_to_re: called CC=2 SCAR=7.0
Pod documented
127 6 my ($glob) = @_;
128 6 ▶ confess "glob_to_re called with no arguments!"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $glob 0 6 6
129 unless defined $glob;
130  
131 6 my $re = glob_to_re_str($glob);
132  
133 6 qr/$re/
134 }
135  
136 8 ▶sub glob_to_re_str ($) {
Subroutine glob_to_re_str: called CC=20 SCAR=33.9
Pod documented
137 ## Currently allows:
138 ## * == .*
139 ## ? == .
140 ## + == literal space
141 ## leading ^ (beginning of str) is accepted
142 ## so is trailing $
143 ## char classes are accepted
144 8 my ($glob) = @_;
145 8 ▶ confess "glob_to_re_str called with no arguments!"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $glob 0 8 8
146 unless defined $glob;
147  
148 8 my($re, $in_esc);
149 8 my ($first, $pos) = (1, 0);
150 8 my @chars = split '', $glob;
151 8 for my $ch (@chars) {
152 82 ++$pos;
153 82 ▶ my $last = 1 if $pos == @chars;
Branch: 1 branch
BranchTrueFalseTotal
if $pos == @chars 8 74 82
154  
155 ## Leading ^ (start) is OK:
156 82 ▶ if ($first) {
Branch: 2 branches
BranchTrueFalseTotal
if ($first) { } 11 71 82
elsif ($last and $ch eq '$') { } 3 131 134
157 11 ▶ if ($ch eq '^') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "^") 3 8 11
158 3 $re .= '^' ;
159 next
160 3 }
161 8 $first = 0;
162 ## So is trailing $ (end):
163 ▶ } elsif ($last && $ch eq '$') {
Condition100%3/3
$last and $ch eq "\$"
!ll&&!rl&&r
6353
MC/DC100%2/2
$last and $ch eq "\$"
$last $ch eq "\$"
Truth table100%3/3
A and B
A= $lastB= $ch eq "\$"
ABresult
0X0
100
111
164 3 $re .= '$' ;
165 last
166 3 }
167  
168 ## Escape metas:
169 76 ▶ if (grep { $_ eq $ch } qw/ . ( ) | ^ $ @ % { } /) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$_ eq $ch;} ".", "(", ")", "|", "^", "\$", "\@", "%", "{", "}") 0 76 76
170 0 $re .= "\\$ch" ;
171 0 $in_esc = 0;
172 next
173 0 }
174  
175 ## Handle * ? + wildcards:
176 76 ▶ if ($ch eq '*') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "*") 6 70 76
177 6 ▶ $re .= $in_esc ? '\*' : '.*' ;
Branch: 1 branch
BranchTrueFalseTotal
$in_esc ? : 0 6 6
178 6 $in_esc = 0;
179 next
180 6 }
181 70 ▶ if ($ch eq '?') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "?") 2 68 70
182 2 ▶ $re .= $in_esc ? '\?' : '.' ;
Branch: 1 branch
BranchTrueFalseTotal
$in_esc ? : 0 2 2
183 2 $in_esc = 0;
184 next
185 2 }
186 68 ▶ if ($ch eq '+') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "+") 3 65 68
187 3 ▶ $re .= $in_esc ? '\+' : '\s' ;
Branch: 1 branch
BranchTrueFalseTotal
$in_esc ? : 0 3 3
188 3 $in_esc = 0;
189 next
190 3 }
191 65 ▶ if ( $ch eq '[' || $ch eq ']' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "[" or $ch eq "]") 0 65 65
Condition33%1/3
$ch eq "[" or $ch eq "]"
l!l&&r!l&&!r
0065
MC/DC0%0/2
$ch eq "[" or $ch eq "]"
$ch eq "[" $ch eq "]"
Truth table33%1/3
A or B
A= $ch eq "["B= $ch eq "]"
ABresult
1X1
011
000
192 0 ▶ $re .= $in_esc ? "\\$ch" : $ch ;
Branch: 1 branch
BranchTrueFalseTotal
$in_esc ? : 0 0 0
193 0 $in_esc = 0;
194 next
195 0 }
196  
197 ## Switch on/off escaping:
198 65 ▶ if ($ch eq "\\") {
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "\\") 0 65 65
199 0 ▶ if ($in_esc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($in_esc) { } 0 0 0
200 0 $re .= "\\\\";
201 0 $in_esc = 0;
202 0 } else { $in_esc = 1; }
203 next
204 0 }
205  
206 65 $re .= $ch;
207 65 $in_esc = 0;
208 }
209  
210 $re
211 8 }
212  
213  
214 ## IRC color codes:
215 4 ▶sub color ($;$) {
Subroutine color: called CC=5 SCAR=16.2
Pod documented
216 ## color($format, $str)
217 ## implements mirc formatting codes, against my better judgement
218 ## if format is unspecified, returns NORMAL
219  
220 ## interpolate bold, reset to NORMAL after:
221 ## $str = color('bold') . "Text" . color;
222 ## -or-
223 ## format specified strings, resetting NORMAL after:
224 ## $str = color('bold', "Some text"); # bold text ending in normal
225  
226 4 my ($format, $str) = @_;
227 4 ▶ $format = uc($format||'normal');
Condition50%1/2
$format || 'normal'
l!l
40
MC/DC0%0/1
$format || 'normal'
$format
Truth table50%1/2
A
A= $format
Aresult
11
00
228  
229 4 my $selected = $COLORS{$format};
230  
231 4 ▶ carp "Invalid COLOR $format passed to color()"
Branch: 1 branch
BranchTrueFalseTotal
unless $selected 1 3 4
232 unless $selected;
233  
234 4 ▶ return $selected . $str . $COLORS{NORMAL} if $str;
Branch: 1 branch
BranchTrueFalseTotal
if $str 0 4 4
235 4 ▶ $selected || $COLORS{NORMAL};
Branch: 1 branch
BranchTrueFalseTotal
$selected or $Bot::Cobalt::Utils::COLORS{"NORMAL"} 3 1 4
236 }
237  
238  
239 ## Time/date ops:
240 4 ▶sub timestr_to_secs ($) {
Subroutine timestr_to_secs: called CC=7 SCAR=19.6
Pod documented
241 ## turn something like 2h3m30s into seconds
242 4 my ($timestr) = @_;
243  
244 4 ▶ unless ($timestr) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($timestr) 0 4 4
245 0 carp "timestr_to_secs() received a false value";
246 0 return 0
247 }
248  
249 ## maybe just seconds:
250 4 ▶ return $timestr if $timestr =~ /^[0-9]+$/;
Branch: 1 branch
BranchTrueFalseTotal
if $timestr =~ /^[0-9]+$/ 0 4 4
251  
252 4 my @chunks = $timestr =~ m/([0-9]+)([dhms])/gc;
253  
254 4 my $secs = 0;
255 4 ▶ while ( my ($ti, $unit) = splice @chunks, 0, 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@chunks, 0, 2)) 6 4 10
256 UNIT: {
257 6 ▶ if ($unit eq 'd') {
Branch: 1 branch
BranchTrueFalseTotal
if ($unit eq "d") 1 5 6
258 1 $secs += $ti * 86400;
259 last UNIT
260 1 }
261  
262 5 ▶ if ($unit eq 'h') {
Branch: 1 branch
BranchTrueFalseTotal
if ($unit eq "h") 1 4 5
263 1 $secs += $ti * 3600;
264 last UNIT
265 1 }
266  
267 4 ▶ if ($unit eq 'm') {
Branch: 1 branch
BranchTrueFalseTotal
if ($unit eq "m") 2 2 4
268 2 $secs += $ti * 60;
269 last UNIT
270 2 }
271  
272 2 $secs += $ti;
273 }
274 }
275  
276 $secs
277 4 }
278  
279 8 ▶sub _time_breakdown ($) {
Subroutine _time_breakdown: called CC=2 SCAR=6.9
280 8 my ($diff) = @_;
281 8 ▶ return unless defined $diff;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $diff 0 8 8
282  
283 8 my $days = int $diff / 86400;
284 8 my $sec = $diff % 86400;
285 8 my $hours = int $sec / 3600;
286 8 $sec %= 3600;
287 8 my $mins = int $sec / 60;
288 8 $sec %= 60;
289  
290 8 ($days, $hours, $mins, $sec)
291 }
292  
293 3 ▶sub secs_to_timestr ($) {
Subroutine secs_to_timestr: called CC=6 SCAR=18.5
Pod documented
294 3 my ($diff) = @_;
295 3 ▶ return unless defined $diff;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $diff 0 3 3
296 3 my ($days, $hours, $mins, $sec) = _time_breakdown($diff);
297  
298 3 my $str;
299 3 ▶ $str .= $days .'d' if $days;
Branch: 1 branch
BranchTrueFalseTotal
if $days 0 3 3
300 3 ▶ $str .= $hours .'h' if $hours;
Branch: 1 branch
BranchTrueFalseTotal
if $hours 0 3 3
301 3 ▶ $str .= $mins .'m' if $mins;
Branch: 1 branch
BranchTrueFalseTotal
if $mins 3 0 3
302 3 ▶ $str .= $sec .'s' if $sec;
Branch: 1 branch
BranchTrueFalseTotal
if $sec 1 2 3
303  
304 3 $str
305 }
306  
307 3 ▶sub secs_to_str ($) {
Subroutine secs_to_str: called CC=3 SCAR=11.0
Pod documented
308 ## turn seconds into a string like '0 days, 00:00:00'
309 3 my ($diff) = @_;
310 3 ▶ return unless defined $diff;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $diff 0 3 3
311 3 my ($days, $hours, $mins, $sec) = _time_breakdown($diff);
312 3 ▶ my $plural = $days == 1 ? 'day' : 'days';
Branch: 1 branch
BranchTrueFalseTotal
$days == 1 ? : 1 2 3
313 3 sprintf "%d $plural, %2.2d:%2.2d:%2.2d", $days, $hours, $mins, $sec
314 }
315  
316 2 ▶sub secs_to_str_y {
Subroutine secs_to_str_y: called CC=5 SCAR=16.4
Pod documented
317 2 my ($diff) = @_;
318 2 ▶ return unless defined $diff;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $diff 0 2 2
319 2 my ($days, $hrs, $mins, $sec) = _time_breakdown($diff);
320 2 my $yrs = int $days / 365;
321 2 $days %= 365;
322 2 ▶ my $plural_y = $yrs > 1 ? 'years' : 'year';
Branch: 1 branch
BranchTrueFalseTotal
$yrs > 1 ? : 1 1 2
323 2 ▶ my $plural_d = $days == 1 ? 'day' : 'days';
Branch: 1 branch
BranchTrueFalseTotal
$days == 1 ? : 0 2 2
324 2 ▶ $yrs ?
Branch: 1 branch
BranchTrueFalseTotal
$yrs ? : 2 0 2
325 sprintf "%d $plural_y, %d $plural_d, %2.2d:%2.2d:%2.2d",
326 $yrs, $days, $hrs, $mins, $sec
327 : sprintf "%d $plural_d, %2.2d:%2.2d:%2.2d",
328 $days, $hrs, $mins, $sec
329 }
330  
331  
332 ## App::bmkpasswd stubs as of 00_35
333 1 ▶sub mkpasswd ($;@) { App::bmkpasswd::mkpasswd(@_) }
Subroutine mkpasswd: called CC=1 SCAR=0.0
Pod documented
334 2 ▶sub passwdcmp ($$) { App::bmkpasswd::passwdcmp(@_) }
Subroutine passwdcmp: called CC=1 SCAR=0.0
Pod documented
335  
336 16 1;
337 __END__
« blib/lib/Bot/Cobalt/IRC/Message/Public.pm ↑ Summary blib/lib/Bot/Cobalt/Frontend/Utils.pm »