]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/IMAPD.pm
imap: support LIST command
[public-inbox.git] / lib / PublicInbox / IMAPD.pm
1 # Copyright (C) 2020 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3
4 # represents an IMAPD (currently a singleton),
5 # see script/public-inbox-imapd for how it is used
6 package PublicInbox::IMAPD;
7 use strict;
8 use parent qw(PublicInbox::NNTPD);
9 use PublicInbox::InboxIdle;
10
11 sub new {
12         my ($class) = @_;
13         bless {
14                 groups => {},
15                 err => \*STDERR,
16                 out => \*STDOUT,
17                 grouplist => [],
18                 # accept_tls => { SSL_server => 1, ..., SSL_reuse_ctx => ... }
19                 # pi_config => PublicInbox::Config
20                 # idler => PublicInbox::InboxIdle
21         }, $class;
22 }
23
24 sub refresh_inboxlist ($) {
25         my ($self) = @_;
26         my @names = map { $_->{newsgroup} } @{delete $self->{grouplist}};
27         my %ns; # "\Noselect \HasChildren"
28         for (@names) {
29                 my $up = $_;
30                 while ($up =~ s/\.[^\.]+\z//) {
31                         $ns{$up} = '\\Noselect \\HasChildren';
32                 }
33         }
34         @names = map {;
35                 my $at = delete($ns{$_}) ? '\\HasChildren' : '\\HasNoChildren';
36                 qq[* LIST ($at) "." $_\r\n]
37         } @names;
38         push(@names, map { qq[* LIST ($ns{$_}) "." $_\r\n] } keys %ns);
39         @names = sort {
40                 my ($xa) = ($a =~ / (\S+)\r\n/g);
41                 my ($xb) = ($b =~ / (\S+)\r\n/g);
42                 length($xa) <=> length($xb);
43         } @names;
44         $self->{inboxlist} = \@names;
45 }
46
47 sub refresh_groups {
48         my ($self) = @_;
49         my $pi_config = $self->{pi_config} = PublicInbox::Config->new;
50         $self->SUPER::refresh_groups($pi_config);
51         refresh_inboxlist($self);
52
53         if (my $idler = $self->{idler}) {
54                 $idler->refresh($pi_config);
55         }
56 }
57
58 sub idler_start {
59         $_[0]->{idler} //= PublicInbox::InboxIdle->new($_[0]->{pi_config});
60 }
61
62 1;