]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/LeiLsMailSource.pm
imap+nntp: share COMPRESS implementation
[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 $lei = $self->{lei};
16         my $json = $lei->{json};
17         my $ORS = $self->{lei}->{opt}->{z} ? "\0" : "\n";
18         my @f;
19         if ($url =~ m!\Aimaps?://!i) {
20                 my $uri = PublicInbox::URIimap->new($url);
21                 my $sec = $lei->{net}->can('uri_section')->($uri);
22                 my $mic = $lei->{net}->mic_get($uri);
23                 my $l = $mic->folders_hash($uri->path); # server-side filter
24                 @$l = map { $_->[2] } # undo Schwartzian transform below:
25                         sort { $a->[0] cmp $b->[0] || $a->[1] <=> $b->[1] }
26                         map { # prepare to sort -imapd slices numerically
27                                 $_->{name} =~ /\A(.+?)\.([0-9]+)\z/ ?
28                                 [ $1, $2 + 0, $_ ] : [ $_->{name}, -1, $_ ];
29                         } @$l;
30                 @f = map { "$sec/$_->{name}" } @$l;
31                 if ($json) {
32                         $_->{url} = "$sec/$_->{name}" for @$l;
33                         $lei->puts($json->encode($l));
34                 } else {
35                         if ($self->{lei}->{opt}->{url}) {
36                                 $_->{name} = "$sec/$_->{name}" for @$l;
37                         }
38                         $lei->out(join($ORS, (map { $_->{name} } @$l), ''));
39                 }
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);
45                 if ($json) {
46                         my $all = $nn->list;
47                         my @x;
48                         for my $ng (sort keys %$l) {
49                                 my $desc = $l->{$ng};
50
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>
54                                 $desc =~ s/\r\z//;
55
56                                 my ($high, $low, $status) = @{$all->{$ng}};
57                                 push @x, { name => $ng, url => "$sec/$ng",
58                                         low => $low + 0,
59                                         high => $high + 0, status => $status,
60                                         description => $desc };
61                         }
62                         @f = map { "$sec/$_" } keys %$all;
63                         $lei->puts($json->encode(\@x));
64                 } else {
65                         @f = map { "$sec/$_" } keys %$l;
66                         if ($self->{lei}->{opt}->{url}) {
67                                 $lei->out(join($ORS, sort(@f), ''));
68                         } else {
69                                 $lei->out(join($ORS, sort(keys %$l), ''));
70                         }
71                 }
72         } else { die "BUG: $url not supported" }
73         if (@f) {
74                 my $fc = $lei->url_folder_cache;
75                 my $lk = $fc->lock_for_scope;
76                 $fc->dbh->begin_work;
77                 my $now = time;
78                 $fc->set($_, $now) for @f;
79                 $fc->dbh->commit;
80         }
81 }
82
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         my $isatty = -t $lei->{1};
91         if ($lei->{opt}->{l}) {
92                 my $json = ref(PublicInbox::Config->json)->new->utf8->canonical;
93                 $lei->{json} = $json;
94                 $json->ascii(1) if $lei->{opt}->{ascii};
95                 $json->pretty(1)->indent(2) if $isatty || $lei->{opt}->{pretty};
96         }
97         $lei->start_pager if $isatty;
98         $lei->{-err_type} = 'non-fatal';
99         $lei->wq1_start($self);
100 }
101
102 sub _complete_ls_mail_source {
103         my ($lei, @argv) = @_;
104         my $match_cb = $lei->complete_url_prepare(\@argv);
105         my @k = $lei->url_folder_cache->keys($argv[-1] // undef, 1);
106         my @m = map { $match_cb->($_) } @k;
107         my %f = map { $_ => 1 } (@m ? @m : @k);
108         if (my $lms = $lei->lms) {
109                 @k = $lms->folders($argv[-1] // undef, 1);
110                 @m = map { $match_cb->($_) } grep(m!\A[a-z]+://!, @k);
111                 if (@m) { @f{@m} = @m } else { @f{@k} = @k }
112         }
113         keys %f;
114 }
115
116 no warnings 'once';
117 *ipc_atfork_child = \&PublicInbox::LeiInput::input_only_atfork_child;
118 *net_merge_all_done = \&PublicInbox::LeiInput::input_only_net_merge_all_done;
119
120 1;