]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/LeiConvert.pm
lei <import|convert>: support NNTP sources
[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);
9 use PublicInbox::Eml;
10 use PublicInbox::InboxWritable qw(eml_from_path);
11 use PublicInbox::LeiStore;
12 use PublicInbox::LeiOverview;
13
14 sub mbox_cb {
15         my ($eml, $self) = @_;
16         my @kw = PublicInbox::LeiStore::mbox_keywords($eml);
17         $eml->header_set($_) for qw(Status X-Status);
18         $self->{wcb}->(undef, { kw => \@kw }, $eml);
19 }
20
21 sub net_cb { # callback for ->imap_each, ->nntp_each
22         my (undef, undef, $kw, $eml, $self) = @_; # @_[0,1]: url + uid ignored
23         $self->{wcb}->(undef, { kw => $kw }, $eml);
24 }
25
26 sub mdir_cb {
27         my ($kw, $eml, $self) = @_;
28         $self->{wcb}->(undef, { kw => $kw }, $eml);
29 }
30
31 sub do_convert { # via wq_do
32         my ($self) = @_;
33         my $lei = $self->{lei};
34         my $in_fmt = $lei->{opt}->{'in-format'};
35         my $mics;
36         if (my $nrd = $lei->{nrd}) { # may prompt user once
37                 $nrd->{mics_cached} = $nrd->imap_common_init($lei);
38                 $nrd->{nn_cached} = $nrd->nntp_common_init($lei);
39         }
40         if (my $stdin = delete $self->{0}) {
41                 PublicInbox::MboxReader->$in_fmt($stdin, \&mbox_cb, $self);
42         }
43         for my $input (@{$self->{inputs}}) {
44                 my $ifmt = lc($in_fmt // '');
45                 if ($input =~ m!\Aimaps?://!) {
46                         $lei->{nrd}->imap_each($input, \&net_cb, $self);
47                         next;
48                 } elsif ($input =~ m!\A(?:nntps?|s?news)://!) {
49                         $lei->{nrd}->nntp_each($input, \&net_cb, $self);
50                         next;
51                 } elsif ($input =~ s!\A([a-z0-9]+):!!i) {
52                         $ifmt = lc $1;
53                 }
54                 if (-f $input) {
55                         open my $fh, '<', $input or
56                                         return $lei->fail("open $input: $!");
57                         PublicInbox::MboxReader->$ifmt($fh, \&mbox_cb, $self);
58                 } elsif (-d _) {
59                         PublicInbox::MdirReader::maildir_each_eml($input,
60                                                         \&mdir_cb, $self);
61                 } else {
62                         die "BUG: $input unhandled"; # should've failed earlier
63                 }
64         }
65         delete $lei->{1};
66         delete $self->{wcb}; # commit
67 }
68
69 sub call { # the main "lei convert" method
70         my ($cls, $lei, @inputs) = @_;
71         my $opt = $lei->{opt};
72         $opt->{kw} //= 1;
73         my $self = $lei->{cnv} = bless {}, $cls;
74         my $in_fmt = $opt->{'in-format'};
75         my ($nrd, @f, @d);
76         $opt->{dedupe} //= 'none';
77         my $ovv = PublicInbox::LeiOverview->new($lei, 'out-format');
78         $lei->{l2m} or return
79                 $lei->fail("output not specified or is not a mail destination");
80         $opt->{augment} = 1 unless $ovv->{dst} eq '/dev/stdout';
81         if ($opt->{stdin}) {
82                 @inputs and return $lei->fail("--stdin and @inputs do not mix");
83                 $lei->check_input_format(undef, 'in-format') or return;
84                 $self->{0} = $lei->{0};
85         }
86         # e.g. Maildir:/home/user/Mail/ or imaps://example.com/INBOX
87         for my $input (@inputs) {
88                 my $input_path = $input;
89                 if ($input =~ m!\A(?:imaps?|nntps?|s?news)://!i) {
90                         require PublicInbox::NetReader;
91                         $nrd //= PublicInbox::NetReader->new;
92                         $nrd->add_url($input);
93                 } elsif ($input_path =~ s/\A([a-z0-9]+)://is) {
94                         my $ifmt = lc $1;
95                         if (($in_fmt // $ifmt) ne $ifmt) {
96                                 return $lei->fail(<<"");
97 --in-format=$in_fmt and `$ifmt:' conflict
98
99                         }
100                         if (-f $input_path) {
101                                 require PublicInbox::MboxReader;
102                                 PublicInbox::MboxReader->can($ifmt) or return
103                                         $lei->fail("$ifmt not supported");
104                         } elsif (-d _) {
105                                 require PublicInbox::MdirReader;
106                                 $ifmt eq 'maildir' or return
107                                         $lei->fail("$ifmt not supported");
108                         } else {
109                                 return $lei->fail("Unable to handle $input");
110                         }
111                 } elsif (-f $input) { push @f, $input }
112                 elsif (-d _) { push @d, $input }
113                 else { return $lei->fail("Unable to handle $input") }
114         }
115         if (@f) { $lei->check_input_format(\@f, 'in-format') or return }
116         if (@d) { # TODO: check for MH vs Maildir, here
117                 require PublicInbox::MdirReader;
118         }
119         $self->{inputs} = \@inputs;
120         if ($nrd) {
121                 if (my $err = $nrd->errors) {
122                         return $lei->fail($err);
123                 }
124                 $nrd->{quiet} = $opt->{quiet};
125                 $lei->{nrd} = $nrd;
126         }
127         my $op = $lei->workers_start($self, 'lei_convert', 1, {
128                 '' => [ $lei->can('dclose'), $lei ]
129         });
130         $self->wq_io_do('do_convert', []);
131         $self->wq_close(1);
132         while ($op && $op->{sock}) { $op->event_step }
133 }
134
135 sub ipc_atfork_child {
136         my ($self) = @_;
137         my $lei = $self->{lei};
138         $lei->lei_atfork_child;
139         my $l2m = delete $lei->{l2m};
140         $l2m->pre_augment($lei);
141         $l2m->do_augment($lei);
142         $l2m->post_augment($lei);
143         $self->{wcb} = $l2m->write_cb($lei);
144         $SIG{__WARN__} = PublicInbox::Eml::warn_ignore_cb();
145         $self->SUPER::ipc_atfork_child;
146 }
147
148 1;