Folder/Mbox.pm

filter: Statementstmt 91.6% 132 / 144 Branchbran 66.1% 45 / 68 Conditioncond 71.4% 15 / 21 MC/DCmcdc 50.0% 7 / 14 Subroutinesub 94.1% 16 / 17 Podpod 60.0% 3 / 5 totaltotal 81.0% 218 / 269 CC 43 SCAR 28.3
CC
43
Cov
82%
next_messageref
30.0
_open_it
11.6
next_from
6.0
CRAP 17.0
↑ Summary Folder/Ezmlm.pm »
1 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 package Email::Folder::Mbox;
4 # ABSTRACT: reads raw RFC822 mails from an mbox file
5 1 $Email::Folder::Mbox::VERSION = '0.859';
6 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Email::Folder::Reader;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use parent 'Email::Folder::Reader';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 #pod =head1 SYNOPSIS
12 #pod
13 #pod This isa Email::Folder::Reader - read about its API there.
14 #pod
15 #pod =head1 DESCRIPTION
16 #pod
17 #pod Does exactly what it says on the tin - fetches raw RFC822 mails from an
18 #pod mbox.
19 #pod
20 #pod The mbox format is described at http://www.qmail.org/man/man5/mbox.html
21 #pod
22 #pod We attempt to read an mbox as through it's the mboxcl2 variant,
23 #pod falling back to regular mbox mode if there is no C<Content-Length>
24 #pod header to be found.
25 #pod
26 #pod =head2 OPTIONS
27 #pod
28 #pod The new constructor takes extra options.
29 #pod
30 #pod =over
31 #pod
32 #pod =item C<fh>
33 #pod
34 #pod When filename is set to C<"FH"> than Email::Folder::Mbox will read mbox
35 #pod archive from filehandle C<fh> instead from disk file C<filename>.
36 #pod
37 #pod =item C<eol>
38 #pod
39 #pod This indicates what the line-ending style is to be. The default is
40 #pod C<"\n">, but for handling files with mac line-endings you would want
41 #pod to specify C<eol =E<gt> "\x0d">
42 #pod
43 #pod =item C<jwz_From_>
44 #pod
45 #pod The value is taken as a boolean that governs what is used match as a
46 #pod message separator.
47 #pod
48 #pod If false we use the mutt style
49 #pod
50 #pod /^From \S+\s+(?:Mon|Tue|Wed|Thu|Fri|Sat|Sun)/
51 #pod /^From (?:Mon|Tue|Wed|Thu|Fri|Sat|Sun)/;
52 #pod
53 #pod If true we use
54 #pod
55 #pod /^From /
56 #pod
57 #pod In deference to this extract from L<http://www.jwz.org/doc/content-length.html>
58 #pod
59 #pod Essentially the only safe way to parse that file format is to
60 #pod consider all lines which begin with the characters ``From ''
61 #pod (From-space), which are preceded by a blank line or
62 #pod beginning-of-file, to be the division between messages. That is, the
63 #pod delimiter is "\n\nFrom .*\n" except for the very first message in the
64 #pod file, where it is "^From .*\n".
65 #pod
66 #pod Some people will tell you that you should do stricter parsing on
67 #pod those lines: check for user names and dates and so on. They are
68 #pod wrong. The random crap that has traditionally been dumped into that
69 #pod line is without bound; comparing the first five characters is the
70 #pod only safe and portable thing to do. Usually, but not always, the next
71 #pod token on the line after ``From '' will be a user-id, or email
72 #pod address, or UUCP path, and usually the next thing on the line will be
73 #pod a date specification, in some format, and usually there's nothing
74 #pod after that. But you can't rely on any of this.
75 #pod
76 #pod Defaults to false.
77 #pod
78 #pod =item C<unescape>
79 #pod
80 #pod This boolean value indicates whenever lines which starts with
81 #pod
82 #pod /^>+From /
83 #pod
84 #pod should be unescaped (= removed leading '>' char). This is needed for
85 #pod mboxrd and mboxcl variants. But there is no way to detect for used mbox
86 #pod variant, so default value is false.
87 #pod
88 #pod =item C<seek_to>
89 #pod
90 #pod Seek to an offset when opening the mbox. When used in combination with
91 #pod ->tell you may be able to resume reading, with a trailing wind.
92 #pod
93 #pod =item <next_message>
94 #pod
95 #pod This returns next message as string
96 #pod
97 #pod =item <next_messageref>
98 #pod
99 #pod This returns next message as ref to string
100 #pod
101 #pod =item C<tell>
102 #pod
103 #pod This returns the current filehandle position in the mbox.
104 #pod
105 #pod =item C<next_from>
106 #pod
107 #pod This returns the From_ line for next message. Call it before ->next_message.
108 #pod
109 #pod =back
110 #pod
111 #pod =cut
112  
113 sub defaults {
114 9 ▶ ( eol => "\n")
Subroutine defaults: called CC=1 SCAR=0.0
Pod undocumented
115 }
116  
117 sub _open_it {
118 9 ▶ my $self = shift;
Subroutine _open_it: called CC=10 SCAR=24.5
119 9 my $file = $self->{_file};
120 9 my $fh = $self->{fh};
121  
122 9 ▶ unless ($file eq "FH" and $fh) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($file eq "FH" and $fh) 9 0 9
Condition33%1/3
$file eq "FH" and $fh
!ll&&!rl&&r
900
MC/DC0%0/2
$file eq "FH" and $fh
$file eq "FH" $fh
Truth table33%1/3
A and B
A= $file eq "FH"B= $fh
ABresult
0X0
100
111
123 # sanity checking
124 9 ▶ croak "$file does not exist" unless (-e $file);
Branch: 1 branch
BranchTrueFalseTotal
unless -e $file 0 9 9
125 9 ▶ croak "$file is not a file" unless (-f $file);
Branch: 1 branch
BranchTrueFalseTotal
unless -f $file 0 9 9
126  
127 9 local $/ = $self->{eol};
128 9 $fh = $self->_get_fh($file);
129 }
130  
131 9 ▶ if (seek $fh, tell($fh), 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (seek $fh, CORE::tell $fh, 0) { } 9 0 9
132 # Enable using seek only if $fh is seekable
133 9 $self->{seekable} = 1;
134 } else {
135 # Otherwise use cache for simulating backward seeks
136 0 $self->{cache} = [];
137 }
138  
139 9 ▶ if ($self->{seek_to}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'seek_to'}) { } 1 8 9
140 1 ▶ unless ($self->{seekable}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"seekable"}) 0 1 1
141 0 croak "$file is not seekable but seek_to was set";
142 }
143 # we were told to seek. hope it all goes well
144 1 seek $fh, $self->{seek_to}, 0;
145 }
146 else {
147 8 local $/ = $self->{eol};
148 8 my $firstline = <$fh>;
149 8 ▶ if ($firstline) {
Branch: 1 branch
BranchTrueFalseTotal
if ($firstline) 7 1 8
150 7 ▶ croak "$file is not an mbox file" unless $firstline =~ /^From /;
Branch: 1 branch
BranchTrueFalseTotal
unless $firstline =~ /^From / 0 7 7
151 }
152 8 $self->{from} = $firstline;
153 }
154  
155 9 $self->{_fh} = $fh;
156 }
157  
158 sub _get_fh {
159 9 ▶ my $self = shift;
Subroutine _get_fh: called CC=2 SCAR=7.0
160 9 my $file = shift;
161 9 ▶ my $fh = IO::File->new($file) or croak "Cannot open $file";
Branch: 1 branch
BranchTrueFalseTotal
my $fh = "IO::File"->new($file) or croak("Cannot open $file") 0 9 9
162 9 binmode($fh);
163 9 return $fh;
164 }
165  
166 sub _read_nextline {
167 2884 ▶ my $self = shift;
Subroutine _read_nextline: called CC=3 SCAR=14.2
168 2884 ▶ if (not $self->{seekable} and @{$self->{cache}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{"seekable"} and @{$self->{"cache"};}) 0 2884 2884
Condition33%1/3
not $self->{"seekable"} and @{$self->{"cache"};}
!ll&&!rl&&r
288400
MC/DC0%0/2
not $self->{"seekable"} and @{$self->{"cache"};}
not $self->{"seekable"} @{$self->{"cache"};}
Truth table33%1/3
not(A) and B
A= not $self->{"seekable"}B= @{$self->{"cache"};}
ABresult
0X0
100
111
169 0 return shift @{$self->{cache}};
170 }
171 2884 my $fh = $self->{_fh};
172 2884 return <$fh>;
173 }
174  
175 1 ▶use constant debug => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
176 1 my $count;
177  
178 sub next_from {
179 0 ▶ my $self = shift;
Subroutine next_from: not called CC=2 SCAR=17.9
Pod documented
180 0 ▶ $self->_open_it unless $self->{_fh};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"_fh"} 0 0 0
181 0 return $self->{from};
182 }
183  
184 sub next_messageref {
185 47 ▶ my $self = shift;
Subroutine next_messageref: called CC=28 SCAR=34.0
Pod undocumented
186  
187 47 ▶ my $fh = $self->{_fh} || $self->_open_it;
Condition66%2/3
$self->{'_fh'} || $self->_open_it
l!l&&r!l&&!r
3890
MC/DC0%0/2
$self->{'_fh'} || $self->_open_it
$self->{'_fh'} $self->_open_it
Truth table66%2/3
A || B
A= $self->{'_fh'}B= $self->_open_it
ABresult
1X1
011
000
188 47 local $/ = $self->{eol};
189  
190 47 my $mail = '';
191 47 my $prev = '';
192 47 my $last;
193 47 my $inheaders = 1;
194 47 ++$count;
195 47 print "$count starting scanning at line $.\n" if debug;
196  
197 47 ▶ while (my $line = _read_nextline($self)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $line = _read_nextline($self)) 2310 9 2319
198 2310 ▶ if ($line eq $/ && $inheaders) { # end of headers
Branch: 1 branch
BranchTrueFalseTotal
if ($line eq $/ and $inheaders) 40 2270 2310
Condition100%3/3
$line eq $/ and $inheaders
!ll&&!rl&&r
205421640
MC/DC100%2/2
$line eq $/ and $inheaders
$line eq $/ $inheaders
Truth table100%3/3
A and B
A= $line eq $/B= $inheaders
ABresult
0X0
100
111
199 40 print "$count end of headers at line $.\n" if debug;
200 40 $inheaders = 0; # stop looking for the end of headers
201 40 my $pos; # where to go back to if it goes wrong
202 40 ▶ $pos = tell $fh if $self->{seekable};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"seekable"} 40 0 40
203  
204 # look for a content length header, and try to use that
205 40 ▶ if ($mail =~ m/^Content-Length:\s*(\d+)$/mi) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mail =~ /^Content-Length:\s*(\d+)$/im) 18 22 40
206 18 my @cache;
207 18 $mail .= $prev;
208 18 $prev = '';
209 18 my $length = $1;
210 18 print " Content-Length: $length\n" if debug;
211 18 my $read = '';
212 18 ▶ while (my $bodyline = _read_nextline($self)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $bodyline = _read_nextline($self)) 506 0 506
213 506 ▶ push @cache, $bodyline unless $self->{seekable};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"seekable"} 0 506 506
214 506 ▶ last if length $read >= $length;
Branch: 1 branch
BranchTrueFalseTotal
if length $read >= $length 18 488 506
215 # unescape From_
216 488 ▶ $bodyline =~ s/^>(>*From )/$1/ if $self->{unescape};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"unescape"} 0 488 488
217 488 $read .= $bodyline;
218 }
219 # grab the next line (should be /^From / or undef)
220 18 my $next = _read_nextline($self);
221 18 ▶ if (!defined $next || $next =~ /^From /) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $next or $next =~ /^From /) 17 1 18
Condition100%3/3
not defined $next or $next =~ /^From /
l!l&&r!l&&!r
3141
MC/DC100%2/2
not defined $next or $next =~ /^From /
not defined $next $next =~ /^From /
Truth table100%3/3
not(A) or B
A= not defined $nextB= $next =~ /^From /
ABresult
1X1
011
000
222 17 $self->{from} = $next;
223 17 $mail .= "$/$read";
224 17 return \$mail;
225 }
226 1 ▶ push @cache, $next unless $self->{seekable};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"seekable"} 0 1 1
227 # seek back and scan line-by-line like the header
228 # wasn't here
229 1 print " Content-Length assertion failed '$next'\n" if debug;
230 1 ▶ if ($self->{seekable}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'seekable'}) { } 1 0 1
231 1 seek $fh, $pos, 0;
232 }
233 else {
234 0 unshift @{$self->{cache}}, @cache;
235 }
236 }
237  
238 # much the same, but with Lines:
239 23 ▶ if ($mail =~ m/^Lines:\s*(\d+)$/mi) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mail =~ /^Lines:\s*(\d+)$/im) 2 21 23
240 2 my @cache;
241 2 $mail .= $prev;
242 2 $prev = '';
243 2 my $lines = $1;
244 2 print " Lines: $lines\n" if debug;
245 2 my $read = '';
246 2 for (1 .. $lines) {
247 37 my $bodyline = _read_nextline($self);
248 37 ▶ last unless defined $bodyline;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $bodyline 0 37 37
249 37 ▶ push @cache, $bodyline unless $self->{seekable};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"seekable"} 0 37 37
250 # unescape From_
251 37 ▶ $bodyline =~ s/^>(>*From )/$1/ if $self->{unescape};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"unescape"} 0 37 37
252 37 $read .= $bodyline;
253 }
254 2 my $ign = _read_nextline($self); # trailing newline
255 2 my $next = _read_nextline($self);
256 2 ▶ if (!defined $next || $next =~ /^From /) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $next or $next =~ /^From /) 1 1 2
Condition66%2/3
not defined $next or $next =~ /^From /
l!l&&r!l&&!r
101
MC/DC50%1/2
not defined $next or $next =~ /^From /
not defined $next $next =~ /^From /
Truth table66%2/3
not(A) or B
A= not defined $nextB= $next =~ /^From /
ABresult
1X1
011
000
257 1 $self->{from} = $next;
258 1 $mail .= "$/$read";
259 1 return \$mail;
260 }
261 1 ▶ push @cache, $ign, $next unless $self->{seekable};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"seekable"} 0 1 1
262 # seek back and scan line-by-line like the header
263 # wasn't here
264 1 print " Lines assertion failed '$next'\n" if debug;
265 1 ▶ if ($self->{seekable}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'seekable'}) { } 1 0 1
266 1 seek $fh, $pos, 0;
267 }
268 else {
269 0 unshift @{$self->{cache}}, @cache;
270 }
271 }
272 }
273  
274 2292 ▶ if ($prev eq $/ && ($line =~ $self->_from_line_re)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prev eq $/ and $line =~ $self->_from_line_re) 20 2272 2292
Condition100%3/3
$prev eq $/ and $line =~ $self->_from_line_re
!ll&&!rl&&r
205621620
MC/DC100%2/2
$prev eq $/ and $line =~ $self->_from_line_re
$prev eq $/ $line =~ $self->_from_line_re
Truth table100%3/3
A and B
A= $prev eq $/B= $line =~ $self->_from_line_re
ABresult
0X0
100
111
275 20 $mail .= $prev;
276 20 $last = $line;
277 20 last;
278 }
279  
280 2272 $mail .= $prev;
281 2272 $prev = $line;
282  
283 # unescape From_
284 2272 ▶ $prev =~ s/^>(>*From )/$1/ if $self->{unescape};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"unescape"} 0 2272 2272
285 }
286 29 print "$count end of message line $.\n" if debug;
287 29 $self->{from} = $last;
288 29 ▶ return unless $mail;
Branch: 1 branch
BranchTrueFalseTotal
unless $mail 7 22 29
289 22 return \$mail;
290 }
291  
292 sub next_message {
293 47 ▶ my $self = shift;
Subroutine next_message: called CC=2 SCAR=6.9
Pod documented
294 47 my $ref = $self->next_messageref;
295 47 ▶ return unless $ref;
Branch: 1 branch
BranchTrueFalseTotal
unless $ref 7 40 47
296 40 return ${$ref};
297 }
298  
299 my @FROM_RE;
300 BEGIN {
301 1 ▶ @FROM_RE = (
Subroutine BEGIN: called CC=1 SCAR=0.0
302 # according to mutt:
303 # A valid message separator looks like:
304 # From [ <return-path> ] <weekday> <month> <day> <time> [ <tz> ] <year>
305 qr/^From (?:\S+\s+)?(?:Mon|Tue|Wed|Thu|Fri|Sat|Sun)/,
306  
307 # though, as jwz rants, only this is reliable and portable
308 qr/^From /,
309 );
310 }
311  
312 sub _from_line_re {
313 236 ▶ return $FROM_RE[ $_[0]->{jwz_From_} ? 1 : 0 ];
Subroutine _from_line_re: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{'jwz_From_'} ? : 0 236 236
314 }
315  
316 sub tell {
317 1 ▶ my $self = shift;
Subroutine tell: called CC=1 SCAR=0.0
Pod documented
318 1 return tell $self->{_fh};
319 }
320  
321 1 1;
322  
323 __END__
↑ Summary Folder/Ezmlm.pm »