1 # Copyright (C) 2021 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 # front-end for the "lei convert" sub-command
5 package PublicInbox::LeiConvert;
8 use parent qw(PublicInbox::IPC);
10 use PublicInbox::InboxWritable qw(eml_from_path);
11 use PublicInbox::LeiStore;
12 use PublicInbox::LeiOverview;
15 my ($eml, $self) = @_;
16 my @kw = PublicInbox::LeiStore::mbox_keywords($eml);
17 $eml->header_set($_) for qw(Status X-Status);
18 $self->{wcb}->(undef, { kw => \@kw }, $eml);
21 sub imap_cb { # ->imap_each
22 my ($url, $uid, $kw, $eml, $self) = @_;
23 $self->{wcb}->(undef, { kw => $kw }, $eml);
27 my ($kw, $eml, $self) = @_;
28 $self->{wcb}->(undef, { kw => $kw }, $eml);
31 sub do_convert { # via wq_do
33 my $lei = $self->{lei};
34 my $in_fmt = $lei->{opt}->{'in-format'};
36 if (my $nrd = $lei->{nrd}) { # may prompt user once
37 $nrd->{mics_cached} = $nrd->imap_common_init($lei);
39 if (my $stdin = delete $self->{0}) {
40 PublicInbox::MboxReader->$in_fmt($stdin, \&mbox_cb, $self);
42 for my $input (@{$self->{inputs}}) {
43 my $ifmt = lc($in_fmt // '');
44 if ($input =~ m!\A(?:imap|nntp)s?://!) { # TODO: nntp
45 $lei->{nrd}->imap_each($input, \&imap_cb, $self);
47 } elsif ($input =~ s!\A([a-z0-9]+):!!i) {
51 open my $fh, '<', $input or
52 return $lei->fail("open $input: $!");
53 PublicInbox::MboxReader->$ifmt($fh, \&mbox_cb, $self);
55 PublicInbox::MdirReader::maildir_each_eml($input,
58 die "BUG: $input unhandled"; # should've failed earlier
62 delete $self->{wcb}; # commit
65 sub convert_start { # LeiAuth->auth_start callback
67 my $self = $lei->{cnv};
68 my $op = $lei->workers_start($self, 'lei_convert', 1, {
69 '' => [ $lei->can('dclose'), $lei ]
71 $self->wq_io_do('do_convert', []);
73 while ($op && $op->{sock}) { $op->event_step }
76 sub call { # the main "lei convert" method
77 my ($cls, $lei, @inputs) = @_;
78 my $opt = $lei->{opt};
80 my $self = $lei->{cnv} = bless {}, $cls;
81 my $in_fmt = $opt->{'in-format'};
83 $opt->{dedupe} //= 'none';
84 my $ovv = PublicInbox::LeiOverview->new($lei, 'out-format');
86 $lei->fail("output not specified or is not a mail destination");
87 $opt->{augment} = 1 unless $ovv->{dst} eq '/dev/stdout';
89 @inputs and return $lei->fail("--stdin and @inputs do not mix");
90 $lei->check_input_format(undef, 'in-format') or return;
91 $self->{0} = $lei->{0};
93 # e.g. Maildir:/home/user/Mail/ or imaps://example.com/INBOX
94 for my $input (@inputs) {
95 my $input_path = $input;
96 if ($input =~ m!\A(?:imap|nntp)s?://!i) {
97 require PublicInbox::NetReader;
98 $nrd //= PublicInbox::NetReader->new;
99 $nrd->add_url($input);
100 } elsif ($input_path =~ s/\A([a-z0-9]+)://is) {
102 if (($in_fmt // $ifmt) ne $ifmt) {
103 return $lei->fail(<<"");
104 --in-format=$in_fmt and `$ifmt:' conflict
107 if (-f $input_path) {
108 require PublicInbox::MboxReader;
109 PublicInbox::MboxReader->can($ifmt) or return
110 $lei->fail("$ifmt not supported");
112 require PublicInbox::MdirReader;
113 $ifmt eq 'maildir' or return
114 $lei->fail("$ifmt not supported");
116 return $lei->fail("Unable to handle $input");
118 } elsif (-f $input) { push @f, $input }
119 elsif (-d _) { push @d, $input }
120 else { return $lei->fail("Unable to handle $input") }
122 if (@f) { $lei->check_input_format(\@f, 'in-format') or return }
123 if (@d) { # TODO: check for MH vs Maildir, here
124 require PublicInbox::MdirReader;
126 $self->{inputs} = \@inputs;
128 if (my $err = $nrd->errors) {
129 return $lei->fail($err);
131 $nrd->{quiet} = $opt->{quiet};
137 sub ipc_atfork_child {
139 my $lei = $self->{lei};
140 $lei->lei_atfork_child;
141 my $l2m = delete $lei->{l2m};
142 $l2m->pre_augment($lei);
143 $l2m->do_augment($lei);
144 $l2m->post_augment($lei);
145 $self->{wcb} = $l2m->write_cb($lei);
146 $SIG{__WARN__} = PublicInbox::Eml::warn_ignore_cb();
147 $self->SUPER::ipc_atfork_child;