]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/LeiInput.pm
lei: share input code between convert and import
[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->can($fmt) or
22                 return $lei->fail("--$opt_key=$fmt unrecognized");
23         1;
24 }
25
26
27 sub prepare_inputs {
28         my ($self, $lei, $inputs) = @_;
29         my $in_fmt = $lei->{opt}->{'in-format'};
30         if ($lei->{opt}->{stdin}) {
31                 @$inputs and return
32                         $lei->fail("--stdin and @$inputs do not mix");
33                 check_input_format($lei) or return;
34                 $self->{0} = $lei->{0};
35         }
36         my $net = $lei->{net}; # NetWriter may be created by l2m
37         my $fmt = $lei->{opt}->{'in-format'};
38         my (@f, @d);
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) {
47                         my $ifmt = lc $1;
48                         if (($in_fmt // $ifmt) ne $ifmt) {
49                                 return $lei->fail(<<"");
50 --in-format=$in_fmt and `$ifmt:' conflict
51
52                         }
53                         if (-f $input_path) {
54                                 require PublicInbox::MboxLock;
55                                 require PublicInbox::MboxReader;
56                                 PublicInbox::MboxReader->can($ifmt) or return
57                                         $lei->fail("$ifmt not supported");
58                         } elsif (-d _) {
59                                 require PublicInbox::MdirReader;
60                                 $ifmt eq 'maildir' or return
61                                         $lei->fail("$ifmt not supported");
62                         } else {
63                                 return $lei->fail("Unable to handle $input");
64                         }
65                 } elsif (-f $input) { push @f, $input }
66                 elsif (-d _) { push @d, $input }
67                 else { return $lei->fail("Unable to handle $input") }
68         }
69         if (@f) { check_input_format($lei, \@f) or return }
70         if (@d) { # TODO: check for MH vs Maildir, here
71                 require PublicInbox::MdirReader;
72         }
73         if ($net) {
74                 if (my $err = $net->errors) {
75                         return $lei->fail($err);
76                 }
77                 $net->{quiet} = $lei->{opt}->{quiet};
78                 require PublicInbox::LeiAuth;
79                 $lei->{auth} //= PublicInbox::LeiAuth->new;
80                 $lei->{net} //= $net;
81         }
82         $self->{inputs} = $inputs;
83 }
84
85 1;