]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/LeiLsMailSource.pm
get rid of unnecessary bytes::length usage
[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                 @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 ($hwm, $lwm, $status) = @{$all->{$ng}};
57                                 push @x, { name => $ng, url => "$sec/$ng",
58                                         lwm => $lwm + 0,
59                                         hwm => $hwm + 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         $lei->start_pager if -t $lei->{1};
91         my $ops = {};
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);
95         $lei->{wq1} = $self;
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}
99 }
100
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         if (my $lms = $lei->lms) {
107                 @m = map { $match_cb->($_) } grep(
108                         m!\A(?:imaps?|nntps?|s?news)://!, $lms->folders);
109                 @f{@m} = @m;
110         }
111         keys %f;
112 }
113
114 no warnings 'once';
115 *ipc_atfork_child = \&PublicInbox::LeiInput::input_only_atfork_child;
116 *net_merge_all_done = \&PublicInbox::LeiInput::input_only_net_merge_all_done;
117 *net_merge_all = \&PublicInbox::LeiAuth::net_merge_all;
118
119 1;