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