1 # Copyright (C) 2021 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 # command for listing NNTP groups and IMAP folders,
5 # handy for users with git-credential-helper configured
6 # TODO: list JMAP labels
7 package PublicInbox::LeiLsMailSource;
10 use parent qw(PublicInbox::IPC PublicInbox::LeiInput);
12 sub input_path_url { # overrides LeiInput version
13 my ($self, $url) = @_;
14 # TODO: support ndjson and other JSONs we support elsewhere
15 my $lei = $self->{lei};
16 my $json = $lei->{json};
17 my $ORS = $self->{lei}->{opt}->{z} ? "\0" : "\n";
19 if ($url =~ m!\Aimaps?://!i) {
20 my $uri = PublicInbox::URIimap->new($url);
21 my $sec = $lei->{net}->can('uri_section')->($uri);
22 my $mic = $lei->{net}->mic_get($uri);
23 my $l = $mic->folders_hash($uri->path); # server-side filter
24 @$l = map { $_->[2] } # undo Schwartzian transform below:
25 sort { $a->[0] cmp $b->[0] || $a->[1] <=> $b->[1] }
26 map { # prepare to sort -imapd slices numerically
27 $_->{name} =~ /\A(.+?)\.([0-9]+)\z/ ?
28 [ $1, $2 + 0, $_ ] : [ $_->{name}, -1, $_ ];
30 @f = map { "$sec/$_->{name}" } @$l;
32 $_->{url} = "$sec/$_->{name}" for @$l;
33 $lei->puts($json->encode($l));
35 if ($self->{lei}->{opt}->{url}) {
36 $_->{name} = "$sec/$_->{name}" for @$l;
38 $lei->out(join($ORS, (map { $_->{name} } @$l), ''));
40 } elsif ($url =~ m!\A(?:nntps?|s?news)://!i) {
41 my $uri = PublicInbox::URInntps->new($url);
42 my $nn = $lei->{net}->nn_get($uri);
43 my $l = $nn->newsgroups($uri->group); # name => description
44 my $sec = $lei->{net}->can('uri_section')->($uri);
48 for my $ng (sort keys %$l) {
51 # we need to drop CR ourselves iff using IO::Socket::SSL since
52 # Net::Cmd::getline doesn't get used by Net::NNTP if TLS is in play, noted in:
53 # <https://rt.cpan.org/Ticket/Display.html?id=129966>
56 my ($high, $low, $status) = @{$all->{$ng}};
57 push @x, { name => $ng, url => "$sec/$ng",
59 high => $high + 0, status => $status,
60 description => $desc };
62 @f = map { "$sec/$_" } keys %$all;
63 $lei->puts($json->encode(\@x));
65 @f = map { "$sec/$_" } keys %$l;
66 if ($self->{lei}->{opt}->{url}) {
67 $lei->out(join($ORS, sort(@f), ''));
69 $lei->out(join($ORS, sort(keys %$l), ''));
72 } else { die "BUG: $url not supported" }
74 my $fc = $lei->url_folder_cache;
75 my $lk = $fc->lock_for_scope;
78 $fc->set($_, $now) for @f;
83 sub lei_ls_mail_source {
84 my ($lei, $url, $pfx) = @_;
85 $url =~ m!\A(?:imaps?|nntps?|s?news)://!i or return
86 $lei->fail('only NNTP and IMAP URLs supported');
87 my $self = bless { pfx => $pfx, -ls_ok => 1 }, __PACKAGE__;
88 $self->{cfg} = $lei->_lei_cfg; # may be undef
89 $self->prepare_inputs($lei, [ $url ]) or return;
90 my $isatty = -t $lei->{1};
91 if ($lei->{opt}->{l}) {
92 my $json = ref(PublicInbox::Config->json)->new->utf8->canonical;
94 $json->ascii(1) if $lei->{opt}->{ascii};
95 $json->pretty(1)->indent(2) if $isatty || $lei->{opt}->{pretty};
97 $lei->start_pager if $isatty;
98 $lei->{-err_type} = 'non-fatal';
99 $lei->wq1_start($self);
102 sub _complete_ls_mail_source {
103 my ($lei, @argv) = @_;
104 my $match_cb = $lei->complete_url_prepare(\@argv);
105 my @k = $lei->url_folder_cache->keys($argv[-1] // undef, 1);
106 my @m = map { $match_cb->($_) } @k;
107 my %f = map { $_ => 1 } (@m ? @m : @k);
108 if (my $lms = $lei->lms) {
109 @k = $lms->folders($argv[-1] // undef, 1);
110 @m = map { $match_cb->($_) } grep(m!\A[a-z]+://!, @k);
111 if (@m) { @f{@m} = @m } else { @f{@k} = @k }
117 *ipc_atfork_child = \&PublicInbox::LeiInput::input_only_atfork_child;
118 *net_merge_all_done = \&PublicInbox::LeiInput::input_only_net_merge_all_done;