SPF/Term.pm

filter: Statementstmt 92.5% 148 / 160 Branchbran 35.7% 10 / 28 Conditioncond 20.0% 3 / 15 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 96.8% 31 / 32 Podpod 10.0% 1 / 10 totaltotal 76.2% 193 / 253 CC 20 SCAR 15.9
CC
20
Cov
79%
parse_ipv4_prefix_length
17.5
parse_ipv6_prefix_length
11.2
text
6.0
CRAP 4.9
« SPF/Mech/Include.pm ↑ Summary SPF/Mech.pm »
1 #
2 # Mail::SPF::Term
3 # SPF record term class.
4 #
5 # (C) 2005-2012 Julian Mehnle <julian@mehnle.net>
6 # 2005 Shevek <cpan@anarres.org>
7 # $Id: Term.pm 57 2012-01-30 08:15:31Z julian $
8 #
9 ##############################################################################
10  
11 package Mail::SPF::Term;
12  
13 =head1 NAME
14  
15 Mail::SPF::Term - SPF record term class
16  
17 =head1 VERSION
18  
19 version 3.20261105
20  
21 =cut
22  
23 6 our $VERSION = '3.20261105'; # VERSION
24  
25 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 6 ▶use utf8; # Hack to keep Perl 5.6 from whining about /[\p{}]/.
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 6 ▶use base 'Mail::SPF::Base';
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 use overload
33 6 ▶ '""' => 'stringify',
Subroutine BEGIN: called CC=1 SCAR=0.0
34 6 fallback => 1;
35  
36 6 ▶use NetAddr::IP;
Subroutine BEGIN: called CC=1 SCAR=0.0
37  
38 6 ▶use constant TRUE => (0 == 0);
Subroutine BEGIN: called CC=1 SCAR=0.0
39 6 ▶use constant FALSE => not TRUE;
Subroutine BEGIN: called CC=1 SCAR=0.0
40  
41 6 ▶use constant name_pattern => qr/ \p{IsAlpha} [\p{IsAlnum}\-_.]* /x;
Subroutine BEGIN: called CC=1 SCAR=0.0
42  
43 6 ▶use constant macro_literal_pattern => qr/[!-\$&-~]/;
Subroutine BEGIN: called CC=1 SCAR=0.0
44 6 ▶use constant macro_delimiter => qr/[.\-+,\/_=]/;
Subroutine BEGIN: called CC=1 SCAR=0.0
45 6 ▶use constant macro_transformers_pattern => qr/\d*r?/;
Subroutine BEGIN: called CC=1 SCAR=0.0
46 6 ▶use constant macro_expand_pattern => qr/
Subroutine BEGIN: called CC=1 SCAR=0.0
47 \%
48 (?:
49 6 { \p{IsAlpha} ${\macro_transformers_pattern} ${\macro_delimiter}* } |
50 [%_-]
51 )
52 6 /x;
53  
54 6 ▶use constant macro_string_pattern => qr/
Subroutine BEGIN: called CC=1 SCAR=0.0
55 (?:
56 6 ${\macro_expand_pattern} |
57 6 ${\macro_literal_pattern}
58 )*
59 6 /x;
60  
61 6 ▶use constant toplabel_pattern => qr/
Subroutine BEGIN: called CC=1 SCAR=0.0
62 \p{IsAlnum}+ - [\p{IsAlnum}-]* \p{IsAlnum} |
63 \p{IsAlnum}* \p{IsAlpha} \p{IsAlnum}*
64 6 /x;
65  
66 6 ▶use constant domain_end_pattern => qr/
Subroutine BEGIN: called CC=1 SCAR=0.0
67 6 \. ${\toplabel_pattern} \.? |
68 6 ${\macro_expand_pattern}
69 6 /x;
70  
71 6 ▶use constant domain_spec_pattern => qr/ ${\macro_string_pattern} ${\domain_end_pattern} /x;
Subroutine BEGIN: called CC=1 SCAR=0.0
72  
73 6 ▶use constant qnum_pattern => qr/ 25[0-5] | 2[0-4]\d | 1\d\d | [1-9]\d | \d /x;
Subroutine BEGIN: called CC=1 SCAR=0.0
74 6 ▶use constant ipv4_address_pattern => qr/ ${\qnum_pattern} (?: \. ${\qnum_pattern} ){3} /x;
Subroutine BEGIN: called CC=1 SCAR=0.0
75  
76 6 ▶use constant hexword_pattern => qr/\p{IsXDigit}{1,4}/;
Subroutine BEGIN: called CC=1 SCAR=0.0
77 6 ▶use constant two_hexwords_or_ipv4_address_pattern => qr/
Subroutine BEGIN: called CC=1 SCAR=0.0
78 6 ${\hexword_pattern} : ${\hexword_pattern} | ${\ipv4_address_pattern}
79 6 /x;
80 6 ▶use constant ipv6_address_pattern => qr/
Subroutine BEGIN: called CC=1 SCAR=0.0
81 # x:x:x:x:x:x:x:x | x:x:x:x:x:x:n.n.n.n
82 6 (?: ${\hexword_pattern} : ){6} ${\two_hexwords_or_ipv4_address_pattern} |
83 # x::x:x:x:x:x:x | x::x:x:x:x:n.n.n.n
84 6 (?: ${\hexword_pattern} : ){1} : (?: ${\hexword_pattern} : ){4} ${\two_hexwords_or_ipv4_address_pattern} |
85 # x[:x]::x:x:x:x:x | x[:x]::x:x:x:n.n.n.n
86 6 (?: ${\hexword_pattern} : ){1,2} : (?: ${\hexword_pattern} : ){3} ${\two_hexwords_or_ipv4_address_pattern} |
87 # x[:...]::x:x:x:x | x[:...]::x:x:n.n.n.n
88 6 (?: ${\hexword_pattern} : ){1,3} : (?: ${\hexword_pattern} : ){2} ${\two_hexwords_or_ipv4_address_pattern} |
89 # x[:...]::x:x:x | x[:...]::x:n.n.n.n
90 6 (?: ${\hexword_pattern} : ){1,4} : (?: ${\hexword_pattern} : ){1} ${\two_hexwords_or_ipv4_address_pattern} |
91 # x[:...]::x:x | x[:...]::n.n.n.n
92 6 (?: ${\hexword_pattern} : ){1,5} : ${\two_hexwords_or_ipv4_address_pattern} |
93 # x[:...]::x | -
94 6 (?: ${\hexword_pattern} : ){1,6} : ${\hexword_pattern} |
95 # x[:...]:: | -
96 6 (?: ${\hexword_pattern} : ){1,7} : |
97 # ::[...:]x | -
98 6 :: (?: ${\hexword_pattern} : ){0,6} ${\hexword_pattern} |
99 # - | ::[...:]n.n.n.n
100 6 :: (?: ${\hexword_pattern} : ){0,5} ${\two_hexwords_or_ipv4_address_pattern} |
101 # :: | -
102 ::
103 6 /x;
104  
105 =head1 DESCRIPTION
106  
107 An object of class B<Mail::SPF::Term> represents a term within an SPF record.
108 Mail::SPF::Term cannot be instantiated directly. Create an instance of a
109 concrete sub-class instead.
110  
111 =head2 Constructor
112  
113 The following constructor is provided:
114  
115 =over
116  
117 =item B<new(%options)>: returns I<Mail::SPF::Term>
118  
119 I<Abstract>. Creates a new SPF record term object.
120  
121 %options is a list of key/value pairs, however Mail::SPF::Term itself specifies
122 no constructor options.
123  
124 =item B<new_from_string($text, %options)>: returns I<Mail::SPF::Term>;
125 throws I<Mail::SPF::ENothingToParse>, I<Mail::SPF::EInvalidTerm>
126  
127 I<Abstract>. Creates a new SPF record term object by parsing the string and
128 any options given.
129  
130 =cut
131  
132 62 ▶sub new_from_string {
Subroutine new_from_string: called CC=1 SCAR=0.0
Pod documented
133 62 my ($self, $text, %options) = @_;
134 62 $self = $self->new(%options, text => $text);
135 62 $self->parse();
136 62 return $self;
137 }
138  
139 =back
140  
141 =head2 Class methods
142  
143 The following class methods are provided:
144  
145 =over
146  
147 =item B<name_pattern>: returns I<Regexp>
148  
149 Returns a regular expression that matches any legal name for an SPF record
150 term.
151  
152 =back
153  
154 =head2 Instance methods
155  
156 The following instance methods are provided:
157  
158 =over
159  
160 =cut
161  
162 24 ▶sub parse_domain_spec {
Subroutine parse_domain_spec: called CC=3 SCAR=11.1
Pod undocumented
163 24 my ($self, $required) = @_;
164 24 ▶ if ($self->{parse_text} =~ s/^(${\$self->domain_spec_pattern})//) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'parse_text'} =~ s/^(${\$self->domain_spec_pattern;})//) { } 19 5 24
elsif ($required) { } 0 5 5
165 19 my $domain_spec = $1;
166 19 $domain_spec =~ s/^(.*?)\.?$/\L$1/;
167 19 $self->{domain_spec} = Mail::SPF::MacroString->new(text => $domain_spec);
168 }
169 elsif ($required) {
170 0 throw Mail::SPF::ETermDomainSpecExpected(
171 "Missing required domain-spec in '" . $self->text . "'");
172 }
173 24 return;
174 }
175  
176 3 ▶sub parse_ipv4_address {
Subroutine parse_ipv4_address: called CC=3 SCAR=12.7
Pod undocumented
177 3 my ($self, $required) = @_;
178 3 ▶ if ($self->{parse_text} =~ s/^(${\$self->ipv4_address_pattern})//) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'parse_text'} =~ s/^(${\$self->ipv4_address_pattern;})//) { } 3 0 3
elsif ($required) { } 0 0 0
179 3 $self->{ip_address} = $1;
180 }
181 elsif ($required) {
182 0 throw Mail::SPF::ETermIPv4AddressExpected(
183 "Missing required IPv4 address in '" . $self->text . "'");
184 }
185 3 return;
186 }
187  
188 9 ▶sub parse_ipv4_prefix_length {
Subroutine parse_ipv4_prefix_length: called CC=6 SCAR=28.6
Pod undocumented
189 9 my ($self, $required) = @_;
190 9 ▶ if ($self->{parse_text} =~ s#^/(\d+)##) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'parse_text'} =~ s[^/(\d+)][]) { } 0 9 9
elsif (not $required) { } 9 0 9
191 0 ▶ $1 >= 0 and $1 <= 32 and $1 !~ /^0./
Branch: 1 branch
BranchTrueFalseTotal
$1 >= 0 and $1 <= 32 and not $1 =~ /^0./ or "Mail::SPF::ETermIPv4PrefixLengthExpected"->throw(q[Invalid IPv4 prefix length encountered in '] . $self->text . q[']) 0 0 0
Condition0%0/6
$1 >= 0 and $1 <= 32 and not $1 =~ /^0./
!ll&&!rl&&r
000
$1 >= 0 and $1 <= 32
!ll&&!rl&&r
000
MC/DC0%0/3
$1 >= 0 and $1 <= 32 and not $1 =~ /^0./
$1 >= 0 $1 <= 32 not $1 =~ /^0./
Truth table0%0/4
A and B and not(C)
A= $1 >= 0B= $1 <= 32C= not $1 =~ /^0./
ABCresult
0XX0
10X0
1100
1111
192 or throw Mail::SPF::ETermIPv4PrefixLengthExpected(
193 "Invalid IPv4 prefix length encountered in '" . $self->text . "'");
194 0 $self->{ipv4_prefix_length} = $1;
195 }
196 elsif (not $required) {
197 9 $self->{ipv4_prefix_length} = $self->default_ipv4_prefix_length;
198 }
199 else {
200 0 throw Mail::SPF::ETermIPv4PrefixLengthExpected(
201 "Missing required IPv4 prefix length in '" . $self->text . "'");
202 }
203 9 return;
204 }
205  
206 3 ▶sub parse_ipv4_network {
Subroutine parse_ipv4_network: called CC=1 SCAR=0.0
Pod undocumented
207 3 my ($self, $required) = @_;
208 3 $self->parse_ipv4_address($required);
209 3 $self->parse_ipv4_prefix_length();
210 3 $self->{ip_network} = NetAddr::IP->new($self->{ip_address}, $self->{ipv4_prefix_length});
211 3 return;
212 }
213  
214 3 ▶sub parse_ipv6_address {
Subroutine parse_ipv6_address: called CC=3 SCAR=12.7
Pod undocumented
215 3 my ($self, $required) = @_;
216 3 ▶ if ($self->{parse_text} =~ s/^(${\$self->ipv6_address_pattern})(?=\/|$)//) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'parse_text'} =~ s[^(${\$self->ipv6_address_pattern;})(?=/|$)][]) { } 3 0 3
elsif ($required) { } 0 0 0
217 3 $self->{ip_address} = $1;
218 }
219 elsif ($required) {
220 0 throw Mail::SPF::ETermIPv6AddressExpected(
221 "Missing required IPv6 address in '" . $self->text . "'");
222 }
223 3 return;
224 }
225  
226 3 ▶sub parse_ipv6_prefix_length {
Subroutine parse_ipv6_prefix_length: called CC=6 SCAR=24.2
Pod undocumented
227 3 my ($self, $required) = @_;
228 3 ▶ if ($self->{parse_text} =~ s#^/(\d+)##) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'parse_text'} =~ s[^/(\d+)][]) { } 3 0 3
elsif (not $required) { } 0 0 0
229 3 ▶ $1 >= 0 and $1 <= 128 and $1 !~ /^0./
Branch: 1 branch
BranchTrueFalseTotal
$1 >= 0 and $1 <= 128 and not $1 =~ /^0./ or "Mail::SPF::ETermIPv6PrefixLengthExpected"->throw(q[Invalid IPv6 prefix length encountered in '] . $self->text . q[']) 3 0 3
Condition33%2/6
$1 >= 0 and $1 <= 128 and not $1 =~ /^0./
!ll&&!rl&&r
003
$1 >= 0 and $1 <= 128
!ll&&!rl&&r
003
MC/DC0%0/3
$1 >= 0 and $1 <= 128 and not $1 =~ /^0./
$1 >= 0 $1 <= 128 not $1 =~ /^0./
Truth table25%1/4
A and B and not(C)
A= $1 >= 0B= $1 <= 128C= not $1 =~ /^0./
ABCresult
0XX0
10X0
1100
1111
230 or throw Mail::SPF::ETermIPv6PrefixLengthExpected(
231 "Invalid IPv6 prefix length encountered in '" . $self->text . "'");
232 3 $self->{ipv6_prefix_length} = $1;
233 }
234 elsif (not $required) {
235 0 $self->{ipv6_prefix_length} = $self->default_ipv6_prefix_length;
236 }
237 else {
238 0 throw Mail::SPF::ETermIPv6PrefixLengthExpected(
239 "Missing required IPv6 prefix length in '" . $self->text . "'");
240 }
241 3 return;
242 }
243  
244 3 ▶sub parse_ipv6_network {
Subroutine parse_ipv6_network: called CC=1 SCAR=0.0
Pod undocumented
245 3 my ($self, $required) = @_;
246 3 $self->parse_ipv6_address($required);
247 3 $self->parse_ipv6_prefix_length();
248 $self->{ip_network} = NetAddr::IP->new(
249 3 $self->{ip_address}, $self->{ipv6_prefix_length});
250 3 return;
251 }
252  
253 6 ▶sub parse_ipv4_ipv6_prefix_lengths {
Subroutine parse_ipv4_ipv6_prefix_lengths: called CC=3 SCAR=12.7
Pod undocumented
254 6 my ($self) = @_;
255 6 $self->parse_ipv4_prefix_length();
256 6 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"ipv4_prefix_length"} and $self->{"parse_text"} =~ s[^/][]) 0 6 6
Condition33%1/3
defined $self->{"ipv4_prefix_length"} and $self->{"parse_text"} =~ s[^/][]
!ll&&!rl&&r
060
MC/DC0%0/2
defined $self->{"ipv4_prefix_length"} and $self->{"parse_text"} =~ s[^/][]
defined $self->{"ipv4_prefix_length"} $self->{"parse_text"} =~ s[^/][]
Truth table33%1/3
A and B
A= defined $self->{"ipv4_prefix_length"}B= $self->{"parse_text"} =~ s[^/][]
ABresult
0X0
100
111
257 defined($self->{ipv4_prefix_length}) and # an IPv4 prefix length has been parsed, and
258 $self->{parse_text} =~ s#^/## # another slash is following
259 ) {
260 # Parse an IPv6 prefix length:
261 0 $self->parse_ipv6_prefix_length(TRUE);
262 }
263 6 return;
264 }
265  
266 =item B<text>: returns I<string>; throws I<Mail::SPF::ENoUnparsedText>
267  
268 Returns the unparsed text of the term. Throws a I<Mail::SPF::ENoUnparsedText>
269 exception if the term was created synthetically instead of being parsed, and no
270 text was provided.
271  
272 =cut
273  
274 0 ▶sub text {
Subroutine text: not called CC=2 SCAR=17.9
Pod undocumented
275 0 my ($self) = @_;
276 defined($self->{text})
277 0 ▶ or throw Mail::SPF::ENoUnparsedText;
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{"text"} or "Mail::SPF::ENoUnparsedText"->throw 0 0 0
278 0 return $self->{text};
279 }
280  
281 =item B<name>: returns I<string>
282  
283 I<Abstract>. Returns the name of the term.
284  
285 =back
286  
287 =head1 SEE ALSO
288  
289 L<Mail::SPF>, L<Mail::SPF::Record>, L<Mail::SPF::Mech>, L<Mail::SPF::Mod>
290  
291 L<http://tools.ietf.org/html/rfc4408>
292  
293 For availability, support, and license information, see the README file
294 included with Mail::SPF.
295  
296 =head1 AUTHORS
297  
298 Julian Mehnle <julian@mehnle.net>, Shevek <cpan@anarres.org>
299  
300 =cut
301  
302 6 TRUE;
« SPF/Mech/Include.pm ↑ Summary SPF/Mech.pm »