]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/LeiInput.pm
lei_input: reduce IPC traffic with multiple inputs
[public-inbox.git] / lib / PublicInbox / LeiInput.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 # parent class for LeiImport, LeiConvert
5 package PublicInbox::LeiInput;
6 use strict;
7 use v5.10.1;
8 use PublicInbox::DS;
9
10 sub check_input_format ($;$) {
11         my ($lei, $files) = @_;
12         my $opt_key = 'in-format';
13         my $fmt = $lei->{opt}->{$opt_key};
14         if (!$fmt) {
15                 my $err = $files ? "regular file(s):\n@$files" : '--stdin';
16                 return $lei->fail("--$opt_key unset for $err");
17         }
18         return 1 if $fmt eq 'eml';
19         require PublicInbox::MboxLock if $files;
20         require PublicInbox::MboxReader;
21         # XXX: should this handle {gz,bz2,xz}? that's currently in LeiToMail
22         PublicInbox::MboxReader->reads($fmt) or
23                 return $lei->fail("--$opt_key=$fmt unrecognized");
24         1;
25 }
26
27 # import a single file handle of $name
28 # Subclass must define ->input_eml_cb and ->input_mbox_cb
29 sub input_fh {
30         my ($self, $ifmt, $fh, $name, @args) = @_;
31         if ($ifmt eq 'eml') {
32                 my $buf = do { local $/; <$fh> } //
33                         return $self->{lei}->child_error(1 << 8, <<"");
34 error reading $name: $!
35
36                 # mutt pipes single RFC822 messages with a "From " line,
37                 # but no Content-Length or "From " escaping.
38                 # "git format-patch" also generates such files by default.
39                 $buf =~ s/\A[\r\n]*From [^\r\n]*\r?\n//s;
40                 $self->input_eml_cb(PublicInbox::Eml->new(\$buf), @args);
41         } else {
42                 # prepare_inputs already validated $ifmt
43                 my $cb = PublicInbox::MboxReader->reads($ifmt) //
44                                 die "BUG: bad fmt=$ifmt";
45                 $cb->(undef, $fh, $self->can('input_mbox_cb'), $self, @args);
46         }
47 }
48
49 sub input_path_url {
50         my ($self, $input, @args) = @_;
51         my $lei = $self->{lei};
52         my $ifmt = lc($lei->{opt}->{'in-format'} // '');
53         # TODO auto-detect?
54         if ($input =~ m!\Aimaps?://!i) {
55                 $lei->{net}->imap_each($input, $self->can('input_net_cb'),
56                                         $self, @args);
57                 return;
58         } elsif ($input =~ m!\A(?:nntps?|s?news)://!i) {
59                 $lei->{net}->nntp_each($input, $self->can('input_net_cb'),
60                                         $self, @args);
61                 return;
62         }
63         if ($input =~ s!\A([a-z0-9]+):!!i) {
64                 $ifmt = lc($1);
65         } elsif ($input =~ /\.(?:patch|eml)\z/i) {
66                 $ifmt = 'eml';
67         }
68         my $devfd = $lei->path_to_fd($input) // return;
69         if ($devfd >= 0) {
70                 $self->input_fh($ifmt, $lei->{$devfd}, $input, @args);
71         } elsif (-f $input && $ifmt eq 'eml') {
72                 open my $fh, '<', $input or
73                                         return $lei->fail("open($input): $!");
74                 $self->input_fh($ifmt, $fh, $input, @args);
75         } elsif (-f _) {
76                 my $m = $lei->{opt}->{'lock'} //
77                         PublicInbox::MboxLock->defaults;
78                 my $mbl = PublicInbox::MboxLock->acq($input, 0, $m);
79                 my $zsfx = PublicInbox::MboxReader::zsfx($input);
80                 if ($zsfx) {
81                         my $in = delete $mbl->{fh};
82                         $mbl->{fh} =
83                              PublicInbox::MboxReader::zsfxcat($in, $zsfx, $lei);
84                 }
85                 local $PublicInbox::DS::in_loop = 0 if $zsfx; # dwaitpid
86                 $self->input_fh($ifmt, $mbl->{fh}, $input, @args);
87         } elsif (-d _ && (-d "$input/cur" || -d "$input/new")) {
88                 return $lei->fail(<<EOM) if $ifmt && $ifmt ne 'maildir';
89 $input appears to a be a maildir, not $ifmt
90 EOM
91                 PublicInbox::MdirReader::maildir_each_eml($input,
92                                         $self->can('input_maildir_cb'),
93                                         $self, @args);
94         } else {
95                 $lei->fail("$input unsupported (TODO)");
96         }
97 }
98
99 sub prepare_inputs { # returns undef on error
100         my ($self, $lei, $inputs) = @_;
101         my $in_fmt = $lei->{opt}->{'in-format'};
102         if ($lei->{opt}->{stdin}) {
103                 @$inputs and return
104                         $lei->fail("--stdin and @$inputs do not mix");
105                 check_input_format($lei) or return;
106                 push @$inputs, '/dev/stdin';
107         }
108         my $net = $lei->{net}; # NetWriter may be created by l2m
109         my (@f, @d);
110         # e.g. Maildir:/home/user/Mail/ or imaps://example.com/INBOX
111         for my $input (@$inputs) {
112                 my $input_path = $input;
113                 if ($input =~ m!\A(?:imaps?|nntps?|s?news)://!i) {
114                         require PublicInbox::NetReader;
115                         $net //= PublicInbox::NetReader->new;
116                         $net->add_url($input);
117                 } elsif ($input_path =~ s/\A([a-z0-9]+)://is) {
118                         my $ifmt = lc $1;
119                         if (($in_fmt // $ifmt) ne $ifmt) {
120                                 return $lei->fail(<<"");
121 --in-format=$in_fmt and `$ifmt:' conflict
122
123                         }
124                         my $devfd = $lei->path_to_fd($input_path) // return;
125                         if ($devfd >= 0 || (-f $input_path || -p _)) {
126                                 require PublicInbox::MboxLock;
127                                 require PublicInbox::MboxReader;
128                                 PublicInbox::MboxReader->reads($ifmt) or return
129                                         $lei->fail("$ifmt not supported");
130                         } elsif (-d $input_path) {
131                                 require PublicInbox::MdirReader;
132                                 $ifmt eq 'maildir' or return
133                                         $lei->fail("$ifmt not supported");
134                         } else {
135                                 return $lei->fail("Unable to handle $input");
136                         }
137                 } elsif ($input =~ /\.(eml|patch)\z/i && -f $input) {
138                         lc($in_fmt//'eml') eq 'eml' or return $lei->fail(<<"");
139 $input is `eml', not --in-format=$in_fmt
140
141                         require PublicInbox::Eml;
142                 } else {
143                         my $devfd = $lei->path_to_fd($input) // return;
144                         if ($devfd >= 0 || -f $input || -p _) {
145                                 push @f, $input
146                         } elsif (-d $input) {
147                                 push @d, $input
148                         } else {
149                                 return $lei->fail("Unable to handle $input")
150                         }
151                 }
152         }
153         if (@f) { check_input_format($lei, \@f) or return }
154         if (@d) { # TODO: check for MH vs Maildir, here
155                 require PublicInbox::MdirReader;
156         }
157         if ($net) {
158                 if (my $err = $net->errors) {
159                         return $lei->fail($err);
160                 }
161                 $net->{quiet} = $lei->{opt}->{quiet};
162                 require PublicInbox::LeiAuth;
163                 $lei->{auth} //= PublicInbox::LeiAuth->new;
164                 $lei->{net} //= $net;
165         }
166         $self->{inputs} = $inputs;
167 }
168
169 sub process_inputs {
170         my ($self) = @_;
171         for my $input (@{$self->{inputs}}) {
172                 $self->input_path_url($input);
173         }
174         my $wait = $self->{lei}->{sto}->ipc_do('done') if $self->{lei}->{sto};
175 }
176
177 sub input_only_atfork_child {
178         my ($self) = @_;
179         my $lei = $self->{lei};
180         $lei->_lei_atfork_child;
181         PublicInbox::IPC::ipc_atfork_child($self);
182         $lei->{auth}->do_auth_atfork($self) if $lei->{auth};
183         undef;
184 }
185
186 1;