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::LeiExportKw;
11 use PublicInbox::InboxWritable qw(eml_from_path);
12 use PublicInbox::Import;
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" }
19 $self->{lei}->{sto}->ipc_do('lms_clear_src', "maildir:$mdir", $id);
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);
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);
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');
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)
64 my $lms = $lei->lms or return $lei->fail(<<EOM);
65 lei mail_sync.sqlite3 uninitialized, see lei-import(1)
67 if (defined(my $all = $lei->{opt}->{all})) {
68 $lms->group2folders($lei, $all, \@folders) or return;
70 my $err = $lms->arg2folder($lei, \@folders);
71 $lei->qerr(@{$err->{qerr}}) if $err->{qerr};
72 return $lei->fail($err->{fail}) if $err->{fail};
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;
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);
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}
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;