]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/NetReader.pm
watch: IMAP: ignore \Deleted and \Draft messages
[public-inbox.git] / lib / PublicInbox / NetReader.pm
index 2a45321769b19230e37ec2e1e23d6f9313642994..d3094fc7137eeb6546f1596ff3fa7d7d669ca399 100644 (file)
@@ -10,11 +10,7 @@ use PublicInbox::Eml;
 
 our %IMAPflags2kw = map {; "\\\u$_" => $_ } qw(seen answered flagged draft);
 
-# TODO: trim this down, this is huge
-our @EXPORT = qw(uri_new uri_section
-               nn_new imap_uri nntp_uri
-               cfg_bool cfg_intvl imap_common_init nntp_common_init
-               );
+our @EXPORT = qw(uri_section imap_uri nntp_uri);
 
 # returns the git config section name, e.g [imap "imaps://user@example.com"]
 # without the mailbox, so we can share connections between different inboxes
@@ -94,15 +90,6 @@ sub mic_for { # mic = Mail::IMAPClient
        $mic;
 }
 
-sub uri_new {
-       my ($url) = @_;
-       require URI;
-
-       # URI::snews exists, URI::nntps does not, so use URI::snews
-       $url =~ s!\Anntps://!snews://!i;
-       URI->new($url);
-}
-
 # Net::NNTP doesn't support CAPABILITIES, yet
 sub try_starttls ($) {
        my ($host) = @_;
@@ -359,13 +346,27 @@ sub _imap_do_msg ($$$$$) {
        $$raw =~ s/\r\n/\n/sg;
        my $kw = [];
        for my $f (split(/ /, $flags)) {
-               my $k = $IMAPflags2kw{$f} // next; # TODO: X-Label?
-               push @$kw, $k;
+               if (my $k = $IMAPflags2kw{$f}) {
+                       push @$kw, $k;
+               } elsif ($f eq "\\Recent") { # not in JMAP
+               } elsif ($f eq "\\Deleted") { # not in JMAP
+                       return;
+               } elsif ($self->{verbose}) {
+                       warn "# unknown IMAP flag $f <$uri;uid=$uid>\n";
+               }
        }
+       @$kw = sort @$kw; # for all UI/UX purposes
        my ($eml_cb, @args) = @{$self->{eml_each}};
        $eml_cb->($uri, $uid, $kw, PublicInbox::Eml->new($raw), @args);
 }
 
+sub run_commit_cb ($) {
+       my ($self) = @_;
+       my $cmt_cb_args = $self->{on_commit} or return;
+       my ($cb, @args) = @$cmt_cb_args;
+       $cb->(@args);
+}
+
 sub _imap_fetch_all ($$$) {
        my ($self, $mic, $uri) = @_;
        my $sec = uri_section($uri);
@@ -414,8 +415,10 @@ sub _imap_fetch_all ($$$) {
                # I wish "UID FETCH $START:*" could work, but:
                # 1) servers do not need to return results in any order
                # 2) Mail::IMAPClient doesn't offer a streaming API
-               $uids = $mic->search("UID $l_uid:*") or
+               unless ($uids = $mic->search("UID $l_uid:*")) {
+                       return if $!{EINTR} && $self->{quit};
                        return "E: $uri UID SEARCH $l_uid:* error: $!";
+               }
                return if scalar(@$uids) == 0;
 
                # RFC 3501 doesn't seem to indicate order of UID SEARCH
@@ -437,6 +440,7 @@ sub _imap_fetch_all ($$$) {
                        local $0 = "UID:$batch $mbx $sec";
                        my $r = $mic->fetch_hash($batch, $req, 'FLAGS');
                        unless ($r) { # network error?
+                               last if $!{EINTR} && $self->{quit};
                                $err = "E: $uri UID FETCH $batch error: $!";
                                last;
                        }
@@ -451,6 +455,7 @@ sub _imap_fetch_all ($$$) {
                        }
                        last if $self->{quit};
                }
+               run_commit_cb($self);
                $itrk->update_last($r_uidval, $last_uid) if $itrk;
        } until ($err || $self->{quit});
        $err;
@@ -490,7 +495,7 @@ sub imap_each {
                local $self->{eml_each} = [ $eml_cb, @args ];
                $err = _imap_fetch_all($self, $mic, $uri);
        } else {
-               $err = "E: not connected: $!";
+               $err = "E: <$uri> not connected: $!";
        }
        warn $err if $err;
        $mic;
@@ -555,6 +560,7 @@ sub _nntp_fetch_all ($$$) {
                last if $self->{quit};
                $art = $_;
                if (--$n < 0) {
+                       run_commit_cb($self);
                        $itrk->update_last(0, $last_art) if $itrk;
                        $n = $self->{max_batch};
                }
@@ -575,6 +581,7 @@ sub _nntp_fetch_all ($$$) {
                $eml_cb->($uri, $art, [], PublicInbox::Eml->new(\$raw), @args);
                $last_art = $art;
        }
+       run_commit_cb($self);
        $itrk->update_last(0, $last_art) if $itrk;
        $err;
 }
@@ -585,12 +592,13 @@ sub nntp_each {
        my $sec = uri_section($uri);
        local $0 = $uri->group ." $sec";
        my $nn = nn_get($self, $uri);
+       return if $self->{quit};
        my $err;
        if ($nn) {
                local $self->{eml_each} = [ $eml_cb, @args ];
                $err = _nntp_fetch_all($self, $nn, $uri);
        } else {
-               $err = "E: not connected: $!";
+               $err = "E: <$uri> not connected: $!";
        }
        warn $err if $err;
        $nn;