]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/IMAPD.pm
imapd: use nntpd_cache to speed up startup/reload time
[public-inbox.git] / lib / PublicInbox / IMAPD.pm
index a3a2598661bbf97d09b0c5ddb550f5c9faeca506..5368ff040edd457111cdb3984f722ebd67b5904f 100644 (file)
@@ -1,62 +1,90 @@
-# Copyright (C) 2020 all contributors <meta@public-inbox.org>
+# Copyright (C) all contributors <meta@public-inbox.org>
 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
 
-# represents an IMAPD (currently a singleton),
-# see script/public-inbox-imapd for how it is used
+# represents an IMAPD, see script/public-inbox-imapd for how it is used
 package PublicInbox::IMAPD;
 use strict;
-use parent qw(PublicInbox::NNTPD);
+use v5.10.1;
+use PublicInbox::Config;
 use PublicInbox::InboxIdle;
+use PublicInbox::IMAP;
+use PublicInbox::DummyInbox;
+my $dummy = bless { uidvalidity => 0 }, 'PublicInbox::DummyInbox';
 
 sub new {
        my ($class) = @_;
        bless {
-               groups => {},
+               # mailboxes => {},
                err => \*STDERR,
                out => \*STDOUT,
-               grouplist => [],
-               # accept_tls => { SSL_server => 1, ..., SSL_reuse_ctx => ... }
-               # pi_config => PublicInbox::Config
+               # ssl_ctx_opt => { SSL_cert_file => ..., SSL_key_file => ... }
+               # pi_cfg => PublicInbox::Config
                # idler => PublicInbox::InboxIdle
        }, $class;
 }
 
-sub refresh_inboxlist ($) {
-       my ($self) = @_;
-       my @names = map { $_->{newsgroup} } @{delete $self->{grouplist}};
-       my %ns; # "\Noselect \HasChildren"
-       for (@names) {
-               my $up = $_;
-               while ($up =~ s/\.[^\.]+\z//) {
-                       $ns{$up} = '\\Noselect \\HasChildren';
-               }
+sub imapd_refresh_ibx { # pi_cfg->each_inbox cb
+       my ($ibx, $imapd, $cache, $dummies) = @_;
+       my $ngname = $ibx->{newsgroup} // return;
+
+       # We require lower-case since IMAP mailbox names are
+       # case-insensitive (but -nntpd matches INN in being
+       # case-sensitive)
+       if ($ngname =~ m![^a-z0-9/_\.\-\~\@\+\=:]! ||
+                       # don't confuse with 50K slices
+                       $ngname =~ /\.[0-9]+\z/) {
+               warn "mailbox name invalid: newsgroup=`$ngname'\n";
+               return;
+       }
+       my $ce = $cache->{$ngname};
+       %$ibx = (%$ibx, %$ce) if $ce;
+       # only valid if msgmap and over works:
+       if (defined($ibx->uidvalidity)) {
+               # fill ->{mailboxes}:
+               PublicInbox::IMAP::ensure_slices_exist($imapd, $ibx);
+               # preload to avoid fragmentation:
+               $ibx->description;
+               $ibx->base_url;
+               # ensure dummies are selectable:
+               do {
+                       $dummies->{$ngname} = $dummy;
+               } while ($ngname =~ s/\.[^\.]+\z//);
        }
-       @names = map {;
-               my $at = delete($ns{$_}) ? '\\HasChildren' : '\\HasNoChildren';
-               qq[* LIST ($at) "." $_\r\n]
-       } @names;
-       push(@names, map { qq[* LIST ($ns{$_}) "." $_\r\n] } keys %ns);
-       @names = sort {
-               my ($xa) = ($a =~ / (\S+)\r\n/g);
-               my ($xb) = ($b =~ / (\S+)\r\n/g);
-               length($xa) <=> length($xb);
-       } @names;
-       $self->{inboxlist} = \@names;
+       delete @$ibx{qw(mm over)};
 }
 
 sub refresh_groups {
-       my ($self) = @_;
-       my $pi_config = $self->{pi_config} = PublicInbox::Config->new;
-       $self->SUPER::refresh_groups($pi_config);
-       refresh_inboxlist($self);
-
+       my ($self, $sig) = @_;
+       my $pi_cfg = PublicInbox::Config->new;
+       my $mailboxes = $self->{mailboxes} = {};
+       my $cache = eval { $pi_cfg->ALL->misc->nntpd_cache_load } // {};
+       my $dummies = {};
+       $pi_cfg->each_inbox(\&imapd_refresh_ibx, $self, $cache, $dummies);
+       %$dummies = (%$dummies, %$mailboxes);
+       $mailboxes = $self->{mailboxes} = $dummies;
+       @{$self->{mailboxlist}} = map { $_->[2] }
+               sort { $a->[0] cmp $b->[0] || $a->[1] <=> $b->[1] }
+               map {
+                       my $u = $_; # capitalize "INBOX" for user-familiarity
+                       $u =~ s/\Ainbox(\.|\z)/INBOX$1/i;
+                       if ($mailboxes->{$_} == $dummy) {
+                               [ $u, -1,
+                                 qq[* LIST (\\HasChildren) "." $u\r\n]]
+                       } else {
+                               $u =~ /\A(.+)\.([0-9]+)\z/ or
+                                       die "BUG: `$u' has no slice digit(s)";
+                               [ $1, $2 + 0,
+                                 qq[* LIST (\\HasNoChildren) "." $u\r\n] ]
+                       }
+               } keys %$mailboxes;
+       $self->{pi_cfg} = $pi_cfg;
        if (my $idler = $self->{idler}) {
-               $idler->refresh($pi_config);
+               $idler->refresh($pi_cfg);
        }
 }
 
 sub idler_start {
-       $_[0]->{idler} //= PublicInbox::InboxIdle->new($_[0]->{pi_config});
+       $_[0]->{idler} //= PublicInbox::InboxIdle->new($_[0]->{pi_cfg});
 }
 
 1;