]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/LeiInput.pm
c04fc2f84d1566bc1a609fe5b771d27ca3fb8761
[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                 $self->input_fh($ifmt, $mbl->{fh}, $input, @args);
79         } elsif (-d _ && (-d "$input/cur" || -d "$input/new")) {
80                 return $lei->fail(<<EOM) if $ifmt && $ifmt ne 'maildir';
81 $input appears to a be a maildir, not $ifmt
82 EOM
83                 PublicInbox::MdirReader::maildir_each_eml($input,
84                                         $self->can('input_maildir_cb'),
85                                         $self, @args);
86         } else {
87                 $lei->fail("$input unsupported (TODO)");
88         }
89 }
90
91 sub prepare_inputs { # returns undef on error
92         my ($self, $lei, $inputs) = @_;
93         my $in_fmt = $lei->{opt}->{'in-format'};
94         if ($lei->{opt}->{stdin}) {
95                 @$inputs and return
96                         $lei->fail("--stdin and @$inputs do not mix");
97                 check_input_format($lei) or return;
98                 push @$inputs, '/dev/stdin';
99         }
100         my $net = $lei->{net}; # NetWriter may be created by l2m
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                 } elsif ($input =~ /\.(eml|patch)\z/i && -f $input) {
130                         lc($in_fmt//'eml') eq 'eml' or return $lei->fail(<<"");
131 $input is `eml', not --in-format=$in_fmt
132
133                         require PublicInbox::Eml;
134                 } else {
135                         my $devfd = $lei->path_to_fd($input) // return;
136                         if ($devfd >= 0 || -f $input || -p _) {
137                                 push @f, $input
138                         } elsif (-d $input) {
139                                 push @d, $input
140                         } else {
141                                 return $lei->fail("Unable to handle $input")
142                         }
143                 }
144         }
145         if (@f) { check_input_format($lei, \@f) or return }
146         if (@d) { # TODO: check for MH vs Maildir, here
147                 require PublicInbox::MdirReader;
148         }
149         if ($net) {
150                 if (my $err = $net->errors) {
151                         return $lei->fail($err);
152                 }
153                 $net->{quiet} = $lei->{opt}->{quiet};
154                 require PublicInbox::LeiAuth;
155                 $lei->{auth} //= PublicInbox::LeiAuth->new;
156                 $lei->{net} //= $net;
157         }
158         $self->{inputs} = $inputs;
159 }
160
161 sub input_only_atfork_child {
162         my ($self) = @_;
163         my $lei = $self->{lei};
164         $lei->_lei_atfork_child;
165         PublicInbox::IPC::ipc_atfork_child($self);
166         $lei->{auth}->do_auth_atfork($self) if $lei->{auth};
167         undef;
168 }
169
170 1;