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);
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 $input =~ s!\A([a-z0-9]+):!!i and $ifmt = lc($1);
64 my $devfd = $lei->path_to_fd($input) // return;
66 $self->input_fh($ifmt, $lei->{$devfd}, $input, @args);
68 my $m = $lei->{opt}->{'lock'} // ($ifmt eq 'eml' ? ['none'] :
69 PublicInbox::MboxLock->defaults);
70 my $mbl = PublicInbox::MboxLock->acq($input, 0, $m);
71 $self->input_fh($ifmt, $mbl->{fh}, $input, @args);
72 } elsif (-d _ && (-d "$input/cur" || -d "$input/new")) {
73 return $lei->fail(<<EOM) if $ifmt && $ifmt ne 'maildir';
74 $input appears to a be a maildir, not $ifmt
76 PublicInbox::MdirReader::maildir_each_eml($input,
77 $self->can('input_maildir_cb'),
80 $lei->fail("$input unsupported (TODO)");
84 sub prepare_inputs { # returns undef on error
85 my ($self, $lei, $inputs) = @_;
86 my $in_fmt = $lei->{opt}->{'in-format'};
87 if ($lei->{opt}->{stdin}) {
89 $lei->fail("--stdin and @$inputs do not mix");
90 check_input_format($lei) or return;
91 push @$inputs, '/dev/stdin';
93 my $net = $lei->{net}; # NetWriter may be created by l2m
94 my $fmt = $lei->{opt}->{'in-format'};
96 # e.g. Maildir:/home/user/Mail/ or imaps://example.com/INBOX
97 for my $input (@$inputs) {
98 my $input_path = $input;
99 if ($input =~ m!\A(?:imaps?|nntps?|s?news)://!i) {
100 require PublicInbox::NetReader;
101 $net //= PublicInbox::NetReader->new;
102 $net->add_url($input);
103 } elsif ($input_path =~ s/\A([a-z0-9]+)://is) {
105 if (($in_fmt // $ifmt) ne $ifmt) {
106 return $lei->fail(<<"");
107 --in-format=$in_fmt and `$ifmt:' conflict
110 my $devfd = $lei->path_to_fd($input_path) // return;
111 if ($devfd >= 0 || (-f $input_path || -p _)) {
112 require PublicInbox::MboxLock;
113 require PublicInbox::MboxReader;
114 PublicInbox::MboxReader->reads($ifmt) or return
115 $lei->fail("$ifmt not supported");
116 } elsif (-d $input_path) {
117 require PublicInbox::MdirReader;
118 $ifmt eq 'maildir' or return
119 $lei->fail("$ifmt not supported");
121 return $lei->fail("Unable to handle $input");
124 my $devfd = $lei->path_to_fd($input) // return;
125 if ($devfd >= 0 || -f $input || -p _) {
127 } elsif (-d $input) {
130 return $lei->fail("Unable to handle $input")
134 if (@f) { check_input_format($lei, \@f) or return }
135 if (@d) { # TODO: check for MH vs Maildir, here
136 require PublicInbox::MdirReader;
139 if (my $err = $net->errors) {
140 return $lei->fail($err);
142 $net->{quiet} = $lei->{opt}->{quiet};
143 require PublicInbox::LeiAuth;
144 $lei->{auth} //= PublicInbox::LeiAuth->new;
145 $lei->{net} //= $net;
147 $self->{inputs} = $inputs;
150 sub input_only_atfork_child {
152 my $lei = $self->{lei};
153 $lei->_lei_atfork_child;
154 PublicInbox::IPC::ipc_atfork_child($self);
155 $lei->{auth}->do_auth_atfork($self) if $lei->{auth};