Toolkit/Modes.pm

filter: Statementstmt 95.8% 70 / 73 Branchbran 81.5% 31 / 38 Conditioncond 56.6% 17 / 30 MC/DCmcdc 41.1% 7 / 17 Subroutinesub 100% 7 / 7 Podpod 100% 3 / 3 totaltotal 80.3% 135 / 168 SCAR 36.3
CC
33
Cov
84%
mode_to_array
23.3
array_to_mode
12.1
mode_to_hash
4.0
CRAP 37.7
↑ Summary Mode/Set.pm »
1 package IRC::Toolkit::Modes;
2 5 $IRC::Toolkit::Modes::VERSION = '0.091001';
3 5 ▶use strictures 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 5 ▶use Scalar::Util 'blessed', 'reftype';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 5 ▶use parent 'Exporter::Tiny';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 our @EXPORT = qw/
10 array_to_mode
11 mode_to_array
12 mode_to_hash
13 /;
14  
15  
16 sub array_to_mode {
17 8 ▶ my ($array) = @_;
Subroutine array_to_mode: called CC=11 SCAR=24.9
Pod documented
18 8 ▶ if (blessed $array && $array->isa('IRC::Mode::Set')) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($array) and $array->isa("IRC::Mode::Set")) 1 7 8
Condition66%2/3
builtin::blessed($array) and $array->isa("IRC::Mode::Set")
!ll&&!rl&&r
701
MC/DC50%1/2
builtin::blessed($array) and $array->isa("IRC::Mode::Set")
builtin::blessed($array) $array->isa("IRC::Mode::Set")
Truth table66%2/3
A and B
A= builtin::blessed($array)B= $array->isa("IRC::Mode::Set")
ABresult
0X0
100
111
19 1 $array = $array->mode_array
20 }
21 8 ▶ confess "Expected an ARRAY but got $array"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $array and builtin::reftype($array) eq "ARRAY" 0 8 8
Condition33%1/3
ref $array and builtin::reftype($array) eq "ARRAY"
!ll&&!rl&&r
008
MC/DC0%0/2
ref $array and builtin::reftype($array) eq "ARRAY"
ref $array builtin::reftype($array) eq "ARRAY"
Truth table33%1/3
A and B
A= ref $arrayB= builtin::reftype($array) eq "ARRAY"
ABresult
0X0
100
111
22 unless ref $array and reftype $array eq 'ARRAY';
23  
24 8 my $mstr;
25 8 my $curflag = my $pstr = '';
26 8 for my $cset (@$array) {
27 23 my ($flag, $mode, $param) = @$cset;
28 23 ▶ confess "Appear to have been given an invalid mode array"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $flag and defined $mode 0 23 23
Condition33%1/3
defined $flag and defined $mode
!ll&&!rl&&r
0023
MC/DC0%0/2
defined $flag and defined $mode
defined $flag defined $mode
Truth table33%1/3
A and B
A= defined $flagB= defined $mode
ABresult
0X0
100
111
29 unless defined $flag and defined $mode;
30 23 ▶ $mstr .= $flag eq $curflag ? $mode : ($curflag = $flag) . $mode ;
Branch: 1 branch
BranchTrueFalseTotal
$flag eq $curflag ? : 4 19 23
31 23 ▶ $pstr .= " $param" if defined $param;
Branch: 1 branch
BranchTrueFalseTotal
if defined $param 19 4 23
32 }
33  
34 8 ▶ $mstr .= $pstr if length $pstr;
Branch: 1 branch
BranchTrueFalseTotal
if length $pstr 7 1 8
35 8 $mstr
36 }
37  
38 sub mode_to_array {
39 ## mode_to_array( $string,
40 ## param_always => [ split //, 'bkov' ],
41 ## param_set => [ 'l' ],
42 ## params => [ @params ],
43 ##
44 ## Returns ARRAY-of-ARRAY like:
45 ## [ [ '+', 'o', 'some_nick' ], [ '-', 't' ] ]
46  
47 8 ▶ my $modestr = shift;
Subroutine mode_to_array: called CC=20 SCAR=31.5
Pod documented
48 8 ▶ confess "mode_to_array() called without mode string"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $modestr 0 8 8
49 unless defined $modestr;
50  
51 8 my %args = @_;
52 8 ▶ if (my $isup = $args{isupport_chanmodes}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $isup = $args{"isupport_chanmodes"}) 0 8 8
53 0 ▶ confess
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($isup) and $isup->can("always") and $isup->can("whenset") 0 0 0
Condition0%0/6
builtin::blessed($isup) and $isup->can("always") and $isup->can("whenset")
!ll&&!rl&&r
000
builtin::blessed($isup) and $isup->can("always")
!ll&&!rl&&r
000
MC/DC0%0/3
builtin::blessed($isup) and $isup->can("always") and $isup->can("whenset")
builtin::blessed($isup) $isup->can("always") $isup->can("whenset")
Truth table0%0/4
A and B and C
A= builtin::blessed($isup)B= $isup->can("always")C= $isup->can("whenset")
ABCresult
0XX0
10X0
1100
1111
54 "isupport_chanmodes specified but not an ISupport chanmodes() obj: $isup"
55 unless blessed $isup and $isup->can('always') and $isup->can('whenset');
56 0 $args{param_always} = $isup->always->unbless;
57 0 $args{param_set} = $isup->whenset->unbless;
58 }
59 8 ▶ $args{param_always} ||= [ split //, 'bkohv' ];
Condition100%2/2
$args{"param_always"} ||= [split(//, "bkohv", 0)]
l!l
53
MC/DC100%1/1
$args{"param_always"} ||= [split(//, "bkohv", 0)]
$args{"param_always"}
Truth table100%2/2
A
A= $args{"param_always"}
Aresult
11
00
60 8 ▶ $args{param_set} ||= ( $args{param_on_set} || [ 'l' ] );
Condition60%3/5
$args{"param_set"} ||= $args{"param_on_set"} || ["l"]
l!l&&r!l&&!r
440
$args{'param_on_set'} || ['l']
l!l
04
MC/DC0%0/2
$args{"param_set"} ||= $args{"param_on_set"} || ["l"]
$args{"param_set"} $args{'param_on_set'}
Truth table33%1/3
A ||= B
A= $args{"param_set"}B= $args{'param_on_set'}
ABresult
1X1
011
000
61 8 ▶ $args{params} ||= [ ];
Condition100%2/2
$args{"params"} ||= []
l!l
35
MC/DC100%1/1
$args{"params"} ||= []
$args{"params"}
Truth table100%2/2
A
A= $args{"params"}
Aresult
11
00
62  
63 8 ▶ if ( index($modestr, ' ') > -1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (index($modestr, " ") != -1) 4 4 8
64 4 my @params;
65 4 ($modestr, @params) = split ' ', $modestr;
66 4 unshift @{ $args{params} }, @params;
67 }
68  
69 8 for (qw/ param_always param_set params /) {
70 confess "$_ should be an ARRAY"
71 24 ▶ unless reftype $args{$_} eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::reftype($args{$_}) eq "ARRAY" 0 24 24
72 }
73  
74 8 my @parsed;
75 8 my %param_always = map {; $_ => 1 } @{ $args{param_always} };
76 8 my %param_set = map {; $_ => 1 } @{ $args{param_set} };
77 8 my @chunks = split //, $modestr;
78 8 my $in = '+';
79 8 ▶ CHUNK: while (my $chunk = shift @chunks) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $chunk = shift @chunks) 51 8 59
80 51 ▶ if ($chunk eq '-' || $chunk eq '+') {
Branch: 1 branch
BranchTrueFalseTotal
if ($chunk eq "-" or $chunk eq "+") 23 28 51
Condition100%3/3
$chunk eq "-" or $chunk eq "+"
l!l&&r!l&&!r
91428
MC/DC100%2/2
$chunk eq "-" or $chunk eq "+"
$chunk eq "-" $chunk eq "+"
Truth table100%3/3
A or B
A= $chunk eq "-"B= $chunk eq "+"
ABresult
1X1
011
000
81 23 $in = $chunk;
82 next CHUNK
83 23 }
84  
85 28 my @current = ( $in, $chunk );
86 28 ▶ if ($in eq '+') {
Branch: 1 branch
BranchTrueFalseTotal
if ($in eq '+') { } 19 9 28
87 16 push @current, shift @{ $args{params} }
88 if exists $param_always{$chunk}
89 19 ▶ or exists $param_set{$chunk};
Branch: 1 branch
BranchTrueFalseTotal
if exists $param_always{$chunk} or exists $param_set{$chunk} 16 3 19
Condition100%3/3
exists $param_always{$chunk} or exists $param_set{$chunk}
l!l&&r!l&&!r
1243
MC/DC100%2/2
exists $param_always{$chunk} or exists $param_set{$chunk}
exists $param_always{$chunk} exists $param_set{$chunk}
Truth table100%3/3
A or B
A= exists $param_always{$chunk}B= exists $param_set{$chunk}
ABresult
1X1
011
000
90 } else {
91 6 push @current, shift @{ $args{params} }
92 9 ▶ if exists $param_always{$chunk};
Branch: 1 branch
BranchTrueFalseTotal
if exists $param_always{$chunk} 6 3 9
93 }
94  
95 28 push @parsed, [ @current ]
96 }
97  
98 8 [ @parsed ]
99 }
100  
101 sub mode_to_hash {
102 ## Returns HASH like:
103 ## add => {
104 ## 'o' => [ 'some_nick' ],
105 ## 't' => 1,
106 ## },
107 ## del => {
108 ## 'k' => [ 'some_key' ],
109 ## },
110  
111 ## This is a 'lossy' approach.
112 ## It won't accomodate batched modes well.
113 ## Use mode_to_array instead.
114 2 ▶ my $array = mode_to_array(@_);
Subroutine mode_to_hash: called CC=4 SCAR=13.9
Pod documented
115 2 my $modes = { add => {}, del => {} };
116 2 ▶ while (my $this_mode = shift @$array) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $this_mode = shift @$array) 5 2 7
117 5 my ($flag, $mode, $param) = @$this_mode;
118 5 ▶ my $key = $flag eq '+' ? 'add' : 'del' ;
Branch: 1 branch
BranchTrueFalseTotal
$flag eq '+' ? : 4 1 5
119 5 ▶ $modes->{$key}->{$mode} = $param ? [ $param ] : 1
Branch: 1 branch
BranchTrueFalseTotal
$param ? : 4 1 5
120 }
121  
122 $modes
123 2 }
124  
125 5 1;
126  
127  
128 =pod
129  
130 =head1 NAME
131  
132 IRC::Toolkit::Modes - IRC mode parsing utilities
133  
134 =head1 SYNOPSIS
135  
136 use IRC::Toolkit::Modes;
137 my $mode_string = '+o-o avenj Joah';
138 my $array = mode_to_array( $mode_string );
139 my $hash = mode_to_hash( $mode_string );
140  
141 =head1 DESCRIPTION
142  
143 Utility functions for parsing IRC mode strings.
144  
145 Also see L<IRC::Mode::Set> for an object-oriented approach to modes.
146  
147 =head2 mode_to_array
148  
149 my $array = mode_to_array(
150 ## Mode change string with or without params, e.g. '+kl-t'
151 $mode_string,
152  
153 ## Modes that always have a param:
154 param_always => ARRAY,
155  
156 ## Modes that only have a param when set:
157 param_set => ARRAY,
158  
159 ## Respective params for modes specified above
160 ## (or can be specified as part of mode string)
161 params => ARRAY,
162 );
163  
164 Given a mode string and some options, return an ARRAY of ARRAYs containing
165 parsed mode changes.
166  
167 The structure looks like:
168  
169 [
170 [ FLAG, MODE, MAYBE_PARAM ],
171 [ . . . ],
172 ]
173  
174 For example:
175  
176 mode_to_array( '+kl-t',
177 params => [ 'key', 10 ],
178 param_always => [ split //, 'bkov' ],
179 param_set => [ 'l' ],
180 );
181  
182 ## Result:
183 [
184 [ '+', 'k', 'key' ],
185 [ '+', 'l', 10 ],
186 [ '-', 't' ],
187 ],
188  
189 If the mode string contains (space-delimited) parameters, they are given
190 precedence ahead of the optional 'params' ARRAY.
191  
192 Instead of manually specifying C<param_always> and C<param_set>, you can pass
193 in the B<chanmodes> object provided by L<IRC::Toolkit::ISupport>:
194  
195 my $isupport = parse_isupport(@isupport_lines);
196 my $array = mode_to_array( '+kl-t',
197 params => [ 'key', 10 ],
198 isupport_chanmodes => $isupport->chanmodes,
199 );
200  
201 C<isupport_chanmodes> will override C<param_always> / C<param_set> -- if
202 that's not acceptable, you can select individual sets:
203  
204 my $array = mode_to_array( '+klX-t',
205 params => [ 'key', 10, 'foo' ],
206 param_always => $isupport->chanmodes->always,
207 param_set => [ 'lX' ],
208  
209 =head2 array_to_mode
210  
211 Takes an ARRAY such as that produced by L</mode_to_array> and returns an IRC
212 mode string.
213  
214 =head2 mode_to_hash
215  
216 Takes the same parameters as L</mode_to_array> -- this is just a way to
217 inflate the ARRAY to a hash.
218  
219 Given a mode string and some options, return a HASH with
220 the keys B<add> and B<del>.
221  
222 B<add> and B<del> are HASHes mapping mode characters to either a simple
223 boolean true value or an ARRAY whose only element is the mode's
224 parameters, e.g.:
225  
226 mode_to_hash( '+kl-t',
227 params => [ 'key', 10 ],
228 param_always => [ split //, 'bkov' ],
229 param_set => [ 'l' ],
230 );
231  
232 ## Result:
233 {
234 add => {
235 'l' => [ 10 ],
236 'k' => [ 'key' ],
237 },
238  
239 del => {
240 't' => 1,
241 },
242 }
243  
244 This is a 'lossy' approach that won't deal well with multiple conflicting mode
245 changes in a single line; L</mode_to_array> should generally be preferred.
246  
247 =head1 AUTHOR
248  
249 Jon Portnoy <avenj@cobaltirc.org>
250  
251 =cut
252  
↑ Summary Mode/Set.pm »