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