2 # Copyright (C) 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 Sys::Hostname qw(hostname);
6 use POSIX qw(strftime);
7 use PublicInbox::OnDestroy;
8 use PublicInbox::URIimap;
9 use PublicInbox::Config;
11 use PublicInbox::InboxIdle;
12 use Fcntl qw(O_EXCL O_WRONLY O_CREAT);
13 my $imap_url = $ENV{TEST_IMAP_WRITE_URL} or
14 plan skip_all => 'TEST_IMAP_WRITE_URL unset';
15 my $uri = PublicInbox::URIimap->new($imap_url);
16 defined($uri->path) and
17 plan skip_all => "$imap_url should not be a mailbox (just host:port)";
18 require_mods('Mail::IMAPClient');
19 require_ok 'PublicInbox::NetWriter';
20 my $host = (split(/\./, hostname))[0];
21 my ($base) = ($0 =~ m!\b([^/]+)\.[^\.]+\z!);
22 my $folder = "INBOX.$base-$host-".strftime('%Y%m%d%H%M%S', gmtime(time)).
23 "-$$-".sprintf('%x', int(rand(0xffffffff)));
24 my $nwr = PublicInbox::NetWriter->new;
25 chop($imap_url) if substr($imap_url, -1) eq '/';
26 my $folder_url = "$imap_url/$folder";
27 my $folder_uri = PublicInbox::URIimap->new($folder_url);
28 is($folder_uri->mailbox, $folder, 'folder correct') or
29 BAIL_OUT "BUG: bad $$uri";
30 $nwr->add_url($$folder_uri);
31 is($nwr->errors, undef, 'no errors');
32 $nwr->{pi_cfg} = bless {}, 'PublicInbox::Config';
34 my $set_cred_helper = sub {
35 my ($f, $cred_set) = @_;
36 sysopen(my $fh, $f, O_CREAT|O_EXCL|O_WRONLY) or BAIL_OUT "open $f: $!";
37 print $fh <<EOF or BAIL_OUT "print $f: $!";
41 close $fh or BAIL_OUT "close $f: $!";
44 # allow testers with git-credential-store configured to reuse
45 # stored credentials inside test_lei(sub {...}) when $ENV{HOME}
46 # is overridden and localized.
47 my ($cred_set, @cred_link, $tmpdir, $for_destroy);
48 chomp(my $cred_helper = `git config credential.helper 2>/dev/null`);
49 if ($cred_helper eq 'store') {
50 my $config = $ENV{XDG_CONFIG_HOME} // "$ENV{HOME}/.config";
51 for my $f ("$ENV{HOME}/.git-credentials", "$config/git/credentials") {
53 @cred_link = ($f, '/.git-credentials');
56 $cred_set = qq("$cred_helper");
57 } elsif ($cred_helper =~ /\Acache(?:[ \t]|\z)/) {
58 my $cache = $ENV{XDG_CACHE_HOME} // "$ENV{HOME}/.cache";
59 for my $d ("$ENV{HOME}/.git-credential-cache",
60 "$cache/git/credential") {
62 @cred_link = ($d, '/.git-credential-cache');
63 $cred_set = qq("$cred_helper");
66 } elsif (!$cred_helper) { # make the test less painful if no creds configured
67 ($tmpdir, $for_destroy) = tmpdir;
68 my $d = "$tmpdir/.git-credential-cache";
69 mkdir($d, 0700) or BAIL_OUT $!;
70 $cred_set = "cache --timeout=60";
71 @cred_link = ($d, '/.git-credential-cache');
73 diag "credential.helper=$cred_helper will not be used for this test";
77 local $ENV{HOME} = $tmpdir // $ENV{HOME};
78 if ($tmpdir && $cred_set) {
79 $set_cred_helper->("$ENV{HOME}/.gitconfig", $cred_set)
81 $nwr->imap_common_init;
83 my $mic = (values %$mics)[0];
84 my $cleanup = PublicInbox::OnDestroy->new($$, sub {
85 my $mic = $nwr->mic_get($uri);
86 $mic->delete($folder) or fail "delete $folder <$folder_uri>: $@";
87 if ($tmpdir && -f "$tmpdir/.gitconfig") {
88 local $ENV{HOME} = $tmpdir;
89 system(qw(git credential-cache exit));
92 my $imap_append = $nwr->can('imap_append');
93 my $smsg = bless { kw => [ 'seen' ] }, 'PublicInbox::Smsg';
94 $imap_append->($mic, $folder, undef, $smsg, eml_load('t/plack-qp.eml'));
96 my $imap_slurp_all = sub {
97 my ($url, $uid, $kw, $eml, $res) = @_;
98 push @$res, [ $kw, $eml ];
100 $nwr->imap_each($folder_uri, $imap_slurp_all, my $res = []);
101 is(scalar(@$res), 1, 'got appended message');
102 my $plack_qp_eml = eml_load('t/plack-qp.eml');
103 is_deeply($res, [ [ [ 'seen' ], $plack_qp_eml ] ],
104 'uploaded message read back');
105 $res = $mic = $mics = undef;
108 my ($ro_home, $cfg_path) = setup_public_inboxes;
109 my $cfg = PublicInbox::Config->new($cfg_path);
110 $cfg->each_inbox(sub {
112 lei_ok qw(add-external -q), $ibx->{inboxdir} or BAIL_OUT;
115 # cred_link[0] may be on a different (hopefully encrypted) FS,
116 # we only symlink to it here, so we don't copy any sensitive data
117 # into the temporary directory
118 if (@cred_link && !symlink($cred_link[0], $ENV{HOME}.$cred_link[1])) {
119 diag "symlink @cred_link: $! (non-fatal)";
122 $set_cred_helper->("$ENV{HOME}/.gitconfig", $cred_set) if $cred_set;
124 lei_ok qw(q f:qp@example.com -o), $folder_url;
125 $nwr->imap_each($folder_uri, $imap_slurp_all, my $res = []);
126 is(scalar(@$res), 1, 'got one deduped result') or diag explain($res);
127 is_deeply($res->[0]->[1], $plack_qp_eml,
128 'lei q wrote expected result');
130 lei_ok qw(q f:matz -a -o), $folder_url;
131 $nwr->imap_each($folder_uri, $imap_slurp_all, my $aug = []);
132 is(scalar(@$aug), 2, '2 results after augment') or diag explain($aug);
133 my $exp = $res->[0]->[1]->as_string;
134 is(scalar(grep { $_->[1]->as_string eq $exp } @$aug), 1,
135 'original remains after augment');
136 $exp = eml_load('t/iso-2202-jp.eml')->as_string;
137 is(scalar(grep { $_->[1]->as_string eq $exp } @$aug), 1,
138 'new result shown after augment');
140 lei_ok qw(q s:thisbetternotgiveanyresult -o), $folder_url;
141 $nwr->imap_each($folder_uri, $imap_slurp_all, my $empty = []);
142 is(scalar(@$empty), 0, 'no results w/o augment');
144 my $f = 't/utf8.eml'; # <testmessage@example.com>
146 lei_ok qw(convert -F eml -o), $folder_url, $f;
148 $nwr->imap_each($folder_uri, sub {
149 my ($u, $uid, $kw, $eml) = @_;
151 push @res, [ $kw, $eml ];
153 is_deeply(\@res, [ [ [], $exp ] ], 'converted to IMAP destination');
154 is(scalar(@uid), 1, 'got one UID back');
155 lei_ok qw(q -o /dev/stdout m:testmessage@example.com --no-external);
156 is_deeply(json_utf8->decode($lei_out), [undef],
157 'no results before import');
159 lei_ok qw(import -F eml), $f, \'import local copy w/o keywords';
161 lei_ok 'import', $folder_url; # populate mail_sync.sqlite3
162 lei_ok qw(tag +kw:seen +kw:answered +kw:flagged), $f;
163 lei_ok 'ls-mail-sync';
164 my @ls = split(/\n/, $lei_out);
165 is(scalar(@ls), 1, 'only one folder in ls-mail-sync') or xbail(\@ls);
167 like($l, qr/;UIDVALIDITY=\d+\z/, 'UIDVALIDITY');
169 lei_ok 'export-kw', $folder_url;
170 $mic = $nwr->mic_for_folder($folder_uri);
171 my $flags = $mic->flags($uid[0]);
172 is_deeply([sort @$flags], [ qw(\\Answered \\Flagged \\Seen) ],
173 'IMAP flags set by export-kw') or diag explain($flags);
175 # ensure this imap_set_kw clobbers
176 $nwr->imap_set_kw($mic, $uid[0], [ 'seen' ])->expunge or
177 BAIL_OUT "expunge $@";
180 $nwr->imap_each($folder_uri, $imap_slurp_all, \@res);
181 is_deeply(\@res, [ [ ['seen'], $exp ] ], 'seen flag set') or
184 lei_ok qw(q s:thisbetternotgiveanyresult -o), $folder_url,
185 \'clobber folder but import flag';
186 $nwr->imap_each($folder_uri, $imap_slurp_all, $empty = []);
187 is_deeply($empty, [], 'clobbered folder');
188 lei_ok qw(q -o /dev/stdout m:testmessage@example.com --no-external);
189 $res = json_utf8->decode($lei_out)->[0];
190 is_deeply([@$res{qw(m kw)}], ['testmessage@example.com', ['seen']],
193 # prepare messages for watch
194 $mic = $nwr->mic_for_folder($folder_uri);
195 for my $kw (qw(Deleted Seen Answered Draft forwarded)) {
198 Message-ID: <$kw\@test.example.com>
201 my $f = $kw eq 'forwarded' ? '$Forwarded' : "\\$kw";
202 $mic->append_string($folder_uri->mailbox, $buf, $f)
203 or BAIL_OUT "append $kw $@";
207 my $inboxdir = "$ENV{HOME}/wtest";
208 my @cmd = (qw(-init -Lbasic wtest), $inboxdir,
209 qw(https://example.com/wtest wtest@example.com));
210 run_script(\@cmd) or BAIL_OUT "init wtest";
211 xsys(qw(git config), "--file=$ENV{HOME}/.public-inbox/config",
212 'publicinbox.wtest.watch',
213 $folder_url) == 0 or BAIL_OUT "git config $?";
214 my $watcherr = "$ENV{HOME}/watch.err";
215 open my $err_wr, '>>', $watcherr or BAIL_OUT $!;
216 my $pub_cfg = PublicInbox::Config->new;
217 PublicInbox::DS->Reset;
218 my $ii = PublicInbox::InboxIdle->new($pub_cfg);
219 my $cb = sub { PublicInbox::DS->SetPostLoopCallback(sub {}) };
220 my $obj = bless \$cb, 'PublicInbox::TestCommon::InboxWakeup';
221 $pub_cfg->each_inbox(sub { $_[0]->subscribe_unlock('ident', $obj) });
222 my $w = start_script(['-watch'], undef, { 2 => $err_wr });
223 diag 'waiting for initial fetch...';
224 PublicInbox::DS->EventLoop;
225 my $ibx = $pub_cfg->lookup_name('wtest');
227 ok(defined($mm->num_for('Seen@test.example.com')),
228 '-watch takes seen message');
229 ok(defined($mm->num_for('Answered@test.example.com')),
230 '-watch takes answered message');
231 ok(!defined($mm->num_for('Deleted@test.example.com')),
232 '-watch ignored \\Deleted');
233 ok(!defined($mm->num_for('Draft@test.example.com')),
234 '-watch ignored \\Draft');
235 ok(defined($mm->num_for('forwarded@test.example.com')),
236 '-watch takes forwarded message');
237 undef $w; # done with watch
238 lei_ok qw(import), $folder_url;
239 lei_ok qw(q m:forwarded@test.example.com);
240 is_deeply(json_utf8->decode($lei_out)->[0]->{kw}, ['forwarded'],
241 'forwarded kw imported from IMAP');
244 undef $cleanup; # remove temporary folder