]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/LeiRefreshMailSync.pm
imap+nntp: share COMPRESS implementation
[public-inbox.git] / lib / PublicInbox / LeiRefreshMailSync.pm
index 71fc348c491e9850f01151b7577d7b9dd44539bd..a60a9a5e9761ca2ca89320adfd121d291b070e8b 100644 (file)
@@ -7,37 +7,27 @@ package PublicInbox::LeiRefreshMailSync;
 use strict;
 use v5.10.1;
 use parent qw(PublicInbox::IPC PublicInbox::LeiInput);
-use PublicInbox::LeiExportKw;
+use PublicInbox::LeiImport;
 use PublicInbox::InboxWritable qw(eml_from_path);
-use PublicInbox::ContentHash qw(git_sha);
 use PublicInbox::Import;
 
-sub eml_match ($$) {
-       my ($eml, $oidbin) = @_;
-       $eml->header_set($_) for @PublicInbox::Import::UNWANTED_HEADERS;
-       $oidbin eq git_sha(length($oidbin) == 20 ? 1 : 256, $eml)->digest;
+sub folder_missing { # may be called by LeiInput
+       my ($self, $folder) = @_;
+       $self->{lms}->forget_folders($folder);
 }
 
 sub prune_mdir { # lms->each_src callback
        my ($oidbin, $id, $self, $mdir) = @_;
        my @try = $$id =~ /:2,[a-zA-Z]*\z/ ? qw(cur new) : qw(new cur);
-       for my $d (@try) {
-               my $src = "$mdir/$d/$$id";
-               if ($self->{verify}) {
-                       my $eml = eml_from_path($src) // next;
-                       return if eml_match($eml, $oidbin);
-               } elsif (-f $src) {
-                       return;
-               }
-       }
+       for (@try) { return if -f "$mdir/$_/$$id" }
        # both tries failed
-       $self->{lei}->{sto}->ipc_do('lms_clear_src', "maildir:$mdir", $id);
+       $self->{lms}->clear_src("maildir:$mdir", $id);
 }
 
 sub prune_imap { # lms->each_src callback
        my ($oidbin, $uid, $self, $uids, $url) = @_;
        return if exists $uids->{$uid};
-       $self->{lei}->{sto}->ipc_do('lms_clear_src', $url, $uid);
+       $self->{lms}->clear_src($url, $uid);
 }
 
 # detects missed file moves
@@ -46,29 +36,31 @@ sub pmdir_cb { # called via LeiPmdir->each_mdir_fn
        my ($folder, $bn) = ($f =~ m!\A(.+?)/(?:new|cur)/([^/]+)\z!) or
                die "BUG: $f was not from a Maildir?";
        substr($folder, 0, 0) = 'maildir:'; # add prefix
-       my $lms = $self->{-lms_ro} //= $self->{lei}->lms;
-       return if defined($lms->name_oidbin($folder, $bn));
+       return if scalar($self->{lms}->name_oidbin($folder, $bn));
        my $eml = eml_from_path($f) // return;
        my $oidbin = $self->{lei}->git_oid($eml)->digest;
-       $self->{lei}->{sto}->ipc_do('lms_set_src', $oidbin, $folder, \$bn);
+       $self->{lms}->set_src($oidbin, $folder, \$bn);
 }
 
 sub input_path_url { # overrides PublicInbox::LeiInput::input_path_url
        my ($self, $input, @args) = @_;
-       my $lms = $self->{-lms_ro} //= $self->{lei}->lms;
        if ($input =~ /\Amaildir:(.+)/i) {
-               $lms->each_src($input, \&prune_mdir, $self, my $mdir = $1);
+               $self->{lms}->each_src($input, \&prune_mdir, $self, $1);
                $self->{lse} //= $self->{lei}->{sto}->search;
                # call pmdir_cb (via maildir_each_file -> each_mdir_fn)
                PublicInbox::LeiInput::input_path_url($self, $input);
        } elsif ($input =~ m!\Aimaps?://!i) {
                my $uri = PublicInbox::URIimap->new($input);
-               my $mic = $self->{lei}->{net}->mic_for_folder($uri);
-               my $uids = $mic->search('UID 1:*');
-               $uids = +{ map { $_ => undef } @$uids };
-               $lms->each_src($$uri, \&prune_imap, $self, $uids, $$uri);
+               if (my $mic = $self->{lei}->{net}->mic_for_folder($uri)) {
+                       my $uids = $mic->search('UID 1:*');
+                       $uids = +{ map { $_ => undef } @$uids };
+                       $self->{lms}->each_src($$uri, \&prune_imap, $self,
+                                               $uids, $$uri)
+               } else {
+                       $self->folder_missing($$uri);
+               }
        } else { die "BUG: $input not supported" }
-       $self->{lei}->{pkt_op_p}->pkt_do('sto_done_request');
+       $self->{lei}->sto_done_request;
 }
 
 sub lei_refresh_mail_sync {
@@ -81,30 +73,37 @@ lei mail_sync.sqlite3 uninitialized, see lei-import(1)
 EOM
        if (defined(my $all = $lei->{opt}->{all})) {
                $lms->group2folders($lei, $all, \@folders) or return;
+               # TODO: handle NNTP servers which delete messages
+               @folders = grep(!m!\Anntps?://!, @folders);
        } else {
-               my $err = $lms->arg2folder($lei, \@folders);
-               $lei->qerr(@{$err->{qerr}}) if $err->{qerr};
-               return $lei->fail($err->{fail}) if $err->{fail};
+               $lms->arg2folder($lei, \@folders); # may die
        }
-       undef $lms; # must be done before fork
+       $lms->lms_pause; # must be done before fork
        $sto->write_prepare($lei);
-       my $self = bless { missing_ok => 1 }, __PACKAGE__;
+       my $self = bless { missing_ok => 1, lms => $lms }, __PACKAGE__;
        $lei->{opt}->{'mail-sync'} = 1; # for prepare_inputs
        $self->prepare_inputs($lei, \@folders) or return;
-       my $j = $lei->{opt}->{jobs} || scalar(@{$self->{inputs}}) || 1;
-       my $ops = {};
-       $lei->{auth}->op_merge($ops, $self) if $lei->{auth};
-       $self->{-wq_nr_workers} = $j // 1; # locked
-       (my $op_c, $ops) = $lei->workers_start($self, $j, $ops);
-       $lei->{wq1} = $self;
        $lei->{-err_type} = 'non-fatal';
-       net_merge_all_done($self) unless $lei->{auth};
-       $lei->wait_wq_events($op_c, $ops); # net_merge_all_done if !{auth}
+       $lei->wq1_start($self);
+}
+
+sub ipc_atfork_child { # needed for PublicInbox::LeiPmdir
+       my ($self) = @_;
+       PublicInbox::LeiInput::input_only_atfork_child($self);
+       $self->{lms}->lms_write_prepare;
+       undef;
+}
+
+sub _complete_refresh_mail_sync {
+       my ($lei, @argv) = @_;
+       my $lms = $lei->lms or return ();
+       my $match_cb = $lei->complete_url_prepare(\@argv);
+       my @k = $lms->folders($argv[-1] // undef, 1);
+       my @m = map { $match_cb->($_) } @k;
+       @m ? @m : @k
 }
 
 no warnings 'once';
-*_complete_refresh_mail_sync = \&PublicInbox::LeiExportKw::_complete_export_kw;
-*ipc_atfork_child = \&PublicInbox::LeiInput::input_only_atfork_child;
 *net_merge_all_done = \&PublicInbox::LeiInput::input_only_net_merge_all_done;
 
 1;