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