blib/lib/Mail/Mbox/MessageParser.pm

filter: Statementstmt 83.3% 296 / 355 Branchbran 56.3% 115 / 204 Conditioncond 33.3% 29 / 87 MC/DCmcdc 9.6% 5 / 52 Subroutinesub 92.6% 38 / 41 Podpod 100% 10 / 10 totaltotal 65.8% 493 / 749 CC 134 SCAR 33.8
CC
134
Cov
68%
_GET_ENDLINE
63.2
new
58.5
_GET_FILE_TYPE
51.9
CRAP 29.5
↑ Summary blib/lib/Mail/Mbox/MessageParser/Grep.pm »
1 package Mail::Mbox::MessageParser;
2  
3 45 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 45 ▶use 5.005;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 45 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 45 ▶use FileHandle::Unget;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 45 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 45 ▶use File::Temp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 sub _dprint;
10  
11 45 ▶use Mail::Mbox::MessageParser::MetaInfo;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 45 ▶use Mail::Mbox::MessageParser::Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 45 ▶use Mail::Mbox::MessageParser::Perl;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 45 ▶use Mail::Mbox::MessageParser::Grep;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 45 ▶use Mail::Mbox::MessageParser::Cache;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 45 ▶use vars qw( @ISA $VERSION $_DEBUG );
Subroutine BEGIN: called CC=1 SCAR=0.0
19 45 ▶use vars qw( $_CACHE $UPDATING_CACHE );
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 45 @ISA = qw(Exporter);
22  
23 45 $VERSION = sprintf "%d.%02d%02d", q/1.51.9/ =~ /(\d+)/g;
24 45 $_DEBUG = 0;
25  
26 #-------------------------------------------------------------------------------
27  
28 # The class-wide cache, which will be read and written when necessary. i.e.
29 # read when an folder reader object is created which uses caching, and
30 # written when a different cache is specified, or when the program exits,
31 45 *_CACHE = \$Mail::Mbox::MessageParser::MetaInfo::_CACHE;
32  
33 45 *UPDATING_CACHE = \$Mail::Mbox::MessageParser::MetaInfo::UPDATING_CACHE;
34 45 *SETUP_CACHE = \&Mail::Mbox::MessageParser::MetaInfo::SETUP_CACHE;
35 sub SETUP_CACHE;
36  
37 #-------------------------------------------------------------------------------
38  
39 # Outputs debug messages if $_DEBUG is true.
40  
41 sub _dprint
42 {
43 5250 ▶ return 1 unless $_DEBUG;
Subroutine _dprint: called CC=3 SCAR=18.4
Branch: 1 branch
BranchTrueFalseTotal
unless $Mail::Mbox::MessageParser::_DEBUG 5250 0 5250
44  
45 0 my $message = join '',@_;
46  
47 0 foreach my $line (split /\n/, $message)
48 {
49 0 warn "DEBUG (" . __PACKAGE__ . "): $line\n";
50 }
51  
52 # Be sure to return 1 so code like '_dprint "blah\n" and exit' works.
53 0 return 1;
54 }
55  
56 #-------------------------------------------------------------------------------
57  
58 sub new
59 {
60 372 ▶ my ($proto, $options, $cache_options) = @_;
Subroutine new: called CC=26 SCAR=40.7
Pod documented
61  
62 372 ▶ my $class = ref($proto) || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
03720
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
63  
64 carp "You must provide either a file name or a file handle"
65 372 ▶ unless defined $options->{'file_name'} || defined $options->{'file_handle'};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $options->{"file_name"} or defined $options->{"file_handle"} 0 372 372
Condition66%2/3
defined $options->{"file_name"} or defined $options->{"file_handle"}
l!l&&r!l&&!r
357150
MC/DC0%0/2
defined $options->{"file_name"} or defined $options->{"file_handle"}
defined $options->{"file_name"} defined $options->{"file_handle"}
Truth table66%2/3
A or B
A= defined $options->{"file_name"}B= defined $options->{"file_handle"}
ABresult
1X1
011
000
66  
67 # Can't use grep or cache unless there is a filename
68 372 ▶ unless (defined $options->{'file_name'})
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $options->{"file_name"}) 15 357 372
69 {
70 15 $options->{'enable_cache'} = 0;
71 15 $options->{'enable_grep'} = 0;
72 }
73  
74 $_DEBUG = $options->{'debug'}
75 372 ▶ if defined $options->{'debug'};
Branch: 1 branch
BranchTrueFalseTotal
if defined $options->{"debug"} 0 372 372
76  
77 372 my ($file_type, $need_to_close_filehandle, $error, $endline);
78  
79 ($options->{'file_handle'}, $file_type, $need_to_close_filehandle, $error, $endline) =
80 372 _PREPARE_FILE_HANDLE($options->{'file_name'}, $options->{'file_handle'});
81  
82 362 ▶ if (defined $error &&
Branch: 1 branch
BranchTrueFalseTotal
if (defined $error and not $error eq "Not a mailbox" && $options->{"force_processing"} and not $error =~ /Found a mix of unix and Windows line endings/ && $options->{"force_processing"}) 0 362 362
Condition16%2/12
defined $error and not $error eq "Not a mailbox" && $options->{"force_processing"} and not $error =~ /Found a mix of unix and Windows line endings/ && $options->{"force_processing"}
!ll&&!rl&&r
36200
defined $error and not $error eq "Not a mailbox" && $options->{"force_processing"}
!ll&&!rl&&r
36200
$error eq 'Not a mailbox' && $options->{'force_processing'}
!ll&&!rl&&r
000
$error =~ /Found a mix of unix and Windows line endings/ && $options->{'force_processing'}
!ll&&!rl&&r
000
MC/DC0%0/5
defined $error and not $error eq "Not a mailbox" && $options->{"force_processing"} and not $error =~ /Found a mix of unix and Windows line endings/ && $options->{"force_processing"}
defined $error $error eq 'Not a mailbox' $options->{'force_processing'} $error =~ /Found a mix of unix and Windows line endings/ $options->{'force_processing'}
Truth table12%1/8
A and not(B && C) and not(D && E)
A= defined $errorB= $error eq 'Not a mailbox'C= $options->{'force_processing'}D= $error =~ /Found a mix of unix and Windows line endings/E= $options->{'force_processing'}
ABCDEresult
0XXXX0
111XX0
10X110
110110
10X0X1
10X101
1100X1
110101
83 !($error eq 'Not a mailbox' && $options->{'force_processing'}) &&
84 !($error =~ 'Found a mix of unix and Windows line endings' && $options->{'force_processing'})
85 )
86 {
87 # Here I assume the only errors for which the filehandle was opened is
88 # "Not a mailbox" and mixed line endings
89 0 ▶ close $options->{'file_handle'}
Branch: 1 branch
BranchTrueFalseTotal
if $error eq "Not a mailbox" or $error =~ /Found a mix of unix and Windows line endings/ 0 0 0
Condition0%0/3
$error eq "Not a mailbox" or $error =~ /Found a mix of unix and Windows line endings/
l!l&&r!l&&!r
000
MC/DC0%0/2
$error eq "Not a mailbox" or $error =~ /Found a mix of unix and Windows line endings/
$error eq "Not a mailbox" $error =~ /Found a mix of unix and Windows line endings/
Truth table0%0/3
A or B
A= $error eq "Not a mailbox"B= $error =~ /Found a mix of unix and Windows line endings/
ABresult
1X1
011
000
90 if $error eq 'Not a mailbox' || $error =~ /Found a mix of unix and Windows line endings/;
91 0 return $error;
92 }
93  
94 # Grep implementation doesn't support compression right now
95 362 ▶ $options->{'enable_grep'} = 0 if _IS_COMPRESSED_TYPE($file_type);
Branch: 1 branch
BranchTrueFalseTotal
if _IS_COMPRESSED_TYPE($file_type) 40 322 362
96  
97 362 ▶ $options->{'enable_cache'} = 1 unless defined $options->{'enable_cache'};;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $options->{"enable_cache"} 0 362 362
98 362 ▶ $options->{'enable_grep'} = 1 unless defined $options->{'enable_grep'};;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $options->{"enable_grep"} 0 362 362
99  
100 362 my $self = undef;
101  
102 362 ▶ if ($options->{'enable_cache'})
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{"enable_cache"}) 227 135 362
103 {
104 227 $self = new Mail::Mbox::MessageParser::Cache($options, $cache_options);
105  
106 227 ▶ unless (ref $self)
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $self) 0 227 227
107 {
108 0 warn "Couldn't instantiate Mail::Mbox::MessageParser::Cache: $self";
109 0 $self = undef;
110 }
111  
112 227 ▶ if ($UPDATING_CACHE)
Branch: 1 branch
BranchTrueFalseTotal
if ($Mail::Mbox::MessageParser::UPDATING_CACHE) 227 0 227
113 {
114 227 _dprint "Couldn't instantiate Mail::Mbox::MessageParser::Cache: " .
115 "Updating cache";
116 227 $self = undef;
117 }
118 }
119  
120 362 ▶ if (!defined $self && $options->{'enable_grep'})
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $self and $options->{"enable_grep"}) 15 347 362
Condition66%2/3
not defined $self and $options->{"enable_grep"}
!ll&&!rl&&r
034715
MC/DC50%1/2
not defined $self and $options->{"enable_grep"}
not defined $self $options->{"enable_grep"}
Truth table66%2/3
not(A) and B
A= not defined $selfB= $options->{"enable_grep"}
ABresult
0X0
100
111
121 {
122 15 $self = new Mail::Mbox::MessageParser::Grep($options);
123  
124 15 ▶ unless (ref $self)
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $self) 15 0 15
125 {
126 15 ▶ if ($self =~ /not installed/)
Branch: 1 branch
BranchTrueFalseTotal
if ($self =~ /not installed/) { } 15 0 15
127 {
128 15 _dprint "Couldn't instantiate Mail::Mbox::MessageParser::Grep: $self";
129 }
130 else
131 {
132 0 warn "Couldn't instantiate Mail::Mbox::MessageParser::Grep: $self";
133 }
134 15 $self = undef;
135 }
136 }
137  
138 362 ▶ if (!defined $self)
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self) 362 0 362
139 {
140 362 $self = new Mail::Mbox::MessageParser::Perl($options);
141  
142 362 ▶ warn "Couldn't instantiate Mail::Mbox::MessageParser::Perl: $self"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 0 362 362
143 unless ref $self;
144 }
145  
146 362 ▶ die "Couldn't instantiate any mailbox parser implementation"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self 0 362 362
147 unless defined $self;
148  
149 362 _dprint "Instantiate mailbox parser implementation: " . ref $self;
150  
151 362 $self->_print_debug_information();
152  
153 362 $self->_read_prologue();
154  
155 362 $self->{'need_to_close_filehandle'} = $need_to_close_filehandle;
156  
157 362 $self->{'endline'} = $endline;
158  
159 362 return $self;
160 }
161  
162 #-------------------------------------------------------------------------------
163  
164 sub _init
165 {
166 589 ▶ my $self = shift;
Subroutine _init: called CC=1 SCAR=0.0
167  
168 589 $self->{'email_line_number'} = 0;
169 589 $self->{'email_offset'} = 0;
170 589 $self->{'email_length'} = 0;
171 589 $self->{'email_number'} = 0;
172 }
173  
174 #-------------------------------------------------------------------------------
175  
176 sub DESTROY
177 {
178 362 ▶ my $self = shift;
Subroutine DESTROY: called CC=2 SCAR=6.9
179  
180 362 ▶ $self->{'file_handle'}->close() if $self->{'need_to_close_filehandle'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"need_to_close_filehandle"} 23 339 362
181 }
182  
183 #-------------------------------------------------------------------------------
184  
185 # Returns:
186 # - a file handle to the decompressed mailbox
187 # - the file type (see _GET_FILE_TYPE)
188 # - a boolean indicating whether the caller needs to close the file handle
189 # - an error message (or undef)
190 # - the endline: "\n", "\r\n", or undef
191 sub _PREPARE_FILE_HANDLE
192 {
193 372 ▶ my $file_name = shift;
Subroutine _PREPARE_FILE_HANDLE: called CC=12 SCAR=25.3
194 372 my $file_handle = shift;
195  
196 372 _dprint "Preparing file handle";
197  
198 372 ▶ if (defined $file_handle)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $file_handle) { } 349 23 372
199 {
200 # Promote this to a FileHandle::Unget if it isn't already
201 349 ▶ $file_handle = new FileHandle::Unget($file_handle)
Branch: 1 branch
BranchTrueFalseTotal
unless UNIVERSAL::isa($file_handle, "FileHandle::Unget") 248 101 349
202 unless UNIVERSAL::isa($file_handle, 'FileHandle::Unget');
203  
204 349 binmode $file_handle;
205  
206 349 my $file_type = _GET_FILE_TYPE(\$file_handle);
207 349 _dprint "Filehandle file type: $file_type";
208  
209 # Do decompression if we need to
210 349 ▶ if (_IS_COMPRESSED_TYPE($file_type))
Branch: 1 branch
BranchTrueFalseTotal
if (_IS_COMPRESSED_TYPE($file_type)) { } 48 301 349
211 {
212 48 my ($decompressed_file_handle,$error) =
213 _DO_DECOMPRESSION($file_handle, $file_type);
214  
215 38 ▶ return ($file_handle,$file_type,0,$error,undef)
Branch: 1 branch
BranchTrueFalseTotal
unless defined $decompressed_file_handle 0 38 38
216 unless defined $decompressed_file_handle;
217  
218 38 ▶ return ($decompressed_file_handle,$file_type,0,"Not a mailbox",undef)
Branch: 1 branch
BranchTrueFalseTotal
if _GET_FILE_TYPE(\$decompressed_file_handle) ne "mailbox" 0 38 38
219 if _GET_FILE_TYPE(\$decompressed_file_handle) ne 'mailbox';
220  
221 38 my $endline;
222 38 ($endline, $error) = _GET_ENDLINE(\$decompressed_file_handle);
223  
224 38 return ($decompressed_file_handle,$file_type,0,$error,$endline);
225 }
226 else
227 {
228 301 _dprint "Filehandle is not compressed";
229  
230 301 my ($endline, $error) = _GET_ENDLINE(\$file_handle);
231  
232 301 ▶ return ($file_handle,$file_type,0,"Not a mailbox",$endline)
Branch: 1 branch
BranchTrueFalseTotal
if not eof $file_handle and $file_type ne "mailbox" 0 301 301
Condition33%1/3
not eof $file_handle and $file_type ne "mailbox"
!ll&&!rl&&r
03010
MC/DC0%0/2
not eof $file_handle and $file_type ne "mailbox"
not eof $file_handle $file_type ne "mailbox"
Truth table33%1/3
not(A) and B
A= not eof $file_handleB= $file_type ne "mailbox"
ABresult
0X0
100
111
233 if !eof($file_handle) && $file_type ne 'mailbox';
234  
235 301 return ($file_handle,$file_type,0,$error,$endline);
236 }
237 }
238 else
239 {
240 23 my $file_type = _GET_FILE_TYPE(\$file_name);
241 23 _dprint "Filename \"$file_name\" file type: $file_type";
242  
243 23 my ($opened_file_handle,$error) =
244 _OPEN_FILE_HANDLE($file_name, $file_type);
245  
246 23 ▶ return ($file_handle,$file_type,0,$error,undef)
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opened_file_handle 0 23 23
247 unless defined $opened_file_handle;
248  
249 23 my $endline;
250 23 ($endline, $error) = _GET_ENDLINE(\$opened_file_handle);
251  
252 23 ▶ if (_IS_COMPRESSED_TYPE($file_type))
Branch: 1 branch
BranchTrueFalseTotal
if (_IS_COMPRESSED_TYPE($file_type)) { } 2 21 23
253 {
254 2 ▶ return ($opened_file_handle,$file_type,1,"Not a mailbox",$endline)
Branch: 1 branch
BranchTrueFalseTotal
if _GET_FILE_TYPE(\$opened_file_handle) ne "mailbox" 0 2 2
255 if _GET_FILE_TYPE(\$opened_file_handle) ne 'mailbox';
256  
257 2 return ($opened_file_handle,$file_type,1,$error,$endline);
258 }
259 else
260 {
261 21 ▶ return ($opened_file_handle,$file_type,1,"Not a mailbox",$endline)
Branch: 1 branch
BranchTrueFalseTotal
if $file_type ne "mailbox" 0 21 21
262 if $file_type ne 'mailbox';
263  
264 21 return ($opened_file_handle,$file_type,1,$error,$endline);
265 }
266 }
267 }
268  
269 #-------------------------------------------------------------------------------
270  
271 # This function does not analyze the file to determine if it is valid. It only
272 # opens it using a suitable decompresson if necessary.
273 sub _OPEN_FILE_HANDLE
274 {
275 23 ▶ my $file_name = shift;
Subroutine _OPEN_FILE_HANDLE: called CC=9 SCAR=23.0
276 23 my $file_type = shift;
277  
278 23 _dprint "Opening file \"$file_name\"";
279  
280 # Non-compressed file
281 23 ▶ unless (_IS_COMPRESSED_TYPE($file_type))
Branch: 1 branch
BranchTrueFalseTotal
unless (_IS_COMPRESSED_TYPE($file_type)) 21 2 23
282 {
283 21 my $file_handle = new FileHandle::Unget($file_name);
284 21 ▶ return (undef,"Can't open $file_name: $!") unless defined $file_handle;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $file_handle 0 21 21
285  
286 21 binmode $file_handle;
287  
288 21 _dprint "File \"$file_name\" is not compressed";
289  
290 21 return ($file_handle,undef);
291 }
292  
293 # It must be a known compressed file type
294 return (undef,"Can't decompress $file_name--no decompressor available")
295 2 ▶ unless defined $Mail::Mbox::MessageParser::Config{'programs'}{$file_type};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $Mail::Mbox::MessageParser::Config{"programs"}{$file_type} 0 2 2
296  
297 2 my $filter_command = qq{"$Mail::Mbox::MessageParser::Config{'programs'}{$file_type}" -cd "$file_name" |};
298  
299 2 _dprint "Calling \"$filter_command\" to decompress file \"$file_name\".";
300  
301 2 my $oldstderr;
302 2 ▶ open $oldstderr,">&STDERR" or die "Can't save STDERR: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open $oldstderr, ">&STDERR" or die "Can't save STDERR: $!\n" 0 2 2
303 2 ▶ open STDERR,">" . File::Spec->devnull()
Branch: 1 branch
BranchTrueFalseTotal
open STDERR, '>' . "File::Spec"->devnull or die q[Can't redirect STDERR to ] . "File::Spec"->devnull . ": $!\n" 0 2 2
304 or die "Can't redirect STDERR to " . File::Spec->devnull() . ": $!\n";
305  
306 2 my $file_handle = new FileHandle::Unget($filter_command);
307  
308 2 ▶ return (undef,"Can't execute \"$filter_command\" for file \"$file_name\": $!")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $file_handle 0 2 2
309 unless defined $file_handle;
310  
311 2 binmode $file_handle;
312  
313 2 ▶ open STDERR, '>&', $oldstderr or die "Can't restore STDERR: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open STDERR, ">&", $oldstderr or die "Can't restore STDERR: $!\n" 0 2 2
314  
315 2 ▶ if (eof($file_handle))
Branch: 1 branch
BranchTrueFalseTotal
if (eof $file_handle) 0 2 2
316 {
317 0 $file_handle->close();
318 0 return (undef,"Can't execute \"$filter_command\" for file \"$file_name\"");
319 }
320  
321 2 return ($file_handle, undef);
322 }
323  
324 #-------------------------------------------------------------------------------
325  
326 # Returns: unknown, unknown binary, mailbox, non-mailbox ascii, bzip,
327 # bzip2, gzip, compress
328 sub _GET_FILE_TYPE
329 {
330 412 ▶ my $file_name_or_handle_ref = shift;
Subroutine _GET_FILE_TYPE: called CC=24 SCAR=39.5
331  
332 # Open the file if we need to
333 412 my $file_handle_ref;
334 412 my $need_to_close_filehandle = 0;
335  
336 412 ▶ if (ref $file_name_or_handle_ref eq 'SCALAR')
Branch: 1 branch
BranchTrueFalseTotal
if (ref $file_name_or_handle_ref eq 'SCALAR') { } 23 389 412
337 {
338 23 my $temp = new FileHandle::Unget($$file_name_or_handle_ref);
339 23 ▶ return 'unknown' unless defined $temp;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $temp 0 23 23
340 23 $file_handle_ref = \$temp;
341  
342 23 $need_to_close_filehandle = 1;
343 }
344 else
345 {
346 389 $file_handle_ref = $file_name_or_handle_ref;
347 }
348  
349
350 # Read test characters
351 412 my $test_chars = '';
352 412 my $readResult;
353  
354 412 ▶ while(index($test_chars,"\n\n") == -1 && index($test_chars,"\r\n\r\n") == -1)
Branch: 1 branch
BranchTrueFalseTotal
if (index($test_chars, "\n\n") == -1 and index($test_chars, "\r\n\r\n") == -1) 427 362 789
Condition100%3/3
index($test_chars, "\n\n") == -1 and index($test_chars, "\r\n\r\n") == -1
!ll&&!rl&&r
32933427
MC/DC100%2/2
index($test_chars, "\n\n") == -1 and index($test_chars, "\r\n\r\n") == -1
index($test_chars, "\n\n") == -1 index($test_chars, "\r\n\r\n") == -1
Truth table100%3/3
A and B
A= index($test_chars, "\n\n") == -1B= index($test_chars, "\r\n\r\n") == -1
ABresult
0X0
100
111
355 {
356 427 $readResult =
357 read($$file_handle_ref,$test_chars,4000,CORE::length($test_chars));
358  
359 427 ▶ last unless defined $readResult && $readResult != 0;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $readResult and $readResult != 0 0 427 427
Condition33%1/3
defined $readResult and $readResult != 0
!ll&&!rl&&r
00427
MC/DC0%0/2
defined $readResult and $readResult != 0
defined $readResult $readResult != 0
Truth table33%1/3
A and B
A= defined $readResultB= $readResult != 0
ABresult
0X0
100
111
360  
361 427 ▶ last if _IS_BINARY_MAILBOX(\$test_chars);
Branch: 1 branch
BranchTrueFalseTotal
if _IS_BINARY_MAILBOX(\$test_chars) 50 377 427
362  
363 377 ▶ if(CORE::length($test_chars) >
Branch: 1 branch
BranchTrueFalseTotal
if (CORE::length $test_chars > $Mail::Mbox::MessageParser::Config{"max_testchar_buffer_size"}) 0 377 377
364 $Mail::Mbox::MessageParser::Config{'max_testchar_buffer_size'})
365 {
366 0 ▶ if(index($test_chars,"\n\n") == -1 && index($test_chars,"\r\n\r\n") == -1)
Branch: 1 branch
BranchTrueFalseTotal
if (index($test_chars, "\n\n") == -1 and index($test_chars, "\r\n\r\n") == -1) 0 0 0
Condition0%0/3
index($test_chars, "\n\n") == -1 and index($test_chars, "\r\n\r\n") == -1
!ll&&!rl&&r
000
MC/DC0%0/2
index($test_chars, "\n\n") == -1 and index($test_chars, "\r\n\r\n") == -1
index($test_chars, "\n\n") == -1 index($test_chars, "\r\n\r\n") == -1
Truth table0%0/3
A and B
A= index($test_chars, "\n\n") == -1B= index($test_chars, "\r\n\r\n") == -1
ABresult
0X0
100
111
367 {
368 0 _dprint "Couldn't find end of first paragraph after " .
369 "$Mail::Mbox::MessageParser::Config{'max_testchar_buffer_size'} bytes."
370 }
371  
372 0 last;
373 }
374 }
375  
376  
377 412 ▶ if($need_to_close_filehandle)
Branch: 1 branch
BranchTrueFalseTotal
if ($need_to_close_filehandle) { } 23 389 412
378 {
379 23 $$file_handle_ref->close();
380 }
381 else
382 {
383 389 $$file_handle_ref->ungets($test_chars);
384 }
385  
386 412 ▶ return 'unknown' unless defined $readResult && $readResult != 0;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $readResult and $readResult != 0 0 412 412
Condition33%1/3
defined $readResult and $readResult != 0
!ll&&!rl&&r
00412
MC/DC0%0/2
defined $readResult and $readResult != 0
defined $readResult $readResult != 0
Truth table33%1/3
A and B
A= defined $readResultB= $readResult != 0
ABresult
0X0
100
111
387  
388  
389 412 ▶ unless (_IS_BINARY_MAILBOX(\$test_chars))
Branch: 1 branch
BranchTrueFalseTotal
unless (_IS_BINARY_MAILBOX(\$test_chars)) 362 50 412
390 {
391 362 ▶ return 'mailbox' if _IS_MAILBOX(\$test_chars);
Branch: 1 branch
BranchTrueFalseTotal
if _IS_MAILBOX(\$test_chars) 362 0 362
392 0 return 'non-mailbox ascii';
393 }
394  
395 # See "magic" on unix systems for details on how to identify file types
396 50 ▶ return 'bzip2' if substr($test_chars, 0, 3) eq 'BZh';
Branch: 1 branch
BranchTrueFalseTotal
if substr($test_chars, 0, 3) eq "BZh" 0 50 50
397 50 ▶ return 'bzip' if substr($test_chars, 0, 2) eq 'BZ';
Branch: 1 branch
BranchTrueFalseTotal
if substr($test_chars, 0, 2) eq "BZ" 0 50 50
398 50 ▶ return 'xz' if substr($test_chars, 1, 4) eq '7zXZ';
Branch: 1 branch
BranchTrueFalseTotal
if substr($test_chars, 1, 4) eq "7zXZ" 17 33 50
399 33 ▶ return 'lzip' if substr($test_chars, 0, 4) eq 'LZIP';
Branch: 1 branch
BranchTrueFalseTotal
if substr($test_chars, 0, 4) eq "LZIP" 0 33 33
400 # return 'zip' if substr($test_chars, 0, 2) eq 'PK' &&
401 # ord(substr($test_chars,3,1)) == 0003 && ord(substr($test_chars,4,1)) == 0004;
402 33 ▶ return 'gzip' if
Branch: 1 branch
BranchTrueFalseTotal
if ord substr($test_chars, 0, 1) == 31 and ord substr($test_chars, 1, 1) == 139 33 0 33
Condition33%1/3
ord substr($test_chars, 0, 1) == 31 and ord substr($test_chars, 1, 1) == 139
!ll&&!rl&&r
0033
MC/DC0%0/2
ord substr($test_chars, 0, 1) == 31 and ord substr($test_chars, 1, 1) == 139
ord substr($test_chars, 0, 1) == 31 ord substr($test_chars, 1, 1) == 139
Truth table33%1/3
A and B
A= ord substr($test_chars, 0, 1) == 31B= ord substr($test_chars, 1, 1) == 139
ABresult
0X0
100
111
403 ord(substr($test_chars,0,1)) == oct(37) && ord(substr($test_chars,1,1)) == oct(213);
404 0 ▶ return 'compress' if
Branch: 1 branch
BranchTrueFalseTotal
if ord substr($test_chars, 0, 1) == 31 and ord substr($test_chars, 1, 1) == 157 0 0 0
Condition0%0/3
ord substr($test_chars, 0, 1) == 31 and ord substr($test_chars, 1, 1) == 157
!ll&&!rl&&r
000
MC/DC0%0/2
ord substr($test_chars, 0, 1) == 31 and ord substr($test_chars, 1, 1) == 157
ord substr($test_chars, 0, 1) == 31 ord substr($test_chars, 1, 1) == 157
Truth table0%0/3
A and B
A= ord substr($test_chars, 0, 1) == 31B= ord substr($test_chars, 1, 1) == 157
ABresult
0X0
100
111
405 ord(substr($test_chars,0,1)) == oct(37) && ord(substr($test_chars,1,1)) == oct(235);
406  
407 0 return 'unknown binary';
408 }
409  
410 #-------------------------------------------------------------------------------
411  
412 # Returns an endline result of either: undef, "\r\n", "\n"
413 # Returns an error message (or undef) as well
414 sub _GET_ENDLINE
415 {
416 362 ▶ my $file_name_or_handle_ref = shift;
Subroutine _GET_ENDLINE: called CC=23 SCAR=41.5
417  
418 # Open the file if we need to
419 362 my $file_handle_ref;
420 362 my $need_to_close_filehandle = 0;
421  
422 362 ▶ if (ref $file_name_or_handle_ref eq 'SCALAR')
Branch: 1 branch
BranchTrueFalseTotal
if (ref $file_name_or_handle_ref eq 'SCALAR') { } 0 362 362
423 {
424 0 my $temp = new FileHandle::Unget($$file_name_or_handle_ref);
425 0 ▶ return 'unknown' unless defined $temp;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $temp 0 0 0
426 0 $file_handle_ref = \$temp;
427  
428 0 $need_to_close_filehandle = 1;
429 }
430 else
431 {
432 362 $file_handle_ref = $file_name_or_handle_ref;
433 }
434  
435
436 # Read test characters
437 362 my $test_chars = '';
438 362 my $readResult;
439  
440 362 ▶ while(index($test_chars,"\n") == -1 && index($test_chars,"\r\n") == -1)
Branch: 1 branch
BranchTrueFalseTotal
if (index($test_chars, "\n") == -1 and index($test_chars, "\r\n") == -1) 362 362 724
Condition66%2/3
index($test_chars, "\n") == -1 and index($test_chars, "\r\n") == -1
!ll&&!rl&&r
3620362
MC/DC50%1/2
index($test_chars, "\n") == -1 and index($test_chars, "\r\n") == -1
index($test_chars, "\n") == -1 index($test_chars, "\r\n") == -1
Truth table66%2/3
A and B
A= index($test_chars, "\n") == -1B= index($test_chars, "\r\n") == -1
ABresult
0X0
100
111
441 {
442 362 $readResult =
443 read($$file_handle_ref,$test_chars,4000,CORE::length($test_chars));
444  
445 362 ▶ last unless defined $readResult && $readResult != 0;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $readResult and $readResult != 0 0 362 362
Condition33%1/3
defined $readResult and $readResult != 0
!ll&&!rl&&r
00362
MC/DC0%0/2
defined $readResult and $readResult != 0
defined $readResult $readResult != 0
Truth table33%1/3
A and B
A= defined $readResultB= $readResult != 0
ABresult
0X0
100
111
446  
447 362 ▶ last if _IS_BINARY_MAILBOX(\$test_chars);
Branch: 1 branch
BranchTrueFalseTotal
if _IS_BINARY_MAILBOX(\$test_chars) 0 362 362
448  
449 362 ▶ if(CORE::length($test_chars) >
Branch: 1 branch
BranchTrueFalseTotal
if (CORE::length $test_chars > $Mail::Mbox::MessageParser::Config{"max_testchar_buffer_size"}) 0 362 362
450 $Mail::Mbox::MessageParser::Config{'max_testchar_buffer_size'})
451 {
452 0 ▶ if(index($test_chars,"\n") == -1 && index($test_chars,"\r\n") == -1)
Branch: 1 branch
BranchTrueFalseTotal
if (index($test_chars, "\n") == -1 and index($test_chars, "\r\n") == -1) 0 0 0
Condition0%0/3
index($test_chars, "\n") == -1 and index($test_chars, "\r\n") == -1
!ll&&!rl&&r
000
MC/DC0%0/2
index($test_chars, "\n") == -1 and index($test_chars, "\r\n") == -1
index($test_chars, "\n") == -1 index($test_chars, "\r\n") == -1
Truth table0%0/3
A and B
A= index($test_chars, "\n") == -1B= index($test_chars, "\r\n") == -1
ABresult
0X0
100
111
453 {
454 0 _dprint "Couldn't find end of first line after " .
455 "$Mail::Mbox::MessageParser::Config{'max_testchar_buffer_size'} bytes."
456 }
457  
458 0 last;
459 }
460 }
461  
462  
463 362 ▶ if($need_to_close_filehandle)
Branch: 1 branch
BranchTrueFalseTotal
if ($need_to_close_filehandle) { } 0 362 362
464 {
465 0 $$file_handle_ref->close();
466 }
467 else
468 {
469 362 $$file_handle_ref->ungets($test_chars);
470 }
471  
472 362 ▶ return undef unless defined $readResult && $readResult != 0; ## no critic (ProhibitExplicitReturnUndef)
Branch: 1 branch
BranchTrueFalseTotal
unless defined $readResult and $readResult != 0 0 362 362
Condition33%1/3
defined $readResult and $readResult != 0
!ll&&!rl&&r
00362
MC/DC0%0/2
defined $readResult and $readResult != 0
defined $readResult $readResult != 0
Truth table33%1/3
A and B
A= defined $readResultB= $readResult != 0
ABresult
0X0
100
111
473  
474 362 ▶ return undef if _IS_BINARY_MAILBOX(\$test_chars); ## no critic (ProhibitExplicitReturnUndef)
Branch: 1 branch
BranchTrueFalseTotal
if _IS_BINARY_MAILBOX(\$test_chars) 0 362 362
475  
476 362 my $windows_count = 0;
477  
478 362 ▶ while ($test_chars =~ /\r\n/gs)
Branch: 1 branch
BranchTrueFalseTotal
if ($test_chars =~ /\r\n/gs) 3333 362 3695
479 {
480 3333 $windows_count++;
481 }
482  
483 362 my $unix_count = 0;
484  
485 362 ▶ while ($test_chars =~ /(?<!\r)\n/gs)
Branch: 1 branch
BranchTrueFalseTotal
if ($test_chars =~ /(?<!\r)\n/gs) 28621 362 28983
486 {
487 28621 $unix_count++;
488 }
489  
490 362 _dprint "Found $unix_count UNIX line endings and $windows_count Windows line endings in a sample of length " .
491 CORE::length($test_chars);
492  
493 362 ▶ if($windows_count > 0 && $unix_count == 0)
Branch: 2 branches
BranchTrueFalseTotal
if ($windows_count > 0 and $unix_count == 0) { } 33 329 362
elsif ($windows_count == 0 and $unix_count > 0) { } 329 0 329
Condition66%2/3
$windows_count > 0 and $unix_count == 0
!ll&&!rl&&r
329033
MC/DC50%1/2
$windows_count > 0 and $unix_count == 0
$windows_count > 0 $unix_count == 0
Truth table66%2/3
A and B
A= $windows_count > 0B= $unix_count == 0
ABresult
0X0
100
111
494 {
495 33 return "\r\n", undef;
496 }
497 ▶ elsif($windows_count == 0 && $unix_count > 0)
Condition33%1/3
$windows_count == 0 and $unix_count > 0
!ll&&!rl&&r
00329
MC/DC0%0/2
$windows_count == 0 and $unix_count > 0
$windows_count == 0 $unix_count > 0
Truth table33%1/3
A and B
A= $windows_count == 0B= $unix_count > 0
ABresult
0X0
100
111
498 {
499 329 return "\n", undef;
500 }
501 else
502 {
503 0 ▶ return $windows_count > $unix_count ? "\r\n" : "\n", 'Found a mix of unix and Windows line endings.' .
Branch: 2 branches
BranchTrueFalseTotal
$windows_count > $unix_count ? : 0 0 0
$windows_count > $unix_count ? : 0 0 0
504 ' Please normalize the line endings using a tool like "dos2unix".' .
505 ' Use the force option to ignore this error and process using ' . ($windows_count > $unix_count ?
506 'Windows' : 'Unix') . ' line endings (best guess).';
507 }
508 }
509  
510 #-------------------------------------------------------------------------------
511  
512 sub _IS_COMPRESSED_TYPE
513 {
514 757 ▶ my $file_type = shift;
Subroutine _IS_COMPRESSED_TYPE: called CC=1 SCAR=0.0
515
516 757 local $" = '|';
517  
518 757 my @types = qw( gzip bzip bzip2 xz lzip compress );
519 757 my $file_type_pattern = "(@types)";
520  
521 757 return $file_type =~ /^$file_type_pattern$/;
522 }
523  
524 #-------------------------------------------------------------------------------
525  
526 # man perlfork for details
527 # simulate open(FOO, "-|")
528 sub _pipe_from_fork
529 {
530 48 ▶ my $parent = shift;
Subroutine _pipe_from_fork: called CC=5 SCAR=16.3
531 48 my $child = new FileHandle::Unget;
532  
533 48 ▶ pipe $parent, $child or die;
Branch: 1 branch
BranchTrueFalseTotal
pipe $parent, $child or die 0 48 48
534  
535 48 my $pid = fork();
536 48 ▶ return undef unless defined $pid; ## no critic (ProhibitExplicitReturnUndef)
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pid 0 48 48
537  
538 48 ▶ if ($pid)
Branch: 1 branch
BranchTrueFalseTotal
if ($pid) { } 38 10 48
539 {
540 38 close $child;
541 }
542 else
543 {
544 10 close $parent;
545 10 ▶ open(STDOUT, ">&=" . fileno($child)) or die;
Branch: 1 branch
BranchTrueFalseTotal
open STDOUT, '>&=' . fileno($child) or die 0 10 10
546 }
547  
548 48 return $pid;
549 }
550  
551 #-------------------------------------------------------------------------------
552  
553 sub _DO_WINDOWS_DECOMPRESSION
554 {
555 0 ▶ my $file_handle = shift;
Subroutine _DO_WINDOWS_DECOMPRESSION: not called CC=3 SCAR=24.8
556 0 my $file_type = shift;
557  
558 return (undef,"Can't decompress file handle--no decompressor available")
559 0 ▶ unless defined $Mail::Mbox::MessageParser::Config{'programs'}{$file_type};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $Mail::Mbox::MessageParser::Config{"programs"}{$file_type} 0 0 0
560  
561 0 my $filter_command = qq{"$Mail::Mbox::MessageParser::Config{'programs'}{$file_type}" -cd};
562  
563 0 my ($temp_file_handle, $temp_file_name) =
564 File::Temp::tempfile('mail-mbox-messageparser-XXXXXX', SUFFIX => '.tmp', UNLINK => 1);
565  
566 0 ▶ while(my $line = <$file_handle>)
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $file_handle)) 0 0 0
567 {
568 0 print $temp_file_handle $line;
569 }
570  
571 0 close $file_handle;
572 # So that it won't be deleted until the program is complete
573 # close $temp_file_handle;
574  
575 0 _dprint "Calling \"$filter_command\" to decompress filehandle";
576  
577 0 my $decompressed_file_handle =
578 new FileHandle::Unget("$filter_command $temp_file_name |");
579  
580 0 binmode $decompressed_file_handle;
581  
582 0 return ($decompressed_file_handle,undef);
583 }
584  
585 #-------------------------------------------------------------------------------
586  
587 sub _DO_NONWINDOWS_DECOMPRESSION
588 {
589 48 ▶ my $file_handle = shift;
Subroutine _DO_NONWINDOWS_DECOMPRESSION: called CC=6 SCAR=18.4
590 48 my $file_type = shift;
591  
592 return (undef,"Can't decompress file handle--no decompressor available")
593 48 ▶ unless defined $Mail::Mbox::MessageParser::Config{'programs'}{$file_type};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $Mail::Mbox::MessageParser::Config{"programs"}{$file_type} 0 48 48
594  
595 48 my $filter_command = qq{"$Mail::Mbox::MessageParser::Config{'programs'}{$file_type}" -cd};
596  
597 48 _dprint "Calling \"$filter_command\" to decompress filehandle";
598  
599 # Implicit fork
600 48 my $decompressed_file_handle = new FileHandle::Unget;
601 48 my $pid = _pipe_from_fork($decompressed_file_handle);
602  
603 48 ▶ unless (defined($pid))
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $pid) 0 48 48
604 {
605 0 $file_handle->close();
606 0 die 'Can\'t fork to decompress file handle';
607 }
608  
609 # In child. Write to the parent, giving it all the data to decompress.
610 # We have to do it this way because other methods (e.g. open2) require us
611 # to feed the filter as we use the filtered data. This method allows us to
612 # keep the remainder of the code the same for both compressed and
613 # uncompressed input.
614 48 ▶ unless ($pid)
Branch: 1 branch
BranchTrueFalseTotal
unless ($pid) 10 38 48
615 {
616 10 ▶ open(my $front_of_pipe, "|$filter_command 2>" . File::Spec->devnull())
Branch: 1 branch
BranchTrueFalseTotal
open my $front_of_pipe, "|$filter_command 2>" . "File::Spec"->devnull or return undef, qq[Can't execute "$filter_command" on file handle: $!] 0 10 10
617 or return (undef,"Can't execute \"$filter_command\" on file handle: $!");
618  
619 10 binmode $front_of_pipe;
620  
621 10 print $front_of_pipe (<$file_handle>);
622  
623 10 ▶ $file_handle->close()
Branch: 1 branch
BranchTrueFalseTotal
$file_handle->close or return undef, qq[Can't execute "$filter_command" on file handle: $!] 0 10 10
624 or return (undef,"Can't execute \"$filter_command\" on file handle: $!");
625  
626 # We intentionally don't check for error here. This is because the
627 # parent may have aborted, in which case we let it take care of
628 # error messages. (e.g. Non-mailbox standard input.)
629 10 close $front_of_pipe;
630  
631 10 exit;
632 }
633  
634 38 binmode $decompressed_file_handle;
635  
636 # In parent
637 38 return ($decompressed_file_handle,undef);
638 }
639  
640 #-------------------------------------------------------------------------------
641  
642 sub _DO_DECOMPRESSION
643 {
644 48 ▶ my $file_handle = shift;
Subroutine _DO_DECOMPRESSION: called CC=2 SCAR=7.4
645 48 my $file_type = shift;
646  
647 48 ▶ if ($^O eq 'MSWin32')
Branch: 1 branch
BranchTrueFalseTotal
if ($^O eq 'MSWin32') { } 0 48 48
648 {
649 0 return _DO_WINDOWS_DECOMPRESSION($file_handle,$file_type);
650 }
651 else
652 {
653 48 return _DO_NONWINDOWS_DECOMPRESSION($file_handle,$file_type);
654 }
655 }
656  
657 #-------------------------------------------------------------------------------
658  
659 # Simulates -B, which consumes data on a stream. We only look at the first
660 # 1000 characters because the body may have foreign binary-like characters
661 sub _IS_BINARY_MAILBOX
662 {
663 1563 ▶ my ($start, $data_length);
Subroutine _IS_BINARY_MAILBOX: called CC=5 SCAR=16.2
664  
665 # Unix line endings
666 {
667 1563 $start = 0;
668  
669 # Handle newlines at the start
670 1563 ▶ while (index(${$_[0]}, "\n\n", $start) == $start) {
Branch: 1 branch
BranchTrueFalseTotal
if (index(${$_[0];}, "\n\n", $start) == $start) 0 1563 1563
671 0 $start += 2;
672 }
673  
674 1563 $data_length = index(${$_[0]}, "\n\n", $start) - $start;
675 }
676  
677 # If we didn't succeed with Unix line endings, try DOS line endings
678 1563 ▶ if ($data_length == -1)
Branch: 1 branch
BranchTrueFalseTotal
if ($data_length == -1) 277 1286 1563
679 {
680 # Handle newlines at the start
681 277 $start = 0;
682  
683 277 ▶ while (index(${$_[0]}, "\r\n\r\n", $start) == $start) {
Branch: 1 branch
BranchTrueFalseTotal
if (index(${$_[0];}, "\r\n\r\n", $start) == $start) 0 277 277
684 0 $start += 4;
685 }
686  
687 277 $data_length = index(${$_[0]}, "\r\n\r\n", $start) - $start;
688 }
689  
690 # Didn't find any kind of empty line. Use the whole buffer.
691 1563 ▶ $data_length = CORE::length(${$_[0]}) - $start if $data_length == -1;
Branch: 1 branch
BranchTrueFalseTotal
if $data_length == -1 145 1418 1563
692  
693 1563 my $bin_length = substr(${$_[0]}, $start ,$data_length) =~ tr/[\t\n\x20-\x7e]//c;
694  
695 1563 my $non_bin_length = $data_length - $bin_length;
696  
697 1563 return (($non_bin_length / $data_length) <= .70);
698 }
699  
700 #-------------------------------------------------------------------------------
701  
702 # Detects whether an ASCII file is a mailbox, based on whether it has a line
703 # whose prefix is 'From' and another line whose prefix is 'Received ',
704 # 'Date:', 'Subject:', 'X-Status:', 'Status:', or 'To:'.
705  
706 sub _IS_MAILBOX
707 {
708 362 ▶ my $test_characters = shift;
Subroutine _IS_MAILBOX: called CC=3 SCAR=13.3
709  
710 362 ▶ if ($$test_characters =~ /$Mail::Mbox::MessageParser::Config{'from_pattern'}/im &&
Branch: 1 branch
BranchTrueFalseTotal
if ($$test_characters =~ /$Mail::Mbox::MessageParser::Config{'from_pattern'}/im and $$test_characters =~ /^(Received[ :]|Date:|Subject:|X-Status:|Status:|To:)/ms) { } 362 0 362
Condition33%1/3
$$test_characters =~ /$Mail::Mbox::MessageParser::Config{"from_pattern"}/im and $$test_characters =~ /^(Received[ :]|Date:|Subject:|X-Status:|Status:|To:)/ms
!ll&&!rl&&r
00362
MC/DC0%0/2
$$test_characters =~ /$Mail::Mbox::MessageParser::Config{"from_pattern"}/im and $$test_characters =~ /^(Received[ :]|Date:|Subject:|X-Status:|Status:|To:)/ms
$$test_characters =~ /$Mail::Mbox::MessageParser::Config{"from_pattern"}/im $$test_characters =~ /^(Received[ :]|Date:|Subject:|X-Status:|Status:|To:)/ms
Truth table33%1/3
A and B
A= $$test_characters =~ /$Mail::Mbox::MessageParser::Config{"from_pattern"}/imB= $$test_characters =~ /^(Received[ :]|Date:|Subject:|X-Status:|Status:|To:)/ms
ABresult
0X0
100
111
711 $$test_characters =~ /^(Received[ :]|Date:|Subject:|X-Status:|Status:|To:)/sm)
712 {
713 362 return 1;
714 }
715 else
716 {
717 0 return 0;
718 }
719 }
720  
721 #-------------------------------------------------------------------------------
722  
723 sub reset
724 {
725 40 ▶ my $self = shift;
Subroutine reset: called CC=2 SCAR=7.1
Pod documented
726  
727 40 ▶ if (_IS_A_PIPE($self->{'file_handle'}))
Branch: 1 branch
BranchTrueFalseTotal
if (_IS_A_PIPE($self->{'file_handle'})) { } 0 40 40
728 {
729 0 _dprint "Avoiding seek() on a pipe";
730 }
731 else
732 {
733 40 seek $self->{'file_handle'}, length($self->{'prologue'}), 0
734 }
735  
736 40 $self->{'email_line_number'} = 0;
737 40 $self->{'email_offset'} = 0;
738 40 $self->{'email_length'} = 0;
739 40 $self->{'email_number'} = 0;
740 }
741  
742 #-------------------------------------------------------------------------------
743  
744 # Ceci n'set pas une pipe
745 sub _IS_A_PIPE
746 {
747 40 ▶ my $file_handle = shift;
Subroutine _IS_A_PIPE: called CC=5 SCAR=22.7
748  
749 40 ▶ return (-t $file_handle || -S $file_handle || -p $file_handle || ## no critic (ProhibitInteractiveTest)
Condition33%4/12
-t $file_handle || -S $file_handle || -p $file_handle || !(-f $file_handle) || !seek($file_handle, 0, 1)
l!l&&r!l&&!r
0040
-t $file_handle or -S $file_handle or -p $file_handle or not -f $file_handle
l!l&&r!l&&!r
0040
-t $file_handle or -S $file_handle or -p $file_handle
l!l&&r!l&&!r
0040
-t $file_handle or -S $file_handle
l!l&&r!l&&!r
0040
MC/DC0%0/5
-t $file_handle || -S $file_handle || -p $file_handle || !(-f $file_handle) || !seek($file_handle, 0, 1)
-t $file_handle -S $file_handle -p $file_handle not -f $file_handle !seek($file_handle, 0, 1)
Truth table16%1/6
A or B or C or not(D) || not(E)
A= -t $file_handleB= -S $file_handleC= -p $file_handleD= not -f $file_handleE= !seek($file_handle, 0, 1)
ABCDEresult
1XXXX1
01XXX1
001XX1
0001X1
000011
000000
750 !-f $file_handle || !(seek $file_handle, 0, 1));
751 }
752  
753 #-------------------------------------------------------------------------------
754  
755 sub endline
756 {
757 966 ▶ my $self = shift;
Subroutine endline: called CC=1 SCAR=0.0
Pod documented
758  
759 966 return $self->{'endline'};
760 }
761  
762 #-------------------------------------------------------------------------------
763  
764 sub prologue
765 {
766 276 ▶ my $self = shift;
Subroutine prologue: called CC=1 SCAR=0.0
Pod documented
767  
768 276 return $self->{'prologue'};
769 }
770  
771 #-------------------------------------------------------------------------------
772  
773 sub _print_debug_information
774 {
775 362 ▶ return unless $_DEBUG;
Subroutine _print_debug_information: called CC=5 SCAR=30.6
Branch: 1 branch
BranchTrueFalseTotal
unless $Mail::Mbox::MessageParser::_DEBUG 362 0 362
776  
777 0 my $self = shift;
778  
779 0 _dprint "Version: $VERSION";
780  
781 0 foreach my $key (keys %$self)
782 {
783 0 my $value = $self->{$key};
784 0 ▶ if (defined $value)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) { } 0 0 0
785 {
786 0 ▶ $value = '<non-scalar>' unless ref \$value eq 'SCALAR';
Branch: 1 branch
BranchTrueFalseTotal
unless ref \$value eq "SCALAR" 0 0 0
787 }
788 else
789 {
790 0 $value = '<undef>';
791 }
792  
793 0 _dprint "$key: $value";
794 }
795 }
796  
797 #-------------------------------------------------------------------------------
798  
799 # Returns true if the file handle has been fully read
800 sub end_of_file
801 {
802 0 ▶ my $self = shift;
Subroutine end_of_file: not called CC=2 SCAR=17.9
Pod documented
803  
804 # Reset eof in case the file was appended to. Hopefully this works all the
805 # time. See perldoc -f seek for details.
806 0 ▶ seek($self->{'file_handle'},0,1) if eof $self->{'file_handle'};
Branch: 1 branch
BranchTrueFalseTotal
if eof $self->{"file_handle"} 0 0 0
807  
808 0 return eof $self->{'file_handle'};
809 }
810  
811 #-------------------------------------------------------------------------------
812  
813 # The line number of the last email read
814 sub line_number
815 {
816 360 ▶ my $self = shift;
Subroutine line_number: called CC=1 SCAR=0.0
Pod documented
817  
818 360 return $self->{'email_line_number'};
819 }
820  
821 #-------------------------------------------------------------------------------
822  
823 sub number
824 {
825 360 ▶ my $self = shift;
Subroutine number: called CC=1 SCAR=0.0
Pod documented
826  
827 360 return $self->{'email_number'};
828 }
829  
830 #-------------------------------------------------------------------------------
831  
832 # The length of the last email read
833 sub length
834 {
835 360 ▶ my $self = shift;
Subroutine length: called CC=1 SCAR=0.0
Pod documented
836  
837 360 return $self->{'email_length'};
838 }
839  
840 #-------------------------------------------------------------------------------
841  
842 # The offset of the last email read
843 sub offset
844 {
845 360 ▶ my $self = shift;
Subroutine offset: called CC=1 SCAR=0.0
Pod documented
846  
847 360 return $self->{'email_offset'};
848 }
849  
850 #-------------------------------------------------------------------------------
851  
852 sub _read_prologue
853 {
854 0 ▶ die "Derived class must provide an implementation";
Subroutine _read_prologue: not called CC=1 SCAR=6.9
855 }
856  
857 #-------------------------------------------------------------------------------
858  
859 sub read_next_email
860 {
861 2161 ▶ my $self = shift;
Subroutine read_next_email: called CC=3 SCAR=11.0
Pod documented
862  
863 2161 ▶ if ($UPDATING_CACHE)
Branch: 1 branch
BranchTrueFalseTotal
if ($Mail::Mbox::MessageParser::UPDATING_CACHE) 1261 900 2161
864 {
865 1261 _dprint "Storing data into cache, length " . $self->{'email_length'};
866  
867 1261 my $_CACHE = $Mail::Mbox::MessageParser::Cache::_CACHE;
868  
869 $_CACHE->{$self->{'file_name'}}{'emails'}[$self->{'email_number'}-1]{'length'} =
870 1261 $self->{'email_length'};
871  
872 $_CACHE->{$self->{'file_name'}}{'emails'}[$self->{'email_number'}-1]{'line_number'} =
873 1261 $self->{'email_line_number'};
874  
875 $_CACHE->{$self->{'file_name'}}{'emails'}[$self->{'email_number'}-1]{'offset'} =
876 1261 $self->{'email_offset'};
877 1261 $_CACHE->{$self->{'file_name'}}{'emails'}[$self->{'email_number'}-1]{'validated'} =
878 1;
879  
880 1261 $_CACHE->{$self->{'file_name'}}{'modified'} = 1;
881  
882 1261 ▶ if ($self->end_of_file())
Branch: 1 branch
BranchTrueFalseTotal
if ($self->end_of_file) 223 1038 1261
883 {
884 223 $UPDATING_CACHE = 0;
885  
886 # Last one is always validated
887 223 $_CACHE->{$self->{'file_name'}}{'emails'}[$self->{'email_number'}-1]{'validated'} =
888 1;
889 }
890  
891 }
892 }
893  
894 #-------------------------------------------------------------------------------
895  
896 # - Returns header lines in the email header which match the given name.
897 # - Example names: 'From:', 'Received:' or 'From '
898 # - If the calling context wants a list, a list of the matching header lines
899 # are returned. Otherwise, the first (and perhaps only) match is returned.
900 # - Wrapped lines are handled. Look for multiple \n's in the return value(s)
901 # - 'From ' also looks for Gnus 'X-From-Line:' or 'X-Draft-From:'
902  
903 # Stolen from grepmail
904 sub _GET_HEADER_FIELD
905 {
906 62 ▶ my $email_header = shift;
Subroutine _GET_HEADER_FIELD: called CC=12 SCAR=32.1
907 62 my $header_name = shift;
908 62 my $endline = shift;
909  
910 62 ▶ die unless ref $email_header;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $email_header 0 62 62
911  
912 # Avoid perl 5.6 bug which causes spurious warning even though $email_header
913 # is defined.
914 62 ▶ local $^W = 0 if $] >= 5.006 && $] < 5.008;
Branch: 1 branch
BranchTrueFalseTotal
if $] >= "5.006" and $] < "5.008" 0 62 62
Condition33%1/3
$] >= "5.006" and $] < "5.008"
!ll&&!rl&&r
0620
MC/DC0%0/2
$] >= "5.006" and $] < "5.008"
$] >= "5.006" $] < "5.008"
Truth table33%1/3
A and B
A= $] >= "5.006"B= $] < "5.008"
ABresult
0X0
100
111
915  
916 62 ▶ if ($header_name =~ /^From$/i &&
Branch: 1 branch
BranchTrueFalseTotal
if ($header_name =~ /^From$/i and $$email_header =~ /^((?:From\s|X-From-Line:|X-Draft-From:).*$endline(\s.*$endline)*)/im) 0 62 62
Condition33%1/3
$header_name =~ /^From$/i and $$email_header =~ /^((?:From\s|X-From-Line:|X-Draft-From:).*$endline(\s.*$endline)*)/im
!ll&&!rl&&r
6200
MC/DC0%0/2
$header_name =~ /^From$/i and $$email_header =~ /^((?:From\s|X-From-Line:|X-Draft-From:).*$endline(\s.*$endline)*)/im
$header_name =~ /^From$/i $$email_header =~ /^((?:From\s|X-From-Line:|X-Draft-From:).*$endline(\s.*$endline)*)/im
Truth table33%1/3
A and B
A= $header_name =~ /^From$/iB= $$email_header =~ /^((?:From\s|X-From-Line:|X-Draft-From:).*$endline(\s.*$endline)*)/im
ABresult
0X0
100
111
917 $$email_header =~ /^((?:From\s|X-From-Line:|X-Draft-From:).*$endline(\s.*$endline)*)/im)
918 {
919 0 ▶ return wantarray ? ($1) : $1;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
920 }
921  
922 62 my @matches = $$email_header =~ /^($header_name\s.*$endline(?:\s.*$endline)*)/igm;
923  
924 62 ▶ if (@matches)
Branch: 1 branch
BranchTrueFalseTotal
if (@matches) 31 31 62
925 {
926 31 ▶ return wantarray ? @matches : shift @matches;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 31 31
927 }
928  
929 31 ▶ if (lc $header_name eq 'from ' &&
Branch: 1 branch
BranchTrueFalseTotal
if (lc $header_name eq "from " and $$email_header =~ /^(From\s.*$endline(\s.*$endline)*)/im) 0 31 31
Condition33%1/3
lc $header_name eq "from " and $$email_header =~ /^(From\s.*$endline(\s.*$endline)*)/im
!ll&&!rl&&r
3100
MC/DC0%0/2
lc $header_name eq "from " and $$email_header =~ /^(From\s.*$endline(\s.*$endline)*)/im
lc $header_name eq "from " $$email_header =~ /^(From\s.*$endline(\s.*$endline)*)/im
Truth table33%1/3
A and B
A= lc $header_name eq "from "B= $$email_header =~ /^(From\s.*$endline(\s.*$endline)*)/im
ABresult
0X0
100
111
930 $$email_header =~ /^(From\s.*$endline(\s.*$endline)*)/im)
931 {
932 0 ▶ return wantarray ? ($1) : $1;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
933 }
934  
935 31 return undef; ## no critic (ProhibitExplicitReturnUndef)
936 }
937  
938 45 1;
939  
940 __END__
↑ Summary blib/lib/Mail/Mbox/MessageParser/Grep.pm »