]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/LeiConvert.pm
lei convert: use "--output" in failure message
[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 use PublicInbox::DS;
11
12 # /^input_/ subs are used by PublicInbox::LeiInput
13
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);
19 }
20
21 sub input_eml_cb { # used by PublicInbox::LeiInput::input_fh
22         my ($self, $eml) = @_;
23         $self->{wcb}->(undef, {}, $eml);
24 }
25
26 sub input_net_cb { # callback for ->imap_each, ->nntp_each
27         my (undef, undef, $kw, $eml, $self) = @_; # @_[0,1]: url + uid ignored
28         $self->{wcb}->(undef, { kw => $kw }, $eml);
29 }
30
31 sub input_maildir_cb {
32         my (undef, $kw, $eml, $self) = @_; # $_[0] $filename ignored
33         $self->{wcb}->(undef, { kw => $kw }, $eml);
34 }
35
36 sub process_inputs { # via wq_do
37         my ($self) = @_;
38         local $PublicInbox::DS::in_loop = 0; # force synchronous dwaitpid
39         $self->SUPER::process_inputs;
40         my $lei = $self->{lei};
41         delete $lei->{1};
42         delete $self->{wcb}; # commit
43         my $nr = delete($lei->{-nr_write}) // 0;
44         $lei->qerr("# converted $nr messages");
45 }
46
47 sub lei_convert { # the main "lei convert" method
48         my ($lei, @inputs) = @_;
49         $lei->{opt}->{kw} //= 1;
50         $lei->{opt}->{dedupe} //= 'none';
51         my $self = bless {}, __PACKAGE__;
52         my $ovv = PublicInbox::LeiOverview->new($lei, 'out-format');
53         $lei->{l2m} or return
54                 $lei->fail('--output unspecified or is not a mail destination');
55         my $devfd = $lei->path_to_fd($ovv->{dst}) // return;
56         $lei->{opt}->{augment} = 1 if $devfd < 0;
57         $self->prepare_inputs($lei, \@inputs) or return;
58         # n.b. {net} {auth} is handled by l2m worker
59         my ($op_c, $ops) = $lei->workers_start($self, 1);
60         $lei->{wq1} = $self;
61         $self->wq_io_do('process_inputs', []);
62         $self->wq_close;
63         $lei->wait_wq_events($op_c, $ops);
64 }
65
66 sub ipc_atfork_child {
67         my ($self) = @_;
68         my $lei = $self->{lei};
69         $lei->_lei_atfork_child;
70         my $l2m = delete $lei->{l2m};
71         if (my $net = $lei->{net}) { # may prompt user once
72                 $net->{mics_cached} = $net->imap_common_init($lei);
73                 $net->{nn_cached} = $net->nntp_common_init($lei);
74         }
75         $l2m->pre_augment($lei);
76         $l2m->do_augment($lei);
77         $l2m->post_augment($lei);
78         $self->{wcb} = $l2m->write_cb($lei);
79         $self->SUPER::ipc_atfork_child;
80 }
81
82 1;