blib/lib/Mail/Pyzor/Digest/Pieces.pm

filter: Statementstmt 48.1% 38 / 79 Branchbran 28.5% 4 / 14 Conditioncond 33.3% 3 / 9 MC/DCmcdc 40.0% 2 / 5 Subroutinesub 71.4% 10 / 14 Podpod 100% 6 / 6 totaltotal 49.6% 63 / 127 CC 14 SCAR 26.3
CC
14
Cov
44%
parse_content_type
42.0
digest_payloads
20.0
assemble_lines
5.0
CRAP 13.8
« blib/lib/Mail/Pyzor/Digest.pm ↑ Summary blib/lib/Mail/Pyzor/Digest/StripHtml.pm »
1 package Mail::Pyzor::Digest::Pieces;
2  
3 # Copyright 2018 cPanel, LLC.
4 # All rights reserved.
5 # http://cpanel.net
6 #
7 # <@LICENSE>
8 # Licensed to the Apache Software Foundation (ASF) under one or more
9 # contributor license agreements. See the NOTICE file distributed with
10 # this work for additional information regarding copyright ownership.
11 # The ASF licenses this file to you under the Apache License, Version 2.0
12 # (the "License"); you may not use this file except in compliance with
13 # the License. You may obtain a copy of the License at:
14 #
15 # http://www.apache.org/licenses/LICENSE-2.0
16 #
17 # Unless required by applicable law or agreed to in writing, software
18 # distributed under the License is distributed on an "AS IS" BASIS,
19 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 # See the License for the specific language governing permissions and
21 # limitations under the License.
22 # </@LICENSE>
23 #
24  
25 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 =encoding utf-8
29  
30 =head1 NAME
31  
32 Mail::Pyzor::Digest::Pieces
33  
34 =head1 DESCRIPTION
35  
36 This module houses backend logic for L<Mail::Pyzor::Digest>.
37  
38 It reimplements logic found in pyzor’s F<digest.py> module
39 (L<https://github.com/SpamExperts/pyzor/blob/master/pyzor/digest.py>).
40  
41 =cut
42  
43 #----------------------------------------------------------------------
44  
45 2 ▶use Email::MIME::ContentType ();
Subroutine BEGIN: called CC=1 SCAR=0.0
46 2 ▶use Encode ();
Subroutine BEGIN: called CC=1 SCAR=0.0
47  
48 2 our $VERSION = '0.06';
49  
50 # each tuple is [ offset, length ]
51 2 ▶use constant _HASH_SPEC => ( [ 20, 3 ], [ 60, 3 ] );
Subroutine BEGIN: called CC=1 SCAR=0.0
52  
53 2 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
54 2 _MIN_LINE_LENGTH => 8,
55  
56 _ATOMIC_NUM_LINES => 4,
57 2 };
58  
59 #----------------------------------------------------------------------
60  
61 =head1 FUNCTIONS
62  
63 =head2 $strings_ar = digest_payloads( $EMAIL_MIME )
64  
65 This imitates the corresponding object method in F<digest.py>.
66 It returns a reference to an array of strings. Each string can be either
67 a byte string or a character string (e.g., UTF-8 decoded).
68  
69 NB: RFC 2822 stipulates that message bodies should use CRLF
70 line breaks, not plain LF (nor plain CR). L<Email::MIME::Encodings>
71 will thus convert any plain CRs in a quoted-printable message
72 body into CRLF. Python, though, doesn’t do this, so the output of
73 our implementation of C<digest_payloads()> diverges from that of the Python
74 original. It doesn’t ultimately make a difference since the line-ending
75 whitespace gets trimmed regardless, but it’s necessary to factor in when
76 comparing the output of our implementation with the Python output.
77  
78 =cut
79  
80 0 ▶sub digest_payloads {
Subroutine digest_payloads: not called CC=4 SCAR=30.0
Pod documented
81 0 my ($parsed) = @_;
82  
83 0 my @subparts = $parsed->subparts();
84  
85 0 my @payloads;
86  
87 0 ▶ if (@subparts) {
Branch: 1 branch
BranchTrueFalseTotal
if (@subparts) { } 0 0 0
88 0 @payloads = map { @{ digest_payloads($_) } } $parsed->subparts();
89 }
90 else {
91 0 my ( $main_type, $subtype, $encoding, $encode_check ) = parse_content_type( $parsed->content_type() );
92  
93 0 my $payload;
94  
95 0 ▶ if ( $main_type eq 'text' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($main_type eq 'text') { } 0 0 0
96  
97 # Decode transfer encoding, but leave us as a byte string.
98 # Note that this is where Email::MIME converts plain LF to CRLF.
99 0 $payload = $parsed->body();
100  
101 # This does the actual character decoding (i.e., “charset”).
102 0 $payload = Encode::decode( $encoding, $payload, $encode_check );
103  
104 0 ▶ if ( $subtype eq 'html' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($subtype eq "html") 0 0 0
105 0 require Mail::Pyzor::Digest::StripHtml;
106 0 $payload = Mail::Pyzor::Digest::StripHtml::strip($payload);
107 }
108 }
109 else {
110  
111 # This does no decoding, even of, e.g., quoted-printable or base64.
112 0 $payload = $parsed->body_raw();
113 }
114  
115 0 push @payloads, $payload;
116 }
117  
118 0 return \@payloads;
119 }
120  
121 #----------------------------------------------------------------------
122  
123 =head2 normalize( $STRING )
124  
125 This imitates the corresponding object method in F<digest.py>.
126 It modifies C<$STRING> in-place.
127  
128 As with the original implementation, if C<$STRING> contains (decoded)
129 Unicode characters, those characters will be parsed accordingly. So:
130  
131 $str = "123\xc2\xa0"; # [ c2 a0 ] == \u00a0, non-breaking space
132  
133 normalize($str);
134  
135 The above will leave C<$str> alone, but this:
136  
137 utf8::decode($str);
138  
139 normalize($str);
140  
141 … will trim off the last two bytes from C<$str>.
142  
143 =cut
144  
145 0 ▶sub normalize { ## no critic qw( Subroutines::RequireArgUnpacking )
Subroutine normalize: not called CC=1 SCAR=6.9
Pod documented
146  
147 # NULs are bad, mm-kay?
148 0 $_[0] =~ tr<\0><>d;
149  
150 # NB: Python’s \s without re.UNICODE is the same as Perl’s \s
151 # with the /a modifier.
152 #
153 # https://docs.python.org/2/library/re.html
154 # https://perldoc.perl.org/perlrecharclass.html#Backslash-sequences
155  
156 # Python: re.compile(r'\S{10,}')
157 0 $_[0] =~ s<\S{10,}><>ag;
158  
159 # Python: re.compile(r'\S+@\S+')
160 0 $_[0] =~ s<\S+ @ \S+><>agx;
161  
162 # Python: re.compile(r'[a-z]+:\S+', re.IGNORECASE)
163 0 $_[0] =~ s<[a-zA-Z]+ : \S+><>agx;
164  
165 # (from digest.py …)
166 # Make sure we do the whitespace last because some of the previous
167 # patterns rely on whitespace.
168 0 $_[0] =~ tr< \x09-\x0d><>d;
169  
170 # This is fun. digest.py’s normalize() does a non-UNICODE whitespace
171 # strip, then calls strip() on the string, which *will* strip Unicode
172 # whitespace from the ends.
173 0 $_[0] =~ s<\A\s+><>;
174 0 $_[0] =~ s<\s+\z><>;
175  
176 0 return;
177 }
178  
179 #----------------------------------------------------------------------
180  
181 =head2 $yn = should_handle_line( $STRING )
182  
183 This imitates the corresponding object method in F<digest.py>.
184 It returns a boolean.
185  
186 =cut
187  
188 6 ▶sub should_handle_line {
Subroutine should_handle_line: called CC=2 SCAR=6.9
Pod documented
189 6 ▶ return $_[0] && length( $_[0] ) >= _MIN_LINE_LENGTH();
Condition100%3/3
$_[0] && length $_[0] >= 8
!ll&&!rl&&r
123
MC/DC100%2/2
$_[0] && length $_[0] >= 8
$_[0] length $_[0] >= 8
Truth table100%3/3
A && B
A= $_[0]B= length $_[0] >= 8
ABresult
0X0
100
111
190 }
191  
192 #----------------------------------------------------------------------
193  
194 =head2 $sr = assemble_lines( \@LINES )
195  
196 This assembles a string buffer out of @LINES. The string is the buffer
197 of octets that will be hashed to produce the message digest.
198  
199 Each member of @LINES is expected to be an B<octet string>, not a
200 character string.
201  
202 =cut
203  
204 4 ▶sub assemble_lines {
Subroutine assemble_lines: called CC=5 SCAR=16.1
Pod documented
205 4 my ($lines_ar) = @_;
206  
207 4 ▶ if ( @$lines_ar <= _ATOMIC_NUM_LINES() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$lines_ar <= 4) 1 3 4
208  
209 # cf. handle_atomic() in digest.py
210 1 return \join( q<>, @$lines_ar );
211 }
212  
213 #----------------------------------------------------------------------
214 # cf. handle_atomic() in digest.py
215  
216 3 my $str = q<>;
217  
218 3 for my $ofs_len ( _HASH_SPEC() ) {
219 6 my ( $offset, $length ) = @$ofs_len;
220  
221 6 for my $i ( 0 .. ( $length - 1 ) ) {
222 18 my $idx = int( $offset * @$lines_ar / 100 ) + $i;
223  
224 18 ▶ next if !defined $lines_ar->[$idx];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $lines_ar->[$idx] 1 17 18
225  
226 17 $str .= $lines_ar->[$idx];
227 }
228 }
229  
230 3 return \$str;
231 }
232  
233 #----------------------------------------------------------------------
234  
235 =head2 ($main, $sub, $encoding, $checkval) = parse_content_type( $CONTENT_TYPE )
236  
237 =cut
238  
239 2 ▶use constant _QUOTED_PRINTABLE_NAMES => (
Subroutine BEGIN: called CC=1 SCAR=0.0
240 "quopri-codec",
241 "quopri",
242 "quoted-printable",
243 "quotedprintable",
244 2 );
245  
246 # Make Encode::decode() ignore anything that doesn’t fit the
247 # given encoding.
248 2 ▶use constant _encode_check_ignore => q<>;
Subroutine BEGIN: called CC=1 SCAR=0.0
249  
250 0 ▶sub parse_content_type {
Subroutine parse_content_type: not called CC=6 SCAR=37.4
Pod documented
251 0 my ($content_type) = @_;
252  
253 0 my $ct_parse = Email::MIME::ContentType::parse_content_type(
254 $content_type,
255 );
256  
257 0 ▶ my $main = $ct_parse->{'type'} || q<>;
Condition0%0/2
$ct_parse->{'type'} || ''
l!l
00
MC/DC0%0/1
$ct_parse->{'type'} || ''
$ct_parse->{'type'}
Truth table0%0/2
A
A= $ct_parse->{'type'}
Aresult
11
00
258 0 ▶ my $sub = $ct_parse->{'subtype'} || q<>;
Condition0%0/2
$ct_parse->{'subtype'} || ''
l!l
00
MC/DC0%0/1
$ct_parse->{'subtype'} || ''
$ct_parse->{'subtype'}
Truth table0%0/2
A
A= $ct_parse->{'subtype'}
Aresult
11
00
259  
260 0 my $encoding = $ct_parse->{'attributes'}{'charset'};
261  
262 0 my $checkval;
263  
264 0 ▶ if ($encoding) {
Branch: 1 branch
BranchTrueFalseTotal
if ($encoding) { } 0 0 0
265  
266 # Lower-case everything, convert underscore to dash, and remove NUL.
267 0 $encoding =~ tr<A-Z_\0><a-z->d;
268  
269 # Apparently pyzor accommodates messages that put the transfer
270 # encoding in the Content-Type.
271 0 ▶ if ( grep { $_ eq $encoding } _QUOTED_PRINTABLE_NAMES() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$_ eq $encoding;} ("quopri-codec", "quopri", "quoted-printable", "quotedprintable")) 0 0 0
272 0 $checkval = Encode::FB_CROAK();
273 }
274 }
275 else {
276 0 $encoding = 'ascii';
277 }
278  
279 # Match Python .decode()’s 'ignore' behavior
280 0 ▶ $checkval ||= \&_encode_check_ignore;
Condition0%0/2
$checkval ||= \&Mail::Pyzor::Digest::Pieces::_encode_check_ignore
l!l
00
MC/DC0%0/1
$checkval ||= \&Mail::Pyzor::Digest::Pieces::_encode_check_ignore
$checkval
Truth table0%0/2
A
A= $checkval
Aresult
11
00
281  
282 0 return ( $main, $sub, $encoding, $checkval );
283 }
284  
285 #----------------------------------------------------------------------
286  
287 =head2 @lines = splitlines( $TEXT )
288  
289 Imitates C<str.splitlines()>. (cf. C<pydoc str>)
290  
291 Returns a plain list in list context. Returns the number of
292 items to be returned in scalar context.
293  
294 =cut
295  
296 0 ▶sub splitlines {
Subroutine splitlines: not called CC=1 SCAR=6.9
Pod documented
297 0 return split m<\r\n?|\n>, $_[0];
298 }
299  
300 2 1;
« blib/lib/Mail/Pyzor/Digest.pm ↑ Summary blib/lib/Mail/Pyzor/Digest/StripHtml.pm »