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