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 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");
26 # import a single file handle of $name
27 # Subclass must define ->input_eml_cb and ->input_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->input_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('input_mbox_cb'), $self, @args);
49 my ($self, $input, @args) = @_;
50 my $lei = $self->{lei};
51 my $ifmt = lc($lei->{opt}->{'in-format'} // '');
53 if ($input =~ m!\Aimaps?://!i) {
54 $lei->{net}->imap_each($input, $self->can('input_net_cb'),
57 } elsif ($input =~ m!\A(?:nntps?|s?news)://!i) {
58 $lei->{net}->nntp_each($input, $self->can('input_net_cb'),
62 if ($input =~ s!\A([a-z0-9]+):!!i) {
64 } elsif ($input =~ /\.(?:patch|eml)\z/i) {
67 my $devfd = $lei->path_to_fd($input) // return;
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);
75 my $m = $lei->{opt}->{'lock'} //
76 PublicInbox::MboxLock->defaults;
77 my $mbl = PublicInbox::MboxLock->acq($input, 0, $m);
78 my $zsfx = PublicInbox::MboxReader::zsfx($input);
80 my $in = delete $mbl->{fh};
82 PublicInbox::MboxReader::zsfxcat($in, $zsfx, $lei);
84 local $PublicInbox::DS::in_loop = 0 if $zsfx; # dwaitpid
85 $self->input_fh($ifmt, $mbl->{fh}, $input, @args);
86 } elsif (-d _ && (-d "$input/cur" || -d "$input/new")) {
87 return $lei->fail(<<EOM) if $ifmt && $ifmt ne 'maildir';
88 $input appears to a be a maildir, not $ifmt
90 PublicInbox::MdirReader::maildir_each_eml($input,
91 $self->can('input_maildir_cb'),
94 $lei->fail("$input unsupported (TODO)");
98 sub prepare_inputs { # returns undef on error
99 my ($self, $lei, $inputs) = @_;
100 my $in_fmt = $lei->{opt}->{'in-format'};
101 if ($lei->{opt}->{stdin}) {
103 $lei->fail("--stdin and @$inputs do not mix");
104 check_input_format($lei) or return;
105 push @$inputs, '/dev/stdin';
107 my $net = $lei->{net}; # NetWriter may be created by l2m
109 # e.g. Maildir:/home/user/Mail/ or imaps://example.com/INBOX
110 for my $input (@$inputs) {
111 my $input_path = $input;
112 if ($input =~ m!\A(?:imaps?|nntps?|s?news)://!i) {
113 require PublicInbox::NetReader;
114 $net //= PublicInbox::NetReader->new;
115 $net->add_url($input);
116 } elsif ($input_path =~ s/\A([a-z0-9]+)://is) {
118 if (($in_fmt // $ifmt) ne $ifmt) {
119 return $lei->fail(<<"");
120 --in-format=$in_fmt and `$ifmt:' conflict
123 my $devfd = $lei->path_to_fd($input_path) // return;
124 if ($devfd >= 0 || (-f $input_path || -p _)) {
125 require PublicInbox::MboxLock;
126 require PublicInbox::MboxReader;
127 PublicInbox::MboxReader->reads($ifmt) or return
128 $lei->fail("$ifmt not supported");
129 } elsif (-d $input_path) {
130 require PublicInbox::MdirReader;
131 $ifmt eq 'maildir' or return
132 $lei->fail("$ifmt not supported");
134 return $lei->fail("Unable to handle $input");
136 } elsif ($input =~ /\.(eml|patch)\z/i && -f $input) {
137 lc($in_fmt//'eml') eq 'eml' or return $lei->fail(<<"");
138 $input is `eml', not --in-format=$in_fmt
140 require PublicInbox::Eml;
142 my $devfd = $lei->path_to_fd($input) // return;
143 if ($devfd >= 0 || -f $input || -p _) {
145 } elsif (-d $input) {
148 return $lei->fail("Unable to handle $input")
152 if (@f) { check_input_format($lei, \@f) or return }
153 if (@d) { # TODO: check for MH vs Maildir, here
154 require PublicInbox::MdirReader;
157 if (my $err = $net->errors) {
158 return $lei->fail($err);
160 $net->{quiet} = $lei->{opt}->{quiet};
161 require PublicInbox::LeiAuth;
162 $lei->{auth} //= PublicInbox::LeiAuth->new;
163 $lei->{net} //= $net;
165 $self->{inputs} = $inputs;
168 sub input_only_atfork_child {
170 my $lei = $self->{lei};
171 $lei->_lei_atfork_child;
172 PublicInbox::IPC::ipc_atfork_child($self);
173 $lei->{auth}->do_auth_atfork($self) if $lei->{auth};