| 1 |
use strict; |
| 2 |
use warnings; |
| 3 |
package Email::LocalDelivery::Mbox 1.201; |
| 4 |
# ABSTRACT: deliver mail to an mbox |
| 5 |
|
| 6 |
#pod =head1 INSTEAD... |
| 7 |
#pod |
| 8 |
#pod Instead, consider using L<Email::Sender::Transport::Maildir>. |
| 9 |
#pod |
| 10 |
#pod =cut |
| 11 |
|
| 12 |
use File::Path; |
| 13 |
use File::Basename; |
| 14 |
use Email::Simple 1.998; # needed for ->header_obj |
| 15 |
use Fcntl ':flock'; |
| 16 |
use Symbol qw(gensym); |
| 17 |
|
| 18 |
sub deliver { |
| 19 |
# The slightly convoluted method of unrolling the stack is intended to limit |
| 20 |
# the scope of which a large string at $_[1] might be in memory before being |
| 21 |
# constructed into an Email::Simple. -- rjbs, 2007-05-25 |
| 22 |
my $class = shift; |
| 23 |
|
| 24 |
my $email; |
| 25 |
if (eval { $_[0]->isa('Email::Simple') }) { |
| 26 |
$email = shift; |
| 27 |
} else { |
| 28 |
my $text = shift; |
| 29 |
$email = Email::Simple->new(\$text); # requires Email::Simple 1.998 or so |
| 30 |
} |
| 31 |
|
| 32 |
my @files = @_; |
| 33 |
|
| 34 |
my @rv; |
| 35 |
|
| 36 |
for my $file (@files) { |
| 37 |
my $fh = $class->_open_fh($file) or next; |
| 38 |
print $fh "\n" if tell($fh) > 0; |
| 39 |
print $fh $class->_from_line($email); |
| 40 |
print $fh $class->_escape_from_body($email); |
| 41 |
|
| 42 |
# This will make streaming a bit more annoying. -- rjbs, 2007-05-25 |
| 43 |
print $fh "\n" unless $email->as_string =~ /\n$/; |
| 44 |
|
| 45 |
$class->_close_fh($fh) || next; |
| 46 |
push @rv, $file; |
| 47 |
} |
| 48 |
return @rv; |
| 49 |
} |
| 50 |
|
| 51 |
sub _open_fh { |
| 52 |
my ($class, $file) = @_; |
| 53 |
my $dir = dirname($file); |
| 54 |
return if !-d $dir and not mkpath($dir); |
| 55 |
|
| 56 |
my $fh = gensym; |
| 57 |
open $fh, ">> $file" or return; |
| 58 |
$class->getlock($fh) || return; |
| 59 |
seek $fh, 0, 2; |
| 60 |
return $fh; |
| 61 |
} |
| 62 |
|
| 63 |
sub _close_fh { |
| 64 |
my ($class, $fh) = @_; |
| 65 |
$class->unlock($fh) || return; |
| 66 |
close $fh or return; |
| 67 |
return 1; |
| 68 |
} |
| 69 |
|
| 70 |
sub _escape_from_body { |
| 71 |
my ($class, $email) = @_; |
| 72 |
|
| 73 |
my $body = $email->body; |
| 74 |
$body =~ s/^(From )/>$1/gm; |
| 75 |
|
| 76 |
return $email->header_obj->as_string . $email->crlf . $body; |
| 77 |
} |
| 78 |
|
| 79 |
sub _from_line { |
| 80 |
my ($class, $email) = @_; |
| 81 |
|
| 82 |
# The qmail way. |
| 83 |
return $ENV{UFLINE} . $ENV{RPLINE} . $ENV{DTLINE} if exists $ENV{UFLINE}; |
| 84 |
|
| 85 |
# The boring way. |
| 86 |
return _from_line_boring($email); |
| 87 |
} |
| 88 |
|
| 89 |
sub _from_line_boring { |
| 90 |
my $mail = shift; |
| 91 |
my $from = $mail->header("Return-path") |
| 92 |
|| $mail->header("Sender") |
| 93 |
|| $mail->header("Reply-To") |
| 94 |
|| $mail->header("From") |
| 95 |
|| 'root@localhost'; |
| 96 |
$from = $1 if $from =~ /<(.*?)>/; # comment <email@address> -> email@address |
| 97 |
$from =~ s/\s*\(.*\)\s*//; # email@address (comment) -> email@address |
| 98 |
$from =~ s/\s+//g; # if any whitespace remains, get rid of it. |
| 99 |
|
| 100 |
my $fromtime = localtime; |
| 101 |
$fromtime =~ s/(:\d\d) \S+ (\d{4})$/$1 $2/; # strip timezone. |
| 102 |
return "From $from $fromtime\n"; |
| 103 |
} |
| 104 |
|
| 105 |
sub getlock { |
| 106 |
my ($class, $fh) = @_; |
| 107 |
for (1 .. 10) { |
| 108 |
return 1 if flock($fh, LOCK_EX | LOCK_NB); |
| 109 |
sleep $_; |
| 110 |
} |
| 111 |
return 0; |
| 112 |
} |
| 113 |
|
| 114 |
sub unlock { |
| 115 |
my ($class, $fh) = @_; |
| 116 |
flock($fh, LOCK_UN); |
| 117 |
} |
| 118 |
|
| 119 |
1; |
| 120 |
|
| 121 |
__END__ |
| 122 |
|
| 123 |
=pod |
| 124 |
|
| 125 |
=encoding UTF-8 |
| 126 |
|
| 127 |
=head1 NAME |
| 128 |
|
| 129 |
Email::LocalDelivery::Mbox - deliver mail to an mbox |
| 130 |
|
| 131 |
=head1 VERSION |
| 132 |
|
| 133 |
version 1.201 |
| 134 |
|
| 135 |
=head1 PERL VERSION |
| 136 |
|
| 137 |
This code is effectively abandonware. Although releases will sometimes be made |
| 138 |
to update contact info or to fix packaging flaws, bug reports will mostly be |
| 139 |
ignored. Feature requests are even more likely to be ignored. (If someone |
| 140 |
takes up maintenance of this code, they will presumably remove this notice.) |
| 141 |
This means that whatever version of perl is currently required is unlikely to |
| 142 |
change -- but also that it might change at any new maintainer's whim. |
| 143 |
|
| 144 |
=head1 INSTEAD... |
| 145 |
|
| 146 |
Instead, consider using L<Email::Sender::Transport::Maildir>. |
| 147 |
|
| 148 |
=head1 AUTHORS |
| 149 |
|
| 150 |
=over 4 |
| 151 |
|
| 152 |
=item * |
| 153 |
|
| 154 |
Simon Cozens |
| 155 |
|
| 156 |
=item * |
| 157 |
|
| 158 |
Casey West <[email protected]> |
| 159 |
|
| 160 |
=item * |
| 161 |
|
| 162 |
Ricardo Signes <[email protected]> |
| 163 |
|
| 164 |
=back |
| 165 |
|
| 166 |
=head1 COPYRIGHT AND LICENSE |
| 167 |
|
| 168 |
This software is copyright (c) 2003 by Simon Cozens. |
| 169 |
|
| 170 |
This is free software; you can redistribute it and/or modify it under |
| 171 |
the same terms as the Perl 5 programming language system itself. |
| 172 |
|
| 173 |
=cut |