| 1 | package Mail::Mbox::MessageParser::Perl; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 51 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 51 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 51 | ▶ | use Mail::Mbox::MessageParser; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 51 | ▶ | use Mail::Mbox::MessageParser::Config; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 51 | ▶ | use vars qw( $VERSION $_DEBUG @ISA ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 51 | @ISA = qw( Exporter Mail::Mbox::MessageParser ); | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 51 | $VERSION = sprintf "%d.%02d%02d", q/1.60.5/ =~ /(\d+)/g; | |||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 51 | *_DEBUG = \$Mail::Mbox::MessageParser::_DEBUG; | |||||||||||||||||||||||||||
| 16 | 51 | *_dprint = \&Mail::Mbox::MessageParser::_dprint; | |||||||||||||||||||||||||||
| 17 | sub _dprint; | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | #------------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | sub new | ||||||||||||||||||||||||||||
| 22 | 406 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||
| 23 | 406 | my ($proto, $self) = @_; | |||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 406 | ▶ | carp "Need file_handle option" unless defined $self->{'file_handle'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 406 | bless ($self, __PACKAGE__); | |||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 406 | $self->_init(); | |||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 406 | return $self; | |||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | #------------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | sub _init | ||||||||||||||||||||||||||||
| 37 | 406 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_init: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 38 | 406 | my $self = shift; | |||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 406 | $self->{'CURRENT_LINE_NUMBER'} = 1; | |||||||||||||||||||||||||||
| 41 | 406 | $self->{'CURRENT_OFFSET'} = 0; | |||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 406 | $self->{'READ_BUFFER'} = ''; | |||||||||||||||||||||||||||
| 44 | 406 | $self->{'START_OF_EMAIL'} = 0; | |||||||||||||||||||||||||||
| 45 | 406 | $self->{'END_OF_EMAIL'} = 0; | |||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | $self->{'READ_CHUNK_SIZE'} = | ||||||||||||||||||||||||||||
| 48 | 406 | $Mail::Mbox::MessageParser::Config{'read_chunk_size'}; | |||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 406 | $self->SUPER::_init(); | |||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | #------------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | sub reset | ||||||||||||||||||||||||||||
| 56 | 40 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
reset: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 57 | 40 | my $self = shift; | |||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 40 | $self->{'CURRENT_LINE_NUMBER'} = ($self->{'prologue'} =~ tr/\n//) + 1; | |||||||||||||||||||||||||||
| 60 | 40 | $self->{'CURRENT_OFFSET'} = length($self->{'prologue'}); | |||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 40 | $self->{'READ_BUFFER'} = ''; | |||||||||||||||||||||||||||
| 63 | 40 | $self->{'START_OF_EMAIL'} = 0; | |||||||||||||||||||||||||||
| 64 | 40 | $self->{'END_OF_EMAIL'} = 0; | |||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 40 | $self->SUPER::reset(); | |||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | #------------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | sub end_of_file | ||||||||||||||||||||||||||||
| 72 | 6329 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
end_of_file: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 73 | 6329 | my $self = shift; | |||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | # Reset eof in case the file was appended to. Hopefully this works all the | ||||||||||||||||||||||||||||
| 76 | # time. See perldoc -f seek for details. | ||||||||||||||||||||||||||||
| 77 | 6329 | ▶ | seek($self->{'file_handle'},0,1) if eof $self->{'file_handle'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | return eof $self->{'file_handle'} && | ||||||||||||||||||||||||||||
| 80 | 6329 | ▶ | $self->{'END_OF_EMAIL'} == length($self->{'READ_BUFFER'}); | ||||||||||||||||||||||||||
|
Condition100%3/3
eof $self->{'file_handle'} && $self->{'END_OF_EMAIL'} == length $self->{'READ_BUFFER'}
MC/DC100%2/2
eof $self->{'file_handle'} && $self->{'END_OF_EMAIL'} == length $self->{'READ_BUFFER'}
eof $self->{'file_handle'}
$self->{'END_OF_EMAIL'} == length $self->{'READ_BUFFER'}
Truth table100%3/3
A && B
A= eof $self->{'file_handle'}B= $self->{'END_OF_EMAIL'} == length $self->{'READ_BUFFER'}
| |||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | #------------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | sub _read_prologue | ||||||||||||||||||||||||||||
| 86 | 406 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_read_prologue: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 87 | 406 | my $self = shift; | |||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 406 | _dprint "Reading mailbox prologue using Perl"; | |||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 406 | $self->_read_until_match( | |||||||||||||||||||||||||||
| 92 | qr/$Mail::Mbox::MessageParser::Config{'from_pattern'}/m,0); | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 406 | my $start_of_email = pos($self->{'READ_BUFFER'}); | |||||||||||||||||||||||||||
| 95 | 406 | $self->{'prologue'} = substr($self->{'READ_BUFFER'}, 0, $start_of_email); | |||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | # Set up for read_next_email | ||||||||||||||||||||||||||||
| 98 | 406 | $self->{'CURRENT_LINE_NUMBER'} += ($self->{'prologue'} =~ tr/\n//); | |||||||||||||||||||||||||||
| 99 | 406 | $self->{'CURRENT_OFFSET'} = $start_of_email; | |||||||||||||||||||||||||||
| 100 | 406 | $self->{'END_OF_EMAIL'} = $start_of_email; | |||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | #------------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | sub read_next_email | ||||||||||||||||||||||||||||
| 106 | 2359 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
read_next_email: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||
| 107 | 2359 | my $self = shift; | |||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 2359 | ▶ | return undef if $self->end_of_file(); ## no critic (ProhibitExplicitReturnUndef) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 2337 | $self->{'email_line_number'} = $self->{'CURRENT_LINE_NUMBER'}; | |||||||||||||||||||||||||||
| 112 | 2337 | $self->{'email_offset'} = $self->{'CURRENT_OFFSET'}; | |||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 2337 | $self->{'START_OF_EMAIL'} = $self->{'END_OF_EMAIL'}; | |||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | # Slurp in an entire multipart email (but continue looking for the next | ||||||||||||||||||||||||||||
| 117 | # header so that we can get any following newlines as well) | ||||||||||||||||||||||||||||
| 118 | 2337 | ▶ | unless ($self->_read_header()) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 119 | { | ||||||||||||||||||||||||||||
| 120 | 0 | return $self->_extract_email_and_finalize(); | |||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 2337 | ▶ | unless ($self->_read_email_parts()) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 124 | { | ||||||||||||||||||||||||||||
| 125 | # Could issue a warning here, but I'm not sure how to do this cleanly for | ||||||||||||||||||||||||||||
| 126 | # a work-only module like this. Maybe something like CGI's cgi_error()? | ||||||||||||||||||||||||||||
| 127 | 62 | _dprint "Inconsistent multi-part message. Could not find ending for " . | |||||||||||||||||||||||||||
| 128 | "boundary \"" . $self->_multipart_boundary() . "\""; | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | # Try to read the content length and use that | ||||||||||||||||||||||||||||
| 131 | my $email_header = substr($self->{'READ_BUFFER'}, $self->{'START_OF_EMAIL'}, | ||||||||||||||||||||||||||||
| 132 | 62 | $self->{'START_OF_BODY'} - $self->{'START_OF_EMAIL'}); | |||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | my $content_length = Mail::Mbox::MessageParser::_GET_HEADER_FIELD( | ||||||||||||||||||||||||||||
| 135 | 62 | \$email_header, 'Content-Length:', $self->{'endline'}); | |||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | 62 | ▶ | if (defined $content_length) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 138 | { | ||||||||||||||||||||||||||||
| 139 | 31 | $content_length =~ s/Content-Length: *(\d+).*/$1/i; | |||||||||||||||||||||||||||
| 140 | 31 | pos($self->{'READ_BUFFER'}) = $self->{'START_OF_EMAIL'} + $content_length; | |||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||
| 142 | # Otherwise use the start of the body | ||||||||||||||||||||||||||||
| 143 | else | ||||||||||||||||||||||||||||
| 144 | { | ||||||||||||||||||||||||||||
| 145 | 31 | pos($self->{'READ_BUFFER'}) = $self->{'START_OF_BODY'}; | |||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | # Reset the search and look for the start of the | ||||||||||||||||||||||||||||
| 149 | # next email. | ||||||||||||||||||||||||||||
| 150 | 62 | $self->_read_rest_of_email(); | |||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | 62 | return $self->_extract_email_and_finalize(); | |||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | 2275 | $self->_read_rest_of_email(); | |||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | 2275 | return $self->_extract_email_and_finalize(); | |||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | #------------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | sub _read_rest_of_email | ||||||||||||||||||||||||||||
| 163 | 2337 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_read_rest_of_email: called CC=10 SCAR=23.0
| |||||||||||||||||||||||||||||
| 164 | 2337 | my $self = shift; | |||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | 2337 | my $previous_backup; | |||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | # Look for the start of the next email | ||||||||||||||||||||||||||||
| 169 | 2337 | while (1) | |||||||||||||||||||||||||||
| 170 | { | ||||||||||||||||||||||||||||
| 171 | 2494 | ▶ | while ($self->{'READ_BUFFER'} =~ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 172 | m/$Mail::Mbox::MessageParser::Config{'from_pattern'}/mg) | ||||||||||||||||||||||||||||
| 173 | { | ||||||||||||||||||||||||||||
| 174 | 3830 | $self->{'END_OF_EMAIL'} = pos($self->{'READ_BUFFER'}) - length($1); | |||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | 3830 | my $endline = $self->{'endline'}; | |||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | # Keep looking if the header we found is part of a "Begin Included | ||||||||||||||||||||||||||||
| 179 | # Message". | ||||||||||||||||||||||||||||
| 180 | 3830 | my $end_of_string = ''; | |||||||||||||||||||||||||||
| 181 | 3830 | my $backup_amount = 100; | |||||||||||||||||||||||||||
| 182 | do | ||||||||||||||||||||||||||||
| 183 | { | ||||||||||||||||||||||||||||
| 184 | 5565 | $backup_amount *= 2; | |||||||||||||||||||||||||||
| 185 | $backup_amount = $self->{'END_OF_EMAIL'} - $self->{'START_OF_EMAIL'} | ||||||||||||||||||||||||||||
| 186 | if $backup_amount > | ||||||||||||||||||||||||||||
| 187 | 5565 | ▶ | $self->{'END_OF_EMAIL'} - $self->{'START_OF_EMAIL'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | $end_of_string = substr($self->{'READ_BUFFER'}, | ||||||||||||||||||||||||||||
| 190 | 5565 | $self->{'END_OF_EMAIL'}-$backup_amount, $backup_amount); | |||||||||||||||||||||||||||
| 191 | } while (index($end_of_string, "$endline$endline") == -1 && | ||||||||||||||||||||||||||||
| 192 | 3830 | ▶ | $backup_amount < $self->{'END_OF_EMAIL'} - $self->{'START_OF_EMAIL'}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
index($end_of_string, "$endline$endline") == -1 and $backup_amount < $self->{"END_OF_EMAIL"} - $self->{"START_OF_EMAIL"}
MC/DC50%1/2
index($end_of_string, "$endline$endline") == -1 and $backup_amount < $self->{"END_OF_EMAIL"} - $self->{"START_OF_EMAIL"}
index($end_of_string, "$endline$endline") == -1
$backup_amount < $self->{"END_OF_EMAIL"} - $self->{"START_OF_EMAIL"}
Truth table66%2/3
A and B
A= index($end_of_string, "$endline$endline") == -1B= $backup_amount < $self->{"END_OF_EMAIL"} - $self->{"START_OF_EMAIL"}
| |||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | 3830 | ▶ | next if $end_of_string =~ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 195 | /$endline-----(?: Begin Included Message |Original Message)-----$endline[^\r\n]*(?:$endline)*$/i; | ||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | 3713 | ▶ | next unless $end_of_string =~ /$endline$endline$/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | # Found the next email! | ||||||||||||||||||||||||||||
| 200 | 1909 | return; | |||||||||||||||||||||||||||
| 201 | } | ||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | # Didn't find next email in current buffer. Most likely we need to read some | ||||||||||||||||||||||||||||
| 204 | # more of the mailbox. Shift the current email to the front of the buffer | ||||||||||||||||||||||||||||
| 205 | # unless we've already done so. | ||||||||||||||||||||||||||||
| 206 | 585 | my $shift_amount = $self->{'START_OF_EMAIL'}; | |||||||||||||||||||||||||||
| 207 | $self->{'READ_BUFFER'} = | ||||||||||||||||||||||||||||
| 208 | 585 | substr($self->{'READ_BUFFER'}, $self->{'START_OF_EMAIL'}); | |||||||||||||||||||||||||||
| 209 | 585 | $self->{'START_OF_EMAIL'} -= $shift_amount; | |||||||||||||||||||||||||||
| 210 | 585 | $self->{'START_OF_BODY'} -= $shift_amount; | |||||||||||||||||||||||||||
| 211 | 585 | pos($self->{'READ_BUFFER'}) = length($self->{'READ_BUFFER'}); | |||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||
| 213 | # Start looking at the end of the buffer, but back up some in case the | ||||||||||||||||||||||||||||
| 214 | # edge of the newly read buffer contains the start of a new header. I | ||||||||||||||||||||||||||||
| 215 | # believe the RFC says header lines can be at most 90 characters long. | ||||||||||||||||||||||||||||
| 216 | 585 | my $backup_amount = 90; | |||||||||||||||||||||||||||
| 217 | $backup_amount = length($self->{'READ_BUFFER'}) - 1 | ||||||||||||||||||||||||||||
| 218 | 585 | ▶ | if length($self->{'READ_BUFFER'}) < $backup_amount; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||
| 220 | # Avoid an infinite loop that can occur if the pattern is within the | ||||||||||||||||||||||||||||
| 221 | # 90-character lookback, but doesn't indicate the start of the next email. | ||||||||||||||||||||||||||||
| 222 | # We detect this case as one where we previously shifted the email to | ||||||||||||||||||||||||||||
| 223 | # the start of the buffer. | ||||||||||||||||||||||||||||
| 224 | 585 | ▶ | if ($shift_amount == 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 225 | 242 | $previous_backup--; | |||||||||||||||||||||||||||
| 226 | 242 | $backup_amount = $previous_backup; | |||||||||||||||||||||||||||
| 227 | } else { | ||||||||||||||||||||||||||||
| 228 | 343 | $previous_backup = $backup_amount; | |||||||||||||||||||||||||||
| 229 | } | ||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||
| 231 | 585 | ▶ | unless ($self->_read_until_match( | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 232 | qr/$Mail::Mbox::MessageParser::Config{'from_pattern'}/m,$backup_amount)) | ||||||||||||||||||||||||||||
| 233 | { | ||||||||||||||||||||||||||||
| 234 | 428 | $self->{'END_OF_EMAIL'} = length($self->{'READ_BUFFER'}); | |||||||||||||||||||||||||||
| 235 | 428 | return; | |||||||||||||||||||||||||||
| 236 | } | ||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||
| 238 | 157 | redo; | |||||||||||||||||||||||||||
| 239 | } | ||||||||||||||||||||||||||||
| 240 | } | ||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||
| 242 | #------------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||
| 244 | sub _multipart_boundary | ||||||||||||||||||||||||||||
| 245 | 2399 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_multipart_boundary: called CC=4 SCAR=14.0
| |||||||||||||||||||||||||||||
| 246 | 2399 | my $self = shift; | |||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||
| 248 | 2399 | my $endline = $self->{'endline'}; | |||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||
| 250 | 2399 | ▶ | if (substr($self->{'READ_BUFFER'},$self->{'START_OF_EMAIL'}, | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 251 | $self->{'START_OF_BODY'}-$self->{'START_OF_EMAIL'}) =~ | ||||||||||||||||||||||||||||
| 252 | /^(content-type: *multipart[^\n\r]*$endline( [^\n\r]*$endline)*)/im) | ||||||||||||||||||||||||||||
| 253 | { | ||||||||||||||||||||||||||||
| 254 | 696 | my $content_type_header = $1; | |||||||||||||||||||||||||||
| 255 | 696 | $content_type_header =~ s/$endline//g; | |||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||
| 257 | 696 | ▶ | if ($content_type_header =~ /boundary *= *"([^"]*)"/i || | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$content_type_header =~ /boundary *= *"([^"]*)"/i or $content_type_header =~ m[boundary *= *([-0-9A-Za-z'()+_,./:=? ]*[-0-9A-Za-z'()+_,./:=?])]i
MC/DC0%0/2
$content_type_header =~ /boundary *= *"([^"]*)"/i or $content_type_header =~ m[boundary *= *([-0-9A-Za-z'()+_,./:=? ]*[-0-9A-Za-z'()+_,./:=?])]i
$content_type_header =~ /boundary *= *"([^"]*)"/i
$content_type_header =~ m[boundary *= *([-0-9A-Za-z'()+_,./:=? ]*[-0-9A-Za-z'()+_,./:=?])]i
Truth table66%2/3
A or B
A= $content_type_header =~ /boundary *= *"([^"]*)"/iB= $content_type_header =~ m[boundary *= *([-0-9A-Za-z'()+_,./:=? ]*[-0-9A-Za-z'()+_,./:=?])]i
| |||||||||||||||||||||||||||||
| 258 | $content_type_header =~ /boundary *= *([-0-9A-Za-z'()+_,.\/:=? ]*[-0-9A-Za-z'()+_,.\/:=?])/i) | ||||||||||||||||||||||||||||
| 259 | { | ||||||||||||||||||||||||||||
| 260 | 696 | return $1 | |||||||||||||||||||||||||||
| 261 | } | ||||||||||||||||||||||||||||
| 262 | } | ||||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||
| 264 | 1703 | return undef; ## no critic (ProhibitExplicitReturnUndef) | |||||||||||||||||||||||||||
| 265 | } | ||||||||||||||||||||||||||||
| 266 | |||||||||||||||||||||||||||||
| 267 | #------------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 268 | |||||||||||||||||||||||||||||
| 269 | sub _read_email_parts | ||||||||||||||||||||||||||||
| 270 | 2337 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_read_email_parts: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 271 | 2337 | my $self = shift; | |||||||||||||||||||||||||||
| 272 | |||||||||||||||||||||||||||||
| 273 | 2337 | my $boundary = $self->_multipart_boundary(); | |||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||
| 275 | 2337 | ▶ | return 1 unless defined $boundary; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||
| 277 | # RFC 1521 says the boundary can be no longer than 70 characters. Back up a | ||||||||||||||||||||||||||||
| 278 | # little more than that. Unlike _read_rest_of_email() we don't have to | ||||||||||||||||||||||||||||
| 279 | # worry about infinite loops here since the pattern is designed to not | ||||||||||||||||||||||||||||
| 280 | # match falsely. | ||||||||||||||||||||||||||||
| 281 | 634 | my $endline = $self->{'endline'}; | |||||||||||||||||||||||||||
| 282 | 634 | ▶ | $self->_read_until_match(qr/^--\Q$boundary\E--$endline/m,76) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 283 | or return 0; | ||||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||
| 285 | 572 | return 1; | |||||||||||||||||||||||||||
| 286 | } | ||||||||||||||||||||||||||||
| 287 | |||||||||||||||||||||||||||||
| 288 | #------------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||
| 290 | sub _extract_email_and_finalize | ||||||||||||||||||||||||||||
| 291 | 2337 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_extract_email_and_finalize: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 292 | 2337 | my $self = shift; | |||||||||||||||||||||||||||
| 293 | |||||||||||||||||||||||||||||
| 294 | 2337 | $self->{'email_length'} = $self->{'END_OF_EMAIL'}-$self->{'START_OF_EMAIL'}; | |||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||
| 296 | my $email = substr($self->{'READ_BUFFER'}, $self->{'START_OF_EMAIL'}, | ||||||||||||||||||||||||||||
| 297 | 2337 | $self->{'email_length'}); | |||||||||||||||||||||||||||
| 298 | |||||||||||||||||||||||||||||
| 299 | 2337 | $self->{'CURRENT_LINE_NUMBER'} += ($email =~ tr/\n//); | |||||||||||||||||||||||||||
| 300 | 2337 | $self->{'CURRENT_OFFSET'} += $self->{'email_length'}; | |||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||
| 302 | 2337 | $self->{'email_number'}++; | |||||||||||||||||||||||||||
| 303 | |||||||||||||||||||||||||||||
| 304 | 2337 | $self->SUPER::read_next_email(); | |||||||||||||||||||||||||||
| 305 | |||||||||||||||||||||||||||||
| 306 | 2337 | return \$email; | |||||||||||||||||||||||||||
| 307 | } | ||||||||||||||||||||||||||||
| 308 | |||||||||||||||||||||||||||||
| 309 | #------------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 310 | |||||||||||||||||||||||||||||
| 311 | sub _read_header | ||||||||||||||||||||||||||||
| 312 | 2337 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_read_header: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||
| 313 | 2337 | my $self = shift; | |||||||||||||||||||||||||||
| 314 | |||||||||||||||||||||||||||||
| 315 | 2337 | ▶ | $self->_read_until_match(qr/$self->{'endline'}$self->{'endline'}/m,0) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 316 | or return 0; | ||||||||||||||||||||||||||||
| 317 | |||||||||||||||||||||||||||||
| 318 | $self->{'START_OF_BODY'} = | ||||||||||||||||||||||||||||
| 319 | 2337 | pos($self->{'READ_BUFFER'}) + length("$self->{'endline'}$self->{'endline'}"); | |||||||||||||||||||||||||||
| 320 | |||||||||||||||||||||||||||||
| 321 | 2337 | return 1; | |||||||||||||||||||||||||||
| 322 | } | ||||||||||||||||||||||||||||
| 323 | |||||||||||||||||||||||||||||
| 324 | #------------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 325 | |||||||||||||||||||||||||||||
| 326 | # The search position is at the start of the pattern when this function | ||||||||||||||||||||||||||||
| 327 | # returns 1. | ||||||||||||||||||||||||||||
| 328 | sub _read_until_match | ||||||||||||||||||||||||||||
| 329 | 3962 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_read_until_match: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||
| 330 | 3962 | my $self = shift; | |||||||||||||||||||||||||||
| 331 | 3962 | my $pattern = shift; | |||||||||||||||||||||||||||
| 332 | 3962 | my $backup = shift; | |||||||||||||||||||||||||||
| 333 | |||||||||||||||||||||||||||||
| 334 | # Start looking at the end of the buffer, but back up some in case the edge | ||||||||||||||||||||||||||||
| 335 | # of the newly read buffer contains part of the pattern. | ||||||||||||||||||||||||||||
| 336 | 3962 | ▶ | if (!defined pos($self->{'READ_BUFFER'}) || | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not defined pos $self->{"READ_BUFFER"} or pos($self->{"READ_BUFFER"}) - $backup <= 0
MC/DC100%2/2
not defined pos $self->{"READ_BUFFER"} or pos($self->{"READ_BUFFER"}) - $backup <= 0
not defined pos $self->{"READ_BUFFER"}
pos($self->{"READ_BUFFER"}) - $backup <= 0
Truth table100%3/3
not(A) or B
A= not defined pos $self->{"READ_BUFFER"}B= pos($self->{"READ_BUFFER"}) - $backup <= 0
| |||||||||||||||||||||||||||||
| 337 | pos($self->{'READ_BUFFER'}) - $backup <= 0) { | ||||||||||||||||||||||||||||
| 338 | 817 | pos($self->{'READ_BUFFER'}) = 0; | |||||||||||||||||||||||||||
| 339 | } else { | ||||||||||||||||||||||||||||
| 340 | 3145 | pos($self->{'READ_BUFFER'}) -= $backup; | |||||||||||||||||||||||||||
| 341 | } | ||||||||||||||||||||||||||||
| 342 | |||||||||||||||||||||||||||||
| 343 | 3962 | while (1) | |||||||||||||||||||||||||||
| 344 | { | ||||||||||||||||||||||||||||
| 345 | 4833 | ▶ | if ($self->{'READ_BUFFER'} =~ m/($pattern)/mg) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 346 | { | ||||||||||||||||||||||||||||
| 347 | 3472 | pos($self->{'READ_BUFFER'}) -= length($1); | |||||||||||||||||||||||||||
| 348 | 3472 | return 1; | |||||||||||||||||||||||||||
| 349 | } | ||||||||||||||||||||||||||||
| 350 | |||||||||||||||||||||||||||||
| 351 | 1361 | pos($self->{'READ_BUFFER'}) = length($self->{'READ_BUFFER'}); | |||||||||||||||||||||||||||
| 352 | |||||||||||||||||||||||||||||
| 353 | 1361 | ▶ | unless ($self->_read_chunk()) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 354 | 490 | $self->{'END_OF_EMAIL'} = length($self->{'READ_BUFFER'}); | |||||||||||||||||||||||||||
| 355 | 490 | return 0; | |||||||||||||||||||||||||||
| 356 | } | ||||||||||||||||||||||||||||
| 357 | |||||||||||||||||||||||||||||
| 358 | 871 | ▶ | if (pos($self->{'READ_BUFFER'}) - $backup <= 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 359 | 446 | pos($self->{'READ_BUFFER'}) = 0; | |||||||||||||||||||||||||||
| 360 | } else { | ||||||||||||||||||||||||||||
| 361 | 425 | pos($self->{'READ_BUFFER'}) -= $backup; | |||||||||||||||||||||||||||
| 362 | } | ||||||||||||||||||||||||||||
| 363 | } | ||||||||||||||||||||||||||||
| 364 | } | ||||||||||||||||||||||||||||
| 365 | |||||||||||||||||||||||||||||
| 366 | #------------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 367 | |||||||||||||||||||||||||||||
| 368 | # Maintains pos($self->{'READ_BUFFER'}) | ||||||||||||||||||||||||||||
| 369 | sub _read_chunk | ||||||||||||||||||||||||||||
| 370 | 1361 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_read_chunk: called CC=6 SCAR=19.2
| |||||||||||||||||||||||||||||
| 371 | 1361 | my $self = shift; | |||||||||||||||||||||||||||
| 372 | |||||||||||||||||||||||||||||
| 373 | 1361 | my $search_position = pos($self->{'READ_BUFFER'}); | |||||||||||||||||||||||||||
| 374 | |||||||||||||||||||||||||||||
| 375 | # Can't use sysread because it doesn't work with ungetc | ||||||||||||||||||||||||||||
| 376 | 1361 | ▶ | if ($self->{'READ_CHUNK_SIZE'} == 0) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 377 | { | ||||||||||||||||||||||||||||
| 378 | 0 | local $/ = undef; | |||||||||||||||||||||||||||
| 379 | |||||||||||||||||||||||||||||
| 380 | 0 | ▶ | return 0 if eof $self->{'file_handle'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 381 | |||||||||||||||||||||||||||||
| 382 | # < $self->{'file_handle'} > doesn't work, so we use readline | ||||||||||||||||||||||||||||
| 383 | 0 | $self->{'READ_BUFFER'} = readline($self->{'file_handle'}); | |||||||||||||||||||||||||||
| 384 | 0 | pos($self->{'READ_BUFFER'}) = $search_position; | |||||||||||||||||||||||||||
| 385 | 0 | return 1; | |||||||||||||||||||||||||||
| 386 | } | ||||||||||||||||||||||||||||
| 387 | else | ||||||||||||||||||||||||||||
| 388 | { | ||||||||||||||||||||||||||||
| 389 | 1361 | my $total_amount_read = 0; | |||||||||||||||||||||||||||
| 390 | 1361 | my $amount_read = 0; | |||||||||||||||||||||||||||
| 391 | |||||||||||||||||||||||||||||
| 392 | 1361 | ▶ | while ($total_amount_read < $self->{'READ_CHUNK_SIZE'}) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 393 | { | ||||||||||||||||||||||||||||
| 394 | $amount_read = read($self->{'file_handle'}, $self->{'READ_BUFFER'}, | ||||||||||||||||||||||||||||
| 395 | $self->{'READ_CHUNK_SIZE'} - $total_amount_read, | ||||||||||||||||||||||||||||
| 396 | 1793 | length($self->{'READ_BUFFER'})); | |||||||||||||||||||||||||||
| 397 | |||||||||||||||||||||||||||||
| 398 | 1793 | pos($self->{'READ_BUFFER'}) = $search_position; | |||||||||||||||||||||||||||
| 399 | |||||||||||||||||||||||||||||
| 400 | 1793 | ▶ | if ($amount_read == 0) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 401 | { | ||||||||||||||||||||||||||||
| 402 | 922 | ▶ | return 1 unless $total_amount_read == 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 403 | |||||||||||||||||||||||||||||
| 404 | 490 | return 0; | |||||||||||||||||||||||||||
| 405 | } | ||||||||||||||||||||||||||||
| 406 | |||||||||||||||||||||||||||||
| 407 | 871 | $total_amount_read += $amount_read; | |||||||||||||||||||||||||||
| 408 | } | ||||||||||||||||||||||||||||
| 409 | |||||||||||||||||||||||||||||
| 410 | 439 | return 1; | |||||||||||||||||||||||||||
| 411 | } | ||||||||||||||||||||||||||||
| 412 | } | ||||||||||||||||||||||||||||
| 413 | |||||||||||||||||||||||||||||
| 414 | #------------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 415 | |||||||||||||||||||||||||||||
| 416 | 51 | 1; | |||||||||||||||||||||||||||
| 417 | |||||||||||||||||||||||||||||
| 418 | __END__ | ||||||||||||||||||||||||||||