2 # Copyright (C) all contributors <meta@public-inbox.org>
3 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 use v5.12; use PublicInbox::TestCommon;
5 use PublicInbox::MboxReader;
6 use PublicInbox::MdirReader;
7 use PublicInbox::NetReader;
9 use IO::Uncompress::Gunzip;
10 use File::Path qw(remove_tree);
11 use PublicInbox::Spawn qw(which run_qx);
14 require_mods(qw(lei -imapd -nntpd Mail::IMAPClient Net::NNTP));
15 my ($tmpdir, $for_destroy) = tmpdir;
16 my $sock = tcp_server;
17 my $cmd = [ '-imapd', '-W0', "--stdout=$tmpdir/i1", "--stderr=$tmpdir/i2" ];
18 my ($ro_home, $cfg_path) = setup_public_inboxes;
19 my $env = { PI_CONFIG => $cfg_path };
20 my $tdi = start_script($cmd, $env, { 3 => $sock }) or BAIL_OUT("-imapd: $?");
21 my $imap_host_port = tcp_host_port($sock);
23 $cmd = [ '-nntpd', '-W0', "--stdout=$tmpdir/n1", "--stderr=$tmpdir/n2" ];
24 my $tdn = start_script($cmd, $env, { 3 => $sock }) or BAIL_OUT("-nntpd: $?");
25 my $nntp_host_port = tcp_host_port($sock);
28 test_lei({ tmpdir => $tmpdir }, sub {
30 lei_ok('convert', '-o', "mboxrd:$d/foo.mboxrd",
31 "imap://$imap_host_port/t.v2.0");
32 my ($nc0) = ($lei_err =~ /converted (\d+) messages/);
33 ok(-f "$d/foo.mboxrd", 'mboxrd created from imap://');
35 lei_ok qw(convert -o), "v2:$d/v2-test", "mboxrd:$d/foo.mboxrd";
36 my ($nc) = ($lei_err =~ /converted (\d+) messages/);
37 is $nc, $nc0, 'converted all messages messages';
38 lei_ok qw(q z:0.. -f jsonl --only), "$d/v2-test";
39 is(scalar(split(/^/sm, $lei_out)), $nc, 'got all messages in v2-test');
41 lei_ok qw(convert -o), "mboxrd:$d/from-v2.mboxrd", "$d/v2-test";
42 like $lei_err, qr/converted $nc messages/;
43 is(compare("$d/foo.mboxrd", "$d/from-v2.mboxrd"), 0,
44 'convert mboxrd -> v2 ->mboxrd roundtrip') or
45 diag run_qx([qw(git diff --no-index),
46 "$d/foo.mboxrd", "$d/from-v2.mboxrd"]);
48 lei_ok [qw(convert -F eml -o), "$d/v2-test"], undef,
49 { 0 => \<<'EOM', %$lei_opt };
52 Subject: append-to-v2-on-convert
53 Message-ID: <append-to-v2-on-convert@example>
54 Date: Fri, 02 Oct 1993 00:00:00 +0000
56 like $lei_err, qr/converted 1 messages/, 'only one message added';
57 lei_ok qw(q z:0.. -f jsonl --only), "$d/v2-test";
58 is(scalar(split(/^/sm, $lei_out)), $nc + 1,
59 'got expected number of messages after append convert');
60 like $lei_out, qr/append-to-v2-on-convert/;
62 lei_ok('convert', '-o', "mboxrd:$d/nntp.mboxrd",
63 "nntp://$nntp_host_port/t.v2");
64 ok(-f "$d/nntp.mboxrd", 'mboxrd created from nntp://');
66 my (@mboxrd, @mboxcl2);
67 open my $fh, '<', "$d/foo.mboxrd" or BAIL_OUT $!;
68 PublicInbox::MboxReader->mboxrd($fh, sub { push @mboxrd, shift });
69 ok(scalar(@mboxrd) > 1, 'got multiple messages');
71 open $fh, '<', "$d/nntp.mboxrd" or BAIL_OUT $!;
73 PublicInbox::MboxReader->mboxrd($fh, sub {
75 is($eml->body, $mboxrd[$i]->body, "body matches #$i");
79 lei_ok('convert', '-o', "mboxcl2:$d/cl2", "mboxrd:$d/foo.mboxrd");
80 ok(-s "$d/cl2", 'mboxcl2 non-empty') or diag $lei_err;
81 open $fh, '<', "$d/cl2" or BAIL_OUT $!;
82 PublicInbox::MboxReader->mboxcl2($fh, sub {
84 $eml->header_set($_) for (qw(Content-Length Lines));
87 is_deeply(\@mboxcl2, \@mboxrd, 'mboxrd and mboxcl2 have same mail');
89 lei_ok('convert', '-o', "$d/md", "mboxrd:$d/foo.mboxrd");
90 ok(-d "$d/md", 'Maildir created');
92 PublicInbox::MdirReader->new->maildir_each_eml("$d/md", sub {
95 is(scalar(@md), scalar(@mboxrd), 'got expected emails in Maildir') or
97 @md = sort { ${$a->{bdy}} cmp ${$b->{bdy}} } @md;
98 @mboxrd = sort { ${$a->{bdy}} cmp ${$b->{bdy}} } @mboxrd;
99 my @rd_nostatus = map {
100 my $eml = PublicInbox::Eml->new(\($_->as_string));
101 $eml->header_set('Status');
104 is_deeply(\@md, \@rd_nostatus, 'Maildir output matches mboxrd');
107 lei_ok('convert', '-o', "mboxrd:$d/bar.mboxrd", "$d/md");
108 open $fh, '<', "$d/bar.mboxrd" or BAIL_OUT $!;
109 PublicInbox::MboxReader->mboxrd($fh, sub { push @bar, shift });
110 @bar = sort { ${$a->{bdy}} cmp ${$b->{bdy}} } @bar;
111 is_deeply(\@mboxrd, \@bar,
112 'mboxrd round-tripped through Maildir w/ flags');
114 open my $in, '<', "$d/foo.mboxrd" or BAIL_OUT;
115 my $rdr = { 0 => $in, 1 => \(my $out), 2 => \$lei_err };
116 lei_ok([qw(convert --stdin -F mboxrd -o mboxrd:/dev/stdout)],
118 open $fh, '<', "$d/foo.mboxrd" or BAIL_OUT;
119 my $exp = do { local $/; <$fh> };
120 is($out, $exp, 'stdin => stdout');
122 lei_ok qw(convert -F eml -o mboxcl2:/dev/fd/1 t/plack-qp.eml);
123 open $fh, '<', \$lei_out or BAIL_OUT;
125 PublicInbox::MboxReader->mboxcl2($fh, sub {
127 for my $h (qw(Content-Length Lines)) {
128 ok(defined($eml->header_raw($h)),
129 "$h defined for mboxcl2");
130 $eml->header_set($h);
134 my $qp_eml = eml_load('t/plack-qp.eml');
135 $qp_eml->header_set('Status', 'O');
136 is_deeply(\@bar, [ $qp_eml ], 'eml => mboxcl2');
138 lei_ok qw(convert t/plack-qp.eml -o), "mboxrd:$d/qp.gz";
139 open $fh, '<', "$d/qp.gz" or xbail $!;
140 ok(-s $fh, 'not empty');
141 $fh = IO::Uncompress::Gunzip->new($fh, MultiStream => 1);
143 PublicInbox::MboxReader->mboxrd($fh, sub { push @bar, shift });
144 is_deeply(\@bar, [ $qp_eml ], 'wrote gzipped mboxrd');
145 lei_ok qw(convert -o mboxrd:/dev/stdout), "mboxrd:$d/qp.gz";
146 open $fh, '<', \$lei_out or xbail;
148 PublicInbox::MboxReader->mboxrd($fh, sub { push @bar, shift });
149 is_deeply(\@bar, [ $qp_eml ], 'readed gzipped mboxrd');
151 # Status => Maildir flag => Status round trip
152 $lei_out =~ s/^Status: O/Status: RO/sm or xbail "`seen' Status";
153 $rdr = { 0 => \($in = $lei_out), %$lei_opt };
154 lei_ok([qw(convert -F mboxrd -o), "$d/md2"], undef, $rdr);
155 @md = glob("$d/md2/*/*");
156 is(scalar(@md), 1, 'one message');
157 like($md[0], qr/:2,S\z/, "`seen' flag set in Maildir");
158 lei_ok(qw(convert -o mboxrd:/dev/stdout), "$d/md2");
159 like($lei_out, qr/^Status: RO/sm, "`seen' flag preserved");
163 for my $x (($ENV{GZIP}//''), qw(pigz gzip)) {
164 $x && (`$x -h 2>&1`//'') =~ /--rsyncable\b/s or next;
168 skip 'pigz || gzip do not support --rsyncable', 1 if !$ok;
169 lei_ok qw(convert --rsyncable), "mboxrd:$d/qp.gz",
170 '-o', "mboxcl2:$d/qp2.gz";
171 undef $fh; # necessary to make IO::Uncompress::Gunzip happy
172 open $fh, '<', "$d/qp2.gz";
173 $fh = IO::Uncompress::Gunzip->new($fh, MultiStream => 1);
175 PublicInbox::MboxReader->mboxcl2($fh, sub {
177 $eml->header_set($_) for qw(Content-Length Lines);
180 is_deeply(\@tmp, \@bar, 'read rsyncable-gzipped mboxcl2');
182 my $cp = which('cp') or xbail 'cp(1) not available (WTF?)';
184 my $ibx_dir = "$ro_home/t$v";
185 lei_ok qw(convert -f mboxrd), $ibx_dir,
186 \"dump v$v inbox to mboxrd";
188 lei_ok qw(convert -f mboxrd), "v$v:$ibx_dir",
189 \"dump v$v inbox to mboxrd w/ v$v:// prefix";
190 is $out, $lei_out, "v$v:// prefix accepted";
191 open my $fh, '<', \$out;
193 PublicInbox::MboxReader->mboxrd($fh, sub {
194 $_[0]->header_set('Status');
195 push @mb, $_[0]->as_string;
198 ok(scalar(@mb), 'got messages output');
199 my $mdir = "$d/v$v-mdir";
200 lei_ok qw(convert -o), $mdir, $ibx_dir,
201 \"dump v$v inbox to Maildir";
202 PublicInbox::MdirReader->new->maildir_each_eml($mdir, sub {
203 push @md, $_[2]->as_string;
205 @md = sort { $a cmp $b } @md;
206 @mb = sort { $a cmp $b } @mb;
207 is_deeply(\@mb, \@md, 'got matching inboxes');
208 xsys_e([$cp, '-Rp', $ibx_dir, "$d/tv$v" ]);
209 remove_tree($mdir, "$d/tv$v/public-inbox",
210 glob("$d/tv$v/xap*"));
212 lei_ok qw(convert -o), $mdir, "$d/tv$v",
213 \"dump u indexed v$v inbox to Maildir";
214 PublicInbox::MdirReader->new->maildir_each_eml($mdir, sub {
215 push @md2, $_[2]->as_string;
217 @md2 = sort { $a cmp $b } @md2;
218 is_deeply(\@md, \@md2, 'got matching inboxes even unindexed');