X-Git-Url: http://www.git.stargrave.org/?a=blobdiff_plain;f=lib%2FPublicInbox%2FIMAPD.pm;h=78323e57c851ac664120109f9eb4c0a1ee9b85e9;hb=refs%2Fheads%2Fmaster;hp=a10fabffb1fbfa0cce2007635d93badd07b91c4c;hpb=48180dbb004b5f59b2e80613b6fa2e5e869316f1;p=public-inbox.git diff --git a/lib/PublicInbox/IMAPD.pm b/lib/PublicInbox/IMAPD.pm index a10fabff..78323e57 100644 --- a/lib/PublicInbox/IMAPD.pm +++ b/lib/PublicInbox/IMAPD.pm @@ -1,118 +1,101 @@ -# Copyright (C) 2020 all contributors +# Copyright (C) all contributors # License: AGPL-3.0+ -# 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; -# *UID_BLOCK = \&PublicInbox::IMAP::UID_BLOCK; +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" +sub _refresh_ibx { # pi_cfg->each_inbox cb + my ($ibx, $imapd, $cache, $dummies) = @_; + my $ngname = $ibx->{newsgroup} // return; - if (my @uc = grep(/[A-Z]/, @names)) { - warn "Uppercase not allowed for IMAP newsgroup(s):\n", - map { "\t$_\n" } @uc; - my %uc = map { $_ => 1 } @uc; - @names = grep { !$uc{$_} } @names; - } - for (@names) { - my $up = $_; - while ($up =~ s/\.[^\.]+\z//) { - $ns{$up} = '\\Noselect \\HasChildren'; - } + # 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; } - @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; -} - -sub imapd_refresh_ibx { # pi_config->each_inbox cb - my ($ibx, $imapd) = @_; - my $ngname = $ibx->{newsgroup} or return; - if (ref $ngname) { - warn 'multiple newsgroups not supported: '. - join(', ', @$ngname). "\n"; - } elsif ($ngname =~ m![^a-z0-9/_\.\-\~\@\+\=:]! || - $ngname =~ /\.[0-9]+-[0-9]+\z/) { - warn "mailbox name invalid: `$ngname'\n"; - } - - my $mm = $ibx->mm or return; - $ibx->{mm} = undef; - defined($ibx->{uidvalidity} = $mm->created_at) or return; - $imapd->{tmp_groups}->{$ngname} = $ibx; - - # preload to avoid fragmentation: - $ibx->description; - $ibx->base_url; - # my $max = $mm->max // 0; - # my $uid_min = UID_BLOCK * int($max/UID_BLOCK) + 1; -} - -sub imapd_refresh_finalize { - my ($imapd, $pi_config) = @_; - $imapd->{groups} = delete $imapd->{tmp_groups}; - $imapd->{grouplist} = [ values %{$imapd->{groups}} ]; - refresh_inboxlist($imapd); - $imapd->{pi_config} = $pi_config; - if (my $idler = $imapd->{idler}) { - $idler->refresh($pi_config); - } -} - -sub imapd_refresh_step { # pi_config->iterate_start cb - my ($pi_config, $section, $imapd) = @_; - if (defined($section)) { - return if $section !~ m!\Apublicinbox\.([^/]+)\z!; - my $ibx = $pi_config->lookup_name($1) or return; - imapd_refresh_ibx($ibx, $imapd); - } else { # "EOF" - imapd_refresh_finalize($imapd, $pi_config); + 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; + # ensure dummies are selectable: + do { + $dummies->{$ngname} = $dummy; + } while ($ngname =~ s/\.[^\.]+\z//); } + delete @$ibx{qw(mm over)}; } sub refresh_groups { my ($self, $sig) = @_; - my $pi_config = PublicInbox::Config->new; - $self->{tmp_groups} = {}; - if (0 && $sig) { # SIGHUP - $pi_config->iterate_start(\&imapd_refresh_step, $self); - PublicInbox::DS::requeue($pi_config); # call event_step - } else { # initial start - $pi_config->each_inbox(\&imapd_refresh_ibx, $self); - imapd_refresh_finalize($self, $pi_config); + my $pi_cfg = PublicInbox::Config->new; + $self->{mailboxes} = $pi_cfg->{-imap_mailboxes} // do { + my $mailboxes = $self->{mailboxes} = {}; + my $cache = eval { $pi_cfg->ALL->misc->nntpd_cache_load } // {}; + my $dummies = {}; + $pi_cfg->each_inbox(\&_refresh_ibx, $self, $cache, $dummies); + %$mailboxes = (%$dummies, %$mailboxes); + @{$pi_cfg->{-imap_mailboxlist}} = map { $_->[2] } + sort { $a->[0] cmp $b->[0] || $a->[1] <=> $b->[1] } + map { + # capitalize "INBOX" for user-familiarity + my $u = $_; + $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, '* LIST '. + qq[(\\HasNoChildren) "." $u\r\n] ] + } + } keys %$mailboxes; + $pi_cfg->{-imap_mailboxes} = $mailboxes; + }; + $self->{mailboxlist} = $pi_cfg->{-imap_mailboxlist} // + die 'BUG: no mailboxlist'; + $self->{pi_cfg} = $pi_cfg; + if (my $idler = $self->{idler}) { + $idler->refresh($pi_cfg); } } sub idler_start { - $_[0]->{idler} //= PublicInbox::InboxIdle->new($_[0]->{pi_config}); + $_[0]->{idler} //= PublicInbox::InboxIdle->new($_[0]->{pi_cfg}); +} + +sub event_step { # called vai requeue for low-priority IMAP clients + my ($self) = @_; + my $imap = shift(@{$self->{-authed_q}}) // return; + PublicInbox::DS::requeue($self) if scalar(@{$self->{-authed_q}}); + $imap->event_step; # PublicInbox::IMAP::event_step } 1;