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->can($fmt) or
22 return $lei->fail("--$opt_key=$fmt unrecognized");
28 my ($self, $lei, $inputs) = @_;
29 my $in_fmt = $lei->{opt}->{'in-format'};
30 if ($lei->{opt}->{stdin}) {
32 $lei->fail("--stdin and @$inputs do not mix");
33 check_input_format($lei) or return;
34 $self->{0} = $lei->{0};
36 my $net = $lei->{net}; # NetWriter may be created by l2m
37 my $fmt = $lei->{opt}->{'in-format'};
39 # e.g. Maildir:/home/user/Mail/ or imaps://example.com/INBOX
40 for my $input (@$inputs) {
41 my $input_path = $input;
42 if ($input =~ m!\A(?:imaps?|nntps?|s?news)://!i) {
43 require PublicInbox::NetReader;
44 $net //= PublicInbox::NetReader->new;
45 $net->add_url($input);
46 } elsif ($input_path =~ s/\A([a-z0-9]+)://is) {
48 if (($in_fmt // $ifmt) ne $ifmt) {
49 return $lei->fail(<<"");
50 --in-format=$in_fmt and `$ifmt:' conflict
54 require PublicInbox::MboxLock;
55 require PublicInbox::MboxReader;
56 PublicInbox::MboxReader->can($ifmt) or return
57 $lei->fail("$ifmt not supported");
59 require PublicInbox::MdirReader;
60 $ifmt eq 'maildir' or return
61 $lei->fail("$ifmt not supported");
63 return $lei->fail("Unable to handle $input");
65 } elsif (-f $input) { push @f, $input }
66 elsif (-d _) { push @d, $input }
67 else { return $lei->fail("Unable to handle $input") }
69 if (@f) { check_input_format($lei, \@f) or return }
70 if (@d) { # TODO: check for MH vs Maildir, here
71 require PublicInbox::MdirReader;
74 if (my $err = $net->errors) {
75 return $lei->fail($err);
77 $net->{quiet} = $lei->{opt}->{quiet};
78 require PublicInbox::LeiAuth;
79 $lei->{auth} //= PublicInbox::LeiAuth->new;
82 $self->{inputs} = $inputs;