]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/LeiRefreshMailSync.pm
lei refresh-mail-sync: replace prune-mail-sync
[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}->qerr("# maildir:$mdir $$id gone");
35         $self->{lei}->{sto}->ipc_do('lms_clear_src', "maildir:$mdir", $id);
36 }
37
38 sub prune_imap { # lms->each_src callback
39         my ($oidbin, $uid, $self, $uids, $url) = @_;
40         return if exists $uids->{$uid};
41         $self->{lei}->qerr("# $url $uid gone");
42         $self->{lei}->{sto}->ipc_do('lms_clear_src', $url, $uid);
43 }
44
45 # detects missed file moves
46 sub pmdir_cb { # called via LeiPmdir->each_mdir_fn
47         my ($self, $f, $fl) = @_;
48         my ($folder, $bn) = ($f =~ m!\A(.+?)/(?:new|cur)/([^/]+)\z!) or
49                 die "BUG: $f was not from a Maildir?";
50         substr($folder, 0, 0) = 'maildir:'; # add prefix
51         my $lms = $self->{-lms_ro} //= $self->{lei}->lms;
52         return if defined($lms->name_oidbin($folder, $bn));
53         my $eml = eml_from_path($f) // return;
54         my $oidbin = $self->{lei}->git_oid($eml)->digest;
55         $self->{lei}->{sto}->ipc_do('lms_set_src', $oidbin, $folder, \$bn);
56 }
57
58 sub input_path_url { # overrides PublicInbox::LeiInput::input_path_url
59         my ($self, $input, @args) = @_;
60         my $lms = $self->{-lms_ro} //= $self->{lei}->lms;
61         if ($input =~ /\Amaildir:(.+)/i) {
62                 $lms->each_src($input, \&prune_mdir, $self, my $mdir = $1);
63                 $self->{lse} //= $self->{lei}->{sto}->search;
64                 # call pmdir_cb (via maildir_each_file -> each_mdir_fn)
65                 PublicInbox::LeiInput::input_path_url($self, $input);
66         } elsif ($input =~ m!\Aimaps?://!i) {
67                 my $uri = PublicInbox::URIimap->new($input);
68                 my $mic = $self->{lei}->{net}->mic_for_folder($uri);
69                 my $uids = $mic->search('UID 1:*');
70                 $uids = +{ map { $_ => undef } @$uids };
71                 $lms->each_src($$uri, \&prune_imap, $self, $uids, $$uri);
72         } else { die "BUG: $input not supported" }
73         $self->{lei}->{pkt_op_p}->pkt_do('sto_done_request');
74 }
75
76 sub lei_refresh_mail_sync {
77         my ($lei, @folders) = @_;
78         my $sto = $lei->_lei_store or return $lei->fail(<<EOM);
79 lei/store uninitialized, see lei-import(1)
80 EOM
81         my $lms = $lei->lms or return $lei->fail(<<EOM);
82 lei mail_sync.sqlite3 uninitialized, see lei-import(1)
83 EOM
84         if (defined(my $all = $lei->{opt}->{all})) {
85                 $lms->group2folders($lei, $all, \@folders) or return;
86         } else {
87                 my $err = $lms->arg2folder($lei, \@folders);
88                 $lei->qerr(@{$err->{qerr}}) if $err->{qerr};
89                 return $lei->fail($err->{fail}) if $err->{fail};
90         }
91         undef $lms; # must be done before fork
92         $sto->write_prepare($lei);
93         my $self = bless { missing_ok => 1 }, __PACKAGE__;
94         $lei->{opt}->{'mail-sync'} = 1; # for prepare_inputs
95         $self->prepare_inputs($lei, \@folders) or return;
96         my $j = $lei->{opt}->{jobs} || scalar(@{$self->{inputs}}) || 1;
97         my $ops = {};
98         $lei->{auth}->op_merge($ops, $self) if $lei->{auth};
99         $self->{-wq_nr_workers} = $j // 1; # locked
100         (my $op_c, $ops) = $lei->workers_start($self, $j, $ops);
101         $lei->{wq1} = $self;
102         $lei->{-err_type} = 'non-fatal';
103         net_merge_all_done($self) unless $lei->{auth};
104         $lei->wait_wq_events($op_c, $ops); # net_merge_all_done if !{auth}
105 }
106
107 no warnings 'once';
108 *_complete_refresh_mail_sync = \&PublicInbox::LeiExportKw::_complete_export_kw;
109 *ipc_atfork_child = \&PublicInbox::LeiInput::input_only_atfork_child;
110 *net_merge_all_done = \&PublicInbox::LeiInput::input_only_net_merge_all_done;
111
112 1;