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;
10 sub check_input_format ($;$) {
11 my ($lei, $files) = @_;
12 my $opt_key = 'in-format';
13 my $fmt = $lei->{opt}->{$opt_key};
15 my $err = $files ? "regular file(s):\n@$files" : '--stdin';
16 return $lei->fail("--$opt_key unset for $err");
18 return 1 if $fmt eq 'eml';
19 require PublicInbox::MboxLock if $files;
20 require PublicInbox::MboxReader;
21 # XXX: should this handle {gz,bz2,xz}? that's currently in LeiToMail
22 PublicInbox::MboxReader->reads($fmt) or
23 return $lei->fail("--$opt_key=$fmt unrecognized");
27 # import a single file handle of $name
28 # Subclass must define ->input_eml_cb and ->input_mbox_cb
30 my ($self, $ifmt, $fh, $name, @args) = @_;
32 my $buf = do { local $/; <$fh> } //
33 return $self->{lei}->child_error(1 << 8, <<"");
34 error reading $name: $!
36 # mutt pipes single RFC822 messages with a "From " line,
37 # but no Content-Length or "From " escaping.
38 # "git format-patch" also generates such files by default.
39 $buf =~ s/\A[\r\n]*From [^\r\n]*\r?\n//s;
40 $self->input_eml_cb(PublicInbox::Eml->new(\$buf), @args);
42 # prepare_inputs already validated $ifmt
43 my $cb = PublicInbox::MboxReader->reads($ifmt) //
44 die "BUG: bad fmt=$ifmt";
45 $cb->(undef, $fh, $self->can('input_mbox_cb'), $self, @args);
50 my ($self, $input, @args) = @_;
51 my $lei = $self->{lei};
52 my $ifmt = lc($lei->{opt}->{'in-format'} // '');
54 if ($input =~ m!\Aimaps?://!i) {
55 $lei->{net}->imap_each($input, $self->can('input_net_cb'),
58 } elsif ($input =~ m!\A(?:nntps?|s?news)://!i) {
59 $lei->{net}->nntp_each($input, $self->can('input_net_cb'),
63 if ($input =~ s!\A([a-z0-9]+):!!i) {
65 } elsif ($input =~ /\.(?:patch|eml)\z/i) {
68 my $devfd = $lei->path_to_fd($input) // return;
70 $self->input_fh($ifmt, $lei->{$devfd}, $input, @args);
71 } elsif (-f $input && $ifmt eq 'eml') {
72 open my $fh, '<', $input or
73 return $lei->fail("open($input): $!");
74 $self->input_fh($ifmt, $fh, $input, @args);
76 my $m = $lei->{opt}->{'lock'} //
77 PublicInbox::MboxLock->defaults;
78 my $mbl = PublicInbox::MboxLock->acq($input, 0, $m);
79 my $zsfx = PublicInbox::MboxReader::zsfx($input);
81 my $in = delete $mbl->{fh};
83 PublicInbox::MboxReader::zsfxcat($in, $zsfx, $lei);
85 local $PublicInbox::DS::in_loop = 0 if $zsfx; # dwaitpid
86 $self->input_fh($ifmt, $mbl->{fh}, $input, @args);
87 } elsif (-d _ && (-d "$input/cur" || -d "$input/new")) {
88 return $lei->fail(<<EOM) if $ifmt && $ifmt ne 'maildir';
89 $input appears to a be a maildir, not $ifmt
91 PublicInbox::MdirReader->new->maildir_each_eml($input,
92 $self->can('input_maildir_cb'),
95 $lei->fail("$input unsupported (TODO)");
99 sub prepare_inputs { # returns undef on error
100 my ($self, $lei, $inputs) = @_;
101 my $in_fmt = $lei->{opt}->{'in-format'};
102 if ($lei->{opt}->{stdin}) {
104 $lei->fail("--stdin and @$inputs do not mix");
105 check_input_format($lei) or return;
106 push @$inputs, '/dev/stdin';
108 my $net = $lei->{net}; # NetWriter may be created by l2m
110 # e.g. Maildir:/home/user/Mail/ or imaps://example.com/INBOX
111 for my $input (@$inputs) {
112 my $input_path = $input;
113 if ($input =~ m!\A(?:imaps?|nntps?|s?news)://!i) {
114 require PublicInbox::NetReader;
115 $net //= PublicInbox::NetReader->new;
116 $net->add_url($input);
117 } elsif ($input_path =~ s/\A([a-z0-9]+)://is) {
119 if (($in_fmt // $ifmt) ne $ifmt) {
120 return $lei->fail(<<"");
121 --in-format=$in_fmt and `$ifmt:' conflict
124 my $devfd = $lei->path_to_fd($input_path) // return;
125 if ($devfd >= 0 || (-f $input_path || -p _)) {
126 require PublicInbox::MboxLock;
127 require PublicInbox::MboxReader;
128 PublicInbox::MboxReader->reads($ifmt) or return
129 $lei->fail("$ifmt not supported");
130 } elsif (-d $input_path) {
131 require PublicInbox::MdirReader;
132 $ifmt eq 'maildir' or return
133 $lei->fail("$ifmt not supported");
135 return $lei->fail("Unable to handle $input");
137 } elsif ($input =~ /\.(eml|patch)\z/i && -f $input) {
138 lc($in_fmt//'eml') eq 'eml' or return $lei->fail(<<"");
139 $input is `eml', not --in-format=$in_fmt
141 require PublicInbox::Eml;
143 my $devfd = $lei->path_to_fd($input) // return;
144 if ($devfd >= 0 || -f $input || -p _) {
146 } elsif (-d $input) {
149 return $lei->fail("Unable to handle $input")
153 if (@f) { check_input_format($lei, \@f) or return }
154 if (@d) { # TODO: check for MH vs Maildir, here
155 require PublicInbox::MdirReader;
158 if (my $err = $net->errors) {
159 return $lei->fail($err);
161 $net->{quiet} = $lei->{opt}->{quiet};
162 require PublicInbox::LeiAuth;
163 $lei->{auth} //= PublicInbox::LeiAuth->new;
164 $lei->{net} //= $net;
166 $self->{inputs} = $inputs;
171 for my $input (@{$self->{inputs}}) {
172 $self->input_path_url($input);
174 my $wait = $self->{lei}->{sto}->ipc_do('done') if $self->{lei}->{sto};
177 sub input_only_atfork_child {
179 my $lei = $self->{lei};
180 $lei->_lei_atfork_child;
181 PublicInbox::IPC::ipc_atfork_child($self);
182 $lei->{auth}->do_auth_atfork($self) if $lei->{auth};