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