2 # Copyright (C) 2020-2021 all contributors <meta@public-inbox.org>
3 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 use strict; use v5.10.1; use PublicInbox::TestCommon;
6 use Fcntl qw(SEEK_SET O_RDONLY O_NONBLOCK);
7 use IO::Uncompress::Gunzip qw(gunzip);
8 use IO::Compress::Gzip qw(gzip);
9 use PublicInbox::MboxReader;
10 use PublicInbox::LeiToMail;
11 use PublicInbox::Spawn qw(popen_rd);
13 '<qp@example.com>' => eml_load('t/plack-qp.eml'),
14 '<testmessage@example.com>' => eml_load('t/utf8.eml'),
16 $exp->{'<qp@example.com>'}->header_set('Status', 'RO');
19 lei_ok(qw(import -F eml t/plack-qp.eml));
20 my $o = "$ENV{HOME}/dst";
21 lei_ok(qw(q -o), "maildir:$o", qw(m:qp@example.com));
22 my @fn = glob("$o/cur/*:2,");
23 scalar(@fn) == 1 or xbail $lei_err, 'wrote multiple or zero files:', \@fn;
24 rename($fn[0], "$fn[0]S") or BAIL_OUT "rename $!";
26 lei_ok(qw(q -o), "maildir:$o", qw(m:bogus-noresults@example.com));
27 ok(!glob("$o/cur/*"), 'last result cleared after augment-import');
29 lei_ok(qw(q -o), "maildir:$o", qw(m:qp@example.com));
30 @fn = glob("$o/cur/*:2,S");
31 is(scalar(@fn), 1, "`seen' flag set on Maildir file") or
32 diag "$o contents: ", explain([glob("$o/*/*")]);
34 # ensure --no-import-before works
37 rename($fn[0], $n) or BAIL_OUT "rename $!";
38 lei_ok(qw(q --no-import-before -o), "maildir:$o",
39 qw(m:bogus-noresults@example.com));
40 ok(!glob("$o/cur/*"), '--no-import-before cleared destination');
41 lei_ok(qw(q -o), "maildir:$o", qw(m:qp@example.com));
42 @fn = glob("$o/cur/*:2,S");
43 is(scalar(@fn), 1, "`seen' flag (but not `replied') set on Maildir file");
46 $o = "$ENV{HOME}/fifo";
47 mkfifo($o, 0600) or skip("mkfifo not supported: $!", 1);
48 # cat(1) since lei() may not execve for FD_CLOEXEC to work
49 my $cat = popen_rd(['cat', $o]);
50 ok(!lei(qw(q --import-before bogus -o), "mboxrd:$o"),
51 '--import-before fails on non-seekable output');
52 like($lei_err, qr/not seekable/, 'unseekable noted in error');
53 is(do { local $/; <$cat> }, '', 'no output on FIFO');
55 $cat = popen_rd(['cat', $o]);
56 lei_ok(qw(q m:qp@example.com -o), "mboxrd:$o");
57 my $buf = do { local $/; <$cat> };
58 open my $fh, '<', \$buf or BAIL_OUT $!;
59 PublicInbox::MboxReader->mboxrd($fh, sub {
61 $eml->header_set('Status', 'RO');
62 is_deeply($eml, $exp->{'<qp@example.com>'},
63 'FIFO output works as expected');
67 lei_ok qw(import -F eml t/utf8.eml), \'for augment test';
69 if ($_[0] =~ /\.gz\z/) {
70 gunzip($_[0] => \(my $buf = ''), MultiStream => 1) or
74 open my $fh, '+<', $_[0] or BAIL_OUT $!;
75 do { local $/; <$fh> };
79 my $write_file = sub {
80 if ($_[0] =~ /\.gz\z/) {
81 gzip(\($_[1]), $_[0]) or BAIL_OUT 'gzip';
83 open my $fh, '>', $_[0] or BAIL_OUT $!;
84 print $fh $_[1] or BAIL_OUT $!;
85 close $fh or BAIL_OUT;
89 for my $sfx ('', '.gz') {
90 $o = "$ENV{HOME}/dst.mboxrd$sfx";
91 lei_ok(qw(q -o), "mboxrd:$o", qw(m:qp@example.com));
92 my $buf = $read_file->($o);
93 $buf =~ s/^Status: [^\n]*\n//sm or BAIL_OUT "no status in $buf";
94 $write_file->($o, $buf);
95 lei_ok(qw(q -o), "mboxrd:$o", qw(rereadandimportkwchange));
96 $buf = $read_file->($o);
97 is($buf, '', 'emptied');
98 lei_ok(qw(q -o), "mboxrd:$o", qw(m:qp@example.com));
99 $buf = $read_file->($o);
100 $buf =~ s/\nStatus: O\n\n/\nStatus: RO\n\n/s or
101 BAIL_OUT "no Status in $buf";
102 $write_file->($o, $buf);
103 lei_ok(qw(q -a -o), "mboxrd:$o", qw(m:testmessage@example.com));
104 $buf = $read_file->($o);
105 open my $fh, '<', \$buf or BAIL_OUT "PerlIO::scalar $!";
107 PublicInbox::MboxReader->mboxrd($fh, sub {
109 my $mid = $eml->header_raw('Message-ID');
110 if ($mid eq '<testmessage@example.com>') {
111 is_deeply([$eml->header('Status')], [],
113 $eml->header_set('Status');
114 } elsif ($mid eq '<qp@example.com>') {
115 is($eml->header('Status'), 'RO', 'status preserved');
117 fail("unknown mid $mid");
121 is_deeply(\%res, $exp, '--augment worked') or diag $lei_err;
123 lei_ok(qw(q -o), "mboxrd:/dev/stdout", qw(m:qp@example.com)) or
125 like($lei_out, qr/^Status: RO\n/sm, 'Status set by previous augment');
126 } # /mbox + mbox.gz tests
128 my ($ro_home, $cfg_path) = setup_public_inboxes;
130 # import keywords-only for external messages:
131 $o = "$ENV{HOME}/kwdir";
132 my $m = 'alpine.DEB.2.20.1608131214070.4924@example';
133 my @inc = ('-I', "$ro_home/t1");
134 lei_ok(qw(q -o), $o, "m:$m", @inc);
136 # emulate MUA marking a Maildir message as read:
137 @fn = glob("$o/cur/*");
138 scalar(@fn) == 1 or xbail $lei_err, 'wrote multiple or zero files:', \@fn;
139 rename($fn[0], "$fn[0]S") or BAIL_OUT "rename $!";
141 lei_ok(qw(q -o), $o, 'bogus', \'clobber output dir to import keywords');
142 @fn = glob("$o/cur/*");
143 is_deeply(\@fn, [], 'output dir actually clobbered');
144 lei_ok('q', "m:$m", @inc);
145 my $res = json_utf8->decode($lei_out);
146 is_deeply($res->[0]->{kw}, ['seen'], 'seen flag set for external message')
147 or diag explain($res);
148 lei_ok('q', "m:$m", '--no-external');
149 is_deeply($res = json_utf8->decode($lei_out), [ undef ],
150 'external message not imported') or diag explain($res);
152 $o = "$ENV{HOME}/kwmboxrd";
153 lei_ok(qw(q -o), "mboxrd:$o", "m:$m", @inc);
155 # emulate MUA marking mboxrd message as unread
156 open my $fh, '<', $o or BAIL_OUT;
157 my $s = do { local $/; <$fh> };
158 $s =~ s/^Status: RO\n/Status: O\nX-Status: AF\n/sm or
159 fail "failed to clear R flag in $s";
160 open $fh, '>', $o or BAIL_OUT;
161 print $fh $s or BAIL_OUT;
162 close $fh or BAIL_OUT;
164 lei_ok(qw(q -o), "mboxrd:$o", 'm:bogus', @inc,
165 \'clobber mbox to import keywords');
166 lei_ok(qw(q -o), "mboxrd:$o", "m:$m", @inc);
167 open $fh, '<', $o or BAIL_OUT;
168 $s = do { local $/; <$fh> };
169 like($s, qr/^Status: O\nX-Status: AF\n/ms,
170 'seen keyword gone in mbox, answered + flagged set');
172 lei_ok(qw(q --pretty), "m:$m", @inc);
173 like($lei_out, qr/^ "kw": \["answered", "flagged"\],\n/sm,
174 '--pretty JSON output shows kw: on one line');
176 # ensure import on previously external-only message works
178 is_deeply(json_utf8->decode($lei_out), [ undef ],
179 'to-be-imported message non-existent');
180 lei_ok(qw(import -F eml t/x-unknown-alpine.eml));
181 is($lei_err, '', 'no errors importing previous external-only message');
183 $res = json_utf8->decode($lei_out);
184 is($res->[1], undef, 'got one result');
185 is_deeply($res->[0]->{kw}, [ qw(answered flagged) ], 'kw preserved on exact');
187 # ensure fuzzy match import works, too
188 $m = 'multipart@example.com';
189 $o = "$ENV{HOME}/fuzz";
190 lei_ok('q', '-o', $o, "m:$m", @inc);
191 @fn = glob("$o/cur/*");
192 scalar(@fn) == 1 or xbail $lei_err, "wrote multiple or zero files", \@fn;
193 rename($fn[0], "$fn[0]S") or BAIL_OUT "rename $!";
194 lei_ok('q', '-o', $o, "m:$m");
195 is_deeply([glob("$o/cur/*")], [], 'clobbered output results');
196 my $eml = eml_load('t/plack-2-txt-bodies.eml');
197 $eml->header_set('List-Id', '<list.example.com>');
198 my $in = $eml->as_string;
199 lei_ok([qw(import -F eml --stdin)], undef, { 0 => \$in, %$lei_opt });
200 is($lei_err, '', 'no errors from import');
201 lei_ok(qw(q -f mboxrd), "m:$m");
202 open $fh, '<', \$lei_out or BAIL_OUT $!;
204 PublicInbox::MboxReader->mboxrd($fh, sub { push @res, shift });
205 is($res[0]->header('Status'), 'RO', 'seen kw set');
206 $res[0]->header_set('Status');
207 is_deeply(\@res, [ $eml ], 'imported message matches w/ List-Id');
209 $eml->header_set('List-Id', '<another.example.com>');
210 $in = $eml->as_string;
211 lei_ok([qw(import -F eml --stdin)], undef, { 0 => \$in, %$lei_opt });
212 is($lei_err, '', 'no errors from 2nd import');
213 lei_ok(qw(q -f mboxrd), "m:$m", 'l:another.example.com');
215 open $fh, '<', \$lei_out or BAIL_OUT $!;
216 PublicInbox::MboxReader->mboxrd($fh, sub { push @another, shift });
217 is($another[0]->header('Status'), 'RO', 'seen kw set');
222 $o = "$ENV{HOME}/forwarded";
223 lei_ok(qw(q -o), $o, "m:$m");
224 my @p = glob("$o/cur/*");
225 scalar(@p) == 1 or xbail('multiple when 1 expected', \@p);
227 $passed =~ s/,S\z/,PS/ or xbail "failed to replace $passed";
228 rename($p[0], $passed) or xbail "rename $!";
229 lei_ok(qw(q -o), $o, 'm:bogus', \'clobber maildir');
230 is_deeply([glob("$o/cur/*")], [], 'old results clobbered');
231 lei_ok(qw(q -o), $o, "m:$m");
232 @p = glob("$o/cur/*");
233 scalar(@p) == 1 or xbail('multiple when 1 expected', \@p);
234 like($p[0], qr/,PS/, 'passed (Forwarded) flag kept');
235 lei_ok(qw(q -o), "mboxrd:$o.mboxrd", "m:$m");
236 open $fh, '<', "$o.mboxrd" or xbail $!;
238 PublicInbox::MboxReader->mboxrd($fh, sub { push @res, shift });
239 scalar(@res) == 1 or xbail('multiple when 1 expected', \@res);
240 is($res[0]->header('Status'), 'RO', 'seen kw set');
241 is($res[0]->header('X-Status'), undef, 'no X-Status');
243 lei_ok(qw(q -o), "mboxrd:$o.mboxrd", 'bogus-for-import-before');
244 lei_ok(qw(q -o), $o, "m:$m");
245 @p = glob("$o/cur/*");
246 scalar(@p) == 1 or xbail('multiple when 1 expected', \@p);
247 like($p[0], qr/,PS/, 'passed (Forwarded) flag still kept');