]> Sergey Matveev's repositories - public-inbox.git/blob - t/lei-q-kw.t
www: drop --subject from "git send-email" instructions
[public-inbox.git] / t / lei-q-kw.t
1 #!perl -w
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;
5 use POSIX qw(mkfifo);
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);
12 my $exp = {
13         '<qp@example.com>' => eml_load('t/plack-qp.eml'),
14         '<testmessage@example.com>' => eml_load('t/utf8.eml'),
15 };
16 $exp->{'<qp@example.com>'}->header_set('Status', 'RO');
17
18 test_lei(sub {
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 $!";
25
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');
28
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/*/*")]);
33
34 # ensure --no-import-before works
35 my $n = $fn[0];
36 $n =~ s/,S\z/,RS/;
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");
44
45 SKIP: {
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');
54         close $cat;
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 {
60                 my ($eml) = @_;
61                 $eml->header_set('Status', 'RO');
62                 is_deeply($eml, $exp->{'<qp@example.com>'},
63                         'FIFO output works as expected');
64         });
65 };
66
67 lei_ok qw(import -F eml t/utf8.eml), \'for augment test';
68 my $read_file = sub {
69         if ($_[0] =~ /\.gz\z/) {
70                 gunzip($_[0] => \(my $buf = ''), MultiStream => 1) or
71                         BAIL_OUT 'gunzip';
72                 $buf;
73         } else {
74                 open my $fh, '+<', $_[0] or BAIL_OUT $!;
75                 do { local $/; <$fh> };
76         }
77 };
78
79 my $write_file = sub {
80         if ($_[0] =~ /\.gz\z/) {
81                 gzip(\($_[1]), $_[0]) or BAIL_OUT 'gzip';
82         } else {
83                 open my $fh, '>', $_[0] or BAIL_OUT $!;
84                 print $fh $_[1] or BAIL_OUT $!;
85                 close $fh or BAIL_OUT;
86         }
87 };
88
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 $!";
106         my %res;
107         PublicInbox::MboxReader->mboxrd($fh, sub {
108                 my ($eml) = @_;
109                 my $mid = $eml->header_raw('Message-ID');
110                 if ($mid eq '<testmessage@example.com>') {
111                         is_deeply([$eml->header('Status')], [],
112                                 "no status $sfx");
113                         $eml->header_set('Status');
114                 } elsif ($mid eq '<qp@example.com>') {
115                         is($eml->header('Status'), 'RO', 'status preserved');
116                 } else {
117                         fail("unknown mid $mid");
118                 }
119                 $res{$mid} = $eml;
120         });
121         is_deeply(\%res, $exp, '--augment worked') or diag $lei_err;
122
123         lei_ok(qw(q -o), "mboxrd:/dev/stdout", qw(m:qp@example.com)) or
124                 diag $lei_err;
125         like($lei_out, qr/^Status: RO\n/sm, 'Status set by previous augment');
126 } # /mbox + mbox.gz tests
127
128 my ($ro_home, $cfg_path) = setup_public_inboxes;
129
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);
135
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 $!";
140
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);
151
152 $o = "$ENV{HOME}/kwmboxrd";
153 lei_ok(qw(q -o), "mboxrd:$o", "m:$m", @inc);
154
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;
163
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');
171
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');
175
176 # ensure import on previously external-only message works
177 lei_ok('q', "m:$m");
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');
182 lei_ok('q', "m:$m");
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');
186
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 $!;
203 my @res;
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');
208
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');
214 my @another;
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');
218
219 # forwarded
220 {
221         local $ENV{DBG} = 1;
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);
226         my $passed = $p[0];
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 $!;
237         my @res;
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');
242
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');
248 }
249
250 }); # test_lei
251 done_testing;