]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/IMAP.pm
imap: split ->logged_in attribute into a separate class
[public-inbox.git] / lib / PublicInbox / IMAP.pm
index 0fe31a77244d16b07e211763b6e4cd4d256a546a..803ce31ff2284adb576bff65faf438c8fcb9f1a8 100644 (file)
@@ -16,7 +16,7 @@
 package PublicInbox::IMAP;
 use strict;
 use base qw(PublicInbox::DS);
-use fields qw(imapd logged_in ibx long_cb -login_tag
+use fields qw(imapd ibx long_cb -login_tag
        uid_min -idle_tag -idle_max);
 use PublicInbox::Eml;
 use PublicInbox::EmlContentFoo qw(parse_content_disposition);
@@ -27,6 +27,7 @@ use Text::ParseWords qw(parse_line);
 use Errno qw(EAGAIN);
 use Time::Local qw(timegm);
 use POSIX qw(strftime);
+use Hash::Util qw(unlock_hash); # dependency of fields for perl 5.10+, anyways
 
 my $Address;
 for my $mod (qw(Email::Address::XS Mail::Address)) {
@@ -41,8 +42,9 @@ sub LINE_MAX () { 512 } # does RFC 3501 have a limit like RFC 977?
 sub UID_BLOCK () { 50_000 }
 
 # these values area also used for sorting
-sub NEED_BLOB () { 1 }
-sub NEED_EML () { NEED_BLOB|2 }
+sub NEED_SMSG () { 1 }
+sub NEED_BLOB () { NEED_SMSG|2 }
+sub NEED_EML () { NEED_BLOB|4 }
 my $OP_EML_NEW = [ NEED_EML - 1, \&op_eml_new ];
 
 my %FETCH_NEED = (
@@ -57,7 +59,7 @@ my %FETCH_NEED = (
        BODYSTRUCTURE => [ NEED_EML, \&emit_bodystructure ],
        ENVELOPE => [ NEED_EML, \&emit_envelope ],
        FLAGS => [ 0, \&emit_flags ],
-       INTERNALDATE => [ 0, \&emit_internaldate ],
+       INTERNALDATE => [ NEED_SMSG, \&emit_internaldate ],
 );
 my %FETCH_ATT = map { $_ => [ $_ ] } keys %FETCH_NEED;
 
@@ -79,6 +81,9 @@ my @MoY = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
 my %MoY;
 @MoY{@MoY} = (0..11);
 
+# RFC 3501 5.4. Autologout Timer needs to be >= 30min
+$PublicInbox::DS::EXPTIME = 60 * 30;
+
 sub greet ($) {
        my ($self) = @_;
        my $capa = capa($self);
@@ -87,7 +92,8 @@ sub greet ($) {
 
 sub new ($$$) {
        my ($class, $sock, $imapd) = @_;
-       my $self = fields::new($class);
+       my $self = fields::new('PublicInbox::IMAP_preauth');
+       unlock_hash(%$self);
        my $ev = EPOLLIN;
        my $wbuf;
        if ($sock->can('accept_SSL') && !$sock->accept_SSL) {
@@ -106,13 +112,15 @@ sub new ($$$) {
        $self;
 }
 
+sub logged_in { 1 }
+
 sub capa ($) {
        my ($self) = @_;
 
        # dovecot advertises IDLE pre-login; perhaps because some clients
        # depend on it, so we'll do the same
        my $capa = 'CAPABILITY IMAP4rev1 IDLE';
-       if ($self->{logged_in}) {
+       if ($self->logged_in) {
                $capa .= ' COMPRESS=DEFLATE';
        } else {
                if (!($self->{sock} // $self)->can('accept_SSL') &&
@@ -125,7 +133,7 @@ sub capa ($) {
 
 sub login_success ($$) {
        my ($self, $tag) = @_;
-       $self->{logged_in} = 1;
+       bless $self, 'PublicInbox::IMAP';
        my $capa = capa($self);
        "$tag OK [$capa] Logged in\r\n";
 }
@@ -150,7 +158,7 @@ sub cmd_close ($$) {
 
 sub cmd_logout ($$) {
        my ($self, $tag) = @_;
-       delete $self->{logged_in};
+       delete $self->{-idle_tag};
        $self->write(\"* BYE logging out\r\n$tag OK Logout done\r\n");
        $self->shutdn; # PublicInbox::DS::shutdn
        undef;
@@ -173,23 +181,63 @@ sub cmd_noop ($$) { "$_[1] OK Noop done\r\n" }
 sub on_inbox_unlock {
        my ($self, $ibx) = @_;
        my $new = $ibx->mm->max;
+       my $uid_end = ($self->{uid_min} // 1) - 1 + UID_BLOCK;
        defined(my $old = $self->{-idle_max}) or die 'BUG: -idle_max unset';
+       $new = $uid_end if $new > $uid_end;
        if ($new > $old) {
                $self->{-idle_max} = $new;
                $self->msg_more("* $_ EXISTS\r\n") for (($old + 1)..($new - 1));
                $self->write(\"* $new EXISTS\r\n");
+       } elsif ($new == $uid_end) { # max exceeded $uid_end
+               # continue idling w/o inotify
+               delete $self->{-idle_max};
+               my $sock = $self->{sock} or return;
+               $ibx->unsubscribe_unlock(fileno($sock));
        }
 }
 
+# called every X minute(s) or so by PublicInbox::DS::later
+my $IDLERS = {};
+my $idle_timer;
+sub idle_tick_all {
+       my $old = $IDLERS;
+       $IDLERS = {};
+       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;
+}
+
 sub cmd_idle ($$) {
        my ($self, $tag) = @_;
        # 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->max // 0;
-       "+ idling\r\n"
+       my $max = $ibx->mm->max // 0;
+       my $uid_end = ($self->{uid_min} // 1) - 1 + UID_BLOCK;
+       my $sock = $self->{sock} or return;
+       my $fd = fileno($sock);
+       # only do inotify on most recent slice
+       if ($max < $uid_end) {
+               $ibx->subscribe_unlock($fd, $self);
+               $self->{imapd}->idler_start;
+               $self->{-idle_max} = $max;
+       }
+       $idle_timer //= PublicInbox::DS::later(\&idle_tick_all);
+       $IDLERS->{$fd} = $self;
+       \"+ idling\r\n"
+}
+
+sub stop_idle ($$) {
+       my ($self, $ibx);
+       my $sock = $self->{sock} or return;
+       my $fd = fileno($sock);
+       delete $IDLERS->{$fd};
+       $ibx->unsubscribe_unlock($fd);
 }
 
 sub cmd_done ($$) {
@@ -200,7 +248,7 @@ sub cmd_done ($$) {
                warn "BUG: idle_tag set w/o inbox";
                return "$tag BAD internal bug\r\n";
        };
-       $ibx->unsubscribe_unlock(fileno($self->{sock}));
+       stop_idle($self, $ibx);
        "$idle_tag OK Idle done\r\n";
 }
 
@@ -578,6 +626,38 @@ sub uid_fetch_smsg { # long_response
        1; # more
 }
 
+sub uid_fetch_uid { # long_response
+       my ($self, $tag, $uids, $range_info, $ops) = @_;
+       while (!@$uids) { # rare
+               my ($beg, $end, $range_csv) = @$range_info;
+               if (scalar(@$uids = @{$self->{ibx}->over->
+                                       uid_range($beg, $end)})) {
+                       $range_info->[0] = $uids->[-1] + 1;
+               } elsif (!$range_csv) {
+                       $self->write(\"$tag OK Fetch done\r\n");
+                       return;
+               } else {
+                       my $next_range = range_step($self, \$range_csv);
+                       if (!ref($next_range)) { # error
+                               $self->write(\"$tag $next_range\r\n");
+                               return;
+                       }
+                       @$range_info = @$next_range;
+               }
+               # continue looping
+       }
+       for (@$uids) {
+               $self->msg_more("* $_ FETCH (UID $_");
+               for (my $i = 0; $i < @$ops;) {
+                       my $k = $ops->[$i++];
+                       $ops->[$i++]->($self, $k);
+               }
+               $self->msg_more(")\r\n");
+       }
+       @$uids = ();
+       1; # more
+}
+
 sub cmd_status ($$$;@) {
        my ($self, $tag, $mailbox, @items) = @_;
        return "$tag BAD no items\r\n" if !scalar(@items);
@@ -794,7 +874,8 @@ sub fetch_compile ($) {
                $r[2] = [ map { [ $_, @{$partial{$_}} ] } sort keys %partial ];
        }
 
-       $r[0] = $need ? \&uid_fetch_msg : \&uid_fetch_smsg;
+       $r[0] = $need & NEED_BLOB ? \&uid_fetch_msg :
+               ($need & NEED_SMSG ? \&uid_fetch_smsg : \&uid_fetch_uid);
 
        # r[1] = [ $key1, $cb1, $key2, $cb2, ... ]
        use sort 'stable'; # makes output more consistent
@@ -1151,9 +1232,7 @@ sub busy {
 sub close {
        my ($self) = @_;
        if (my $ibx = delete $self->{ibx}) {
-               if (my $sock = $self->{sock}) {;
-                       $ibx->unsubscribe_unlock(fileno($sock));
-               }
+               stop_idle($self, $ibx);
        }
        $self->SUPER::close; # PublicInbox::DS::close
 }
@@ -1163,4 +1242,9 @@ no warnings 'once';
 *cmd_select = \&cmd_examine;
 *cmd_fetch = \&cmd_uid_fetch;
 
+package PublicInbox::IMAP_preauth;
+our @ISA = qw(PublicInbox::IMAP);
+
+sub logged_in { 0 }
+
 1;