| 1 | package Email::Sender::Transport::Mbox 2.603; | ||||||||||||||||||||||||||||||||
| 2 | # ABSTRACT: deliver mail to an mbox on disk | ||||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use Moo; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 5 | 2 | with 'Email::Sender::Transport'; | |||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||
| 7 | 2 | ▶ | use Carp; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use File::Path; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 9 | 2 | ▶ | use File::Basename; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 10 | 2 | ▶ | use IO::File 1.11; # binmode | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 11 | 2 | ▶ | use Email::Simple 1.998; # needed for ->header_obj | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 12 | 2 | ▶ | use Fcntl ':flock'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||
| 14 | #pod =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||
| 15 | #pod | ||||||||||||||||||||||||||||||||
| 16 | #pod This transport delivers into an mbox. The mbox file may be given by the | ||||||||||||||||||||||||||||||||
| 17 | #pod F<filename> argument to the constructor, and defaults to F<mbox>. | ||||||||||||||||||||||||||||||||
| 18 | #pod | ||||||||||||||||||||||||||||||||
| 19 | #pod The transport I<currently> assumes that the mbox is in F<mboxo> format, but | ||||||||||||||||||||||||||||||||
| 20 | #pod this may change or be configurable in the future. | ||||||||||||||||||||||||||||||||
| 21 | #pod | ||||||||||||||||||||||||||||||||
| 22 | #pod =cut | ||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||
| 24 | 0 | ▶ | has 'filename' => (is => 'ro', default => sub { 'mbox' }, required => 1); | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||
| 26 | 2 | ▶ | sub send_email { | ||||||||||||||||||||||||||||||
|
Subroutine
send_email: called CC=9 SCAR=24.4
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 27 | 2 | my ($self, $email, $env) = @_; | |||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||
| 29 | 2 | my $filename = $self->filename; | |||||||||||||||||||||||||||||||
| 30 | 2 | my $fh = $self->_open_fh($filename); | |||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||
| 32 | 2 | my $ok = eval { | |||||||||||||||||||||||||||||||
| 33 | 2 | ▶ | if ($fh->tell > 0) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 34 | 1 | ▶ | $fh->print("\n") or Carp::confess("couldn't write to $filename: $!"); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||
| 37 | 2 | ▶ | $fh->print($self->_from_line($email, $env)) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 38 | or Carp::confess("couldn't write to $filename: $!"); | ||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||
| 40 | 2 | ▶ | $fh->print($self->_escape_from_body($email)) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 41 | or Carp::confess("couldn't write to $filename: $!"); | ||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||
| 43 | # This will make streaming a bit more annoying. -- rjbs, 2007-05-25 | ||||||||||||||||||||||||||||||||
| 44 | 2 | ▶ | $fh->print("\n") | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition20%1/5
$email->as_string =~ /\n$/ || ($fh->print("\n") || Carp::confess("couldn't write to ${filename}: $!"))
$fh->print("\n") or Carp::confess("couldn't write to ${filename}: $!")
MC/DC0%0/2
$email->as_string =~ /\n$/ || ($fh->print("\n") || Carp::confess("couldn't write to ${filename}: $!"))
$email->as_string =~ /\n$/
$fh->print("\n")
Truth table33%1/3
A || B
A= $email->as_string =~ /\n$/B= $fh->print("\n")
| |||||||||||||||||||||||||||||||||
| 45 | or Carp::confess("couldn't write to $filename: $!") | ||||||||||||||||||||||||||||||||
| 46 | unless $email->as_string =~ /\n$/; | ||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||
| 48 | 2 | ▶ | $self->_close_fh($fh) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 49 | or Carp::confess "couldn't close file $filename: $!"; | ||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||
| 51 | 2 | 1; | |||||||||||||||||||||||||||||||
| 52 | }; | ||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||
| 54 | 2 | ▶ | die unless $ok; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 55 | # Email::Sender::Failure->throw($@ || 'unknown error') unless $ok; | ||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||
| 57 | 2 | return $self->success; | |||||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||
| 60 | 2 | ▶ | sub _open_fh { | ||||||||||||||||||||||||||||||
|
Subroutine
_open_fh: called CC=4 SCAR=14.6
| |||||||||||||||||||||||||||||||||
| 61 | 2 | my ($class, $file) = @_; | |||||||||||||||||||||||||||||||
| 62 | 2 | my $dir = dirname($file); | |||||||||||||||||||||||||||||||
| 63 | 2 | ▶ | Carp::confess "couldn't make path $dir: $!" if not -d $dir or mkpath($dir); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not -d $dir or mkpath($dir)
MC/DC0%0/2
not -d $dir or mkpath($dir)
not -d $dir
mkpath($dir)
Truth table33%1/3
not(A) or B
A= not -d $dirB= mkpath($dir)
| |||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||
| 65 | 2 | ▶ | my $fh = IO::File->new($file, '>>') | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 66 | or Carp::confess "couldn't open $file for appending: $!"; | ||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||
| 68 | 2 | $fh->binmode(':raw'); | |||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||
| 70 | 2 | $class->_getlock($fh, $file); | |||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||
| 72 | 2 | $fh->seek(0, 2); | |||||||||||||||||||||||||||||||
| 73 | 2 | return $fh; | |||||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||
| 76 | 2 | ▶ | sub _close_fh { | ||||||||||||||||||||||||||||||
|
Subroutine
_close_fh: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 77 | 2 | my ($class, $fh, $file) = @_; | |||||||||||||||||||||||||||||||
| 78 | 2 | $class->_unlock($fh); | |||||||||||||||||||||||||||||||
| 79 | 2 | return $fh->close; | |||||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||
| 82 | 2 | ▶ | sub _escape_from_body { | ||||||||||||||||||||||||||||||
|
Subroutine
_escape_from_body: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 83 | 2 | my ($class, $email) = @_; | |||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||
| 85 | 2 | my $body = $email->get_body; | |||||||||||||||||||||||||||||||
| 86 | 2 | $body =~ s/^(From )/>$1/gm; | |||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||
| 88 | 2 | my $simple = $email->cast('Email::Simple'); | |||||||||||||||||||||||||||||||
| 89 | 2 | return $simple->header_obj->as_string . $simple->crlf . $body; | |||||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||
| 92 | 2 | ▶ | sub _from_line { | ||||||||||||||||||||||||||||||
|
Subroutine
_from_line: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 93 | 2 | my ($class, $email, $envelope) = @_; | |||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||
| 95 | 2 | my $fromtime = localtime; | |||||||||||||||||||||||||||||||
| 96 | 2 | $fromtime =~ s/(:\d\d) \S+ (\d{4})$/$1 $2/; # strip timezone. | |||||||||||||||||||||||||||||||
| 97 | 2 | return "From $envelope->{from} $fromtime\n"; | |||||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||
| 100 | 2 | ▶ | sub _getlock { | ||||||||||||||||||||||||||||||
|
Subroutine
_getlock: called CC=3 SCAR=13.1
| |||||||||||||||||||||||||||||||||
| 101 | 2 | my ($class, $fh, $fn) = @_; | |||||||||||||||||||||||||||||||
| 102 | 2 | for (1 .. 10) { | |||||||||||||||||||||||||||||||
| 103 | 2 | ▶ | return 1 if flock($fh, LOCK_EX | LOCK_NB); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 104 | 0 | sleep $_; | |||||||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||||||
| 106 | 0 | Carp::confess "couldn't lock file $fn"; | |||||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||
| 109 | 2 | ▶ | sub _unlock { | ||||||||||||||||||||||||||||||
|
Subroutine
_unlock: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 110 | 2 | my ($class, $fh) = @_; | |||||||||||||||||||||||||||||||
| 111 | 2 | flock($fh, LOCK_UN); | |||||||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||
| 114 | 2 | ▶ | no Moo; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 115 | 2 | 1; | |||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||
| 117 | __END__ | ||||||||||||||||||||||||||||||||