Toolkit/Masks.pm

filter: Statementstmt 100% 38 / 38 Branchbran 83.3% 15 / 18 Conditioncond 83.3% 10 / 12 MC/DCmcdc 75.0% 6 / 8 Subroutinesub 100% 7 / 7 Podpod 100% 3 / 3 totaltotal 91.8% 79 / 86 CC 14 SCAR 18.1
CC
14
Cov
93%
normalize_mask
10.0
matches_mask
3.2
parse_user
3.0
CRAP 6.1
« Message/Object.pm ↑ Summary Toolkit/Colors.pm »
1 package IRC::Toolkit::Masks;
2 3 $IRC::Toolkit::Masks::VERSION = '0.092001';
3 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use strictures 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 3 ▶use parent 'Exporter::Tiny';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 our @EXPORT = qw/
8 matches_mask
9 normalize_mask
10 parse_user
11 /;
12  
13 3 ▶use IRC::Toolkit::Case;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15  
16 sub matches_mask {
17 ## Imported from IRC::Utils:
18 3 ▶ my ($mask, $nuh, $casemap) = @_;
Subroutine matches_mask: called CC=3 SCAR=11.6
Pod documented
19 3 ▶ confess "Expected a mask, a string to match, and optional casemap"
Branch: 1 branch
BranchTrueFalseTotal
unless length $mask and defined $nuh 0 3 3
Condition33%1/3
length $mask and defined $nuh
!ll&&!rl&&r
003
MC/DC0%0/2
length $mask and defined $nuh
length $mask defined $nuh
Truth table33%1/3
A and B
A= length $maskB= defined $nuh
ABresult
0X0
100
111
20 unless length $mask and defined $nuh;
21  
22 3 my $quoted = quotemeta uc_irc $mask, $casemap;
23 3 $quoted =~ s/\\\*/[\x01-\xFF]{0,}/g;
24 3 $quoted =~ s/\\\?/[\x01-\xFF]{1,1}/g;
25  
26 3 !! ( uc_irc($nuh, $casemap) =~ /^$quoted$/ )
27 }
28  
29 sub normalize_mask {
30 7 ▶ my ($orig) = @_;
Subroutine normalize_mask: called CC=10 SCAR=23.0
Pod documented
31 7 ▶ confess "normalize_mask expected a mask" unless defined $orig;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $orig 0 7 7
32  
33 ## Inlined with some tweaks from IRC::Utils
34  
35 ## **+ --> *
36 7 $orig =~ s/\*{2,}/*/g;
37  
38 7 my ($piece, @mask);
39 7 ▶ if ( index($orig, '!') == -1 && index($orig, '@') > -1) {
Branch: 1 branch
BranchTrueFalseTotal
if (index($orig, '!') == -1 and index($orig, '@') != -1) { } 4 3 7
Condition100%3/3
index($orig, "!") == -1 and index($orig, "\@") != -1
!ll&&!rl&&r
214
MC/DC100%2/2
index($orig, "!") == -1 and index($orig, "\@") != -1
index($orig, "!") == -1 index($orig, "\@") != -1
Truth table100%3/3
A and B
A= index($orig, "!") == -1B= index($orig, "\@") != -1
ABresult
0X0
100
111
40 # no nick, add '*'
41 4 $piece = $orig;
42 4 @mask = '*';
43 } else {
44 3 ($mask[0], $piece) = split /!/, $orig, 2;
45 }
46  
47 ## user/host
48 7 ▶ if (defined $piece) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $piece) 6 1 7
49 6 $piece =~ s/!//g;
50 6 @mask[1, 2] = split /@/, $piece, 2;
51 }
52 7 ▶ $mask[2] =~ s/@//g if defined $mask[2];
Branch: 1 branch
BranchTrueFalseTotal
if defined $mask[2] 5 2 7
53  
54 7 ▶ $mask[0]
Branch: 2 branches
BranchTrueFalseTotal
defined $mask[1] && length $mask[1] ? : 4 3 7
defined $mask[2] && length $mask[2] ? : 3 4 7
Condition100%6/6
defined $mask[1] and length $mask[1]
!ll&&!rl&&r
214
defined $mask[2] and length $mask[2]
!ll&&!rl&&r
223
MC/DC100%2/2
defined $mask[1] and length $mask[1]
defined $mask[1] length $mask[1]
MC/DC100%2/2
defined $mask[2] and length $mask[2]
defined $mask[2] length $mask[2]
Truth table100%3/3
A and B
A= defined $mask[1]B= length $mask[1]
ABresult
0X0
100
111
Truth table100%3/3
A and B
A= defined $mask[2]B= length $mask[2]
ABresult
0X0
100
111
55 # defined+length is annoying but elsewise we get fatal warnings on 5.10
56 . '!' . (defined $mask[1] && length $mask[1] ? $mask[1] : '*' )
57 . '@' . (defined $mask[2] && length $mask[2] ? $mask[2] : '*' )
58 }
59  
60 sub parse_user {
61 2 ▶ my ($full) = @_;
Subroutine parse_user: called CC=3 SCAR=11.0
Pod documented
62  
63 2 ▶ confess "parse_user() called with no arguments"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $full 0 2 2
64 unless defined $full;
65  
66 2 my ($nick, $user, $host) = split /[!@]/, $full;
67  
68 2 ▶ wantarray ? ($nick, $user, $host) : $nick
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 1 2
69 }
70  
71  
72 3 1;
73  
74 =pod
75  
76 =head1 NAME
77  
78 IRC::Toolkit::Masks - IRC mask-related utilities
79  
80 =head1 SYNOPSIS
81  
82 use IRC::Toolkit::Masks;
83
84 my $mask = '*!avenj@*.cobaltirc.org';
85 my $full = 'avenj!avenj@eris.cobaltirc.org';
86 my $casemap = 'rfc1459';
87 if ( matches_mask($mask, $full, $casemap) ) {
88 ...
89 }
90  
91 my $bmask = normalize_mask( 'somenick' ); # somenick!*@*
92 my $bmask = normalize_mask( 'user@host' ); # *!user@host
93  
94 my ($nick, $user, $host) = parse_user( $full );
95 my $nick = parse_user( $full );
96  
97 =head1 DESCRIPTION
98  
99 IRC mask manipulation utilities derived from L<IRC::Utils>.
100  
101 =head2 matches_mask
102  
103 Takes an IRC mask, a string to match it against, and an optional IRC casemap
104 (see L<IRC::Toolkit::Case>).
105  
106 Returns boolean true if the match applies successfully.
107  
108 =head2 normalize_mask
109  
110 Takes an IRC mask and returns the "normalized" version of the mask.
111  
112 =head2 parse_user
113  
114 Splits an IRC mask into components.
115  
116 Returns all available pieces (nickname, username, and host, if applicable) in
117 list context.
118  
119 Returns just the nickname in scalar context.
120  
121 =head1 AUTHOR
122  
123 Mask-matching and normalization code derived from L<IRC::Utils>,
124 copyright Chris Williams, HINRIK et al.
125  
126 Jon Portnoy <avenj@cobaltirc.org>
127  
128 =cut
129  
« Message/Object.pm ↑ Summary Toolkit/Colors.pm »