1 # Copyright (C) 2021 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 # parent class for LeiImport, LeiConvert
5 package PublicInbox::LeiInput;
9 sub check_input_format ($;$) {
10 my ($lei, $files) = @_;
11 my $opt_key = 'in-format';
12 my $fmt = $lei->{opt}->{$opt_key};
14 my $err = $files ? "regular file(s):\n@$files" : '--stdin';
15 return $lei->fail("--$opt_key unset for $err");
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");
26 # import a single file handle of $name
27 # Subclass must define ->eml_cb and ->mbox_cb
29 my ($self, $ifmt, $fh, $name, @args) = @_;
31 my $buf = do { local $/; <$fh> } //
32 return $self->{lei}->child_error(1 << 8, <<"");
33 error reading $name: $!
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->eml_cb(PublicInbox::Eml->new(\$buf), @args);
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('mbox_cb'), $self, @args);
48 sub prepare_inputs { # returns undef on error
49 my ($self, $lei, $inputs) = @_;
50 my $in_fmt = $lei->{opt}->{'in-format'};
51 if ($lei->{opt}->{stdin}) {
53 $lei->fail("--stdin and @$inputs do not mix");
54 check_input_format($lei) or return;
55 $self->{0} = $lei->{0};
57 my $net = $lei->{net}; # NetWriter may be created by l2m
58 my $fmt = $lei->{opt}->{'in-format'};
60 # e.g. Maildir:/home/user/Mail/ or imaps://example.com/INBOX
61 for my $input (@$inputs) {
62 my $input_path = $input;
63 if ($input =~ m!\A(?:imaps?|nntps?|s?news)://!i) {
64 require PublicInbox::NetReader;
65 $net //= PublicInbox::NetReader->new;
66 $net->add_url($input);
67 } elsif ($input_path =~ s/\A([a-z0-9]+)://is) {
69 if (($in_fmt // $ifmt) ne $ifmt) {
70 return $lei->fail(<<"");
71 --in-format=$in_fmt and `$ifmt:' conflict
75 require PublicInbox::MboxLock;
76 require PublicInbox::MboxReader;
77 PublicInbox::MboxReader->reads($ifmt) or return
78 $lei->fail("$ifmt not supported");
80 require PublicInbox::MdirReader;
81 $ifmt eq 'maildir' or return
82 $lei->fail("$ifmt not supported");
84 return $lei->fail("Unable to handle $input");
86 } elsif (-f $input) { push @f, $input }
87 elsif (-d _) { push @d, $input }
88 else { return $lei->fail("Unable to handle $input") }
90 if (@f) { check_input_format($lei, \@f) or return }
91 if (@d) { # TODO: check for MH vs Maildir, here
92 require PublicInbox::MdirReader;
95 if (my $err = $net->errors) {
96 return $lei->fail($err);
98 $net->{quiet} = $lei->{opt}->{quiet};
99 require PublicInbox::LeiAuth;
100 $lei->{auth} //= PublicInbox::LeiAuth->new;
101 $lei->{net} //= $net;
103 $self->{inputs} = $inputs;
106 sub input_only_atfork_child {
108 my $lei = $self->{lei};
109 $lei->_lei_atfork_child;
110 PublicInbox::IPC::ipc_atfork_child($self);
111 $lei->{auth}->do_auth_atfork($self) if $lei->{auth};