]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/IMAPD.pm
imap: support LIST command
[public-inbox.git] / lib / PublicInbox / IMAPD.pm
index 1922c16046a3119723e560eb18fdfc010bbc9bd2..a3a2598661bbf97d09b0c5ddb550f5c9faeca506 100644 (file)
@@ -16,18 +16,47 @@ sub new {
                out => \*STDOUT,
                grouplist => [],
                # accept_tls => { SSL_server => 1, ..., SSL_reuse_ctx => ... }
+               # pi_config => PublicInbox::Config
                # idler => PublicInbox::InboxIdle
        }, $class;
 }
 
-sub refresh_groups {
+sub refresh_inboxlist ($) {
        my ($self) = @_;
-       if (my $old_idler = delete $self->{idler}) {
-               $old_idler->close; # PublicInbox::DS::close
+       my @names = map { $_->{newsgroup} } @{delete $self->{grouplist}};
+       my %ns; # "\Noselect \HasChildren"
+       for (@names) {
+               my $up = $_;
+               while ($up =~ s/\.[^\.]+\z//) {
+                       $ns{$up} = '\\Noselect \\HasChildren';
+               }
        }
-       my $pi_config = PublicInbox::Config->new;
-       $self->{idler} = PublicInbox::InboxIdle->new($pi_config);
+       @names = map {;
+               my $at = delete($ns{$_}) ? '\\HasChildren' : '\\HasNoChildren';
+               qq[* LIST ($at) "." $_\r\n]
+       } @names;
+       push(@names, map { qq[* LIST ($ns{$_}) "." $_\r\n] } keys %ns);
+       @names = sort {
+               my ($xa) = ($a =~ / (\S+)\r\n/g);
+               my ($xb) = ($b =~ / (\S+)\r\n/g);
+               length($xa) <=> length($xb);
+       } @names;
+       $self->{inboxlist} = \@names;
+}
+
+sub refresh_groups {
+       my ($self) = @_;
+       my $pi_config = $self->{pi_config} = PublicInbox::Config->new;
        $self->SUPER::refresh_groups($pi_config);
+       refresh_inboxlist($self);
+
+       if (my $idler = $self->{idler}) {
+               $idler->refresh($pi_config);
+       }
+}
+
+sub idler_start {
+       $_[0]->{idler} //= PublicInbox::InboxIdle->new($_[0]->{pi_config});
 }
 
 1;