]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/IMAP.pm
imap: STATUS: count messages properly
[public-inbox.git] / lib / PublicInbox / IMAP.pm
index 37e07daedeefe0edc510d215e5aa8df35a071e75..7e695fd8e0eb20c5e37ffb10152cd361967aa1b1 100644 (file)
@@ -1,4 +1,4 @@
-# Copyright (C) 2020-2021 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>
 #
 # Each instance of this represents an IMAP client connected to
@@ -99,9 +99,6 @@ undef %FETCH_NEED;
 my $valid_range = '[0-9]+|[0-9]+:[0-9]+|[0-9]+:\*';
 $valid_range = qr/\A(?:$valid_range)(?:,(?:$valid_range))*\z/;
 
-# RFC 3501 5.4. Autologout Timer needs to be >= 30min
-$PublicInbox::DS::EXPTIME = 60 * 30;
-
 sub greet ($) {
        my ($self) = @_;
        my $capa = capa($self);
@@ -124,7 +121,6 @@ sub new ($$$) {
        } else {
                greet($self);
        }
-       $self->update_idle_time;
        $self;
 }
 
@@ -316,20 +312,18 @@ sub on_inbox_unlock {
        }
 }
 
-# called every X minute(s) or so by PublicInbox::DS::later
-my $IDLERS = {};
-my $idle_timer;
+# called every minute or so by PublicInbox::DS::later
+my $IDLERS; # fileno($obj->{sock}) => PublicInbox::IMAP
 sub idle_tick_all {
        my $old = $IDLERS;
-       $IDLERS = {};
+       $IDLERS = undef;
        for my $i (values %$old) {
                next if ($i->{wbuf} || !exists($i->{-idle_tag}));
-               $i->update_idle_time or next;
                $IDLERS->{fileno($i->{sock})} = $i;
                $i->write(\"* OK Still here\r\n");
        }
-       $idle_timer = scalar keys %$IDLERS ?
-                       PublicInbox::DS::later(\&idle_tick_all) : undef;
+       $IDLERS and
+               PublicInbox::DS::add_uniq_timer('idle', 60, \&idle_tick_all);
 }
 
 sub cmd_idle ($$) {
@@ -346,7 +340,7 @@ sub cmd_idle ($$) {
                $ibx->subscribe_unlock($fd, $self);
                $self->{imapd}->idler_start;
        }
-       $idle_timer //= PublicInbox::DS::later(\&idle_tick_all);
+       PublicInbox::DS::add_uniq_timer('idle', 60, \&idle_tick_all);
        $IDLERS->{$fd} = $self;
        \"+ idling\r\n"
 }
@@ -384,7 +378,7 @@ sub ensure_slices_exist ($$$) {
                push @created, $sub_mailbox;
        }
        return unless @created;
-       my $l = $imapd->{inboxlist} or return;
+       my $l = $imapd->{mailboxlist} or return;
        push @$l, map { qq[* LIST (\\HasNoChildren) "." $_\r\n] } @created;
 }
 
@@ -399,14 +393,14 @@ sub inbox_lookup ($$;$) {
                                die "BUG: unexpected dummy mailbox: $mailbox\n";
                $uid_base = $1 * UID_SLICE;
 
-               # ->num_highwater caches for writers, so use ->meta_accessor
-               $uidmax = $ibx->mm->meta_accessor('num_highwater') // 0;
+               $uidmax = $ibx->mm->num_highwater // 0;
                if ($examine) {
                        $self->{uid_base} = $uid_base;
                        $self->{ibx} = $ibx;
                        $self->{uo2m} = uo2m_ary_new($self, \$exists);
                } else {
-                       $exists = $over->imap_exists;
+                       my $uid_end = $uid_base + UID_SLICE;
+                       $exists = $over->imap_exists($uid_base, $uid_end);
                }
                ensure_slices_exist($self->{imapd}, $ibx, $over->max);
        } else {
@@ -850,7 +844,7 @@ sub cmd_status ($$$;@) {
 my %patmap = ('*' => '.*', '%' => '[^\.]*');
 sub cmd_list ($$$$) {
        my ($self, $tag, $refname, $wildcard) = @_;
-       my $l = $self->{imapd}->{inboxlist};
+       my $l = $self->{imapd}->{mailboxlist};
        if ($refname eq '' && $wildcard eq '') {
                # request for hierarchy delimiter
                $l = [ qq[* LIST (\\Noselect) "." ""\r\n] ];
@@ -878,12 +872,12 @@ sub eml_index_offs_i { # PublicInbox::Eml::each_part callback
 # prepares an index for BODY[$SECTION_IDX] fetches
 sub eml_body_idx ($$) {
        my ($eml, $section_idx) = @_;
-       my $idx = $eml->{imap_all_parts} //= do {
+       my $idx = $eml->{imap_all_parts} // do {
                my $all = {};
                $eml->each_part(\&eml_index_offs_i, $all, 0, 1);
                # top-level of multipart, BODY[0] not allowed (nz-number)
                delete $all->{0};
-               $all;
+               $eml->{imap_all_parts} = $all;
        };
        $idx->{$section_idx};
 }
@@ -1160,15 +1154,6 @@ sub cmd_search ($$$;) {
        search_common($self, $tag, $query, 1);
 }
 
-sub args_ok ($$) { # duplicated from PublicInbox::NNTP
-       my ($cb, $argc) = @_;
-       my $tot = prototype $cb;
-       my ($nreq, undef) = split(';', $tot);
-       $nreq = ($nreq =~ tr/$//) - 1;
-       $tot = ($tot =~ tr/$//) - 1;
-       ($argc <= $tot && $argc >= $nreq);
-}
-
 # returns 1 if we can continue, 0 if not due to buffered writes or disconnect
 sub process_line ($$) {
        my ($self, $l) = @_;
@@ -1227,8 +1212,6 @@ sub long_step {
                out($self, " deferred[$fd] aborted - %0.6f", $elapsed);
                $self->close;
        } elsif ($more) { # $self->{wbuf}:
-               $self->update_idle_time;
-
                # control passed to ibx_async_cat if $more == \undef
                requeue_once($self) if !ref($more);
        } else { # all done!
@@ -1270,7 +1253,6 @@ sub event_step {
 
        return unless $self->flush_write && $self->{sock} && !$self->{long_cb};
 
-       $self->update_idle_time;
        # only read more requests if we've drained the write buffer,
        # otherwise we can be buffering infinitely w/o backpressure
 
@@ -1296,7 +1278,6 @@ sub event_step {
 
        return $self->close if $r < 0;
        $self->rbuf_idle($rbuf);
-       $self->update_idle_time;
 
        # maybe there's more pipelined data, or we'll have
        # to register it for socket-readiness notifications
@@ -1335,14 +1316,14 @@ sub cmd_starttls ($$) {
        undef;
 }
 
-# for graceful shutdown in PublicInbox::Daemon:
-sub busy {
-       my ($self, $now) = @_;
+sub busy { # for graceful shutdown in PublicInbox::Daemon:
+       my ($self) = @_;
        if (defined($self->{-idle_tag})) {
                $self->write(\"* BYE server shutting down\r\n");
                return; # not busy anymore
        }
-       ($self->{rbuf} || $self->{wbuf} || $self->not_idle_long($now));
+       defined($self->{rbuf}) || defined($self->{wbuf}) ||
+               !$self->write(\"* BYE server shutting down\r\n");
 }
 
 sub close {