]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/LeiLsMailSource.pm
lei ls-mail-source: use "high"/"low" for NNTP
[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         my @f;
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, $_ ];
35                         } @$l;
36                 @f = map { "$sec/$_->{name}" } @$l;
37                 if ($json) {
38                         $_->{url} = "$sec/$_->{name}" for @$l;
39                         $lei->puts($json->encode($l));
40                 } else {
41                         if ($self->{lei}->{opt}->{url}) {
42                                 $_->{name} = "$sec/$_->{name}" for @$l;
43                         }
44                         $lei->out(join($ORS, (map { $_->{name} } @$l), ''));
45                 }
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);
51                 if ($json) {
52                         my $all = $nn->list;
53                         my @x;
54                         for my $ng (sort keys %$l) {
55                                 my $desc = $l->{$ng};
56
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>
60                                 $desc =~ s/\r\z//;
61
62                                 my ($high, $low, $status) = @{$all->{$ng}};
63                                 push @x, { name => $ng, url => "$sec/$ng",
64                                         low => $low + 0,
65                                         high => $high + 0, status => $status,
66                                         description => $desc };
67                         }
68                         @f = map { "$sec/$_" } keys %$all;
69                         $lei->puts($json->encode(\@x));
70                 } else {
71                         @f = map { "$sec/$_" } keys %$l;
72                         if ($self->{lei}->{opt}->{url}) {
73                                 $lei->out(join($ORS, sort(@f), ''));
74                         } else {
75                                 $lei->out(join($ORS, sort(keys %$l), ''));
76                         }
77                 }
78         } else { die "BUG: $url not supported" }
79         if (@f) {
80                 my $fc = $lei->url_folder_cache;
81                 my $lk = $fc->lock_for_scope;
82                 $fc->dbh->begin_work;
83                 my $now = time;
84                 $fc->set($_, $now) for @f;
85                 $fc->dbh->commit;
86         }
87 }
88
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};
97         my $ops = {};
98         $lei->{auth}->op_merge($ops, $self);
99         (my $op_c, $ops) = $lei->workers_start($self, 1, $ops);
100         $lei->{wq1} = $self;
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}
104 }
105
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);
114                 @f{@m} = @m;
115         }
116         keys %f;
117 }
118
119 no warnings 'once';
120 *ipc_atfork_child = \&PublicInbox::LeiInput::input_only_atfork_child;
121 *net_merge_all_done = \&PublicInbox::LeiInput::input_only_net_merge_all_done;
122
123 1;