]> 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 68cab12c819323787f4341cfc02de9d9d8538b46..13e817d0b7a54b9fc2057f1f6c7502616da3d730 100644 (file)
@@ -7,7 +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 qw(pkt_do);
 
 sub _import_eml { # MboxReader callback
        my ($eml, $sto, $set_kw) = @_;
@@ -28,24 +28,30 @@ 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 $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;
        }
-       my $op = $lei->workers_start($self, 'lei_import', $j, {
-               '' => [ \&import_done, $lei ],
-       });
+       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);
+       net_merge_complete($self) unless $lei->{auth};
        while ($op && $op->{sock}) { $op->event_step }
 }
 
@@ -53,7 +59,7 @@ 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}) {
@@ -67,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) {
@@ -98,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 {
@@ -142,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 : ());
 }
 
@@ -153,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;
        }
@@ -183,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;