]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/LeiConvert.pm
49e2b7af2e7b915ba8e8029a80be9f2ae381f8b9
[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::Eml;
10 use PublicInbox::LeiStore;
11 use PublicInbox::LeiOverview;
12
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);
18 }
19
20 sub eml_cb { # used by PublicInbox::LeiInput::input_fh
21         my ($self, $eml) = @_;
22         $self->{wcb}->(undef, { kw => [] }, $eml);
23 }
24
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);
28 }
29
30 sub mdir_cb {
31         my ($f, $kw, $eml, $self) = @_;
32         $self->{wcb}->(undef, { kw => $kw }, $eml);
33 }
34
35 sub do_convert { # via wq_do
36         my ($self) = @_;
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>');
41         }
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);
46                         next;
47                 } elsif ($input =~ m!\A(?:nntps?|s?news)://!) {
48                         $lei->{net}->nntp_each($input, \&net_cb, $self);
49                         next;
50                 } elsif ($input =~ s!\A([a-z0-9]+):!!i) {
51                         $ifmt = lc $1;
52                 }
53                 if (-f $input) {
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);
59                 } elsif (-d _) {
60                         PublicInbox::MdirReader::maildir_each_eml($input,
61                                                         \&mdir_cb, $self);
62                 } else {
63                         die "BUG: $input unhandled"; # should've failed earlier
64                 }
65         }
66         delete $lei->{1};
67         delete $self->{wcb}; # commit
68 }
69
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');
76         $lei->{l2m} or return
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', []);
82         $self->wq_close(1);
83         while ($op && $op->{sock}) { $op->event_step }
84 }
85
86 sub ipc_atfork_child {
87         my ($self) = @_;
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);
94         }
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;
101 }
102
103 1;