-sub call { # the main "lei import" method
- my ($cls, $lei, @inputs) = @_;
- my $sto = $lei->_lei_store(1);
- $sto->write_prepare($lei);
- my ($net, @f, @d);
- $lei->{opt}->{kw} //= 1;
- my $self = $lei->{imp} = bless { inputs => \@inputs }, $cls;
- if ($lei->{opt}->{stdin}) {
- @inputs and return $lei->fail("--stdin and @inputs do not mix");
- $lei->check_input_format or return;
- $self->{0} = $lei->{0};
- }
-
- # TODO: do we need --format for non-stdin?
- my $fmt = $lei->{opt}->{'format'};
- # e.g. Maildir:/home/user/Mail/ or imaps://example.com/INBOX
- for my $input (@inputs) {
- my $input_path = $input;
- if ($input =~ m!\A(?:imap|nntp)s?://!i) {
- require PublicInbox::NetReader;
- $net //= PublicInbox::NetReader->new;
- $net->add_url($input);
- } elsif ($input_path =~ s/\A([a-z0-9]+)://is) {
- my $ifmt = lc $1;
- if (($fmt // $ifmt) ne $ifmt) {
- return $lei->fail(<<"");
---format=$fmt and `$ifmt:' conflict
-
- }
- if (-f $input_path) {
- require PublicInbox::MboxReader;
- PublicInbox::MboxReader->can($ifmt) or return
- $lei->fail("$ifmt not supported");
- } elsif (-d _) {
- require PublicInbox::MdirReader;
- $ifmt eq 'maildir' or return
- $lei->fail("$ifmt not supported");
- } else {
- return $lei->fail("Unable to handle $input");
- }
- } elsif (-f $input) { push @f, $input
- } elsif (-d _) { push @d, $input
- } else { return $lei->fail("Unable to handle $input") }
- }
- if (@f) { $lei->check_input_format(\@f) or return }
- if (@d) { # TODO: check for MH vs Maildir, here
- require PublicInbox::MdirReader;
- }
- $self->{inputs} = \@inputs;
- if ($net) {
- if (my $err = $net->errors) {
- return $lei->fail($err);
- }
- $net->{quiet} = $lei->{opt}->{quiet};
- $lei->{net} = $net;
- require PublicInbox::LeiAuth;
- $lei->{auth} = PublicInbox::LeiAuth->new($net);
- }
- import_start($lei);
-}
-
-sub ipc_atfork_child {
- my ($self) = @_;
- my $lei = $self->{lei};
- delete $lei->{imp}; # drop circular ref
- $lei->lei_atfork_child;
- $self->SUPER::ipc_atfork_child;
- my $net = $lei->{net};
- if ($net && $self->{-wq_worker_nr} == 0) {
- my $mics = $net->imap_common_init($lei);
- PublicInbox::LeiAuth::net_merge($lei, $net);
- $net->{mics_cached} = $mics;
- }
- undef;
-}
-
-sub _import_fh {
- my ($lei, $fh, $input, $ifmt) = @_;
- my $set_kw = $lei->{opt}->{kw};
- eval {
- if ($ifmt eq 'eml') {
- my $buf = do { local $/; <$fh> } //
- return $lei->child_error(1 << 8, <<"");
-error reading $input: $!
-
- my $eml = PublicInbox::Eml->new(\$buf);
- _import_eml($eml, $lei->{sto}, $set_kw);
- } else { # some mbox (->can already checked in call);
- my $cb = PublicInbox::MboxReader->can($ifmt) //
- die "BUG: bad fmt=$ifmt";
- $cb->(undef, $fh, \&_import_eml, $lei->{sto}, $set_kw);
- }
- };
- $lei->child_error(1 << 8, "<stdin>: $@") if $@;