blib/lib/Mail/Mbox/MessageParser.pm

filter: Statementstmt 83.3% 296 / 355 Branchbran 56.8% 116 / 204 Conditioncond 33.3% 29 / 87 MC/DCmcdc 9.6% 5 / 52 Subroutinesub 92.6% 38 / 41 Podpod 100% 10 / 10 totaltotal 65.9% 494 / 749 CC 134 SCAR 33.7
CC
134
Cov
68%
_GET_ENDLINE
63.2
new
58.5
_GET_FILE_TYPE
49.3
CRAP 29.1
↑ Summary blib/lib/Mail/Mbox/MessageParser/Grep.pm »
1 package Mail::Mbox::MessageParser;
2  
3 51 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 51 ▶use 5.005;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 51 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 51 ▶use FileHandle::Unget;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 51 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 51 ▶use File::Temp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 sub _dprint;
10  
11 51 ▶use Mail::Mbox::MessageParser::MetaInfo;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 51 ▶use Mail::Mbox::MessageParser::Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 51 ▶use Mail::Mbox::MessageParser::Perl;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 51 ▶use Mail::Mbox::MessageParser::Grep;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 51 ▶use Mail::Mbox::MessageParser::Cache;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 51 ▶use vars qw( @ISA $VERSION $_DEBUG );
Subroutine BEGIN: called CC=1 SCAR=0.0
19 51 ▶use vars qw( $_CACHE $UPDATING_CACHE );
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 51 @ISA = qw(Exporter);
22  
23 51 $VERSION = sprintf "%d.%02d%02d", q/1.51.11/ =~ /(\d+)/g;
24 51 $_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 51 *_CACHE = \$Mail::Mbox::MessageParser::MetaInfo::_CACHE;
32  
33 51 *UPDATING_CACHE = \$Mail::Mbox::MessageParser::MetaInfo::UPDATING_CACHE;
34 51 *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 5884 ▶{
Subroutine _dprint: called CC=3 SCAR=18.4
43 5884 ▶ return 1 unless $_DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
unless $Mail::Mbox::MessageParser::_DEBUG 5884 0 5884
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 421 ▶{
Subroutine new: called CC=26 SCAR=40.7
Pod documented
60 421 my ($proto, $options, $cache_options) = @_;
61  
62 421 ▶ my $class = ref($proto) || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
04210
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 421 ▶ unless defined $options->{'file_name'} || defined $options->{'file_handle'};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $options->{"file_name"} or defined $options->{"file_handle"} 0 421 421
Condition66%2/3
defined $options->{"file_name"} or defined $options->{"file_handle"}
l!l&&r!l&&!r
404170
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 421 ▶ unless (defined $options->{'file_name'})
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $options->{"file_name"}) 17 404 421
69 {
70 17 $options->{'enable_cache'} = 0;
71 17 $options->{'enable_grep'} = 0;
72 }
73  
74 $_DEBUG = $options->{'debug'}
75 421 ▶ if defined $options->{'debug'};
Branch: 1 branch
BranchTrueFalseTotal
if defined $options->{"debug"} 0 421 421
76  
77 421 my ($file_type, $need_to_close_filehandle, $error, $endline);
78  
79 ($options->{'file_handle'}, $file_type, $need_to_close_filehandle, $error, $endline) =
80 421 _PREPARE_FILE_HANDLE($options->{'file_name'}, $options->{'file_handle'});
81  
82 406 ▶ 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 406 406
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
40600
defined $error and not $error eq "Not a mailbox" && $options->{"force_processing"}
!ll&&!rl&&r
40600
$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 406 ▶ $options->{'enable_grep'} = 0 if _IS_COMPRESSED_TYPE($file_type);
Branch: 1 branch
BranchTrueFalseTotal
if _IS_COMPRESSED_TYPE($file_type) 84 322 406
96  
97 406 ▶ $options->{'enable_cache'} = 1 unless defined $options->{'enable_cache'};;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $options->{"enable_cache"} 0 406 406
98 406 ▶ $options->{'enable_grep'} = 1 unless defined $options->{'enable_grep'};;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $options->{"enable_grep"} 0 406 406
99  
100 406 my $self = undef;
101  
102 406 ▶ if ($options->{'enable_cache'})
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{"enable_cache"}) 257 149 406
103 {
104 257 $self = new Mail::Mbox::MessageParser::Cache($options, $cache_options);
105  
106 257 ▶ unless (ref $self)
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $self) 0 257 257
107 {
108 0 warn "Couldn't instantiate Mail::Mbox::MessageParser::Cache: $self";
109 0 $self = undef;
110 }
111  
112 257 ▶ if ($UPDATING_CACHE)
Branch: 1 branch
BranchTrueFalseTotal
if ($Mail::Mbox::MessageParser::UPDATING_CACHE) 257 0 257
113 {
114 257 _dprint "Couldn't instantiate Mail::Mbox::MessageParser::Cache: " .
115 "Updating cache";
116 257 $self = undef;
117 }
118 }
119  
120 406 ▶ if (!defined $self && $options->{'enable_grep'})
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $self and $options->{"enable_grep"}) 15 391 406
Condition66%2/3
not defined $self and $options->{"enable_grep"}
!ll&&!rl&&r
039115
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 406 ▶ if (!defined $self)
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self) 406 0 406
139 {
140 406 $self = new Mail::Mbox::MessageParser::Perl($options);
141  
142 406 ▶ warn "Couldn't instantiate Mail::Mbox::MessageParser::Perl: $self"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 0 406 406
143 unless ref $self;
144 }
145  
146 406 ▶ die "Couldn't instantiate any mailbox parser implementation"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self 0 406 406
147 unless defined $self;
148  
149 406 _dprint "Instantiate mailbox parser implementation: " . ref $self;
150  
151 406 $self->_print_debug_information();
152  
153 406 $self->_read_prologue();
154  
155 406 $self->{'need_to_close_filehandle'} = $need_to_close_filehandle;
156  
157 406 $self->{'endline'} = $endline;
158  
159 406 return $self;
160 }
161  
162 #-------------------------------------------------------------------------------
163  
164 sub _init
165 663 ▶{
Subroutine _init: called CC=1 SCAR=0.0
166 663 my $self = shift;
167  
168 663 $self->{'email_line_number'} = 0;
169 663 $self->{'email_offset'} = 0;
170 663 $self->{'email_length'} = 0;
171 663 $self->{'email_number'} = 0;
172 }
173  
174 #-------------------------------------------------------------------------------
175  
176 sub DESTROY
177 406 ▶{
Subroutine DESTROY: called CC=2 SCAR=6.9
178 406 my $self = shift;
179  
180 406 ▶ $self->{'file_handle'}->close() if $self->{'need_to_close_filehandle'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"need_to_close_filehandle"} 24 382 406
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 421 ▶{
Subroutine _PREPARE_FILE_HANDLE: called CC=12 SCAR=25.3
193 421 my $file_name = shift;
194 421 my $file_handle = shift;
195  
196 421 _dprint "Preparing file handle";
197  
198 421 ▶ if (defined $file_handle)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $file_handle) { } 397 24 421
199 {
200 # Promote this to a FileHandle::Unget if it isn't already
201 397 ▶ $file_handle = new FileHandle::Unget($file_handle)
Branch: 1 branch
BranchTrueFalseTotal
unless UNIVERSAL::isa($file_handle, "FileHandle::Unget") 284 113 397
202 unless UNIVERSAL::isa($file_handle, 'FileHandle::Unget');
203  
204 397 binmode $file_handle;
205  
206 397 my $file_type = _GET_FILE_TYPE(\$file_handle);
207 397 _dprint "Filehandle file type: $file_type";
208  
209 # Do decompression if we need to
210 397 ▶ if (_IS_COMPRESSED_TYPE($file_type))
Branch: 1 branch
BranchTrueFalseTotal
if (_IS_COMPRESSED_TYPE($file_type)) { } 96 301 397
211 {
212 96 my ($decompressed_file_handle,$error) =
213 _DO_DECOMPRESSION($file_handle, $file_type);
214  
215 81 ▶ return ($file_handle,$file_type,0,$error,undef)
Branch: 1 branch
BranchTrueFalseTotal
unless defined $decompressed_file_handle 0 81 81
216 unless defined $decompressed_file_handle;
217  
218 81 ▶ 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 81 81
219 if _GET_FILE_TYPE(\$decompressed_file_handle) ne 'mailbox';
220  
221 81 my $endline;
222 81 ($endline, $error) = _GET_ENDLINE(\$decompressed_file_handle);
223  
224 81 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 24 my $file_type = _GET_FILE_TYPE(\$file_name);
241 24 _dprint "Filename \"$file_name\" file type: $file_type";
242  
243 24 my ($opened_file_handle,$error) =
244 _OPEN_FILE_HANDLE($file_name, $file_type);
245  
246 24 ▶ return ($file_handle,$file_type,0,$error,undef)
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opened_file_handle 0 24 24
247 unless defined $opened_file_handle;
248  
249 24 my $endline;
250 24 ($endline, $error) = _GET_ENDLINE(\$opened_file_handle);
251  
252 24 ▶ if (_IS_COMPRESSED_TYPE($file_type))
Branch: 1 branch
BranchTrueFalseTotal
if (_IS_COMPRESSED_TYPE($file_type)) { } 3 21 24
253 {
254 3 ▶ 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 3 3
255 if _GET_FILE_TYPE(\$opened_file_handle) ne 'mailbox';
256  
257 3 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 24 ▶{
Subroutine _OPEN_FILE_HANDLE: called CC=9 SCAR=23.0
275 24 my $file_name = shift;
276 24 my $file_type = shift;
277  
278 24 _dprint "Opening file \"$file_name\"";
279  
280 # Non-compressed file
281 24 ▶ unless (_IS_COMPRESSED_TYPE($file_type))
Branch: 1 branch
BranchTrueFalseTotal
unless (_IS_COMPRESSED_TYPE($file_type)) 21 3 24
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 3 ▶ unless defined $Mail::Mbox::MessageParser::Config{'programs'}{$file_type};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $Mail::Mbox::MessageParser::Config{"programs"}{$file_type} 0 3 3
296  
297 3 my $filter_command = qq{"$Mail::Mbox::MessageParser::Config{'programs'}{$file_type}" -cd "$file_name" |};
298  
299 3 _dprint "Calling \"$filter_command\" to decompress file \"$file_name\".";
300  
301 3 my $oldstderr;
302 3 ▶ open $oldstderr,">&STDERR" or die "Can't save STDERR: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open $oldstderr, ">&STDERR" or die "Can't save STDERR: $!\n" 3 0 3
303 3 ▶ 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" 3 0 3
304 or die "Can't redirect STDERR to " . File::Spec->devnull() . ": $!\n";
305  
306 3 my $file_handle = new FileHandle::Unget($filter_command);
307  
308 3 ▶ return (undef,"Can't execute \"$filter_command\" for file \"$file_name\": $!")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $file_handle 0 3 3
309 unless defined $file_handle;
310  
311 3 binmode $file_handle;
312  
313 3 ▶ open STDERR, '>&', $oldstderr or die "Can't restore STDERR: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open STDERR, ">&", $oldstderr or die "Can't restore STDERR: $!\n" 3 0 3
314  
315 3 ▶ if (eof($file_handle))
Branch: 1 branch
BranchTrueFalseTotal
if (eof $file_handle) 0 3 3
316 {
317 0 $file_handle->close();
318 0 return (undef,"Can't execute \"$filter_command\" for file \"$file_name\"");
319 }
320  
321 3 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 505 ▶{
Subroutine _GET_FILE_TYPE: called CC=24 SCAR=39.0
330 505 my $file_name_or_handle_ref = shift;
331  
332 # Open the file if we need to
333 505 my $file_handle_ref;
334 505 my $need_to_close_filehandle = 0;
335  
336 505 ▶ if (ref $file_name_or_handle_ref eq 'SCALAR')
Branch: 1 branch
BranchTrueFalseTotal
if (ref $file_name_or_handle_ref eq 'SCALAR') { } 24 481 505
337 {
338 24 my $temp = new FileHandle::Unget($$file_name_or_handle_ref);
339 24 ▶ return 'unknown' unless defined $temp;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $temp 0 24 24
340 24 $file_handle_ref = \$temp;
341  
342 24 $need_to_close_filehandle = 1;
343 }
344 else
345 {
346 481 $file_handle_ref = $file_name_or_handle_ref;
347 }
348  
349
350 # Read test characters
351 505 my $test_chars = '';
352 505 my $readResult;
353  
354 505 ▶ 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) 520 406 926
Condition100%3/3
index($test_chars, "\n\n") == -1 and index($test_chars, "\r\n\r\n") == -1
!ll&&!rl&&r
37333520
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 520 $readResult =
357 read($$file_handle_ref,$test_chars,4000,CORE::length($test_chars));
358  
359 520 ▶ last unless defined $readResult && $readResult != 0;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $readResult and $readResult != 0 0 520 520
Condition33%1/3
defined $readResult and $readResult != 0
!ll&&!rl&&r
00520
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 520 ▶ last if _IS_BINARY_MAILBOX(\$test_chars);
Branch: 1 branch
BranchTrueFalseTotal
if _IS_BINARY_MAILBOX(\$test_chars) 99 421 520
362  
363 421 ▶ if(CORE::length($test_chars) >
Branch: 1 branch
BranchTrueFalseTotal
if (CORE::length $test_chars > $Mail::Mbox::MessageParser::Config{"max_testchar_buffer_size"}) 0 421 421
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 505 ▶ if($need_to_close_filehandle)
Branch: 1 branch
BranchTrueFalseTotal
if ($need_to_close_filehandle) { } 24 481 505
378 {
379 24 $$file_handle_ref->close();
380 }
381 else
382 {
383 481 $$file_handle_ref->ungets($test_chars);
384 }
385  
386 505 ▶ return 'unknown' unless defined $readResult && $readResult != 0;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $readResult and $readResult != 0 0 505 505
Condition33%1/3
defined $readResult and $readResult != 0
!ll&&!rl&&r
00505
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 505 ▶ unless (_IS_BINARY_MAILBOX(\$test_chars))
Branch: 1 branch
BranchTrueFalseTotal
unless (_IS_BINARY_MAILBOX(\$test_chars)) 406 99 505
390 {
391 406 ▶ return 'mailbox' if _IS_MAILBOX(\$test_chars);
Branch: 1 branch
BranchTrueFalseTotal
if _IS_MAILBOX(\$test_chars) 406 0 406
392 0 return 'non-mailbox ascii';
393 }
394  
395 # See "magic" on unix systems for details on how to identify file types
396 99 ▶ return 'bzip2' if substr($test_chars, 0, 3) eq 'BZh';
Branch: 1 branch
BranchTrueFalseTotal
if substr($test_chars, 0, 3) eq "BZh" 49 50 99
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 406 ▶{
Subroutine _GET_ENDLINE: called CC=23 SCAR=41.5
416 406 my $file_name_or_handle_ref = shift;
417  
418 # Open the file if we need to
419 406 my $file_handle_ref;
420 406 my $need_to_close_filehandle = 0;
421  
422 406 ▶ if (ref $file_name_or_handle_ref eq 'SCALAR')
Branch: 1 branch
BranchTrueFalseTotal
if (ref $file_name_or_handle_ref eq 'SCALAR') { } 0 406 406
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 406 $file_handle_ref = $file_name_or_handle_ref;
433 }
434  
435
436 # Read test characters
437 406 my $test_chars = '';
438 406 my $readResult;
439  
440 406 ▶ 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) 406 406 812
Condition66%2/3
index($test_chars, "\n") == -1 and index($test_chars, "\r\n") == -1
!ll&&!rl&&r
4060406
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 406 $readResult =
443 read($$file_handle_ref,$test_chars,4000,CORE::length($test_chars));
444  
445 406 ▶ last unless defined $readResult && $readResult != 0;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $readResult and $readResult != 0 0 406 406
Condition33%1/3
defined $readResult and $readResult != 0
!ll&&!rl&&r
00406
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 406 ▶ last if _IS_BINARY_MAILBOX(\$test_chars);
Branch: 1 branch
BranchTrueFalseTotal
if _IS_BINARY_MAILBOX(\$test_chars) 0 406 406
448  
449 406 ▶ if(CORE::length($test_chars) >
Branch: 1 branch
BranchTrueFalseTotal
if (CORE::length $test_chars > $Mail::Mbox::MessageParser::Config{"max_testchar_buffer_size"}) 0 406 406
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 406 ▶ if($need_to_close_filehandle)
Branch: 1 branch
BranchTrueFalseTotal
if ($need_to_close_filehandle) { } 0 406 406
464 {
465 0 $$file_handle_ref->close();
466 }
467 else
468 {
469 406 $$file_handle_ref->ungets($test_chars);
470 }
471  
472 406 ▶ return undef unless defined $readResult && $readResult != 0; ## no critic (ProhibitExplicitReturnUndef)
Branch: 1 branch
BranchTrueFalseTotal
unless defined $readResult and $readResult != 0 0 406 406
Condition33%1/3
defined $readResult and $readResult != 0
!ll&&!rl&&r
00406
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 406 ▶ return undef if _IS_BINARY_MAILBOX(\$test_chars); ## no critic (ProhibitExplicitReturnUndef)
Branch: 1 branch
BranchTrueFalseTotal
if _IS_BINARY_MAILBOX(\$test_chars) 0 406 406
475  
476 406 my $windows_count = 0;
477  
478 406 ▶ while ($test_chars =~ /\r\n/gs)
Branch: 1 branch
BranchTrueFalseTotal
if ($test_chars =~ /\r\n/gs) 3333 406 3739
479 {
480 3333 $windows_count++;
481 }
482  
483 406 my $unix_count = 0;
484  
485 406 ▶ while ($test_chars =~ /(?<!\r)\n/gs)
Branch: 1 branch
BranchTrueFalseTotal
if ($test_chars =~ /(?<!\r)\n/gs) 32669 406 33075
486 {
487 32669 $unix_count++;
488 }
489  
490 406 _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 406 ▶ if($windows_count > 0 && $unix_count == 0)
Branch: 2 branches
BranchTrueFalseTotal
if ($windows_count > 0 and $unix_count == 0) { } 33 373 406
elsif ($windows_count == 0 and $unix_count > 0) { } 373 0 373
Condition66%2/3
$windows_count > 0 and $unix_count == 0
!ll&&!rl&&r
373033
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
00373
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 373 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 851 ▶{
Subroutine _IS_COMPRESSED_TYPE: called CC=1 SCAR=0.0
514 851 my $file_type = shift;
515
516 851 local $" = '|';
517  
518 851 my @types = qw( gzip bzip bzip2 xz lzip compress );
519 851 my $file_type_pattern = "(@types)";
520  
521 851 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 96 ▶{
Subroutine _pipe_from_fork: called CC=5 SCAR=16.3
530 96 my $parent = shift;
531 96 my $child = new FileHandle::Unget;
532  
533 96 ▶ pipe $parent, $child or die;
Branch: 1 branch
BranchTrueFalseTotal
pipe $parent, $child or die 96 0 96
534  
535 96 my $pid = fork();
536 96 ▶ return undef unless defined $pid; ## no critic (ProhibitExplicitReturnUndef)
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pid 0 96 96
537  
538 96 ▶ if ($pid)
Branch: 1 branch
BranchTrueFalseTotal
if ($pid) { } 81 15 96
539 {
540 81 close $child;
541 }
542 else
543 {
544 15 close $parent;
545 15 ▶ open(STDOUT, ">&=" . fileno($child)) or die;
Branch: 1 branch
BranchTrueFalseTotal
open STDOUT, '>&=' . fileno($child) or die 15 0 15
546 }
547  
548 96 return $pid;
549 }
550  
551 #-------------------------------------------------------------------------------
552  
553 sub _DO_WINDOWS_DECOMPRESSION
554 0 ▶{
Subroutine _DO_WINDOWS_DECOMPRESSION: not called CC=3 SCAR=24.8
555 0 my $file_handle = shift;
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', TMPDIR => 1, 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 96 ▶{
Subroutine _DO_NONWINDOWS_DECOMPRESSION: called CC=6 SCAR=18.4
589 96 my $file_handle = shift;
590 96 my $file_type = shift;
591  
592 return (undef,"Can't decompress file handle--no decompressor available")
593 96 ▶ unless defined $Mail::Mbox::MessageParser::Config{'programs'}{$file_type};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $Mail::Mbox::MessageParser::Config{"programs"}{$file_type} 0 96 96
594  
595 96 my $filter_command = qq{"$Mail::Mbox::MessageParser::Config{'programs'}{$file_type}" -cd};
596  
597 96 _dprint "Calling \"$filter_command\" to decompress filehandle";
598  
599 # Implicit fork
600 96 my $decompressed_file_handle = new FileHandle::Unget;
601 96 my $pid = _pipe_from_fork($decompressed_file_handle);
602  
603 96 ▶ unless (defined($pid))
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $pid) 0 96 96
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 96 ▶ unless ($pid)
Branch: 1 branch
BranchTrueFalseTotal
unless ($pid) 15 81 96
615 {
616 15 ▶ 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: $!] 15 0 15
617 or return (undef,"Can't execute \"$filter_command\" on file handle: $!");
618  
619 15 binmode $front_of_pipe;
620  
621 15 print $front_of_pipe (<$file_handle>);
622  
623 15 ▶ $file_handle->close()
Branch: 1 branch
BranchTrueFalseTotal
$file_handle->close or return undef, qq[Can't execute "$filter_command" on file handle: $!] 15 0 15
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 15 close $front_of_pipe;
630  
631 15 exit;
632 }
633  
634 81 binmode $decompressed_file_handle;
635  
636 # In parent
637 81 return ($decompressed_file_handle,undef);
638 }
639  
640 #-------------------------------------------------------------------------------
641  
642 sub _DO_DECOMPRESSION
643 96 ▶{
Subroutine _DO_DECOMPRESSION: called CC=2 SCAR=7.4
644 96 my $file_handle = shift;
645 96 my $file_type = shift;
646  
647 96 ▶ if ($^O eq 'MSWin32')
Branch: 1 branch
BranchTrueFalseTotal
if ($^O eq 'MSWin32') { } 0 96 96
648 {
649 0 return _DO_WINDOWS_DECOMPRESSION($file_handle,$file_type);
650 }
651 else
652 {
653 96 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 1837 ▶{
Subroutine _IS_BINARY_MAILBOX: called CC=5 SCAR=16.2
663 1837 my ($start, $data_length);
664  
665 # Unix line endings
666 {
667 1837 $start = 0;
668  
669 # Handle newlines at the start
670 1837 ▶ while (index(${$_[0]}, "\n\n", $start) == $start) {
Branch: 1 branch
BranchTrueFalseTotal
if (index(${$_[0];}, "\n\n", $start) == $start) 0 1837 1837
671 0 $start += 2;
672 }
673  
674 1837 $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 1837 ▶ if ($data_length == -1)
Branch: 1 branch
BranchTrueFalseTotal
if ($data_length == -1) 375 1462 1837
679 {
680 # Handle newlines at the start
681 375 $start = 0;
682  
683 375 ▶ while (index(${$_[0]}, "\r\n\r\n", $start) == $start) {
Branch: 1 branch
BranchTrueFalseTotal
if (index(${$_[0];}, "\r\n\r\n", $start) == $start) 0 375 375
684 0 $start += 4;
685 }
686  
687 375 $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 1837 ▶ $data_length = CORE::length(${$_[0]}) - $start if $data_length == -1;
Branch: 1 branch
BranchTrueFalseTotal
if $data_length == -1 243 1594 1837
692  
693 1837 my $bin_length = substr(${$_[0]}, $start ,$data_length) =~ tr/[\t\n\x20-\x7e]//c;
694  
695 1837 my $non_bin_length = $data_length - $bin_length;
696  
697 1837 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 406 ▶{
Subroutine _IS_MAILBOX: called CC=3 SCAR=13.3
708 406 my $test_characters = shift;
709  
710 406 ▶ 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) { } 406 0 406
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
00406
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 406 return 1;
714 }
715 else
716 {
717 0 return 0;
718 }
719 }
720  
721 #-------------------------------------------------------------------------------
722  
723 sub reset
724 40 ▶{
Subroutine reset: called CC=2 SCAR=7.1
Pod documented
725 40 my $self = shift;
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 40 ▶{
Subroutine _IS_A_PIPE: called CC=5 SCAR=22.7
747 40 my $file_handle = shift;
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 966 ▶{
Subroutine endline: called CC=1 SCAR=0.0
Pod documented
757 966 my $self = shift;
758  
759 966 return $self->{'endline'};
760 }
761  
762 #-------------------------------------------------------------------------------
763  
764 sub prologue
765 320 ▶{
Subroutine prologue: called CC=1 SCAR=0.0
Pod documented
766 320 my $self = shift;
767  
768 320 return $self->{'prologue'};
769 }
770  
771 #-------------------------------------------------------------------------------
772  
773 sub _print_debug_information
774 406 ▶{
Subroutine _print_debug_information: called CC=5 SCAR=30.6
775 406 ▶ return unless $_DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
unless $Mail::Mbox::MessageParser::_DEBUG 406 0 406
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 0 ▶{
Subroutine end_of_file: not called CC=2 SCAR=17.9
Pod documented
802 0 my $self = shift;
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 360 ▶{
Subroutine line_number: called CC=1 SCAR=0.0
Pod documented
816 360 my $self = shift;
817  
818 360 return $self->{'email_line_number'};
819 }
820  
821 #-------------------------------------------------------------------------------
822  
823 sub number
824 360 ▶{
Subroutine number: called CC=1 SCAR=0.0
Pod documented
825 360 my $self = shift;
826  
827 360 return $self->{'email_number'};
828 }
829  
830 #-------------------------------------------------------------------------------
831  
832 # The length of the last email read
833 sub length
834 360 ▶{
Subroutine length: called CC=1 SCAR=0.0
Pod documented
835 360 my $self = shift;
836  
837 360 return $self->{'email_length'};
838 }
839  
840 #-------------------------------------------------------------------------------
841  
842 # The offset of the last email read
843 sub offset
844 360 ▶{
Subroutine offset: called CC=1 SCAR=0.0
Pod documented
845 360 my $self = shift;
846  
847 360 return $self->{'email_offset'};
848 }
849  
850 #-------------------------------------------------------------------------------
851  
852 sub _read_prologue
853 0 ▶{
Subroutine _read_prologue: not called CC=1 SCAR=6.9
854 0 die "Derived class must provide an implementation";
855 }
856  
857 #-------------------------------------------------------------------------------
858  
859 sub read_next_email
860 2337 ▶{
Subroutine read_next_email: called CC=3 SCAR=11.0
Pod documented
861 2337 my $self = shift;
862  
863 2337 ▶ if ($UPDATING_CACHE)
Branch: 1 branch
BranchTrueFalseTotal
if ($Mail::Mbox::MessageParser::UPDATING_CACHE) 1381 956 2337
864 {
865 1381 _dprint "Storing data into cache, length " . $self->{'email_length'};
866  
867 1381 my $_CACHE = $Mail::Mbox::MessageParser::Cache::_CACHE;
868  
869 $_CACHE->{$self->{'file_name'}}{'emails'}[$self->{'email_number'}-1]{'length'} =
870 1381 $self->{'email_length'};
871  
872 $_CACHE->{$self->{'file_name'}}{'emails'}[$self->{'email_number'}-1]{'line_number'} =
873 1381 $self->{'email_line_number'};
874  
875 $_CACHE->{$self->{'file_name'}}{'emails'}[$self->{'email_number'}-1]{'offset'} =
876 1381 $self->{'email_offset'};
877 1381 $_CACHE->{$self->{'file_name'}}{'emails'}[$self->{'email_number'}-1]{'validated'} =
878 1;
879  
880 1381 $_CACHE->{$self->{'file_name'}}{'modified'} = 1;
881  
882 1381 ▶ if ($self->end_of_file())
Branch: 1 branch
BranchTrueFalseTotal
if ($self->end_of_file) 253 1128 1381
883 {
884 253 $UPDATING_CACHE = 0;
885  
886 # Last one is always validated
887 253 $_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 62 ▶{
Subroutine _GET_HEADER_FIELD: called CC=12 SCAR=32.1
906 62 my $email_header = shift;
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 51 1;
939  
940 __END__
↑ Summary blib/lib/Mail/Mbox/MessageParser/Grep.pm »