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 PublicInbox::LeiInput);
10 use PublicInbox::LeiStore;
11 use PublicInbox::LeiOverview;
14 my ($eml, $self) = @_;
15 my $kw = PublicInbox::MboxReader::mbox_keywords($eml);
16 $eml->header_set($_) for qw(Status X-Status);
17 $self->{wcb}->(undef, { kw => $kw }, $eml);
20 sub net_cb { # callback for ->imap_each, ->nntp_each
21 my (undef, undef, $kw, $eml, $self) = @_; # @_[0,1]: url + uid ignored
22 $self->{wcb}->(undef, { kw => $kw }, $eml);
26 my ($f, $kw, $eml, $self) = @_;
27 $self->{wcb}->(undef, { kw => $kw }, $eml);
30 sub convert_fh ($$$$) {
31 my ($self, $ifmt, $fh, $name) = @_;
33 my $buf = do { local $/; <$fh> } //
34 return $self->{lei}->child_error(1 << 8, <<"");
35 error reading $name: $!
37 my $eml = PublicInbox::Eml->new(\$buf);
38 $self->{wcb}->(undef, { kw => [] }, $eml);
40 PublicInbox::MboxReader->$ifmt($fh, \&mbox_cb, $self);
44 sub do_convert { # via wq_do
46 my $lei = $self->{lei};
47 my $in_fmt = $lei->{opt}->{'in-format'};
49 if (my $stdin = delete $self->{0}) {
50 convert_fh($self, $in_fmt, $stdin, '<stdin>');
52 for my $input (@{$self->{inputs}}) {
53 my $ifmt = lc($in_fmt // '');
54 if ($input =~ m!\Aimaps?://!) {
55 $lei->{net}->imap_each($input, \&net_cb, $self);
57 } elsif ($input =~ m!\A(?:nntps?|s?news)://!) {
58 $lei->{net}->nntp_each($input, \&net_cb, $self);
60 } elsif ($input =~ s!\A([a-z0-9]+):!!i) {
64 my $m = $lei->{opt}->{'lock'} //
65 ($ifmt eq 'eml' ? ['none'] :
66 PublicInbox::MboxLock->defaults);
67 my $mbl = PublicInbox::MboxLock->acq($input, 0, $m);
68 convert_fh($self, $ifmt, $mbl->{fh}, $input);
70 PublicInbox::MdirReader::maildir_each_eml($input,
73 die "BUG: $input unhandled"; # should've failed earlier
77 delete $self->{wcb}; # commit
80 sub lei_convert { # the main "lei convert" method
81 my ($lei, @inputs) = @_;
82 $lei->{opt}->{kw} //= 1;
83 $lei->{opt}->{dedupe} //= 'none';
84 my $self = $lei->{cnv} = bless {}, __PACKAGE__;
85 my $ovv = PublicInbox::LeiOverview->new($lei, 'out-format');
87 $lei->fail("output not specified or is not a mail destination");
88 $lei->{opt}->{augment} = 1 unless $ovv->{dst} eq '/dev/stdout';
89 $self->prepare_inputs($lei, \@inputs) or return;
90 my $op = $lei->workers_start($self, 'lei_convert', 1, {
91 '' => [ $lei->can('dclose'), $lei ]
93 $self->wq_io_do('do_convert', []);
95 while ($op && $op->{sock}) { $op->event_step }
98 sub ipc_atfork_child {
100 my $lei = $self->{lei};
101 $lei->lei_atfork_child;
102 my $l2m = delete $lei->{l2m};
103 if (my $net = $lei->{net}) { # may prompt user once
104 $net->{mics_cached} = $net->imap_common_init($lei);
105 $net->{nn_cached} = $net->nntp_common_init($lei);
107 $SIG{__WARN__} = PublicInbox::Eml::warn_ignore_cb();
108 $l2m->pre_augment($lei);
109 $l2m->do_augment($lei);
110 $l2m->post_augment($lei);
111 $self->{wcb} = $l2m->write_cb($lei);
112 $self->SUPER::ipc_atfork_child;