| 1 | use strict; | ||
| 2 | use warnings; | ||
| 3 | package Email::LocalDelivery::Maildir 1.201; | ||
| 4 | # ABSTRACT: deliver mail to a Maildir | ||
| 5 | use Email::Simple; | ||
| 6 | use File::Path; | ||
| 7 | use Symbol qw(gensym); | ||
| 8 | |||
| 9 | #pod =head1 INSTEAD... | ||
| 10 | #pod | ||
| 11 | #pod Instead, consider using L<Email::Sender::Transport::Maildir>. | ||
| 12 | #pod | ||
| 13 | #pod =cut | ||
| 14 | |||
| 15 | my $maildir_time = 0; | ||
| 16 | my $maildir_counter = 0; | ||
| 17 | use Sys::Hostname; | ||
| 18 | (my $HOSTNAME = hostname) =~ s/\..*//; | ||
| 19 | |||
| 20 | sub deliver { | ||
| 21 | my ($class, $mail, @files) = @_; | ||
| 22 | |||
| 23 | $mail = Email::Simple->new($mail) | ||
| 24 | unless eval { $mail->isa('Email::Simple') }; # For when we recurse | ||
| 25 | |||
| 26 | $class->fix_lines($mail); | ||
| 27 | $class->update_time(); | ||
| 28 | |||
| 29 | my $temp_file = $class->write_temp($mail, @files) or return; | ||
| 30 | |||
| 31 | my @written = $class->write_links($mail, $temp_file, @files); | ||
| 32 | unlink $temp_file; | ||
| 33 | return @written; | ||
| 34 | } | ||
| 35 | |||
| 36 | sub fix_lines { | ||
| 37 | my ($class, $mail) = @_; | ||
| 38 | return if $mail->header("Lines"); | ||
| 39 | my @lines = split /\n/, $mail->body; | ||
| 40 | $mail->header_set("Lines", scalar @lines); | ||
| 41 | } | ||
| 42 | |||
| 43 | sub update_time { | ||
| 44 | if ($maildir_time != time) { | ||
| 45 | $maildir_time = time; | ||
| 46 | $maildir_counter = 0; | ||
| 47 | } else { | ||
| 48 | $maildir_counter++; | ||
| 49 | } | ||
| 50 | } | ||
| 51 | |||
| 52 | sub write_temp { | ||
| 53 | my ($class, $mail, @files) = @_; | ||
| 54 | for my $file (@files) { | ||
| 55 | $file =~ s{/$}{}; | ||
| 56 | my $tmp_file = $class->get_filename_in($file . "/tmp"); | ||
| 57 | eval { | ||
| 58 | mkpath([ map { "$file/$_" } qw(tmp new cur) ]); | ||
| 59 | 1; | ||
| 60 | } or next; | ||
| 61 | $class->write_message($mail, $tmp_file) | ||
| 62 | and return $tmp_file; | ||
| 63 | } | ||
| 64 | return; | ||
| 65 | } | ||
| 66 | |||
| 67 | sub get_filename_in { | ||
| 68 | my ($class, $tmpdir) = @_; | ||
| 69 | my ($msg_file, $tmppath); | ||
| 70 | do { | ||
| 71 | $msg_file = join ".", ($maildir_time, $$ . "_$maildir_counter", $HOSTNAME); | ||
| 72 | } while -e ($tmppath = "$tmpdir/$msg_file") | ||
| 73 | and ++$maildir_counter; | ||
| 74 | return $tmppath; | ||
| 75 | } | ||
| 76 | |||
| 77 | sub write_links { | ||
| 78 | my ($class, $mail, $temp_file, @files) = @_; | ||
| 79 | my @rv; | ||
| 80 | for my $file (@files) { | ||
| 81 | $file =~ s{/$}{}; | ||
| 82 | my $new_location = $class->get_filename_in($file . "/new"); | ||
| 83 | eval { | ||
| 84 | mkpath([ map { "$file/$_" } qw(tmp new cur) ]); | ||
| 85 | 1; | ||
| 86 | } or next; | ||
| 87 | if (link $temp_file, $new_location) { | ||
| 88 | push @rv, $new_location; | ||
| 89 | } else { | ||
| 90 | require Errno; | ||
| 91 | import Errno qw(EXDEV); | ||
| 92 | if ($! == &EXDEV) { | ||
| 93 | push @rv, $class->deliver($mail, $file); | ||
| 94 | } | ||
| 95 | } | ||
| 96 | } | ||
| 97 | return @rv; | ||
| 98 | } | ||
| 99 | |||
| 100 | sub write_message { | ||
| 101 | my ($class, $mail, $file) = @_; | ||
| 102 | my $fh = gensym; | ||
| 103 | open $fh, ">$file" or return; | ||
| 104 | |||
| 105 | if (eval { $mail->can('stream_to') }) { | ||
| 106 | eval { $mail->stream_to($fh); 1 } or return; | ||
| 107 | } else { | ||
| 108 | print $fh $mail->as_string or return; | ||
| 109 | } | ||
| 110 | |||
| 111 | return close $fh; | ||
| 112 | } | ||
| 113 | |||
| 114 | 1; | ||
| 115 | |||
| 116 | __END__ |