]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/IMAPD.pm
d8814324f23e333ba4b5016f36f5c619b9d582b6
[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 (currently a singleton),
5 # see script/public-inbox-imapd for how it is used
6 package PublicInbox::IMAPD;
7 use strict;
8 use v5.10.1;
9 use PublicInbox::Config;
10 use PublicInbox::ConfigIter;
11 use PublicInbox::InboxIdle;
12 use PublicInbox::IMAPdeflate; # loads PublicInbox::IMAP
13 use PublicInbox::DummyInbox;
14 my $dummy = bless { uidvalidity => 0 }, 'PublicInbox::DummyInbox';
15
16 sub new {
17         my ($class) = @_;
18         bless {
19                 mailboxes => {},
20                 err => \*STDERR,
21                 out => \*STDOUT,
22                 # accept_tls => { SSL_server => 1, ..., SSL_reuse_ctx => ... }
23                 # pi_cfg => PublicInbox::Config
24                 # idler => PublicInbox::InboxIdle
25         }, $class;
26 }
27
28 sub imapd_refresh_ibx { # pi_cfg->each_inbox cb
29         my ($ibx, $imapd) = @_;
30         my $ngname = $ibx->{newsgroup} or return;
31
32         # We require lower-case since IMAP mailbox names are
33         # case-insensitive (but -nntpd matches INN in being
34         # case-sensitive
35         if ($ngname =~ m![^a-z0-9/_\.\-\~\@\+\=:]! ||
36                         # don't confuse with 50K slices
37                         $ngname =~ /\.[0-9]+\z/) {
38                 warn "mailbox name invalid: newsgroup=`$ngname'\n";
39                 return;
40         }
41         $ibx->over or return;
42         $ibx->{over} = 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         eval { $ibx->uidvalidity };
48         my $mm = delete($ibx->{mm}) or return;
49         defined($ibx->{uidvalidity}) or return;
50         PublicInbox::IMAP::ensure_slices_exist($imapd, $ibx, $mm->max);
51
52         # preload to avoid fragmentation:
53         $ibx->description;
54         $ibx->base_url;
55
56         # ensure dummies are selectable
57         my $dummies = $imapd->{dummies};
58         do {
59                 $dummies->{$ngname} = $dummy;
60         } while ($ngname =~ s/\.[^\.]+\z//);
61 }
62
63 sub imapd_refresh_finalize {
64         my ($imapd, $pi_cfg) = @_;
65         my $mailboxes;
66         if (my $next = delete $imapd->{imapd_next}) {
67                 $imapd->{mailboxes} = delete $next->{mailboxes};
68                 $mailboxes = delete $next->{dummies};
69         } else {
70                 $mailboxes = delete $imapd->{dummies};
71         }
72         %$mailboxes = (%$mailboxes, %{$imapd->{mailboxes}});
73         $imapd->{mailboxes} = $mailboxes;
74         $imapd->{mailboxlist} = [
75                 map { $_->[2] }
76                 sort { $a->[0] cmp $b->[0] || $a->[1] <=> $b->[1] }
77                 map {
78                         my $u = $_; # capitalize "INBOX" for user-familiarity
79                         $u =~ s/\Ainbox(\.|\z)/INBOX$1/i;
80                         if ($mailboxes->{$_} == $dummy) {
81                                 [ $u, -1,
82                                   qq[* LIST (\\HasChildren) "." $u\r\n]]
83                         } else {
84                                 $u =~ /\A(.+)\.([0-9]+)\z/ or
85                                         die "BUG: `$u' has no slice digit(s)";
86                                 [ $1, $2 + 0,
87                                   qq[* LIST (\\HasNoChildren) "." $u\r\n] ]
88                         }
89                 } keys %$mailboxes
90         ];
91         $imapd->{pi_cfg} = $pi_cfg;
92         if (my $idler = $imapd->{idler}) {
93                 $idler->refresh($pi_cfg);
94         }
95 }
96
97 sub imapd_refresh_step { # PublicInbox::ConfigIter cb
98         my ($pi_cfg, $section, $imapd) = @_;
99         if (defined($section)) {
100                 return if $section !~ m!\Apublicinbox\.([^/]+)\z!;
101                 my $ibx = $pi_cfg->lookup_name($1) or return;
102                 imapd_refresh_ibx($ibx, $imapd->{imapd_next});
103         } else { # undef == "EOF"
104                 imapd_refresh_finalize($imapd, $pi_cfg);
105         }
106 }
107
108 sub refresh_groups {
109         my ($self, $sig) = @_;
110         my $pi_cfg = PublicInbox::Config->new;
111         if ($sig) { # SIGHUP is handled through the event loop
112                 $self->{imapd_next} = { dummies => {}, mailboxes => {} };
113                 my $iter = PublicInbox::ConfigIter->new($pi_cfg,
114                                                 \&imapd_refresh_step, $self);
115                 $iter->event_step;
116         } else { # initial start is synchronous
117                 $self->{dummies} = {};
118                 $pi_cfg->each_inbox(\&imapd_refresh_ibx, $self);
119                 imapd_refresh_finalize($self, $pi_cfg);
120         }
121 }
122
123 sub idler_start {
124         $_[0]->{idler} //= PublicInbox::InboxIdle->new($_[0]->{pi_cfg});
125 }
126
127 1;