]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/LeiInput.pm
lei mark: command for (un)setting keywords and labels
[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 ->eml_cb and ->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->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('mbox_cb'), $self, @args);
45         }
46 }
47
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}) {
52                 @$inputs and return
53                         $lei->fail("--stdin and @$inputs do not mix");
54                 check_input_format($lei) or return;
55                 $self->{0} = $lei->{0};
56         }
57         my $net = $lei->{net}; # NetWriter may be created by l2m
58         my $fmt = $lei->{opt}->{'in-format'};
59         my (@f, @d);
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) {
68                         my $ifmt = lc $1;
69                         if (($in_fmt // $ifmt) ne $ifmt) {
70                                 return $lei->fail(<<"");
71 --in-format=$in_fmt and `$ifmt:' conflict
72
73                         }
74                         if (-f $input_path) {
75                                 require PublicInbox::MboxLock;
76                                 require PublicInbox::MboxReader;
77                                 PublicInbox::MboxReader->reads($ifmt) or return
78                                         $lei->fail("$ifmt not supported");
79                         } elsif (-d _) {
80                                 require PublicInbox::MdirReader;
81                                 $ifmt eq 'maildir' or return
82                                         $lei->fail("$ifmt not supported");
83                         } else {
84                                 return $lei->fail("Unable to handle $input");
85                         }
86                 } elsif (-f $input) { push @f, $input }
87                 elsif (-d _) { push @d, $input }
88                 else { return $lei->fail("Unable to handle $input") }
89         }
90         if (@f) { check_input_format($lei, \@f) or return }
91         if (@d) { # TODO: check for MH vs Maildir, here
92                 require PublicInbox::MdirReader;
93         }
94         if ($net) {
95                 if (my $err = $net->errors) {
96                         return $lei->fail($err);
97                 }
98                 $net->{quiet} = $lei->{opt}->{quiet};
99                 require PublicInbox::LeiAuth;
100                 $lei->{auth} //= PublicInbox::LeiAuth->new;
101                 $lei->{net} //= $net;
102         }
103         $self->{inputs} = $inputs;
104 }
105
106 sub input_only_atfork_child {
107         my ($self) = @_;
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};
112         undef;
113 }
114
115 1;