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
16 my $lei = $self->{lei};
18 if ($self->{lei}->{opt}->{l}) {
19 $json = ref(PublicInbox::Config->json)->new->utf8->canonical;
20 $json->ascii(1) if $lei->{opt}->{ascii};
21 } elsif ($self->{lei}->{opt}->{z}) {
25 if ($url =~ m!\Aimaps?://!i) {
26 my $uri = PublicInbox::URIimap->new($url);
27 my $sec = $lei->{net}->can('uri_section')->($uri);
28 my $mic = $lei->{net}->mic_get($uri);
29 my $l = $mic->folders_hash($uri->path); # server-side filter
30 @$l = map { $_->[2] } # undo Schwartzian transform below:
31 sort { $a->[0] cmp $b->[0] || $a->[1] <=> $b->[1] }
32 map { # prepare to sort -imapd slices numerically
33 $_->{name} =~ /\A(.+?)\.([0-9]+)\z/ ?
34 [ $1, $2 + 0, $_ ] : [ $_->{name}, -1, $_ ];
36 @f = map { "$sec/$_->{name}" } @$l;
38 $_->{url} = "$sec/$_->{name}" for @$l;
39 $lei->puts($json->encode($l));
41 if ($self->{lei}->{opt}->{url}) {
42 $_->{name} = "$sec/$_->{name}" for @$l;
44 $lei->out(join($ORS, (map { $_->{name} } @$l), ''));
46 } elsif ($url =~ m!\A(?:nntps?|s?news)://!i) {
47 my $uri = PublicInbox::URInntps->new($url);
48 my $nn = $lei->{net}->nn_get($uri);
49 my $l = $nn->newsgroups($uri->group); # name => description
50 my $sec = $lei->{net}->can('uri_section')->($uri);
54 for my $ng (sort keys %$l) {
57 # we need to drop CR ourselves iff using IO::Socket::SSL since
58 # Net::Cmd::getline doesn't get used by Net::NNTP if TLS is in play, noted in:
59 # <https://rt.cpan.org/Ticket/Display.html?id=129966>
62 my ($hwm, $lwm, $status) = @{$all->{$ng}};
63 push @x, { name => $ng, url => "$sec/$ng",
65 hwm => $hwm + 0, status => $status,
66 description => $desc };
68 @f = map { "$sec/$_" } keys %$all;
69 $lei->puts($json->encode(\@x));
71 @f = map { "$sec/$_" } keys %$l;
72 if ($self->{lei}->{opt}->{url}) {
73 $lei->out(join($ORS, sort(@f), ''));
75 $lei->out(join($ORS, sort(keys %$l), ''));
78 } else { die "BUG: $url not supported" }
80 my $fc = $lei->url_folder_cache;
81 my $lk = $fc->lock_for_scope;
84 $fc->set($_, $now) for @f;
89 sub lei_ls_mail_source {
90 my ($lei, $url, $pfx) = @_;
91 $url =~ m!\A(?:imaps?|nntps?|s?news)://!i or return
92 $lei->fail('only NNTP and IMAP URLs supported');
93 my $self = bless { pfx => $pfx, -ls_ok => 1 }, __PACKAGE__;
94 $self->{cfg} = $lei->_lei_cfg; # may be undef
95 $self->prepare_inputs($lei, [ $url ]) or return;
96 $lei->start_pager if -t $lei->{1};
98 $lei->{auth}->op_merge($ops, $self);
99 (my $op_c, $ops) = $lei->workers_start($self, 1, $ops);
101 $lei->{-err_type} = 'non-fatal';
102 net_merge_all_done($self) unless $lei->{auth};
103 $lei->wait_wq_events($op_c, $ops); # net_merge_all_done if !{auth}
106 sub _complete_ls_mail_source {
107 my ($lei, @argv) = @_;
108 my $match_cb = $lei->complete_url_prepare(\@argv);
109 my @m = map { $match_cb->($_) } $lei->url_folder_cache->keys;
110 my %f = map { $_ => 1 } @m;
111 if (my $lms = $lei->lms) {
112 @m = map { $match_cb->($_) } grep(
113 m!\A(?:imaps?|nntps?|s?news)://!, $lms->folders);
120 *ipc_atfork_child = \&PublicInbox::LeiInput::input_only_atfork_child;
121 *net_merge_all_done = \&PublicInbox::LeiInput::input_only_net_merge_all_done;