1 # Copyright (C) 2021 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 # "lei refresh-mail-sync" drops dangling sync information
5 # and attempts to detect moved files
6 package PublicInbox::LeiRefreshMailSync;
9 use parent qw(PublicInbox::IPC PublicInbox::LeiInput);
10 use PublicInbox::LeiImport;
11 use PublicInbox::InboxWritable qw(eml_from_path);
12 use PublicInbox::Import;
14 sub folder_missing { # may be called by LeiInput
15 my ($self, $folder) = @_;
16 $self->{lms}->forget_folders($folder);
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" }
24 $self->{lms}->clear_src("maildir:$mdir", $id);
27 sub prune_imap { # lms->each_src callback
28 my ($oidbin, $uid, $self, $uids, $url) = @_;
29 return if exists $uids->{$uid};
30 $self->{lms}->clear_src($url, $uid);
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 return if scalar($self->{lms}->name_oidbin($folder, $bn));
40 my $eml = eml_from_path($f) // return;
41 my $oidbin = $self->{lei}->git_oid($eml)->digest;
42 $self->{lms}->set_src($oidbin, $folder, \$bn);
45 sub input_path_url { # overrides PublicInbox::LeiInput::input_path_url
46 my ($self, $input, @args) = @_;
47 if ($input =~ /\Amaildir:(.+)/i) {
48 $self->{lms}->each_src($input, \&prune_mdir, $self, $1);
49 $self->{lse} //= $self->{lei}->{sto}->search;
50 # call pmdir_cb (via maildir_each_file -> each_mdir_fn)
51 PublicInbox::LeiInput::input_path_url($self, $input);
52 } elsif ($input =~ m!\Aimaps?://!i) {
53 my $uri = PublicInbox::URIimap->new($input);
54 if (my $mic = $self->{lei}->{net}->mic_for_folder($uri)) {
55 my $uids = $mic->search('UID 1:*');
56 $uids = +{ map { $_ => undef } @$uids };
57 $self->{lms}->each_src($$uri, \&prune_imap, $self,
60 $self->folder_missing($$uri);
62 } else { die "BUG: $input not supported" }
63 $self->{lei}->sto_done_request;
66 sub lei_refresh_mail_sync {
67 my ($lei, @folders) = @_;
68 my $sto = $lei->_lei_store or return $lei->fail(<<EOM);
69 lei/store uninitialized, see lei-import(1)
71 my $lms = $lei->lms or return $lei->fail(<<EOM);
72 lei mail_sync.sqlite3 uninitialized, see lei-import(1)
74 if (defined(my $all = $lei->{opt}->{all})) {
75 $lms->group2folders($lei, $all, \@folders) or return;
77 $lms->arg2folder($lei, \@folders); # may die
79 $lms->lms_pause; # must be done before fork
80 $sto->write_prepare($lei);
81 my $self = bless { missing_ok => 1, lms => $lms }, __PACKAGE__;
82 $lei->{opt}->{'mail-sync'} = 1; # for prepare_inputs
83 $self->prepare_inputs($lei, \@folders) or return;
84 $lei->{-err_type} = 'non-fatal';
85 $lei->wq1_start($self);
88 sub ipc_atfork_child { # needed for PublicInbox::LeiPmdir
90 PublicInbox::LeiInput::input_only_atfork_child($self);
91 $self->{lms}->lms_write_prepare;
95 sub _complete_refresh_mail_sync {
96 my ($lei, @argv) = @_;
97 my $lms = $lei->lms or return ();
98 my $match_cb = $lei->complete_url_prepare(\@argv);
99 my @k = $lms->folders($argv[-1] // undef, 1);
100 my @m = map { $match_cb->($_) } @k;
105 *net_merge_all_done = \&PublicInbox::LeiInput::input_only_net_merge_all_done;