]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/LeiRefreshMailSync.pm
lei refresh-mail-sync: remove "gone" notices
[public-inbox.git] / lib / PublicInbox / LeiRefreshMailSync.pm
1 # Copyright (C) 2021 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3
4 # "lei refresh-mail-sync" drops dangling sync information
5 # and attempts to detect moved files
6 package PublicInbox::LeiRefreshMailSync;
7 use strict;
8 use v5.10.1;
9 use parent qw(PublicInbox::IPC PublicInbox::LeiInput);
10 use PublicInbox::LeiExportKw;
11 use PublicInbox::InboxWritable qw(eml_from_path);
12 use PublicInbox::ContentHash qw(git_sha);
13 use PublicInbox::Import;
14
15 sub eml_match ($$) {
16         my ($eml, $oidbin) = @_;
17         $eml->header_set($_) for @PublicInbox::Import::UNWANTED_HEADERS;
18         $oidbin eq git_sha(length($oidbin) == 20 ? 1 : 256, $eml)->digest;
19 }
20
21 sub prune_mdir { # lms->each_src callback
22         my ($oidbin, $id, $self, $mdir) = @_;
23         my @try = $$id =~ /:2,[a-zA-Z]*\z/ ? qw(cur new) : qw(new cur);
24         for my $d (@try) {
25                 my $src = "$mdir/$d/$$id";
26                 if ($self->{verify}) {
27                         my $eml = eml_from_path($src) // next;
28                         return if eml_match($eml, $oidbin);
29                 } elsif (-f $src) {
30                         return;
31                 }
32         }
33         # both tries failed
34         $self->{lei}->{sto}->ipc_do('lms_clear_src', "maildir:$mdir", $id);
35 }
36
37 sub prune_imap { # lms->each_src callback
38         my ($oidbin, $uid, $self, $uids, $url) = @_;
39         return if exists $uids->{$uid};
40         $self->{lei}->{sto}->ipc_do('lms_clear_src', $url, $uid);
41 }
42
43 # detects missed file moves
44 sub pmdir_cb { # called via LeiPmdir->each_mdir_fn
45         my ($self, $f, $fl) = @_;
46         my ($folder, $bn) = ($f =~ m!\A(.+?)/(?:new|cur)/([^/]+)\z!) or
47                 die "BUG: $f was not from a Maildir?";
48         substr($folder, 0, 0) = 'maildir:'; # add prefix
49         my $lms = $self->{-lms_ro} //= $self->{lei}->lms;
50         return if defined($lms->name_oidbin($folder, $bn));
51         my $eml = eml_from_path($f) // return;
52         my $oidbin = $self->{lei}->git_oid($eml)->digest;
53         $self->{lei}->{sto}->ipc_do('lms_set_src', $oidbin, $folder, \$bn);
54 }
55
56 sub input_path_url { # overrides PublicInbox::LeiInput::input_path_url
57         my ($self, $input, @args) = @_;
58         my $lms = $self->{-lms_ro} //= $self->{lei}->lms;
59         if ($input =~ /\Amaildir:(.+)/i) {
60                 $lms->each_src($input, \&prune_mdir, $self, my $mdir = $1);
61                 $self->{lse} //= $self->{lei}->{sto}->search;
62                 # call pmdir_cb (via maildir_each_file -> each_mdir_fn)
63                 PublicInbox::LeiInput::input_path_url($self, $input);
64         } elsif ($input =~ m!\Aimaps?://!i) {
65                 my $uri = PublicInbox::URIimap->new($input);
66                 my $mic = $self->{lei}->{net}->mic_for_folder($uri);
67                 my $uids = $mic->search('UID 1:*');
68                 $uids = +{ map { $_ => undef } @$uids };
69                 $lms->each_src($$uri, \&prune_imap, $self, $uids, $$uri);
70         } else { die "BUG: $input not supported" }
71         $self->{lei}->{pkt_op_p}->pkt_do('sto_done_request');
72 }
73
74 sub lei_refresh_mail_sync {
75         my ($lei, @folders) = @_;
76         my $sto = $lei->_lei_store or return $lei->fail(<<EOM);
77 lei/store uninitialized, see lei-import(1)
78 EOM
79         my $lms = $lei->lms or return $lei->fail(<<EOM);
80 lei mail_sync.sqlite3 uninitialized, see lei-import(1)
81 EOM
82         if (defined(my $all = $lei->{opt}->{all})) {
83                 $lms->group2folders($lei, $all, \@folders) or return;
84         } else {
85                 my $err = $lms->arg2folder($lei, \@folders);
86                 $lei->qerr(@{$err->{qerr}}) if $err->{qerr};
87                 return $lei->fail($err->{fail}) if $err->{fail};
88         }
89         undef $lms; # must be done before fork
90         $sto->write_prepare($lei);
91         my $self = bless { missing_ok => 1 }, __PACKAGE__;
92         $lei->{opt}->{'mail-sync'} = 1; # for prepare_inputs
93         $self->prepare_inputs($lei, \@folders) or return;
94         my $j = $lei->{opt}->{jobs} || scalar(@{$self->{inputs}}) || 1;
95         my $ops = {};
96         $lei->{auth}->op_merge($ops, $self) if $lei->{auth};
97         $self->{-wq_nr_workers} = $j // 1; # locked
98         (my $op_c, $ops) = $lei->workers_start($self, $j, $ops);
99         $lei->{wq1} = $self;
100         $lei->{-err_type} = 'non-fatal';
101         net_merge_all_done($self) unless $lei->{auth};
102         $lei->wait_wq_events($op_c, $ops); # net_merge_all_done if !{auth}
103 }
104
105 no warnings 'once';
106 *_complete_refresh_mail_sync = \&PublicInbox::LeiExportKw::_complete_export_kw;
107 *ipc_atfork_child = \&PublicInbox::LeiInput::input_only_atfork_child;
108 *net_merge_all_done = \&PublicInbox::LeiInput::input_only_net_merge_all_done;
109
110 1;