]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/LeiImport.pm
lei convert: support IMAP output and "-F eml" inputs
[public-inbox.git] / lib / PublicInbox / LeiImport.pm
index 62a2a412456542b9e128d74690978dd32734a675..13e817d0b7a54b9fc2057f1f6c7502616da3d730 100644 (file)
@@ -7,8 +7,7 @@ use strict;
 use v5.10.1;
 use parent qw(PublicInbox::IPC);
 use PublicInbox::Eml;
-use PublicInbox::InboxWritable qw(eml_from_path);
-use PublicInbox::PktOp;
+use PublicInbox::PktOp qw(pkt_do);
 
 sub _import_eml { # MboxReader callback
        my ($eml, $sto, $set_kw) = @_;
@@ -29,42 +28,38 @@ sub import_done { # EOF callback for main daemon
        $imp->wq_wait_old(\&import_done_wait, $lei);
 }
 
+sub net_merge_complete { # callback used by LeiAuth
+       my ($self) = @_;
+       for my $input (@{$self->{inputs}}) {
+               $self->wq_io_do('import_path_url', [], $input);
+       }
+       $self->wq_close(1);
+}
+
 sub import_start {
        my ($lei) = @_;
-       my $ops = {
-               '!' => [ $lei->can('fail_handler'), $lei ],
-               'x_it' => [ $lei->can('x_it'), $lei ],
-               'child_error' => [ $lei->can('child_error'), $lei ],
-               '' => [ \&import_done, $lei ],
-       };
-       ($lei->{pkt_op_c}, $lei->{pkt_op_p}) = PublicInbox::PktOp->pair($ops);
        my $self = $lei->{imp};
        my $j = $lei->{opt}->{jobs} // scalar(@{$self->{inputs}}) || 1;
-       if (my $nrd = $lei->{nrd}) {
-               # $j = $nrd->net_concurrency($j); TODO
+       if (my $net = $lei->{net}) {
+               # $j = $net->net_concurrency($j); TODO
        } else {
                my $nproc = $self->detect_nproc;
                $j = $nproc if $j > $nproc;
        }
-       $self->wq_workers_start('lei_import', $j, $lei->oldset, {lei => $lei});
-       my $op = delete $lei->{pkt_op_c};
-       delete $lei->{pkt_op_p};
+       my $ops = { '' => [ \&import_done, $lei ] };
+       $lei->{auth}->op_merge($ops, $self) if $lei->{auth};
+       $self->{-wq_nr_workers} = $j // 1; # locked
+       my $op = $lei->workers_start($self, 'lei_import', undef, $ops);
        $self->wq_io_do('import_stdin', []) if $self->{0};
-       for my $input (@{$self->{inputs}}) {
-               $self->wq_io_do('import_path_url', [], $input);
-       }
-       $self->wq_close(1);
-       $lei->event_step_init; # wait for shutdowns
-       if ($lei->{oneshot}) {
-               while ($op->{sock}) { $op->event_step }
-       }
+       net_merge_complete($self) unless $lei->{auth};
+       while ($op && $op->{sock}) { $op->event_step }
 }
 
 sub call { # the main "lei import" method
        my ($cls, $lei, @inputs) = @_;
        my $sto = $lei->_lei_store(1);
        $sto->write_prepare($lei);
-       my ($nrd, @f, @d);
+       my ($net, @f, @d);
        $lei->{opt}->{kw} //= 1;
        my $self = $lei->{imp} = bless { inputs => \@inputs }, $cls;
        if ($lei->{opt}->{stdin}) {
@@ -78,10 +73,10 @@ sub call { # the main "lei import" method
        # 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) {
+               if ($input =~ m!\A(?:imaps?|nntps?|s?news)://!i) {
                        require PublicInbox::NetReader;
-                       $nrd //= PublicInbox::NetReader->new;
-                       $nrd->add_url($input);
+                       $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) {
@@ -109,23 +104,26 @@ sub call { # the main "lei import" method
                require PublicInbox::MdirReader;
        }
        $self->{inputs} = \@inputs;
-       return import_start($lei) if !$nrd;
-
-       if (my $err = $nrd->errors) {
-               return $lei->fail($err);
+       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;
        }
-       $nrd->{quiet} = $lei->{opt}->{quiet};
-       $lei->{nrd} = $nrd;
-       require PublicInbox::LeiAuth;
-       my $auth = $lei->{auth} = PublicInbox::LeiAuth->new($nrd);
-       $auth->auth_start($lei, \&import_start, $lei);
+       import_start($lei);
 }
 
 sub ipc_atfork_child {
        my ($self) = @_;
-       delete $self->{lei}->{imp}; # drop circular ref
-       $self->{lei}->lei_atfork_child;
+       my $lei = $self->{lei};
+       delete $lei->{imp}; # drop circular ref
+       $lei->lei_atfork_child;
        $self->SUPER::ipc_atfork_child;
+       $lei->{auth}->do_auth_atfork($self) if $lei->{auth};
+       undef;
 }
 
 sub _import_fh {
@@ -153,9 +151,8 @@ sub _import_maildir { # maildir_each_file cb
        $sto->ipc_do('set_eml_from_maildir', $f, $set_kw);
 }
 
-sub _import_imap { # imap_each cb
+sub _import_net { # imap_each, nntp_each cb
        my ($url, $uid, $kw, $eml, $sto, $set_kw) = @_;
-       warn "$url $uid";
        $sto->ipc_do('set_eml', $eml, $set_kw ? @$kw : ());
 }
 
@@ -164,10 +161,13 @@ sub import_path_url {
        my $lei = $self->{lei};
        my $ifmt = lc($lei->{opt}->{'format'} // '');
        # TODO auto-detect?
-       if ($input =~ m!\A(imap|nntp)s?://!i) {
-               $lei->{nrd}->imap_each($input, \&_import_imap, $lei->{sto},
+       if ($input =~ m!\Aimaps?://!i) {
+               $lei->{net}->imap_each($input, \&_import_net, $lei->{sto},
                                        $lei->{opt}->{kw});
                return;
+       } elsif ($input =~ m!\A(?:nntps?|s?news)://!i) {
+               $lei->{net}->nntp_each($input, \&_import_net, $lei->{sto}, 0);
+               return;
        } elsif ($input =~ s!\A([a-z0-9]+):!!i) {
                $ifmt = lc $1;
        }
@@ -194,4 +194,6 @@ sub import_stdin {
        _import_fh($lei, delete $self->{0}, '<stdin>', $lei->{opt}->{'format'});
 }
 
+no warnings 'once'; # the following works even when LeiAuth is lazy-loaded
+*net_merge_all = \&PublicInbox::LeiAuth::net_merge_all;
 1;