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 popen_rd);
12 use PublicInbox::LeiDedupe;
13 use Symbol qw(gensym);
14 use IO::Handle; # ->autoflush
15 use Fcntl qw(SEEK_SET);
17 my %kw2char = ( # Maildir characters
25 flagged => [ 'X-Status' => 'F' ],
26 answered => [ 'X-Status' => 'A' ],
27 seen => [ 'Status' => 'R' ],
28 draft => [ 'X-Status' => 'T' ],
31 sub _mbox_hdr_buf ($$$) {
32 my ($eml, $type, $kw) = @_;
33 $eml->header_set($_) for (qw(Lines Bytes Content-Length));
34 my %hdr; # set Status, X-Status
36 if (my $ent = $kw2status{$k}) {
37 push @{$hdr{$ent->[0]}}, $ent->[1];
39 warn "TODO: keyword `$k' not supported for mbox\n";
42 while (my ($name, $chars) = each %hdr) {
43 $eml->header_set($name, join('', sort @$chars));
45 my $buf = delete $eml->{hdr};
47 # fixup old bug from import (pre-a0c07cba0e5d8b6a)
48 $$buf =~ s/\A[\r\n]*From [^\r\n]*\r?\n//s;
50 substr($$buf, 0, 0, # prepend From line
51 "From lei\@$type Thu Jan 1 00:00:00 1970$eml->{crlf}");
55 sub write_in_full ($$$) {
56 my ($fh, $buf, $atomic) = @_;
58 defined(my $w = syswrite($fh, $$buf)) or die "write: $!";
59 $w == length($$buf) or die "short write: $w != ".length($$buf);
61 print $fh $$buf or die "print: $!";
65 sub eml2mboxrd ($;$) {
67 my $buf = _mbox_hdr_buf($eml, 'mboxrd', $kw);
68 if (my $bdy = delete $eml->{bdy}) {
69 $$bdy =~ s/^(>*From )/>$1/gm;
70 $$buf .= $eml->{crlf};
71 substr($$bdy, 0, 0, $$buf); # prepend header
74 $$buf .= $eml->{crlf};
80 my $buf = _mbox_hdr_buf($eml, 'mboxo', $kw);
81 if (my $bdy = delete $eml->{bdy}) {
82 $$bdy =~ s/^From />From /gm;
83 $$buf .= $eml->{crlf};
84 substr($$bdy, 0, 0, $$buf); # prepend header
87 $$buf .= $eml->{crlf};
91 # mboxcl still escapes "From " lines
94 my $buf = _mbox_hdr_buf($eml, 'mboxcl', $kw);
95 my $crlf = $eml->{crlf};
96 if (my $bdy = delete $eml->{bdy}) {
97 $$bdy =~ s/^From />From /gm;
98 $$buf .= 'Content-Length: '.length($$bdy).$crlf.$crlf;
99 substr($$bdy, 0, 0, $$buf); # prepend header
106 # mboxcl2 has no "From " escaping
109 my $buf = _mbox_hdr_buf($eml, 'mboxcl2', $kw);
110 my $crlf = $eml->{crlf};
111 if (my $bdy = delete $eml->{bdy}) {
112 $$buf .= 'Content-Length: '.length($$bdy).$crlf.$crlf;
113 substr($$bdy, 0, 0, $$buf); # prepend header
122 for (qw(new tmp cur)) {
123 my $d = "$maildir/$_";
126 if (!File::Path::mkpath($d) && !-d $d) {
127 die "failed to mkpath($d): $!\n";
132 sub git_to_mail { # git->cat_async callback
133 my ($bref, $oid, $type, $size, $arg) = @_;
134 if ($type ne 'blob') {
135 if ($type eq 'missing') {
136 warn "missing $oid\n";
138 warn "unexpected type=$type for $oid\n";
142 my ($write_cb, $kw) = @$arg;
143 $write_cb->($bref, $oid, $kw);
147 sub reap_compress { # dwaitpid callback
148 my ($lei, $pid) = @_;
149 my $cmd = delete $lei->{"pid.$pid"};
151 $lei->fail("@$cmd failed", $? >> 8);
154 # all of these support -c for stdout and -d for decompression,
155 # mutt is commonly distributed with hooks for gz, bz2 and xz, at least
156 # { foo => '' } means "--foo" is passed to the command-line,
157 # otherwise { foo => '--bar' } passes "--bar"
159 gz => [ qw(GZIP pigz gzip), {
160 rsyncable => '', threads => '-p' } ],
161 bz2 => [ 'bzip2', {} ],
162 xz => [ 'xz', { threads => '-T' } ],
163 # XXX does anybody care for these? I prefer zstd on entire FSes,
164 # so it's probably not necessary on a per-file basis
165 # zst => [ 'zstd', { -default => [ qw(-q) ], # it's noisy by default
166 # rsyncable => '', threads => '-T' } ],
167 # zz => [ 'pigz', { -default => [ '--zlib' ],
168 # rsyncable => '', threads => '-p' }],
169 # lzo => [ 'lzop', {} ],
170 # lzma => [ 'lzma', {} ],
174 my ($zsfx, $decompress, $lei) = @_;
175 my $x = $zsfx2cmd{$zsfx} // die "no support for suffix=.$zsfx";
177 my $cmd_opt = pop @info;
178 my @cmd = (undef, $decompress ? qw(-dc) : qw(-c));
179 for my $exe (@info) {
180 # I think respecting client's ENV{GZIP} is OK, not sure
181 # about ENV overrides for other, less-common compressors
182 if ($exe eq uc($exe)) {
183 $exe = $lei->{env}->{$exe} or next;
185 $cmd[0] = which($exe) and last;
187 $cmd[0] // die join(' or ', @info)." missing for .$zsfx";
188 # push @cmd, @{$cmd_opt->{-default}} if $cmd_opt->{-default};
189 for my $bool (qw(rsyncable)) {
190 my $switch = $cmd_opt->{rsyncable} // next;
191 push @cmd, '--'.($switch || $bool);
193 for my $key (qw(threads)) { # support compression level?
194 my $switch = $cmd_opt->{$key} // next;
195 my $val = $lei->{opt}->{$key} // next;
196 push @cmd, $switch, $val;
202 my ($out, $zsfx, $lei) = @_;
203 my $cmd = zsfx2cmd($zsfx, undef, $lei);
204 pipe(my ($r, $w)) or die "pipe: $!";
205 my $rdr = { 0 => $r, 1 => $out, 2 => $lei->{2} };
206 my $pid = spawn($cmd, $lei->{env}, $rdr);
207 $lei->{"pid.$pid"} = $cmd;
209 tie *$pp, 'PublicInbox::ProcessPipe', $pid, $w, \&reap_compress, $lei;
210 my $pipe_lk = ($lei->{opt}->{jobs} // 0) > 1 ?
211 PublicInbox::Lock->new_tmp($zsfx) : undef;
215 sub decompress_src ($$$) {
216 my ($in, $zsfx, $lei) = @_;
217 my $cmd = zsfx2cmd($zsfx, 1, $lei);
218 my $rdr = { 0 => $in, 2 => $lei->{2} };
219 popen_rd($cmd, $lei->{env}, $rdr);
224 open my $dup, '+>>&', $in or die "dup: $!";
228 # --augment existing output destination, with deduplication
229 sub _augment { # MboxReader eml_cb
230 my ($eml, $lei) = @_;
231 # ignore return value, just populate the skv
232 $lei->{dedupe_cb}->is_dup($eml);
235 sub _mbox_write_cb ($$$$) {
236 my ($cls, $mbox, $dst, $lei) = @_;
238 my $eml2mbox = $cls->can($m) or die "$cls->$m missing";
240 open $out, '+>>', $dst or die "open $dst: $!";
241 # Perl does SEEK_END even with O_APPEND :<
242 seek($out, 0, SEEK_SET) or die "seek $dst: $!";
243 my $jobs = $lei->{opt}->{jobs} // 0;
244 my $atomic = $jobs > 1;
245 my $dedupe = $lei->{dedupe} = PublicInbox::LeiDedupe->new($lei);
246 state $zsfx_allow = join('|', keys %zsfx2cmd);
247 my ($zsfx) = ($dst =~ /\.($zsfx_allow)\z/);
248 if ($lei->{opt}->{augment}) {
249 if (-s $out && $dedupe->prepare_dedupe) {
250 my $rd = $zsfx ? decompress_src($out, $zsfx, $lei) :
252 PublicInbox::MboxReader->$mbox($rd, \&_augment, $lei);
254 $dedupe->pause_dedupe if $jobs; # are we forking?
256 truncate($out, 0) or die "truncate $dst: $!";
257 $dedupe->prepare_dedupe if !$jobs;
259 ($out, $pipe_lk) = compress_dst($out, $zsfx, $lei) if $zsfx;
261 my ($buf, $oid, $kw) = @_;
262 my $eml = PublicInbox::Eml->new($buf);
263 if (!$lei->{dedupe}->is_dup($eml, $oid)) {
264 $buf = $eml2mbox->($eml, $kw);
265 my $lock = $pipe_lk->lock_for_scope if $pipe_lk;
266 write_in_full($out, $buf, $atomic);
271 sub write_cb { # returns a callback for git_to_mail
272 my ($cls, $dst, $lei) = @_;
273 if ($dst =~ s!\A(mbox(?:rd|cl|cl2|o))?:!!) {
274 _mbox_write_cb($cls, $1, $dst, $lei);