]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/LeiConvert.pm
lei <import|convert>: support NNTP sources
[public-inbox.git] / lib / PublicInbox / LeiConvert.pm
index ba375772369065d725c08ec3d0cc845a68225b18..a7e4787125506134744440c8fc4e465d66abd504 100644 (file)
@@ -18,8 +18,8 @@ sub mbox_cb {
        $self->{wcb}->(undef, { kw => \@kw }, $eml);
 }
 
-sub imap_cb { # ->imap_each
-       my ($url, $uid, $kw, $eml, $self) = @_;
+sub net_cb { # callback for ->imap_each, ->nntp_each
+       my (undef, undef, $kw, $eml, $self) = @_; # @_[0,1]: url + uid ignored
        $self->{wcb}->(undef, { kw => $kw }, $eml);
 }
 
@@ -32,13 +32,21 @@ sub do_convert { # via wq_do
        my ($self) = @_;
        my $lei = $self->{lei};
        my $in_fmt = $lei->{opt}->{'in-format'};
+       my $mics;
+       if (my $nrd = $lei->{nrd}) { # may prompt user once
+               $nrd->{mics_cached} = $nrd->imap_common_init($lei);
+               $nrd->{nn_cached} = $nrd->nntp_common_init($lei);
+       }
        if (my $stdin = delete $self->{0}) {
                PublicInbox::MboxReader->$in_fmt($stdin, \&mbox_cb, $self);
        }
        for my $input (@{$self->{inputs}}) {
                my $ifmt = lc($in_fmt // '');
-               if ($input =~ m!\A(?:imap|nntp)s?://!) { # TODO: nntp
-                       $lei->{nrd}->imap_each($input, \&imap_cb, $self);
+               if ($input =~ m!\Aimaps?://!) {
+                       $lei->{nrd}->imap_each($input, \&net_cb, $self);
+                       next;
+               } elsif ($input =~ m!\A(?:nntps?|s?news)://!) {
+                       $lei->{nrd}->nntp_each($input, \&net_cb, $self);
                        next;
                } elsif ($input =~ s!\A([a-z0-9]+):!!i) {
                        $ifmt = lc $1;
@@ -58,17 +66,6 @@ sub do_convert { # via wq_do
        delete $self->{wcb}; # commit
 }
 
-sub convert_start { # LeiAuth->auth_start callback
-       my ($lei) = @_;
-       my $self = $lei->{cnv};
-       my $op = $lei->workers_start($self, 'lei_convert', 1, {
-               '' => [ $lei->can('dclose'), $lei ]
-       });
-       $self->wq_io_do('do_convert', []);
-       $self->wq_close(1);
-       while ($op && $op->{sock}) { $op->event_step }
-}
-
 sub call { # the main "lei convert" method
        my ($cls, $lei, @inputs) = @_;
        my $opt = $lei->{opt};
@@ -89,7 +86,7 @@ sub call { # the main "lei convert" 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);
@@ -120,16 +117,19 @@ sub call { # the main "lei convert" method
                require PublicInbox::MdirReader;
        }
        $self->{inputs} = \@inputs;
-       return convert_start($lei) if !$nrd;
-
-       if (my $err = $nrd->errors) {
-               return $lei->fail($err);
+       if ($nrd) {
+               if (my $err = $nrd->errors) {
+                       return $lei->fail($err);
+               }
+               $nrd->{quiet} = $opt->{quiet};
+               $lei->{nrd} = $nrd;
        }
-       $nrd->{quiet} = $opt->{quiet};
-       $lei->{nrd} = $nrd;
-       require PublicInbox::LeiAuth;
-       my $auth = $lei->{auth} = PublicInbox::LeiAuth->new($nrd);
-       $auth->auth_start($lei, \&convert_start, $lei);
+       my $op = $lei->workers_start($self, 'lei_convert', 1, {
+               '' => [ $lei->can('dclose'), $lei ]
+       });
+       $self->wq_io_do('do_convert', []);
+       $self->wq_close(1);
+       while ($op && $op->{sock}) { $op->event_step }
 }
 
 sub ipc_atfork_child {