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;
13 sub mbox_cb { # MboxReader callback used by PublicInbox::LeiInput::input_fh
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 eml_cb { # used by PublicInbox::LeiInput::input_fh
21 my ($self, $eml) = @_;
22 $self->{wcb}->(undef, { kw => [] }, $eml);
25 sub net_cb { # callback for ->imap_each, ->nntp_each
26 my (undef, undef, $kw, $eml, $self) = @_; # @_[0,1]: url + uid ignored
27 $self->{wcb}->(undef, { kw => $kw }, $eml);
31 my ($f, $kw, $eml, $self) = @_;
32 $self->{wcb}->(undef, { kw => $kw }, $eml);
35 sub do_convert { # via wq_do
37 my $lei = $self->{lei};
38 my $ifmt = $lei->{opt}->{'in-format'};
39 if (my $stdin = delete $self->{0}) {
40 $self->input_fh($ifmt, $stdin, '<stdin>');
42 for my $input (@{$self->{inputs}}) {
43 my $ifmt = lc($ifmt // '');
44 if ($input =~ m!\Aimaps?://!) {
45 $lei->{net}->imap_each($input, \&net_cb, $self);
47 } elsif ($input =~ m!\A(?:nntps?|s?news)://!) {
48 $lei->{net}->nntp_each($input, \&net_cb, $self);
50 } elsif ($input =~ s!\A([a-z0-9]+):!!i) {
54 my $m = $lei->{opt}->{'lock'} //
55 ($ifmt eq 'eml' ? ['none'] :
56 PublicInbox::MboxLock->defaults);
57 my $mbl = PublicInbox::MboxLock->acq($input, 0, $m);
58 $self->input_fh($ifmt, $mbl->{fh}, $input);
60 PublicInbox::MdirReader::maildir_each_eml($input,
63 die "BUG: $input unhandled"; # should've failed earlier
67 delete $self->{wcb}; # commit
70 sub lei_convert { # the main "lei convert" method
71 my ($lei, @inputs) = @_;
72 $lei->{opt}->{kw} //= 1;
73 $lei->{opt}->{dedupe} //= 'none';
74 my $self = $lei->{cnv} = bless {}, __PACKAGE__;
75 my $ovv = PublicInbox::LeiOverview->new($lei, 'out-format');
77 $lei->fail("output not specified or is not a mail destination");
78 $lei->{opt}->{augment} = 1 unless $ovv->{dst} eq '/dev/stdout';
79 $self->prepare_inputs($lei, \@inputs) or return;
80 my $op = $lei->workers_start($self, 'lei_convert', 1);
81 $self->wq_io_do('do_convert', []);
83 while ($op && $op->{sock}) { $op->event_step }
86 sub ipc_atfork_child {
88 my $lei = $self->{lei};
89 $lei->lei_atfork_child;
90 my $l2m = delete $lei->{l2m};
91 if (my $net = $lei->{net}) { # may prompt user once
92 $net->{mics_cached} = $net->imap_common_init($lei);
93 $net->{nn_cached} = $net->nntp_common_init($lei);
95 $SIG{__WARN__} = PublicInbox::Eml::warn_ignore_cb();
96 $l2m->pre_augment($lei);
97 $l2m->do_augment($lei);
98 $l2m->post_augment($lei);
99 $self->{wcb} = $l2m->write_cb($lei);
100 $self->SUPER::ipc_atfork_child;