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 $self->input_fh($ifmt, $mbl->{fh}, $input, @args);
79 } elsif (-d _ && (-d "$input/cur" || -d "$input/new")) {
80 return $lei->fail(<<EOM) if $ifmt && $ifmt ne 'maildir';
81 $input appears to a be a maildir, not $ifmt
83 PublicInbox::MdirReader::maildir_each_eml($input,
84 $self->can('input_maildir_cb'),
87 $lei->fail("$input unsupported (TODO)");
91 sub prepare_inputs { # returns undef on error
92 my ($self, $lei, $inputs) = @_;
93 my $in_fmt = $lei->{opt}->{'in-format'};
94 if ($lei->{opt}->{stdin}) {
96 $lei->fail("--stdin and @$inputs do not mix");
97 check_input_format($lei) or return;
98 push @$inputs, '/dev/stdin';
100 my $net = $lei->{net}; # NetWriter may be created by l2m
102 # e.g. Maildir:/home/user/Mail/ or imaps://example.com/INBOX
103 for my $input (@$inputs) {
104 my $input_path = $input;
105 if ($input =~ m!\A(?:imaps?|nntps?|s?news)://!i) {
106 require PublicInbox::NetReader;
107 $net //= PublicInbox::NetReader->new;
108 $net->add_url($input);
109 } elsif ($input_path =~ s/\A([a-z0-9]+)://is) {
111 if (($in_fmt // $ifmt) ne $ifmt) {
112 return $lei->fail(<<"");
113 --in-format=$in_fmt and `$ifmt:' conflict
116 my $devfd = $lei->path_to_fd($input_path) // return;
117 if ($devfd >= 0 || (-f $input_path || -p _)) {
118 require PublicInbox::MboxLock;
119 require PublicInbox::MboxReader;
120 PublicInbox::MboxReader->reads($ifmt) or return
121 $lei->fail("$ifmt not supported");
122 } elsif (-d $input_path) {
123 require PublicInbox::MdirReader;
124 $ifmt eq 'maildir' or return
125 $lei->fail("$ifmt not supported");
127 return $lei->fail("Unable to handle $input");
129 } elsif ($input =~ /\.(eml|patch)\z/i && -f $input) {
130 lc($in_fmt//'eml') eq 'eml' or return $lei->fail(<<"");
131 $input is `eml', not --in-format=$in_fmt
133 require PublicInbox::Eml;
135 my $devfd = $lei->path_to_fd($input) // return;
136 if ($devfd >= 0 || -f $input || -p _) {
138 } elsif (-d $input) {
141 return $lei->fail("Unable to handle $input")
145 if (@f) { check_input_format($lei, \@f) or return }
146 if (@d) { # TODO: check for MH vs Maildir, here
147 require PublicInbox::MdirReader;
150 if (my $err = $net->errors) {
151 return $lei->fail($err);
153 $net->{quiet} = $lei->{opt}->{quiet};
154 require PublicInbox::LeiAuth;
155 $lei->{auth} //= PublicInbox::LeiAuth->new;
156 $lei->{net} //= $net;
158 $self->{inputs} = $inputs;
161 sub input_only_atfork_child {
163 my $lei = $self->{lei};
164 $lei->_lei_atfork_child;
165 PublicInbox::IPC::ipc_atfork_child($self);
166 $lei->{auth}->do_auth_atfork($self) if $lei->{auth};