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 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");
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 require PublicInbox::Eml;
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);
51 my $in = delete $self->{0} or return;
52 $self->input_fh($self->{lei}->{opt}->{'in-format'}, $in, '<stdin>');
56 my ($self, $input, @args) = @_;
57 my $lei = $self->{lei};
58 my $ifmt = lc($lei->{opt}->{'in-format'} // '');
60 if ($input =~ m!\Aimaps?://!i) {
61 $lei->{net}->imap_each($input, $self->can('input_net_cb'),
64 } elsif ($input =~ m!\A(?:nntps?|s?news)://!i) {
65 $lei->{net}->nntp_each($input, $self->can('input_net_cb'),
69 $input =~ s!\A([a-z0-9]+):!!i and $ifmt = lc($1);
71 my $m = $lei->{opt}->{'lock'} // ($ifmt eq 'eml' ? ['none'] :
72 PublicInbox::MboxLock->defaults);
73 my $mbl = PublicInbox::MboxLock->acq($input, 0, $m);
74 $self->input_fh($ifmt, $mbl->{fh}, $input, @args);
75 } elsif (-d _ && (-d "$input/cur" || -d "$input/new")) {
76 return $lei->fail(<<EOM) if $ifmt && $ifmt ne 'maildir';
77 $input appears to a be a maildir, not $ifmt
79 PublicInbox::MdirReader::maildir_each_eml($input,
80 $self->can('input_maildir_cb'),
83 $lei->fail("$input unsupported (TODO)");
87 sub prepare_inputs { # returns undef on error
88 my ($self, $lei, $inputs) = @_;
89 my $in_fmt = $lei->{opt}->{'in-format'};
90 if ($lei->{opt}->{stdin}) {
92 $lei->fail("--stdin and @$inputs do not mix");
93 check_input_format($lei) or return;
94 $self->{0} = $lei->{0};
96 my $net = $lei->{net}; # NetWriter may be created by l2m
97 my $fmt = $lei->{opt}->{'in-format'};
99 # e.g. Maildir:/home/user/Mail/ or imaps://example.com/INBOX
100 for my $input (@$inputs) {
101 my $input_path = $input;
102 if ($input =~ m!\A(?:imaps?|nntps?|s?news)://!i) {
103 require PublicInbox::NetReader;
104 $net //= PublicInbox::NetReader->new;
105 $net->add_url($input);
106 } elsif ($input_path =~ s/\A([a-z0-9]+)://is) {
108 if (($in_fmt // $ifmt) ne $ifmt) {
109 return $lei->fail(<<"");
110 --in-format=$in_fmt and `$ifmt:' conflict
113 if (-f $input_path) {
114 require PublicInbox::MboxLock;
115 require PublicInbox::MboxReader;
116 PublicInbox::MboxReader->reads($ifmt) or return
117 $lei->fail("$ifmt not supported");
119 require PublicInbox::MdirReader;
120 $ifmt eq 'maildir' or return
121 $lei->fail("$ifmt not supported");
123 return $lei->fail("Unable to handle $input");
125 } elsif (-f $input) { push @f, $input }
126 elsif (-d _) { push @d, $input }
127 else { return $lei->fail("Unable to handle $input") }
129 if (@f) { check_input_format($lei, \@f) or return }
130 if (@d) { # TODO: check for MH vs Maildir, here
131 require PublicInbox::MdirReader;
134 if (my $err = $net->errors) {
135 return $lei->fail($err);
137 $net->{quiet} = $lei->{opt}->{quiet};
138 require PublicInbox::LeiAuth;
139 $lei->{auth} //= PublicInbox::LeiAuth->new;
140 $lei->{net} //= $net;
142 $self->{inputs} = $inputs;
145 sub input_only_atfork_child {
147 my $lei = $self->{lei};
148 $lei->_lei_atfork_child;
149 PublicInbox::IPC::ipc_atfork_child($self);
150 $lei->{auth}->do_auth_atfork($self) if $lei->{auth};