]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/IMAPD.pm
No ext_urls
[public-inbox.git] / lib / PublicInbox / IMAPD.pm
1 # Copyright (C) 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, see script/public-inbox-imapd for how it is used
5 package PublicInbox::IMAPD;
6 use strict;
7 use v5.10.1;
8 use PublicInbox::Config;
9 use PublicInbox::InboxIdle;
10 use PublicInbox::IMAP;
11 use PublicInbox::DummyInbox;
12 my $dummy = bless { uidvalidity => 0 }, 'PublicInbox::DummyInbox';
13
14 sub new {
15         my ($class) = @_;
16         bless {
17                 # mailboxes => {},
18                 err => \*STDERR,
19                 out => \*STDOUT,
20                 # ssl_ctx_opt => { SSL_cert_file => ..., SSL_key_file => ... }
21                 # pi_cfg => PublicInbox::Config
22                 # idler => PublicInbox::InboxIdle
23         }, $class;
24 }
25
26 sub _refresh_ibx { # pi_cfg->each_inbox cb
27         my ($ibx, $imapd, $cache, $dummies) = @_;
28         my $ngname = $ibx->{newsgroup} // return;
29
30         # We require lower-case since IMAP mailbox names are
31         # case-insensitive (but -nntpd matches INN in being
32         # case-sensitive)
33         if ($ngname =~ m![^a-z0-9/_\.\-\~\@\+\=:]! ||
34                         # don't confuse with 50K slices
35                         $ngname =~ /\.[0-9]+\z/) {
36                 warn "mailbox name invalid: newsgroup=`$ngname'\n";
37                 return;
38         }
39         my $ce = $cache->{$ngname};
40         %$ibx = (%$ibx, %$ce) if $ce;
41         # only valid if msgmap and over works:
42         if (defined($ibx->uidvalidity)) {
43                 # fill ->{mailboxes}:
44                 PublicInbox::IMAP::ensure_slices_exist($imapd, $ibx);
45                 # preload to avoid fragmentation:
46                 $ibx->description;
47                 # ensure dummies are selectable:
48                 do {
49                         $dummies->{$ngname} = $dummy;
50                 } while ($ngname =~ s/\.[^\.]+\z//);
51         }
52         delete @$ibx{qw(mm over)};
53 }
54
55 sub refresh_groups {
56         my ($self, $sig) = @_;
57         my $pi_cfg = PublicInbox::Config->new;
58         $self->{mailboxes} = $pi_cfg->{-imap_mailboxes} // do {
59                 my $mailboxes = $self->{mailboxes} = {};
60                 my $cache = eval { $pi_cfg->ALL->misc->nntpd_cache_load } // {};
61                 my $dummies = {};
62                 $pi_cfg->each_inbox(\&_refresh_ibx, $self, $cache, $dummies);
63                 %$mailboxes = (%$dummies, %$mailboxes);
64                 @{$pi_cfg->{-imap_mailboxlist}} = map { $_->[2] }
65                         sort { $a->[0] cmp $b->[0] || $a->[1] <=> $b->[1] }
66                         map {
67                                 # capitalize "INBOX" for user-familiarity
68                                 my $u = $_;
69                                 $u =~ s/\Ainbox(\.|\z)/INBOX$1/i;
70                                 if ($mailboxes->{$_} == $dummy) {
71                                         [ $u, -1,
72                                           qq[* LIST (\\HasChildren) "." $u\r\n]]
73                                 } else {
74                                         $u =~ /\A(.+)\.([0-9]+)\z/ or die
75 "BUG: `$u' has no slice digit(s)";
76                                         [ $1, $2 + 0, '* LIST '.
77                                           qq[(\\HasNoChildren) "." $u\r\n] ]
78                                 }
79                         } keys %$mailboxes;
80                 $pi_cfg->{-imap_mailboxes} = $mailboxes;
81         };
82         $self->{mailboxlist} = $pi_cfg->{-imap_mailboxlist} //
83                         die 'BUG: no mailboxlist';
84         $self->{pi_cfg} = $pi_cfg;
85         if (my $idler = $self->{idler}) {
86                 $idler->refresh($pi_cfg);
87         }
88 }
89
90 sub idler_start {
91         $_[0]->{idler} //= PublicInbox::InboxIdle->new($_[0]->{pi_cfg});
92 }
93
94 sub event_step { # called vai requeue for low-priority IMAP clients
95         my ($self) = @_;
96         my $imap = shift(@{$self->{-authed_q}}) // return;
97         PublicInbox::DS::requeue($self) if scalar(@{$self->{-authed_q}});
98         $imap->event_step; # PublicInbox::IMAP::event_step
99 }
100
101 1;