]> 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 5e2e61afcb0610156d75dab41e32971196bf281a..13e817d0b7a54b9fc2057f1f6c7502616da3d730 100644 (file)
@@ -7,7 +7,6 @@ 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
@@ -29,20 +28,7 @@ sub import_done { # EOF callback for main daemon
        $imp->wq_wait_old(\&import_done_wait, $lei);
 }
 
-sub net_merge_all { # via wq_broadcast
-       my ($self, $net_new) = @_;
-       my $net = $self->{lei}->{net};
-       %$net = (%$net, %$net_new);
-       pkt_do($self->{lei}->{pkt_op_p}, 'net_merge_done1') or
-               die "pkt_op_do net_merge_done1: $!";
-}
-
-sub net_merge_continue { # first worker is done with auth
-       my ($self, $net_new) = @_;
-       $self->wq_broadcast('net_merge_all', $net_new);
-}
-
-sub net_merge_complete {
+sub net_merge_complete { # callback used by LeiAuth
        my ($self) = @_;
        for my $input (@{$self->{inputs}}) {
                $self->wq_io_do('import_path_url', [], $input);
@@ -50,13 +36,6 @@ sub net_merge_complete {
        $self->wq_close(1);
 }
 
-sub net_merge_done1 {
-       my ($self) = @_;
-       my $lei = $self->{lei};
-       return if ++$lei->{nr_net_merge_done} != $self->{-wq_nr_workers};
-       net_merge_complete($self);
-}
-
 sub import_start {
        my ($lei) = @_;
        my $self = $lei->{imp};
@@ -68,15 +47,11 @@ sub import_start {
                $j = $nproc if $j > $nproc;
        }
        my $ops = { '' => [ \&import_done, $lei ] };
-       my $auth = $lei->{auth};
-       if ($auth) {
-               $ops->{net_merge} = [ \&net_merge_continue, $self ];
-               $ops->{net_merge_done1} = [ \&net_merge_done1, $self ];
-       }
+       $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};
-       net_merge_complete($self) if !$auth;
+       net_merge_complete($self) unless $lei->{auth};
        while ($op && $op->{sock}) { $op->event_step }
 }
 
@@ -98,7 +73,7 @@ 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;
                        $net //= PublicInbox::NetReader->new;
                        $net->add_url($input);
@@ -136,7 +111,7 @@ sub call { # the main "lei import" method
                $net->{quiet} = $lei->{opt}->{quiet};
                $lei->{net} = $net;
                require PublicInbox::LeiAuth;
-               $lei->{auth} = PublicInbox::LeiAuth->new($net);
+               $lei->{auth} = PublicInbox::LeiAuth->new;
        }
        import_start($lei);
 }
@@ -147,12 +122,7 @@ sub ipc_atfork_child {
        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;
-       }
+       $lei->{auth}->do_auth_atfork($self) if $lei->{auth};
        undef;
 }
 
@@ -181,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 : ());
 }
 
@@ -192,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->{net}->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;
        }
@@ -222,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;