]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/LeiConvert.pm
lei_input: more common code between <mark|convert|import>
[public-inbox.git] / lib / PublicInbox / LeiConvert.pm
1 # Copyright (C) 2021 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3
4 # front-end for the "lei convert" sub-command
5 package PublicInbox::LeiConvert;
6 use strict;
7 use v5.10.1;
8 use parent qw(PublicInbox::IPC PublicInbox::LeiInput);
9 use PublicInbox::LeiOverview;
10
11 # /^input_/ subs are used by PublicInbox::LeiInput
12
13 sub input_mbox_cb { # MboxReader callback
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);
18 }
19
20 sub input_eml_cb { # used by PublicInbox::LeiInput::input_fh
21         my ($self, $eml) = @_;
22         $self->{wcb}->(undef, {}, $eml);
23 }
24
25 sub input_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);
28 }
29
30 sub input_maildir_cb {
31         my (undef, $kw, $eml, $self) = @_; # $_[0] $filename ignored
32         $self->{wcb}->(undef, { kw => $kw }, $eml);
33 }
34
35 sub do_convert { # via wq_do
36         my ($self) = @_;
37         $self->input_stdin;
38         for my $input (@{$self->{inputs}}) {
39                 $self->input_path_url($input);
40         }
41         delete $self->{lei}->{1};
42         delete $self->{wcb}; # commit
43 }
44
45 sub lei_convert { # the main "lei convert" method
46         my ($lei, @inputs) = @_;
47         $lei->{opt}->{kw} //= 1;
48         $lei->{opt}->{dedupe} //= 'none';
49         my $self = $lei->{cnv} = bless {}, __PACKAGE__;
50         my $ovv = PublicInbox::LeiOverview->new($lei, 'out-format');
51         $lei->{l2m} or return
52                 $lei->fail("output not specified or is not a mail destination");
53         $lei->{opt}->{augment} = 1 unless $ovv->{dst} eq '/dev/stdout';
54         $self->prepare_inputs($lei, \@inputs) or return;
55         my $op = $lei->workers_start($self, 'lei_convert', 1);
56         $self->wq_io_do('do_convert', []);
57         $self->wq_close(1);
58         while ($op && $op->{sock}) { $op->event_step }
59 }
60
61 sub ipc_atfork_child {
62         my ($self) = @_;
63         my $lei = $self->{lei};
64         $lei->_lei_atfork_child;
65         my $l2m = delete $lei->{l2m};
66         if (my $net = $lei->{net}) { # may prompt user once
67                 $net->{mics_cached} = $net->imap_common_init($lei);
68                 $net->{nn_cached} = $net->nntp_common_init($lei);
69         }
70         $SIG{__WARN__} = PublicInbox::Eml::warn_ignore_cb();
71         $l2m->pre_augment($lei);
72         $l2m->do_augment($lei);
73         $l2m->post_augment($lei);
74         $self->{wcb} = $l2m->write_cb($lei);
75         $self->SUPER::ipc_atfork_child;
76 }
77
78 1;