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 net_cb { # callback for ->imap_each, ->nntp_each
22 my (undef, undef, $kw, $eml, $self) = @_; # @_[0,1]: url + uid ignored
23 $self->{wcb}->(undef, { kw => $kw }, $eml);
27 my ($kw, $eml, $self) = @_;
28 $self->{wcb}->(undef, { kw => $kw }, $eml);
31 sub convert_fh ($$$$) {
32 my ($self, $ifmt, $fh, $name) = @_;
34 my $buf = do { local $/; <$fh> } //
35 return $self->{lei}->child_error(1 << 8, <<"");
36 error reading $name: $!
38 my $eml = PublicInbox::Eml->new(\$buf);
39 $self->{wcb}->(undef, { kw => [] }, $eml);
41 PublicInbox::MboxReader->$ifmt($fh, \&mbox_cb, $self);
45 sub do_convert { # via wq_do
47 my $lei = $self->{lei};
48 my $in_fmt = $lei->{opt}->{'in-format'};
50 if (my $stdin = delete $self->{0}) {
51 convert_fh($self, $in_fmt, $stdin, '<stdin>');
53 for my $input (@{$self->{inputs}}) {
54 my $ifmt = lc($in_fmt // '');
55 if ($input =~ m!\Aimaps?://!) {
56 $lei->{net}->imap_each($input, \&net_cb, $self);
58 } elsif ($input =~ m!\A(?:nntps?|s?news)://!) {
59 $lei->{net}->nntp_each($input, \&net_cb, $self);
61 } elsif ($input =~ s!\A([a-z0-9]+):!!i) {
65 open my $fh, '<', $input or
66 return $lei->fail("open $input: $!");
67 convert_fh($self, $ifmt, $fh, $input);
69 PublicInbox::MdirReader::maildir_each_eml($input,
72 die "BUG: $input unhandled"; # should've failed earlier
76 delete $self->{wcb}; # commit
79 sub call { # the main "lei convert" method
80 my ($cls, $lei, @inputs) = @_;
81 my $opt = $lei->{opt};
83 my $self = $lei->{cnv} = bless {}, $cls;
84 my $in_fmt = $opt->{'in-format'};
86 $opt->{dedupe} //= 'none';
87 my $ovv = PublicInbox::LeiOverview->new($lei, 'out-format');
89 $lei->fail("output not specified or is not a mail destination");
90 my $net = $lei->{net}; # NetWriter may be created by l2m
91 $opt->{augment} = 1 unless $ovv->{dst} eq '/dev/stdout';
93 @inputs and return $lei->fail("--stdin and @inputs do not mix");
94 $lei->check_input_format(undef) or return;
95 $self->{0} = $lei->{0};
97 # e.g. Maildir:/home/user/Mail/ or imaps://example.com/INBOX
98 for my $input (@inputs) {
99 my $input_path = $input;
100 if ($input =~ m!\A(?:imaps?|nntps?|s?news)://!i) {
101 require PublicInbox::NetReader;
102 $net //= PublicInbox::NetReader->new;
103 $net->add_url($input);
104 } elsif ($input_path =~ s/\A([a-z0-9]+)://is) {
106 if (($in_fmt // $ifmt) ne $ifmt) {
107 return $lei->fail(<<"");
108 --in-format=$in_fmt and `$ifmt:' conflict
111 if (-f $input_path) {
112 require PublicInbox::MboxReader;
113 PublicInbox::MboxReader->can($ifmt) or return
114 $lei->fail("$ifmt not supported");
116 require PublicInbox::MdirReader;
117 $ifmt eq 'maildir' or return
118 $lei->fail("$ifmt not supported");
120 return $lei->fail("Unable to handle $input");
122 } elsif (-f $input) { push @f, $input }
123 elsif (-d _) { push @d, $input }
124 else { return $lei->fail("Unable to handle $input") }
126 if (@f) { $lei->check_input_format(\@f) or return }
127 if (@d) { # TODO: check for MH vs Maildir, here
128 require PublicInbox::MdirReader;
130 $self->{inputs} = \@inputs;
132 if (my $err = $net->errors) {
133 return $lei->fail($err);
135 $net->{quiet} = $opt->{quiet};
136 $lei->{net} //= $net;
138 my $op = $lei->workers_start($self, 'lei_convert', 1, {
139 '' => [ $lei->can('dclose'), $lei ]
141 $self->wq_io_do('do_convert', []);
143 while ($op && $op->{sock}) { $op->event_step }
146 sub ipc_atfork_child {
148 my $lei = $self->{lei};
149 $lei->lei_atfork_child;
150 my $l2m = delete $lei->{l2m};
151 if (my $net = $lei->{net}) { # may prompt user once
152 $net->{mics_cached} = $net->imap_common_init($lei);
153 $net->{nn_cached} = $net->nntp_common_init($lei);
155 $SIG{__WARN__} = PublicInbox::Eml::warn_ignore_cb();
156 $l2m->pre_augment($lei);
157 $l2m->do_augment($lei);
158 $l2m->post_augment($lei);
159 $self->{wcb} = $l2m->write_cb($lei);
160 $self->SUPER::ipc_atfork_child;