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