]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/IMAPD.pm
config: split out iterator into separate object
[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 PublicInbox::Config;
9 use PublicInbox::ConfigIter;
10 use PublicInbox::InboxIdle;
11 use PublicInbox::IMAP;
12 use PublicInbox::DummyInbox;
13 my $dummy = bless { uidvalidity => 0 }, 'PublicInbox::DummyInbox';
14
15 sub new {
16         my ($class) = @_;
17         bless {
18                 mailboxes => {},
19                 err => \*STDERR,
20                 out => \*STDOUT,
21                 # accept_tls => { SSL_server => 1, ..., SSL_reuse_ctx => ... }
22                 # pi_config => PublicInbox::Config
23                 # idler => PublicInbox::InboxIdle
24         }, $class;
25 }
26
27 sub imapd_refresh_ibx { # pi_config->each_inbox cb
28         my ($ibx, $imapd) = @_;
29         my $ngname = $ibx->{newsgroup} or return;
30         if (ref $ngname) {
31                 warn 'multiple newsgroups not supported: '.
32                         join(', ', @$ngname). "\n";
33                 return;
34         } elsif ($ngname =~ m![^a-z0-9/_\.\-\~\@\+\=:]! ||
35                  $ngname =~ /\.[0-9]+\z/) {
36                 warn "mailbox name invalid: newsgroup=`$ngname'\n";
37                 return;
38         }
39         $ibx->over or return;
40         $ibx->{over} = undef;
41         my $mm = $ibx->mm or return;
42         $ibx->{mm} = undef;
43
44         # RFC 3501 2.3.1.1 -  "A good UIDVALIDITY value to use in
45         # this case is a 32-bit representation of the creation
46         # date/time of the mailbox"
47         defined($ibx->{uidvalidity} = $mm->created_at) or return;
48         PublicInbox::IMAP::ensure_slices_exist($imapd, $ibx, $mm->max // 0);
49
50         # preload to avoid fragmentation:
51         $ibx->description;
52         $ibx->base_url;
53
54         # ensure dummies are selectable
55         my $dummies = $imapd->{dummies};
56         do {
57                 $dummies->{$ngname} = $dummy;
58         } while ($ngname =~ s/\.[^\.]+\z//);
59 }
60
61 sub imapd_refresh_finalize {
62         my ($imapd, $pi_config) = @_;
63         my $mailboxes;
64         if (my $next = delete $imapd->{imapd_next}) {
65                 $imapd->{mailboxes} = delete $next->{mailboxes};
66                 $mailboxes = delete $next->{dummies};
67         } else {
68                 $mailboxes = delete $imapd->{dummies};
69         }
70         %$mailboxes = (%$mailboxes, %{$imapd->{mailboxes}});
71         $imapd->{mailboxes} = $mailboxes;
72         $imapd->{inboxlist} = [
73                 map {
74                         my $no = $mailboxes->{$_} == $dummy ? '' : 'No';
75                         my $u = $_; # capitalize "INBOX" for user-familiarity
76                         $u =~ s/\Ainbox(\.|\z)/INBOX$1/i;
77                         qq[* LIST (\\Has${no}Children) "." $u\r\n]
78                 } keys %$mailboxes
79         ];
80         $imapd->{pi_config} = $pi_config;
81         if (my $idler = $imapd->{idler}) {
82                 $idler->refresh($pi_config);
83         }
84 }
85
86 sub imapd_refresh_step { # pi_config->iterate_start cb
87         my ($pi_config, $section, $imapd) = @_;
88         if (defined($section)) {
89                 return if $section !~ m!\Apublicinbox\.([^/]+)\z!;
90                 my $ibx = $pi_config->lookup_name($1) or return;
91                 imapd_refresh_ibx($ibx, $imapd->{imapd_next});
92         } else { # undef == "EOF"
93                 imapd_refresh_finalize($imapd, $pi_config);
94         }
95 }
96
97 sub refresh_groups {
98         my ($self, $sig) = @_;
99         my $pi_config = PublicInbox::Config->new;
100         if ($sig) { # SIGHUP is handled through the event loop
101                 $self->{imapd_next} = { dummies => {}, mailboxes => {} };
102                 my $iter = PublicInbox::ConfigIter->new($pi_config,
103                                                 \&imapd_refresh_step, $self);
104                 $iter->event_step;
105         } else { # initial start is synchronous
106                 $self->{dummies} = {};
107                 $pi_config->each_inbox(\&imapd_refresh_ibx, $self);
108                 imapd_refresh_finalize($self, $pi_config);
109         }
110 }
111
112 sub idler_start {
113         $_[0]->{idler} //= PublicInbox::InboxIdle->new($_[0]->{pi_config});
114 }
115
116 1;