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