1 # Copyright (C) 2020 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 # Writes PublicInbox::Eml objects atomically to a mbox variant or Maildir
5 package PublicInbox::LeiToMail;
10 use PublicInbox::ProcessPipe;
11 use PublicInbox::Spawn qw(which spawn);
12 use Symbol qw(gensym);
14 use IO::Handle; # ->autoflush
16 my %kw2char = ( # Maildir characters
24 flagged => [ 'X-Status' => 'F' ],
25 answered => [ 'X-Status' => 'A' ],
26 seen => [ 'Status' => 'R' ],
27 draft => [ 'X-Status' => 'T' ],
30 sub _mbox_hdr_buf ($$$) {
31 my ($eml, $type, $kw) = @_;
32 $eml->header_set($_) for (qw(Lines Bytes Content-Length));
33 my %hdr; # set Status, X-Status
35 if (my $ent = $kw2status{$k}) {
36 push @{$hdr{$ent->[0]}}, $ent->[1];
38 warn "TODO: keyword `$k' not supported for mbox\n";
41 while (my ($name, $chars) = each %hdr) {
42 $eml->header_set($name, join('', sort @$chars));
44 my $buf = delete $eml->{hdr};
46 # fixup old bug from import (pre-a0c07cba0e5d8b6a)
47 $$buf =~ s/\A[\r\n]*From [^\r\n]*\r?\n//s;
49 substr($$buf, 0, 0, # prepend From line
50 "From lei\@$type Thu Jan 1 00:00:00 1970$eml->{crlf}");
54 sub write_in_full ($$$) {
55 my ($fh, $buf, $atomic) = @_;
57 defined(my $w = syswrite($fh, $$buf)) or die "write: $!";
58 $w == length($$buf) or die "short write: $w != ".length($$buf);
60 print $fh $$buf or die "print: $!";
64 sub eml2mboxrd ($;$) {
66 my $buf = _mbox_hdr_buf($eml, 'mboxrd', $kw);
67 if (my $bdy = delete $eml->{bdy}) {
68 $$bdy =~ s/^(>*From )/>$1/gm;
69 $$buf .= $eml->{crlf};
70 substr($$bdy, 0, 0, $$buf); # prepend header
73 $$buf .= $eml->{crlf};
79 my $buf = _mbox_hdr_buf($eml, 'mboxo', $kw);
80 if (my $bdy = delete $eml->{bdy}) {
81 $$bdy =~ s/^From />From /gm;
82 $$buf .= $eml->{crlf};
83 substr($$bdy, 0, 0, $$buf); # prepend header
86 $$buf .= $eml->{crlf};
90 # mboxcl still escapes "From " lines
93 my $buf = _mbox_hdr_buf($eml, 'mboxcl', $kw);
94 my $crlf = $eml->{crlf};
95 if (my $bdy = delete $eml->{bdy}) {
96 $$bdy =~ s/^From />From /gm;
97 $$buf .= 'Content-Length: '.length($$bdy).$crlf.$crlf;
98 substr($$bdy, 0, 0, $$buf); # prepend header
105 # mboxcl2 has no "From " escaping
108 my $buf = _mbox_hdr_buf($eml, 'mboxcl2', $kw);
109 my $crlf = $eml->{crlf};
110 if (my $bdy = delete $eml->{bdy}) {
111 $$buf .= 'Content-Length: '.length($$bdy).$crlf.$crlf;
112 substr($$bdy, 0, 0, $$buf); # prepend header
121 for (qw(new tmp cur)) {
122 my $d = "$maildir/$_";
125 if (!File::Path::mkpath($d) && !-d $d) {
126 die "failed to mkpath($d): $!\n";
131 sub git_to_mail { # git->cat_async callback
132 my ($bref, $oid, $type, $size, $arg) = @_;
133 if ($type ne 'blob') {
134 if ($type eq 'missing') {
135 warn "missing $oid\n";
137 warn "unexpected type=$type for $oid\n";
141 my ($write_cb, $kw) = @$arg;
142 $write_cb->($bref, $oid, $kw);
146 sub reap_compress { # dwaitpid callback
147 my ($lei, $pid) = @_;
148 my $cmd = delete $lei->{"pid.$pid"};
150 $lei->fail("@$cmd failed", $? >> 8);
154 my ($out, $sfx, $lei) = @_;
157 $cmd->[0] = which($lei->{env}->{GZIP} // 'pigz') //
159 die "pigz or gzip missing for $sfx\n";
160 # TODO: use IO::Compress::Gzip
161 push @$cmd, '-c'; # stdout
162 push @$cmd, '--rsyncable' if $lei->{opt}->{rsyncable};
166 pipe(my ($r, $w)) or die "pipe: $!";
167 my $rdr = { 0 => $r, 1 => $out, 2 => $lei->{2} };
168 my $pid = spawn($cmd, $lei->{env}, $rdr);
169 $lei->{"pid.$pid"} = $cmd;
171 tie *$pp, 'PublicInbox::ProcessPipe', $pid, $w, \&reap_compress, $lei;
172 my $tmp = File::Temp->new("$sfx.lock-XXXXXX", TMPDIR => 1);
173 my $pipe_lk = ($lei->{opt}->{jobs} // 0) > 1 ? bless({
174 lock_path => $tmp->filename,
176 }, 'PublicInbox::Lock') : undef;
181 my ($cls, $dst, $lei) = @_;
182 if ($dst =~ s!\A(mbox(?:rd|cl|cl2|o))?:!!) {
184 my $eml2mbox = $cls->can($m) or die "$cls->$m missing";
186 open $out, '>>', $dst or die "open $dst: $!";
187 my $atomic = !!(($lei->{opt}->{jobs} // 0) > 1);
188 if ($dst =~ /\.(gz|bz2|xz)\z/) {
189 ($out, $pipe_lk) = compress_dst($out, $1, $lei);
192 my ($buf, $oid, $kw) = @_;
193 $buf = $eml2mbox->(PublicInbox::Eml->new($buf), $kw);
194 my $lock = $pipe_lk->lock_for_scope if $pipe_lk;
195 write_in_full($out, $buf, $atomic);