]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/LeiLsMailSource.pm
lei: always pass $lei to LeiAuth->op_merge
[public-inbox.git] / lib / PublicInbox / LeiLsMailSource.pm
index e95c0b34cb5e4d0c9441e364a2ead66848bcb547..5eb7032d78b7252e47dd861c76f1e48ad14b7dad 100644 (file)
@@ -12,28 +12,36 @@ use parent qw(PublicInbox::IPC PublicInbox::LeiInput);
 sub input_path_url { # overrides LeiInput version
        my ($self, $url) = @_;
        # TODO: support ndjson and other JSONs we support elsewhere
-       my $json;
        my $lei = $self->{lei};
-       my $ORS = "\n";
-       if ($self->{lei}->{opt}->{l}) {
-               $json = ref(PublicInbox::Config->json)->new->utf8->canonical;
-               $json->ascii(1) if $lei->{opt}->{ascii};
-       } elsif ($self->{lei}->{opt}->{z}) {
-               $ORS = "\0";
-       }
+       my $json = $lei->{json};
+       my $ORS = $self->{lei}->{opt}->{z} ? "\0" : "\n";
+       my @f;
        if ($url =~ m!\Aimaps?://!i) {
                my $uri = PublicInbox::URIimap->new($url);
+               my $sec = $lei->{net}->can('uri_section')->($uri);
                my $mic = $lei->{net}->mic_get($uri);
                my $l = $mic->folders_hash($uri->path); # server-side filter
+               @$l = map { $_->[2] } # undo Schwartzian transform below:
+                       sort { $a->[0] cmp $b->[0] || $a->[1] <=> $b->[1] }
+                       map { # prepare to sort -imapd slices numerically
+                               $_->{name} =~ /\A(.+?)\.([0-9]+)\z/ ?
+                               [ $1, $2 + 0, $_ ] : [ $_->{name}, -1, $_ ];
+                       } @$l;
+               @f = map { "$sec/$_->{name}" } @$l;
                if ($json) {
+                       $_->{url} = "$sec/$_->{name}" for @$l;
                        $lei->puts($json->encode($l));
                } else {
+                       if ($self->{lei}->{opt}->{url}) {
+                               $_->{name} = "$sec/$_->{name}" for @$l;
+                       }
                        $lei->out(join($ORS, (map { $_->{name} } @$l), ''));
                }
        } elsif ($url =~ m!\A(?:nntps?|s?news)://!i) {
                my $uri = PublicInbox::URInntps->new($url);
                my $nn = $lei->{net}->nn_get($uri);
                my $l = $nn->newsgroups($uri->group); # name => description
+               my $sec = $lei->{net}->can('uri_section')->($uri);
                if ($json) {
                        my $all = $nn->list;
                        my @x;
@@ -45,16 +53,31 @@ sub input_path_url { # overrides LeiInput version
 # <https://rt.cpan.org/Ticket/Display.html?id=129966>
                                $desc =~ s/\r\z//;
 
-                               my ($hwm, $lwm, $status) = @{$all->{$ng}};
-                               push @x, { name => $ng, lwm => $lwm + 0,
-                                       hwm => $hwm + 0, status => $status,
+                               my ($high, $low, $status) = @{$all->{$ng}};
+                               push @x, { name => $ng, url => "$sec/$ng",
+                                       low => $low + 0,
+                                       high => $high + 0, status => $status,
                                        description => $desc };
                        }
+                       @f = map { "$sec/$_" } keys %$all;
                        $lei->puts($json->encode(\@x));
                } else {
-                       $lei->out(join($ORS, sort(keys %$l), ''));
+                       @f = map { "$sec/$_" } keys %$l;
+                       if ($self->{lei}->{opt}->{url}) {
+                               $lei->out(join($ORS, sort(@f), ''));
+                       } else {
+                               $lei->out(join($ORS, sort(keys %$l), ''));
+                       }
                }
        } else { die "BUG: $url not supported" }
+       if (@f) {
+               my $fc = $lei->url_folder_cache;
+               my $lk = $fc->lock_for_scope;
+               $fc->dbh->begin_work;
+               my $now = time;
+               $fc->set($_, $now) for @f;
+               $fc->dbh->commit;
+       }
 }
 
 sub lei_ls_mail_source {
@@ -62,21 +85,41 @@ sub lei_ls_mail_source {
        $url =~ m!\A(?:imaps?|nntps?|s?news)://!i or return
                $lei->fail('only NNTP and IMAP URLs supported');
        my $self = bless { pfx => $pfx, -ls_ok => 1 }, __PACKAGE__;
+       $self->{cfg} = $lei->_lei_cfg; # may be undef
        $self->prepare_inputs($lei, [ $url ]) or return;
-       $lei->start_pager if -t $lei->{1};
+       my $isatty = -t $lei->{1};
+       if ($lei->{opt}->{l}) {
+               my $json = ref(PublicInbox::Config->json)->new->utf8->canonical;
+               $lei->{json} = $json;
+               $json->ascii(1) if $lei->{opt}->{ascii};
+               $json->pretty(1)->indent(2) if $isatty || $lei->{opt}->{pretty};
+       }
+       $lei->start_pager if $isatty;
        my $ops = {};
-       $lei->{auth}->op_merge($ops, $self);
-       my $j = $self->{-wq_nr_workers} = 1; # locked
-       (my $op_c, $ops) = $lei->workers_start($self, $j, $ops);
+       $lei->{auth}->op_merge($ops, $self, $lei);
+       (my $op_c, $ops) = $lei->workers_start($self, 1, $ops);
        $lei->{wq1} = $self;
        $lei->{-err_type} = 'non-fatal';
        net_merge_all_done($self) unless $lei->{auth};
        $lei->wait_wq_events($op_c, $ops); # net_merge_all_done if !{auth}
 }
 
+sub _complete_ls_mail_source {
+       my ($lei, @argv) = @_;
+       my $match_cb = $lei->complete_url_prepare(\@argv);
+       my @k = $lei->url_folder_cache->keys($argv[-1] // undef, 1);
+       my @m = map { $match_cb->($_) } @k;
+       my %f = map { $_ => 1 } (@m ? @m : @k);
+       if (my $lms = $lei->lms) {
+               @k = $lms->folders($argv[-1] // undef, 1);
+               @m = map { $match_cb->($_) } grep(m!\A[a-z]+://!, @k);
+               if (@m) { @f{@m} = @m } else { @f{@k} = @k }
+       }
+       keys %f;
+}
+
 no warnings 'once';
 *ipc_atfork_child = \&PublicInbox::LeiInput::input_only_atfork_child;
 *net_merge_all_done = \&PublicInbox::LeiInput::input_only_net_merge_all_done;
-*net_merge_all = \&PublicInbox::LeiAuth::net_merge_all;
 
 1;