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}) {
24 if ($url =~ m!\Aimaps?://!i) {
25 my $uri = PublicInbox::URIimap->new($url);
26 my $mic = $lei->{net}->mic_get($uri);
27 my $l = $mic->folders_hash($uri->path); # server-side filter
29 $lei->puts($json->encode($l));
31 $lei->out(join($ORS, (map { $_->{name} } @$l), ''));
33 } elsif ($url =~ m!\A(?:nntps?|s?news)://!i) {
34 my $uri = PublicInbox::URInntps->new($url);
35 my $nn = $lei->{net}->nn_get($uri);
36 my $l = $nn->newsgroups($uri->group); # name => description
40 for my $ng (sort keys %$l) {
43 # we need to drop CR ourselves iff using IO::Socket::SSL since
44 # Net::Cmd::getline doesn't get used by Net::NNTP if TLS is in play, noted in:
45 # <https://rt.cpan.org/Ticket/Display.html?id=129966>
48 my ($hwm, $lwm, $status) = @{$all->{$ng}};
49 push @x, { name => $ng, lwm => $lwm + 0,
50 hwm => $hwm + 0, status => $status,
51 description => $desc };
53 $lei->puts($json->encode(\@x));
55 $lei->out(join($ORS, sort(keys %$l), ''));
57 } else { die "BUG: $url not supported" }
60 sub lei_ls_mail_source {
61 my ($lei, $url, $pfx) = @_;
62 $url =~ m!\A(?:imaps?|nntps?|s?news)://!i or return
63 $lei->fail('only NNTP and IMAP URLs supported');
64 my $self = bless { pfx => $pfx, -ls_ok => 1 }, __PACKAGE__;
65 $self->prepare_inputs($lei, [ $url ]) or return;
66 $lei->start_pager if -t $lei->{1};
68 $lei->{auth}->op_merge($ops, $self);
69 my $j = $self->{-wq_nr_workers} = 1; # locked
70 (my $op_c, $ops) = $lei->workers_start($self, $j, $ops);
72 $lei->{-err_type} = 'non-fatal';
73 net_merge_all_done($self) unless $lei->{auth};
74 $lei->wait_wq_events($op_c, $ops); # net_merge_all_done if !{auth}
78 *ipc_atfork_child = \&PublicInbox::LeiInput::input_only_atfork_child;
79 *net_merge_all_done = \&PublicInbox::LeiInput::input_only_net_merge_all_done;
80 *net_merge_all = \&PublicInbox::LeiAuth::net_merge_all;