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 @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 ($hwm, $lwm, $status) = @{$all->{$ng}};
57 push @x, { name => $ng, url => "$sec/$ng",
59 hwm => $hwm + 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 $lei->start_pager if -t $lei->{1};
92 $lei->{auth}->op_merge($ops, $self);
93 my $j = $self->{-wq_nr_workers} = 1; # locked
94 (my $op_c, $ops) = $lei->workers_start($self, $j, $ops);
96 $lei->{-err_type} = 'non-fatal';
97 net_merge_all_done($self) unless $lei->{auth};
98 $lei->wait_wq_events($op_c, $ops); # net_merge_all_done if !{auth}
101 sub _complete_ls_mail_source {
102 my ($lei, @argv) = @_;
103 my $match_cb = $lei->complete_url_prepare(\@argv);
104 my @m = map { $match_cb->($_) } $lei->url_folder_cache->keys;
105 my %f = map { $_ => 1 } @m;
106 my $sto = $lei->_lei_store;
107 if (my $lms = $sto ? $sto->search->lms : undef) {
108 @m = map { $match_cb->($_) } grep(
109 m!\A(?:imaps?|nntps?|s?news)://!, $lms->folders);
116 *ipc_atfork_child = \&PublicInbox::LeiInput::input_only_atfork_child;
117 *net_merge_all_done = \&PublicInbox::LeiInput::input_only_net_merge_all_done;
118 *net_merge_all = \&PublicInbox::LeiAuth::net_merge_all;