]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/LeiInput.pm
lei_input: avoid special case sub for --stdin
[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         require PublicInbox::MboxLock if $files;
18         require PublicInbox::MboxReader;
19         return 1 if $fmt eq 'eml';
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                 require PublicInbox::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         $input =~ s!\A([a-z0-9]+):!!i and $ifmt = lc($1);
64         my $devfd = $lei->path_to_fd($input) // return;
65         if ($devfd >= 0) {
66                 $self->input_fh($ifmt, $lei->{$devfd}, $input, @args);
67         } elsif (-f $input) {
68                 my $m = $lei->{opt}->{'lock'} // ($ifmt eq 'eml' ? ['none'] :
69                                 PublicInbox::MboxLock->defaults);
70                 my $mbl = PublicInbox::MboxLock->acq($input, 0, $m);
71                 $self->input_fh($ifmt, $mbl->{fh}, $input, @args);
72         } elsif (-d _ && (-d "$input/cur" || -d "$input/new")) {
73                 return $lei->fail(<<EOM) if $ifmt && $ifmt ne 'maildir';
74 $input appears to a be a maildir, not $ifmt
75 EOM
76                 PublicInbox::MdirReader::maildir_each_eml($input,
77                                         $self->can('input_maildir_cb'),
78                                         $self, @args);
79         } else {
80                 $lei->fail("$input unsupported (TODO)");
81         }
82 }
83
84 sub prepare_inputs { # returns undef on error
85         my ($self, $lei, $inputs) = @_;
86         my $in_fmt = $lei->{opt}->{'in-format'};
87         if ($lei->{opt}->{stdin}) {
88                 @$inputs and return
89                         $lei->fail("--stdin and @$inputs do not mix");
90                 check_input_format($lei) or return;
91                 push @$inputs, '/dev/stdin';
92         }
93         my $net = $lei->{net}; # NetWriter may be created by l2m
94         my $fmt = $lei->{opt}->{'in-format'};
95         my (@f, @d);
96         # e.g. Maildir:/home/user/Mail/ or imaps://example.com/INBOX
97         for my $input (@$inputs) {
98                 my $input_path = $input;
99                 if ($input =~ m!\A(?:imaps?|nntps?|s?news)://!i) {
100                         require PublicInbox::NetReader;
101                         $net //= PublicInbox::NetReader->new;
102                         $net->add_url($input);
103                 } elsif ($input_path =~ s/\A([a-z0-9]+)://is) {
104                         my $ifmt = lc $1;
105                         if (($in_fmt // $ifmt) ne $ifmt) {
106                                 return $lei->fail(<<"");
107 --in-format=$in_fmt and `$ifmt:' conflict
108
109                         }
110                         my $devfd = $lei->path_to_fd($input_path) // return;
111                         if ($devfd >= 0 || (-f $input_path || -p _)) {
112                                 require PublicInbox::MboxLock;
113                                 require PublicInbox::MboxReader;
114                                 PublicInbox::MboxReader->reads($ifmt) or return
115                                         $lei->fail("$ifmt not supported");
116                         } elsif (-d $input_path) {
117                                 require PublicInbox::MdirReader;
118                                 $ifmt eq 'maildir' or return
119                                         $lei->fail("$ifmt not supported");
120                         } else {
121                                 return $lei->fail("Unable to handle $input");
122                         }
123                 } else {
124                         my $devfd = $lei->path_to_fd($input) // return;
125                         if ($devfd >= 0 || -f $input || -p _) {
126                                 push @f, $input
127                         } elsif (-d $input) {
128                                 push @d, $input
129                         } else {
130                                 return $lei->fail("Unable to handle $input")
131                         }
132                 }
133         }
134         if (@f) { check_input_format($lei, \@f) or return }
135         if (@d) { # TODO: check for MH vs Maildir, here
136                 require PublicInbox::MdirReader;
137         }
138         if ($net) {
139                 if (my $err = $net->errors) {
140                         return $lei->fail($err);
141                 }
142                 $net->{quiet} = $lei->{opt}->{quiet};
143                 require PublicInbox::LeiAuth;
144                 $lei->{auth} //= PublicInbox::LeiAuth->new;
145                 $lei->{net} //= $net;
146         }
147         $self->{inputs} = $inputs;
148 }
149
150 sub input_only_atfork_child {
151         my ($self) = @_;
152         my $lei = $self->{lei};
153         $lei->_lei_atfork_child;
154         PublicInbox::IPC::ipc_atfork_child($self);
155         $lei->{auth}->do_auth_atfork($self) if $lei->{auth};
156         undef;
157 }
158
159 1;