]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/IMAPD.pm
imap: case-insensitive mailbox name comparisons
[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
29         if (my @uc = grep(/[A-Z]/, @names)) {
30                 warn "Uppercase not allowed for IMAP newsgroup(s):\n",
31                         map { "\t$_\n" } @uc;
32                 my %uc = map { $_ => 1 } @uc;
33                 @names = grep { !$uc{$_} } @names;
34         }
35         for (@names) {
36                 my $up = $_;
37                 while ($up =~ s/\.[^\.]+\z//) {
38                         $ns{$up} = '\\Noselect \\HasChildren';
39                 }
40         }
41         @names = map {;
42                 my $at = delete($ns{$_}) ? '\\HasChildren' : '\\HasNoChildren';
43                 qq[* LIST ($at) "." $_\r\n]
44         } @names;
45         push(@names, map { qq[* LIST ($ns{$_}) "." $_\r\n] } keys %ns);
46         @names = sort {
47                 my ($xa) = ($a =~ / (\S+)\r\n/g);
48                 my ($xb) = ($b =~ / (\S+)\r\n/g);
49                 length($xa) <=> length($xb);
50         } @names;
51         $self->{inboxlist} = \@names;
52 }
53
54 sub refresh_groups {
55         my ($self) = @_;
56         my $pi_config = $self->{pi_config} = PublicInbox::Config->new;
57         $self->SUPER::refresh_groups($pi_config);
58         refresh_inboxlist($self);
59
60         if (my $idler = $self->{idler}) {
61                 $idler->refresh($pi_config);
62         }
63 }
64
65 sub idler_start {
66         $_[0]->{idler} //= PublicInbox::InboxIdle->new($_[0]->{pi_config});
67 }
68
69 1;