Sometimes users lose interest in a Maildir or IMAP folder, etc; this drops sync information to not clutter up disk space, "ls-mail-sync" output, etc. Eric Wong (3): lei export-kw: use lei->abs_path instead of rel2abs lei_mail_sync: args2folder: common folder lookup sub lei forget-mail-sync: new command to drop sync information MANIFEST | 1 + lib/PublicInbox/LEI.pm | 3 +- lib/PublicInbox/LeiExportKw.pm | 34 +++--------------- lib/PublicInbox/LeiForgetMailSync.pm | 30 ++++++++++++++++ lib/PublicInbox/LeiInspect.pm | 21 +++++------ lib/PublicInbox/LeiMailSync.pm | 54 ++++++++++++++++++++++++++++ t/lei-import-imap.t | 4 +++ 7 files changed, 104 insertions(+), 43 deletions(-) create mode 100644 lib/PublicInbox/LeiForgetMailSync.pm
This is to match LeiInput behavior when mail-sync is enabled, since rel2abs won't resolve symlinks. --- lib/PublicInbox/LeiExportKw.pm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/PublicInbox/LeiExportKw.pm b/lib/PublicInbox/LeiExportKw.pm index d8ba8fd5..fabc01f8 100644 --- a/lib/PublicInbox/LeiExportKw.pm +++ b/lib/PublicInbox/LeiExportKw.pm @@ -130,7 +130,7 @@ EOM for (@folders) { next if $all{$_}; # ok if (-d "$_/new" && -d "$_/cur") { - my $d = 'maildir:'.$lei->rel2abs($_); + my $d = 'maildir:'.$lei->abs_path($_); push(@no, $_) unless $all{$d}; $_ = $d; } elsif (m!\Aimaps?://!i) {
This lets us have a more consistent UX for mapping easily-typed command-line arguments to canonical folder locations. --- lib/PublicInbox/LeiExportKw.pm | 34 ++++----------------------- lib/PublicInbox/LeiInspect.pm | 21 +++++++---------- lib/PublicInbox/LeiMailSync.pm | 43 ++++++++++++++++++++++++++++++++++ 3 files changed, 56 insertions(+), 42 deletions(-) diff --git a/lib/PublicInbox/LeiExportKw.pm b/lib/PublicInbox/LeiExportKw.pm index fabc01f8..92c3aa43 100644 --- a/lib/PublicInbox/LeiExportKw.pm +++ b/lib/PublicInbox/LeiExportKw.pm @@ -95,7 +95,6 @@ lei mail_sync uninitialized, see lei-import(1) EOM my $opt = $lei->{opt}; my $all = $opt->{all}; - my @all = $lms->folders; if (defined $all) { # --all=<local|remote> my %x = map { $_ => $_ } split(/,/, $all); my @ok = grep(defined, delete(@x{qw(local remote), ''})); @@ -107,6 +106,7 @@ EOM EOM } my (%seen, @inc); + my @all = $lms->folders; for my $ok (@ok) { if ($ok eq 'local') { @inc = grep(!m!\A[a-z0-9\+]+://!i, @all); @@ -125,35 +125,9 @@ EOM no --mail-sync folders known to lei EOM } else { - my %all = map { $_ => 1 } @all; - my @no; - for (@folders) { - next if $all{$_}; # ok - if (-d "$_/new" && -d "$_/cur") { - my $d = 'maildir:'.$lei->abs_path($_); - push(@no, $_) unless $all{$d}; - $_ = $d; - } elsif (m!\Aimaps?://!i) { - my $orig = $_; - my $res = $lms->match_imap_url($orig, $all); - if (ref $res) { - $_ = $$res; - $lei->qerr(<<EOM); -# using `$res' instead of `$orig' -EOM - } else { - $lei->err($res) if defined $res; - push @no, $orig; - } - } else { - push @no, $_; - } - } - my $no = join("\n\t", @no); - return $lei->fail(<<EOF) if @no; -No sync information for: $no -Run `lei ls-mail-sync' to display valid choices -EOF + my $err = $lms->arg2folder($lei, \@folders); + $lei->qerr(@{$err->{qerr}}) if $err->{qerr}; + return $lei->fail($err->{fail}) if $err->{fail}; } my $self = bless { lse => $lse }, __PACKAGE__; $lei->{opt}->{'mail-sync'} = 1; # for prepare_inputs diff --git a/lib/PublicInbox/LeiInspect.pm b/lib/PublicInbox/LeiInspect.pm index 7fd33289..46b9197f 100644 --- a/lib/PublicInbox/LeiInspect.pm +++ b/lib/PublicInbox/LeiInspect.pm @@ -29,19 +29,16 @@ sub inspect_sync_folder ($$) { my $ent = {}; my $lse = $lei->{lse} or return $ent; my $lms = $lse->lms or return $ent; - my @folders; - if ($folder =~ m!\Aimaps?://!i) { - @folders = map { $_->as_string } $lms->match_imap_url($folder); - } elsif ($folder =~ m!\A(maildir|mh):(.+)!i) { - my $type = lc $1; - $folders[0] = "$type:".$lei->abs_path($2); - } elsif (-d $folder) { - $folders[0] = 'maildir:'.$lei->abs_path($folder); - } else { - $lei->fail("$folder not understood"); + my $folders = [ $folder ]; + my $err = $lms->arg2folder($lei, $folders); + if ($err) { + if ($err->{fail}) { + $lei->qerr("# no folders match $folder (non-fatal)"); + @$folders = (); + } + $lei->qerr(@{$err->{qerr}}) if $err->{qerr}; } - $lei->qerr("# no folders match $folder (non-fatal)") if !@folders; - for my $f (@folders) { + for my $f (@$folders) { $ent->{$f} = $lms->location_stats($f); # may be undef } $ent diff --git a/lib/PublicInbox/LeiMailSync.pm b/lib/PublicInbox/LeiMailSync.pm index 63076fa1..094cf1fd 100644 --- a/lib/PublicInbox/LeiMailSync.pm +++ b/lib/PublicInbox/LeiMailSync.pm @@ -302,4 +302,47 @@ sub match_imap_url { "E: `$url' is ambiguous:\n\t".join("\n\t", @match)."\n"; } +# map CLI args to folder table entries, returns undef on failure +sub arg2folder { + my ($self, $lei, $folders) = @_; + my @all = $self->folders; + my %all = map { $_ => 1 } @all; + my ($err, @no); + for (@$folders) { + next if $all{$_}; # ok + if (m!\A(maildir|mh):(.+)!i) { + my $type = lc $1; + my $d = "$type:".$lei->abs_path($2); + push(@no, $_) unless $all{$d}; + $_ = $d; + } elsif (-d "$_/new" && -d "$_/cur") { + my $d = 'maildir:'.$lei->abs_path($_); + push(@no, $_) unless $all{$d}; + $_ = $d; + } elsif (m!\Aimaps?://!i) { + my $orig = $_; + my $res = match_imap_url($self, $orig, \@all); + if (ref $res) { + $_ = $$res; + push(@{$err->{qerr}}, <<EOM); +# using `$res' instead of `$orig' +EOM + } else { + $lei->err($res) if defined $res; + push @no, $orig; + } + } else { + push @no, $_; + } + } + if (@no) { + my $no = join("\n\t", @no); + $err->{fail} = <<EOF; +No sync information for: $no +Run `lei ls-mail-sync' to display valid choices +EOF + } + $err; +} + 1;
Sometimes a user stops caring to sync an IMAP or Maildir folder, or wants to force a resync. Let them run this command to have lei forget all the sync information about the mail folder. This won't delete any stored messages in git, but will leave "lei index" users with dangling references. --- MANIFEST | 1 + lib/PublicInbox/LEI.pm | 3 ++- lib/PublicInbox/LeiForgetMailSync.pm | 30 ++++++++++++++++++++++++++++ lib/PublicInbox/LeiMailSync.pm | 11 ++++++++++ t/lei-import-imap.t | 4 ++++ 5 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 lib/PublicInbox/LeiForgetMailSync.pm diff --git a/MANIFEST b/MANIFEST index 2d1ad5c3..23423e0b 100644 --- a/MANIFEST +++ b/MANIFEST @@ -204,6 +204,7 @@ lib/PublicInbox/LeiDedupe.pm lib/PublicInbox/LeiEditSearch.pm lib/PublicInbox/LeiExportKw.pm lib/PublicInbox/LeiExternal.pm +lib/PublicInbox/LeiForgetMailSync.pm lib/PublicInbox/LeiForgetSearch.pm lib/PublicInbox/LeiHelp.pm lib/PublicInbox/LeiImport.pm diff --git a/lib/PublicInbox/LEI.pm b/lib/PublicInbox/LEI.pm index 628908b5..c8d2f315 100644 --- a/lib/PublicInbox/LEI.pm +++ b/lib/PublicInbox/LEI.pm @@ -243,7 +243,8 @@ our %CMD = ( # sorted in order of importance/use: qw(stdin| offset=i recursive|r exclude=s include|I=s lock=s@ in-format|F=s kw! verbose|v+ incremental! mail-sync!), qw(no-torsocks torsocks=s), PublicInbox::LeiQuery::curl_opt(), @c_opt ], - +'forget-mail-sync' => [ 'LOCATION...', + 'forget sync information for a mail folder', @c_opt ], 'export-kw' => [ 'LOCATION...|--all', 'one-time export of keywords of sync sources', qw(all:s mode=s), @c_opt ], diff --git a/lib/PublicInbox/LeiForgetMailSync.pm b/lib/PublicInbox/LeiForgetMailSync.pm new file mode 100644 index 00000000..46dde1a7 --- /dev/null +++ b/lib/PublicInbox/LeiForgetMailSync.pm @@ -0,0 +1,30 @@ +# Copyright (C) 2021 all contributors <meta@public-inbox.org> +# License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt> + +# "lei forget-mail-sync" drop synchronization information +# TODO: figure out what to do about "lei index" users having +# dangling references. Perhaps just documenting "lei index" +# use being incompatible with "forget-mail-sync" use is +# sufficient. + +package PublicInbox::LeiForgetMailSync; +use strict; +use v5.10.1; +use PublicInbox::LeiExportKw; + +sub lei_forget_mail_sync { + my ($lei, @folders) = @_; + my $sto = $lei->_lei_store or return; + my $lms = $sto->search->lms or return; + my $err = $lms->arg2folder($lei, \@folders); + $lei->qerr(@{$err->{qerr}}) if $err->{qerr}; + return $lei->fail($err->{fail}) if $err->{fail}; + delete $lms->{dbh}; + $lms->lms_begin; + $lms->forget_folder($_) for @folders; + $lms->lms_commit; +} + +*_complete_forget_mail_sync = \&PublicInbox::LeiExportKw::_complete_export_kw; + +1; diff --git a/lib/PublicInbox/LeiMailSync.pm b/lib/PublicInbox/LeiMailSync.pm index 094cf1fd..d9c30580 100644 --- a/lib/PublicInbox/LeiMailSync.pm +++ b/lib/PublicInbox/LeiMailSync.pm @@ -345,4 +345,15 @@ EOF $err; } +sub forget_folder { + my ($self, $folder) = @_; + my ($fid, $sth); + $fid = delete($self->{fmap}->{$folder}) // + _fid_for($self, $folder) // return; + my $dbh = $self->{dbh}; + $dbh->do('DELETE FROM blob2name WHERE fid = ?', undef, $fid); + $dbh->do('DELETE FROM blob2num WHERE fid = ?', undef, $fid); + $dbh->do('DELETE FROM folders WHERE fid = ?', undef, $fid); +} + 1; diff --git a/t/lei-import-imap.t b/t/lei-import-imap.t index d3935c82..5283cc23 100644 --- a/t/lei-import-imap.t +++ b/t/lei-import-imap.t @@ -71,5 +71,9 @@ test_lei({ tmpdir => $tmpdir }, sub { like($lei_out, qr!^Content-Type: multipart/mixed;!sm, 'got full blob'); lei_ok('blob', "$psgi_attach:2"); is($lei_out, "b64\xde\xad\xbe\xef\n", 'got attachment'); + + lei_ok 'forget-mail-sync', $url; + lei_ok 'ls-mail-sync'; + unlike($lei_out, qr!\Q$host_port\E!, 'sync info gone after forget'); }); done_testing;