1 # Copyright (C) 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);
9 use PublicInbox::LeiOverview;
12 # /^input_/ subs are used by PublicInbox::LeiInput
14 sub input_mbox_cb { # MboxReader callback
15 my ($eml, $self) = @_;
16 my $kw = PublicInbox::MboxReader::mbox_keywords($eml);
17 $eml->header_set($_) for qw(Status X-Status);
18 $self->{wcb}->(undef, { kw => $kw }, $eml);
21 sub input_eml_cb { # used by PublicInbox::LeiInput::input_fh
22 my ($self, $eml) = @_;
23 $self->{wcb}->(undef, {}, $eml);
26 sub input_maildir_cb {
27 my (undef, $kw, $eml, $self) = @_; # $_[0] $filename ignored
28 $self->{wcb}->(undef, { kw => $kw }, $eml);
31 sub process_inputs { # via wq_do
33 local $PublicInbox::DS::in_loop = 0; # force synchronous dwaitpid
34 $self->SUPER::process_inputs;
35 my $lei = $self->{lei};
37 delete $self->{wcb}; # commit
38 my $nr_w = delete($lei->{-nr_write}) // 0;
39 my $d = (delete($lei->{-nr_seen}) // 0) - $nr_w;
40 $d = $d ? " ($d duplicates)" : '';
41 $lei->qerr("# converted $nr_w messages$d");
44 sub lei_convert { # the main "lei convert" method
45 my ($lei, @inputs) = @_;
46 $lei->{opt}->{kw} //= 1;
47 $lei->{opt}->{dedupe} //= 'none';
48 my $self = bless {}, __PACKAGE__;
49 my $ovv = PublicInbox::LeiOverview->new($lei, 'out-format');
51 $lei->fail('--output unspecified or is not a mail destination');
52 my $devfd = $lei->path_to_fd($ovv->{dst}) // return;
53 $lei->{opt}->{augment} = 1 if $devfd < 0;
54 $self->prepare_inputs($lei, \@inputs) or return;
55 # n.b. {net} {auth} is handled by l2m worker
56 my ($op_c, $ops) = $lei->workers_start($self, 1);
58 $self->wq_io_do('process_inputs', []);
60 $lei->wait_wq_events($op_c, $ops);
63 sub ipc_atfork_child {
65 my $lei = $self->{lei};
66 $lei->_lei_atfork_child;
67 my $l2m = delete $lei->{l2m};
68 if (my $net = $lei->{net}) { # may prompt user once
69 $net->{mics_cached} = $net->imap_common_init($lei);
70 $net->{nn_cached} = $net->nntp_common_init($lei);
72 $l2m->pre_augment($lei);
73 $l2m->do_augment($lei);
74 $l2m->post_augment($lei);
75 $self->{wcb} = $l2m->write_cb($lei);
76 $self->SUPER::ipc_atfork_child;