/usr/share/perl5/Email/Folder/Mbox.pm is in libemail-folder-perl 0.860-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 | use strict;
use warnings;
package Email::Folder::Mbox;
{
$Email::Folder::Mbox::VERSION = '0.860';
}
# ABSTRACT: reads raw RFC822 mails from an mbox file
use Carp;
use IO::File;
use Email::Folder::Reader;
use parent 'Email::Folder::Reader';
sub defaults {
( eol => "\n")
}
sub _open_it {
my $self = shift;
my $file = $self->{_file};
my $fh = $self->{fh};
unless ($file eq "FH" and $fh) {
# sanity checking
croak "$file does not exist" unless (-e $file);
croak "$file is a directory" if (-d $file);
local $/ = $self->{eol};
$fh = $self->_get_fh($file);
}
if (seek $fh, tell($fh), 0) {
# Enable using seek only if $fh is seekable
$self->{seekable} = 1;
} else {
# Otherwise use cache for simulating backward seeks
$self->{cache} = [];
}
if ($self->{seek_to}) {
unless ($self->{seekable}) {
croak "$file is not seekable but seek_to was set";
}
# we were told to seek. hope it all goes well
seek $fh, $self->{seek_to}, 0;
}
else {
local $/ = $self->{eol};
my $firstline = <$fh>;
if ($firstline) {
croak "$file is not an mbox file" unless $firstline =~ /^From /;
}
$self->{from} = $firstline;
}
$self->{_fh} = $fh;
}
sub _get_fh {
my $self = shift;
my $file = shift;
my $fh = IO::File->new($file) or croak "Cannot open $file";
binmode($fh);
return $fh;
}
sub _read_nextline {
my $self = shift;
if (not $self->{seekable} and @{$self->{cache}}) {
return shift @{$self->{cache}};
}
my $fh = $self->{_fh};
return <$fh>;
}
use constant debug => 0;
my $count;
sub next_from {
my $self = shift;
$self->_open_it unless $self->{_fh};
return $self->{from};
}
sub next_messageref {
my $self = shift;
my $fh = $self->{_fh} || $self->_open_it;
local $/ = $self->{eol};
$self->{messageid} = undef;
my $mail = '';
my $prev = '';
my $last;
my $inheaders = 1;
++$count;
print "$count starting scanning at line $.\n" if debug;
while (my $line = _read_nextline($self)) {
if ($line eq $/ && $inheaders) { # end of headers
print "$count end of headers at line $.\n" if debug;
$inheaders = 0; # stop looking for the end of headers
my $pos; # where to go back to if it goes wrong
$pos = tell $fh if $self->{seekable};
# look for a content length header, and try to use that
if ($mail =~ m/^Content-Length:\s*(\d+)$/mi) {
my @cache;
$mail .= $prev;
$prev = '';
my $length = $1;
print " Content-Length: $length\n" if debug;
my $read = '';
while (my $bodyline = _read_nextline($self)) {
push @cache, $bodyline unless $self->{seekable};
last if length $read >= $length;
# unescape From_
$bodyline =~ s/^>(>*From )/$1/ if $self->{unescape};
$read .= $bodyline;
}
# grab the next line (should be /^From / or undef)
my $next = _read_nextline($self);
if (!defined $next || $next =~ /^From /) {
$self->{from} = $next;
$mail .= "$/$read";
return \$mail;
}
push @cache, $next unless $self->{seekable};
# seek back and scan line-by-line like the header
# wasn't here
print " Content-Length assertion failed '$next'\n" if debug;
if ($self->{seekable}) {
seek $fh, $pos, 0;
}
else {
unshift @{$self->{cache}}, @cache;
}
}
# much the same, but with Lines:
if ($mail =~ m/^Lines:\s*(\d+)$/mi) {
my @cache;
$mail .= $prev;
$prev = '';
my $lines = $1;
print " Lines: $lines\n" if debug;
my $read = '';
for (1 .. $lines) {
my $bodyline = _read_nextline($self);
last unless defined $bodyline;
push @cache, $bodyline unless $self->{seekable};
# unescape From_
$bodyline =~ s/^>(>*From )/$1/ if $self->{unescape};
$read .= $bodyline;
}
my $ign = _read_nextline($self); # trailing newline
my $next = _read_nextline($self);
if (!defined $next || $next =~ /^From /) {
$self->{from} = $next;
$mail .= "$/$read";
return \$mail;
}
push @cache, $ign, $next unless $self->{seekable};
# seek back and scan line-by-line like the header
# wasn't here
print " Lines assertion failed '$next'\n" if debug;
if ($self->{seekable}) {
seek $fh, $pos, 0;
}
else {
unshift @{$self->{cache}}, @cache;
}
}
}
if ($prev eq $/ && ($line =~ $self->_from_line_re)) {
$last = $line;
last;
}
if ($inheaders && !defined $self->{messageid} && ($line =~ /^Message-Id:\s*(.+)/i)) {
$self->{messageid} = $1;
}
$mail .= $prev;
$prev = $line;
# unescape From_
$prev =~ s/^>(>*From )/$1/ if $self->{unescape};
}
$mail .= $prev;
print "$count end of message line $.\n" if debug;
$self->{from} = $last;
return unless $mail;
return \$mail;
}
sub next_message {
my $self = shift;
my $ref = $self->next_messageref;
return unless $ref;
return ${$ref};
}
my @FROM_RE;
BEGIN {
@FROM_RE = (
# according to mutt:
# A valid message separator looks like:
# From [ <return-path> ] <weekday> <month> <day> <time> [ <tz> ] <year>
qr/^From (?:\S+\s+)?(?:Mon|Tue|Wed|Thu|Fri|Sat|Sun)/,
# though, as jwz rants, only this is reliable and portable
qr/^From /,
);
}
sub _from_line_re {
return $FROM_RE[ $_[0]->{jwz_From_} ? 1 : 0 ];
}
sub tell {
my $self = shift;
return tell $self->{_fh};
}
sub messageid {
my $self = shift;
return $self->{messageid};
}
1;
__END__
=pod
=encoding UTF-8
=head1 NAME
Email::Folder::Mbox - reads raw RFC822 mails from an mbox file
=head1 VERSION
version 0.860
=head1 SYNOPSIS
This isa Email::Folder::Reader - read about its API there.
=head1 DESCRIPTION
Does exactly what it says on the tin - fetches raw RFC822 mails from an
mbox.
The mbox format is described at http://www.qmail.org/man/man5/mbox.html
We attempt to read an mbox as through it's the mboxcl2 variant,
falling back to regular mbox mode if there is no C<Content-Length>
header to be found.
=head2 OPTIONS
The new constructor takes extra options.
=over
=item C<fh>
When filename is set to C<"FH"> than Email::Folder::Mbox will read mbox
archive from filehandle C<fh> instead from disk file C<filename>.
=item C<eol>
This indicates what the line-ending style is to be. The default is
C<"\n">, but for handling files with mac line-endings you would want
to specify C<eol =E<gt> "\x0d">
=item C<jwz_From_>
The value is taken as a boolean that governs what is used match as a
message separator.
If false we use the mutt style
/^From \S+\s+(?:Mon|Tue|Wed|Thu|Fri|Sat|Sun)/
/^From (?:Mon|Tue|Wed|Thu|Fri|Sat|Sun)/;
If true we use
/^From /
In deference to this extract from L<http://www.jwz.org/doc/content-length.html>
Essentially the only safe way to parse that file format is to
consider all lines which begin with the characters ``From ''
(From-space), which are preceded by a blank line or
beginning-of-file, to be the division between messages. That is, the
delimiter is "\n\nFrom .*\n" except for the very first message in the
file, where it is "^From .*\n".
Some people will tell you that you should do stricter parsing on
those lines: check for user names and dates and so on. They are
wrong. The random crap that has traditionally been dumped into that
line is without bound; comparing the first five characters is the
only safe and portable thing to do. Usually, but not always, the next
token on the line after ``From '' will be a user-id, or email
address, or UUCP path, and usually the next thing on the line will be
a date specification, in some format, and usually there's nothing
after that. But you can't rely on any of this.
Defaults to false.
=item C<unescape>
This boolean value indicates whenever lines which starts with
/^>+From /
should be unescaped (= removed leading '>' char). This is needed for
mboxrd and mboxcl variants. But there is no way to detect for used mbox
variant, so default value is false.
=item C<seek_to>
Seek to an offset when opening the mbox. When used in combination with
->tell you may be able to resume reading, with a trailing wind.
=item C<next_message>
This returns next message as string
=item C<next_messageref>
This returns next message as ref to string
=item C<tell>
This returns the current filehandle position in the mbox.
=item C<next_from>
This returns the From_ line for next message. Call it before ->next_message.
=item C<messageid>
This returns the messageid of last read message. Call if after ->next_message.
=back
=head1 AUTHORS
=over 4
=item *
Simon Wistow <simon@thegestalt.org>
=item *
Richard Clamp <richardc@unixbeard.net>
=item *
Pali <pali@cpan.org>
=back
=head1 COPYRIGHT AND LICENSE
This software is copyright (c) 2006 by Simon Wistow.
This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.
=cut
|