| 1 | package Email::Sender::Transport::Maildir 2.603; | ||||||||||||||||||||||||||||
| 2 | # ABSTRACT: deliver mail to a maildir 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 Errno (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use Fcntl; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 2 | ▶ | use File::Path 2.06; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 2 | ▶ | use File::Spec; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 2 | ▶ | use Sys::Hostname; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 2 | ▶ | use MooX::Types::MooseLike::Base qw(Bool); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | #pod =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 17 | #pod | ||||||||||||||||||||||||||||
| 18 | #pod This transport delivers into a maildir. The maildir's location may be given as | ||||||||||||||||||||||||||||
| 19 | #pod the F<dir> argument to the constructor, and defaults to F<Maildir> in the | ||||||||||||||||||||||||||||
| 20 | #pod current directory (at the time of transport initialization). | ||||||||||||||||||||||||||||
| 21 | #pod | ||||||||||||||||||||||||||||
| 22 | #pod If the directory does not exist, it will be created. | ||||||||||||||||||||||||||||
| 23 | #pod | ||||||||||||||||||||||||||||
| 24 | #pod By default, three headers will be added: | ||||||||||||||||||||||||||||
| 25 | #pod | ||||||||||||||||||||||||||||
| 26 | #pod * X-Email-Sender-From - the envelope sender | ||||||||||||||||||||||||||||
| 27 | #pod * X-Email-Sender-To - the envelope recipients (one header per rcpt) | ||||||||||||||||||||||||||||
| 28 | #pod * Lines - the number of lines in the body | ||||||||||||||||||||||||||||
| 29 | #pod | ||||||||||||||||||||||||||||
| 30 | #pod These can be controlled with the C<add_lines_header> and | ||||||||||||||||||||||||||||
| 31 | #pod C<add_envelope_headers> constructor arguments. | ||||||||||||||||||||||||||||
| 32 | #pod | ||||||||||||||||||||||||||||
| 33 | #pod The L<Email::Sender::Success> object returned on success has a C<filename> | ||||||||||||||||||||||||||||
| 34 | #pod method that returns the filename to which the message was delivered. | ||||||||||||||||||||||||||||
| 35 | #pod | ||||||||||||||||||||||||||||
| 36 | #pod =cut | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | { | ||||||||||||||||||||||||||||
| 39 | package | ||||||||||||||||||||||||||||
| 40 | Email::Sender::Success::MaildirSuccess; | ||||||||||||||||||||||||||||
| 41 | 2 | ▶ | use Moo; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 42 | 2 | ▶ | use MooX::Types::MooseLike::Base qw(Str); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 43 | 2 | extends 'Email::Sender::Success'; | |||||||||||||||||||||||||||
| 44 | 2 | has filename => ( | |||||||||||||||||||||||||||
| 45 | is => 'ro', | ||||||||||||||||||||||||||||
| 46 | isa => Str, | ||||||||||||||||||||||||||||
| 47 | required => 1, | ||||||||||||||||||||||||||||
| 48 | ); | ||||||||||||||||||||||||||||
| 49 | 2 | ▶ | no Moo; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 2 | my $HOSTNAME; | |||||||||||||||||||||||||||
| 54 | 2 | ▶ | BEGIN { ($HOSTNAME = hostname) =~ s/\..*//; } | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 55 | 2 | ▶ | sub _hostname { $HOSTNAME } | ||||||||||||||||||||||||||
|
Subroutine
_hostname: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 2 | my $MAILDIR_TIME = 0; | |||||||||||||||||||||||||||
| 58 | 2 | my $MAILDIR_COUNTER = 0; | |||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | has [ qw(add_lines_header add_envelope_headers) ] => ( | ||||||||||||||||||||||||||||
| 61 | is => 'ro', | ||||||||||||||||||||||||||||
| 62 | isa => Bool, | ||||||||||||||||||||||||||||
| 63 | 2 | ▶ | default => sub { 1 }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 64 | 2 | ); | |||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | has dir => ( | ||||||||||||||||||||||||||||
| 67 | is => 'ro', | ||||||||||||||||||||||||||||
| 68 | required => 1, | ||||||||||||||||||||||||||||
| 69 | 0 | ▶ | default => sub { File::Spec->catdir(File::Spec->curdir, 'Maildir') }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 70 | 2 | ); | |||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 2 | ▶ | sub send_email { | ||||||||||||||||||||||||||
|
Subroutine
send_email: called CC=5 SCAR=16.4
Pod
undocumented
| |||||||||||||||||||||||||||||
| 73 | 2 | my ($self, $email, $env) = @_; | |||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 2 | my $dupe = Email::Abstract->new(\do { $email->as_string }); | |||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 2 | ▶ | if ($self->add_envelope_headers) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 78 | $dupe->set_header('X-Email-Sender-From' => | ||||||||||||||||||||||||||||
| 79 | 2 | ▶ | (defined $env->{from} ? $env->{from} : '-'), | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | ); | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 2 | my @to = grep {; defined } @{ $env->{to} }; | |||||||||||||||||||||||||||
| 83 | 2 | ▶ | $dupe->set_header('X-Email-Sender-To' => (@to ? @to : '-')); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 2 | $self->_ensure_maildir_exists; | |||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 2 | ▶ | $self->_add_lines_header($dupe) if $self->add_lines_header; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 89 | 2 | $self->_update_time; | |||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 2 | my $fn = $self->_deliver_email($dupe); | |||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 2 | return Email::Sender::Success::MaildirSuccess->new({ | |||||||||||||||||||||||||||
| 94 | filename => $fn, | ||||||||||||||||||||||||||||
| 95 | }); | ||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 2 | ▶ | sub _ensure_maildir_exists { | ||||||||||||||||||||||||||
|
Subroutine
_ensure_maildir_exists: called CC=5 SCAR=16.8
| |||||||||||||||||||||||||||||
| 99 | 2 | my ($self) = @_; | |||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 2 | for my $dir (qw(cur tmp new)) { | |||||||||||||||||||||||||||
| 102 | 6 | my $subdir = File::Spec->catdir($self->dir, $dir); | |||||||||||||||||||||||||||
| 103 | 6 | ▶ | next if -d $subdir; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 3 | ▶ | Email::Sender::Failure->throw("couldn't create $subdir: $!") | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
File::Path::make_path($subdir) or -d $subdir
MC/DC0%0/2
File::Path::make_path($subdir) or -d $subdir
File::Path::make_path($subdir)
-d $subdir
Truth table33%1/3
A or B
A= File::Path::make_path($subdir)B= -d $subdir
| |||||||||||||||||||||||||||||
| 106 | unless File::Path::make_path($subdir) || -d $subdir; | ||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 2 | ▶ | sub _add_lines_header { | ||||||||||||||||||||||||||
|
Subroutine
_add_lines_header: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||
| 111 | 2 | my ($class, $email) = @_; | |||||||||||||||||||||||||||
| 112 | 2 | ▶ | return if $email->get_header("Lines"); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 113 | 2 | my $lines = $email->get_body =~ tr/\n/\n/; | |||||||||||||||||||||||||||
| 114 | 2 | $email->set_header("Lines", $lines); | |||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 2 | ▶ | sub _update_time { | ||||||||||||||||||||||||||
|
Subroutine
_update_time: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 118 | 2 | my $time = time; | |||||||||||||||||||||||||||
| 119 | 2 | ▶ | if ($MAILDIR_TIME != $time) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 120 | 1 | $MAILDIR_TIME = $time; | |||||||||||||||||||||||||||
| 121 | 1 | $MAILDIR_COUNTER = 0; | |||||||||||||||||||||||||||
| 122 | } else { | ||||||||||||||||||||||||||||
| 123 | 1 | $MAILDIR_COUNTER++; | |||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | 2 | ▶ | sub _deliver_email { | ||||||||||||||||||||||||||
|
Subroutine
_deliver_email: called CC=5 SCAR=16.6
| |||||||||||||||||||||||||||||
| 128 | 2 | my ($self, $email) = @_; | |||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | 2 | my ($tmp_filename, $tmp_fh) = $self->_delivery_fh; | |||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | # if (eval { $email->can('stream_to') }) { | ||||||||||||||||||||||||||||
| 133 | # eval { $mail->stream_to($fh); 1 } or return; | ||||||||||||||||||||||||||||
| 134 | #} else { | ||||||||||||||||||||||||||||
| 135 | 2 | my $string = $email->as_string; | |||||||||||||||||||||||||||
| 136 | 2 | ▶ | $string =~ s/\x0D\x0A/\x0A/g unless $^O eq 'MSWin32'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 137 | 2 | ▶ | print $tmp_fh $string | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 138 | or Email::Sender::Failure->throw("could not write to $tmp_filename: $!"); | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | 2 | ▶ | close $tmp_fh | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 141 | or Email::Sender::Failure->throw("error closing $tmp_filename: $!"); | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | 2 | my $target_name = File::Spec->catfile($self->dir, 'new', $tmp_filename); | |||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | 2 | my $ok = rename( | |||||||||||||||||||||||||||
| 146 | File::Spec->catfile($self->dir, 'tmp', $tmp_filename), | ||||||||||||||||||||||||||||
| 147 | $target_name, | ||||||||||||||||||||||||||||
| 148 | ); | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | 2 | ▶ | Email::Sender::Failure->throw("could not move $tmp_filename from tmp to new") | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 151 | unless $ok; | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | 2 | return $target_name; | |||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | 2 | ▶ | sub _delivery_fh { | ||||||||||||||||||||||||||
|
Subroutine
_delivery_fh: called CC=4 SCAR=14.1
| |||||||||||||||||||||||||||||
| 157 | 2 | my ($self) = @_; | |||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | 2 | my $hostname = $self->_hostname; | |||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | 2 | my ($filename, $fh); | |||||||||||||||||||||||||||
| 162 | 2 | ▶ | until ($fh) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 163 | 2 | $filename = join q{.}, $MAILDIR_TIME, $$, ++$MAILDIR_COUNTER, $hostname; | |||||||||||||||||||||||||||
| 164 | 2 | my $filespec = File::Spec->catfile($self->dir, 'tmp', $filename); | |||||||||||||||||||||||||||
| 165 | 2 | sysopen $fh, $filespec, O_CREAT|O_EXCL|O_WRONLY; | |||||||||||||||||||||||||||
| 166 | 2 | binmode $fh; | |||||||||||||||||||||||||||
| 167 | Email::Sender::Failure->throw("cannot create $filespec for delivery: $!") | ||||||||||||||||||||||||||||
| 168 | 2 | ▶ | unless $fh or $!{EEXIST}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$fh or $!{"EEXIST"}
MC/DC0%0/2
$fh or $!{"EEXIST"}
$fh
$!{"EEXIST"}
Truth table33%1/3
A or B
A= $fhB= $!{"EEXIST"}
| |||||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | 2 | return ($filename, $fh); | |||||||||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | 2 | ▶ | no Moo; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 175 | 2 | 1; | |||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | __END__ | ||||||||||||||||||||||||||||