]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/LeiLsMailSource.pm
lei ls-mail-source: list IMAP folders and NNTP groups
[public-inbox.git] / lib / PublicInbox / LeiLsMailSource.pm
1 # Copyright (C) 2021 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3
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;
8 use strict;
9 use v5.10.1;
10 use parent qw(PublicInbox::IPC PublicInbox::LeiInput);
11
12 sub input_path_url { # overrides LeiInput version
13         my ($self, $url) = @_;
14         # TODO: support ndjson and other JSONs we support elsewhere
15         my $json;
16         my $lei = $self->{lei};
17         my $ORS = "\n";
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}) {
22                 $ORS = "\0";
23         }
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
28                 if ($json) {
29                         $lei->puts($json->encode($l));
30                 } else {
31                         $lei->out(join($ORS, (map { $_->{name} } @$l), ''));
32                 }
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
37                 if ($json) {
38                         my $all = $nn->list;
39                         my @x;
40                         for my $ng (sort keys %$l) {
41                                 my $desc = $l->{$ng};
42
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>
46                                 $desc =~ s/\r\z//;
47
48                                 my ($hwm, $lwm, $status) = @{$all->{$ng}};
49                                 push @x, { name => $ng, lwm => $lwm + 0,
50                                         hwm => $hwm + 0, status => $status,
51                                         description => $desc };
52                         }
53                         $lei->puts($json->encode(\@x));
54                 } else {
55                         $lei->out(join($ORS, sort(keys %$l), ''));
56                 }
57         } else { die "BUG: $url not supported" }
58 }
59
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};
67         my $ops = {};
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);
71         $lei->{wq1} = $self;
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}
75 }
76
77 no warnings 'once';
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;
81
82 1;