]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/IMAP.pm
imap: implement STATUS command
[public-inbox.git] / lib / PublicInbox / IMAP.pm
index 99c6c817fd799b48995e068d232d09467d12770b..a2d59e5cccca9014e26416820bfdb4aca83b87d7 100644 (file)
@@ -146,7 +146,7 @@ sub cmd_noop ($$) { "$_[1] OK NOOP completed\r\n" }
 # called by PublicInbox::InboxIdle
 sub on_inbox_unlock {
        my ($self, $ibx) = @_;
-       my $new = ($ibx->mm->minmax)[1];
+       my $new = $ibx->mm->max;
        defined(my $old = $self->{-idle_max}) or die 'BUG: -idle_max unset';
        if ($new > $old) {
                $self->{-idle_max} = $new;
@@ -160,8 +160,9 @@ sub cmd_idle ($$) {
        # IDLE seems allowed by dovecot w/o a mailbox selected *shrug*
        my $ibx = $self->{ibx} or return "$tag BAD no mailbox selected\r\n";
        $ibx->subscribe_unlock(fileno($self->{sock}), $self);
+       $self->{imapd}->idler_start;
        $self->{-idle_tag} = $tag;
-       $self->{-idle_max} = ($ibx->mm->minmax)[1] // 0;
+       $self->{-idle_max} = $ibx->mm->max // 0;
        "+ idling\r\n"
 }
 
@@ -182,7 +183,7 @@ sub cmd_examine ($$$) {
        my $ibx = $self->{imapd}->{groups}->{$mailbox} or
                return "$tag NO Mailbox doesn't exist: $mailbox\r\n";
        my $mm = $ibx->mm;
-       my $max = $mm->num_highwater // 0;
+       my $max = $mm->max // 0;
        # RFC 3501 2.3.1.1 -  "A good UIDVALIDITY value to use in
        # this case is a 32-bit representation of the creation
        # date/time of the mailbox"
@@ -305,6 +306,35 @@ sub uid_fetch_m { # long_response
        1;
 }
 
+sub cmd_status ($$$;@) {
+       my ($self, $tag, $mailbox, @items) = @_;
+       my $ibx = $self->{imapd}->{groups}->{$mailbox} or
+               return "$tag NO Mailbox doesn't exist: $mailbox\r\n";
+       return "$tag BAD no items\r\n" if !scalar(@items);
+       ($items[0] !~ s/\A\(//s || $items[-1] !~ s/\)\z//s) and
+               return "$tag BAD invalid args\r\n";
+
+       my $mm = $ibx->mm;
+       my ($max, @it);
+       for my $it (@items) {
+               $it = uc($it);
+               push @it, $it;
+               if ($it =~ /\A(?:MESSAGES|UNSEEN|RECENT)\z/) {
+                       push(@it, ($max //= $mm->max // 0));
+               } elsif ($it eq 'UIDNEXT') {
+                       push(@it, ($max //= $mm->max // 0) + 1);
+               } elsif ($it eq 'UIDVALIDITY') {
+                       push(@it, $mm->created_at //
+                               return("$tag BAD UIDVALIDITY\r\n"));
+               } else {
+                       return "$tag BAD invalid item\r\n";
+               }
+       }
+       return "$tag BAD no items\r\n" if !@it;
+       "* STATUS $mailbox (".join(' ', @it).")\r\n" .
+       "$tag OK Status complete\r\n";
+}
+
 sub cmd_uid_fetch ($$$;@) {
        my ($self, $tag, $range, @want) = @_;
        my $ibx = $self->{ibx} or return "$tag BAD No mailbox selected\r\n";
@@ -320,7 +350,7 @@ sub cmd_uid_fetch ($$$;@) {
        if ($range =~ /\A([0-9]+):([0-9]+)\z/s) {
                ($beg, $end) = ($1, $2);
        } elsif ($range =~ /\A([0-9]+):\*\z/s) {
-               ($beg, $end) =  ($1, $ibx->mm->num_highwater // 0);
+               ($beg, $end) =  ($1, $ibx->mm->max // 0);
        } elsif ($range =~ /\A[0-9]+\z/) {
                my $smsg = $ibx->over->get_art($range) or return "$tag OK\r\n";
                push @$msgs, $smsg;
@@ -365,7 +395,7 @@ sub cmd_uid_search ($$$;) {
        } elsif ($arg eq 'UID' && scalar(@rest) == 1) {
                if ($rest[0] =~ /\A([0-9]+):([0-9]+|\*)\z/s) {
                        my ($beg, $end) = ($1, $2);
-                       $end = ($ibx->mm->minmax)[1] if $end eq '*';
+                       $end = $ibx->mm->max if $end eq '*';
                        $self->msg_more('* SEARCH');
                        long_response($self, \&uid_search_uid_range,
                                        $tag, $ibx, \$beg, $end);