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 PublicInbox::MboxReader;
6 use PublicInbox::MdirReader;
7 use PublicInbox::NetReader;
9 use IO::Uncompress::Gunzip;
10 require_mods(qw(lei -imapd -nntpd Mail::IMAPClient Net::NNTP));
11 my ($tmpdir, $for_destroy) = tmpdir;
12 my $sock = tcp_server;
13 my $cmd = [ '-imapd', '-W0', "--stdout=$tmpdir/i1", "--stderr=$tmpdir/i2" ];
14 my ($ro_home, $cfg_path) = setup_public_inboxes;
15 my $env = { PI_CONFIG => $cfg_path };
16 my $tdi = start_script($cmd, $env, { 3 => $sock }) or BAIL_OUT("-imapd: $?");
17 my $imap_host_port = tcp_host_port($sock);
19 $cmd = [ '-nntpd', '-W0', "--stdout=$tmpdir/n1", "--stderr=$tmpdir/n2" ];
20 my $tdn = start_script($cmd, $env, { 3 => $sock }) or BAIL_OUT("-nntpd: $?");
21 my $nntp_host_port = tcp_host_port($sock);
24 test_lei({ tmpdir => $tmpdir }, sub {
26 lei_ok('convert', '-o', "mboxrd:$d/foo.mboxrd",
27 "imap://$imap_host_port/t.v2.0");
28 ok(-f "$d/foo.mboxrd", 'mboxrd created from imap://');
30 lei_ok('convert', '-o', "mboxrd:$d/nntp.mboxrd",
31 "nntp://$nntp_host_port/t.v2");
32 ok(-f "$d/nntp.mboxrd", 'mboxrd created from nntp://');
34 my (@mboxrd, @mboxcl2);
35 open my $fh, '<', "$d/foo.mboxrd" or BAIL_OUT $!;
36 PublicInbox::MboxReader->mboxrd($fh, sub { push @mboxrd, shift });
37 ok(scalar(@mboxrd) > 1, 'got multiple messages');
39 open $fh, '<', "$d/nntp.mboxrd" or BAIL_OUT $!;
41 PublicInbox::MboxReader->mboxrd($fh, sub {
43 is($eml->body, $mboxrd[$i]->body, "body matches #$i");
47 lei_ok('convert', '-o', "mboxcl2:$d/cl2", "mboxrd:$d/foo.mboxrd");
48 ok(-s "$d/cl2", 'mboxcl2 non-empty') or diag $lei_err;
49 open $fh, '<', "$d/cl2" or BAIL_OUT $!;
50 PublicInbox::MboxReader->mboxcl2($fh, sub {
52 $eml->header_set($_) for (qw(Content-Length Lines));
55 is_deeply(\@mboxcl2, \@mboxrd, 'mboxrd and mboxcl2 have same mail');
57 lei_ok('convert', '-o', "$d/md", "mboxrd:$d/foo.mboxrd");
58 ok(-d "$d/md", 'Maildir created');
60 PublicInbox::MdirReader->new->maildir_each_eml("$d/md", sub {
63 is(scalar(@md), scalar(@mboxrd), 'got expected emails in Maildir') or
65 @md = sort { ${$a->{bdy}} cmp ${$b->{bdy}} } @md;
66 @mboxrd = sort { ${$a->{bdy}} cmp ${$b->{bdy}} } @mboxrd;
67 my @rd_nostatus = map {
68 my $eml = PublicInbox::Eml->new(\($_->as_string));
69 $eml->header_set('Status');
72 is_deeply(\@md, \@rd_nostatus, 'Maildir output matches mboxrd');
75 lei_ok('convert', '-o', "mboxrd:$d/bar.mboxrd", "$d/md");
76 open $fh, '<', "$d/bar.mboxrd" or BAIL_OUT $!;
77 PublicInbox::MboxReader->mboxrd($fh, sub { push @bar, shift });
78 @bar = sort { ${$a->{bdy}} cmp ${$b->{bdy}} } @bar;
79 is_deeply(\@mboxrd, \@bar,
80 'mboxrd round-tripped through Maildir w/ flags');
82 open my $in, '<', "$d/foo.mboxrd" or BAIL_OUT;
83 my $rdr = { 0 => $in, 1 => \(my $out), 2 => \$lei_err };
84 lei_ok([qw(convert --stdin -F mboxrd -o mboxrd:/dev/stdout)],
86 open $fh, '<', "$d/foo.mboxrd" or BAIL_OUT;
87 my $exp = do { local $/; <$fh> };
88 is($out, $exp, 'stdin => stdout');
90 lei_ok qw(convert -F eml -o mboxcl2:/dev/fd/1 t/plack-qp.eml);
91 open $fh, '<', \$lei_out or BAIL_OUT;
93 PublicInbox::MboxReader->mboxcl2($fh, sub {
95 for my $h (qw(Content-Length Lines)) {
96 ok(defined($eml->header_raw($h)),
97 "$h defined for mboxcl2");
102 my $qp_eml = eml_load('t/plack-qp.eml');
103 $qp_eml->header_set('Status', 'O');
104 is_deeply(\@bar, [ $qp_eml ], 'eml => mboxcl2');
106 lei_ok qw(convert t/plack-qp.eml -o), "mboxrd:$d/qp.gz";
107 open $fh, '<', "$d/qp.gz" or xbail $!;
108 ok(-s $fh, 'not empty');
109 $fh = IO::Uncompress::Gunzip->new($fh, MultiStream => 1);
111 PublicInbox::MboxReader->mboxrd($fh, sub { push @bar, shift });
112 is_deeply(\@bar, [ $qp_eml ], 'wrote gzipped mboxrd');
113 lei_ok qw(convert -o mboxrd:/dev/stdout), "mboxrd:$d/qp.gz";
114 open $fh, '<', \$lei_out or xbail;
116 PublicInbox::MboxReader->mboxrd($fh, sub { push @bar, shift });
117 is_deeply(\@bar, [ $qp_eml ], 'readed gzipped mboxrd');