unofficial mirror of meta@public-inbox.org
 help / color / mirror / Atom feed
* [PATCH 0/3] lei: some cleanup stuff
@ 2021-10-22  8:22 Eric Wong
  2021-10-22  8:22 ` [PATCH 1/3] lei export-kw: don't recreate deleted IMAP folders Eric Wong
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Eric Wong @ 2021-10-22  8:22 UTC (permalink / raw)
  To: meta

Sometimes users delete stuff, and ordering shouldn't matter.
"lei fsck" should be easier to deal with if users have more
cleanup mechanisms available to them.

Eric Wong (3):
  lei export-kw: don't recreate deleted IMAP folders
  lei export-kw: completion returns all Maildir+IMAP
  lei forget-search: support --prune=<local|remote>

 Documentation/lei-forget-search.pod | 13 ++++++++
 lib/PublicInbox/LEI.pm              |  4 +--
 lib/PublicInbox/LeiExportKw.pm      | 13 +++++---
 lib/PublicInbox/LeiForgetSearch.pm  | 52 +++++++++++++++++++++++++++--
 lib/PublicInbox/LeiUp.pm            | 26 +++++++++------
 lib/PublicInbox/NetReader.pm        |  1 +
 t/lei-q-save.t                      |  7 ++++
 xt/net_writer-imap.t                | 15 +++++++--
 8 files changed, 110 insertions(+), 21 deletions(-)

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH 1/3] lei export-kw: don't recreate deleted IMAP folders
  2021-10-22  8:22 [PATCH 0/3] lei: some cleanup stuff Eric Wong
@ 2021-10-22  8:22 ` Eric Wong
  2021-10-22  8:22 ` [PATCH 2/3] lei export-kw: completion returns all Maildir+IMAP Eric Wong
  2021-10-22  8:22 ` [PATCH 3/3] lei forget-search: support --prune=<local|remote> Eric Wong
  2 siblings, 0 replies; 4+ messages in thread
From: Eric Wong @ 2021-10-22  8:22 UTC (permalink / raw)
  To: meta

In case an IMAP folder is deleted, just set an error and
ignore it rather than creating an empty folder which we
attempt to export keywords to for non-existent messages.
---
 lib/PublicInbox/LeiExportKw.pm | 11 ++++++++---
 lib/PublicInbox/NetReader.pm   |  1 +
 xt/net_writer-imap.t           | 15 +++++++++++++--
 3 files changed, 22 insertions(+), 5 deletions(-)

diff --git a/lib/PublicInbox/LeiExportKw.pm b/lib/PublicInbox/LeiExportKw.pm
index ceeef7f21d54..12c8f4067e9e 100644
--- a/lib/PublicInbox/LeiExportKw.pm
+++ b/lib/PublicInbox/LeiExportKw.pm
@@ -67,9 +67,13 @@ sub input_path_url {
 		$self->{lms}->each_src($input, \&export_kw_md, $self, $mdir);
 	} elsif ($input =~ m!\Aimaps?://!i) {
 		my $uri = PublicInbox::URIimap->new($input);
-		my $mic = $self->{nwr}->mic_for_folder($uri);
-		$self->{lms}->each_src($$uri, \&export_kw_imap, $self, $mic);
-		$mic->expunge;
+		if (my $mic = $self->{nwr}->mic_for_folder($uri)) {
+			$self->{lms}->each_src($$uri, \&export_kw_imap,
+						$self, $mic);
+			$mic->expunge;
+		} else {
+			$self->{lei}->child_error(0, "$input unavailable: $@");
+		}
 	} else { die "BUG: $input not supported" }
 }
 
@@ -108,6 +112,7 @@ EOM
 		$self->{nwr} = bless $net, 'PublicInbox::NetWriter';
 		$self->{imap_mod_kw} = $net->can($self->{-merge_kw} ?
 					'imap_add_kw' : 'imap_set_kw');
+		$self->{nwr}->{-skip_creat} = 1;
 	}
 	my $ops = {};
 	$lei->{auth}->op_merge($ops, $self) if $lei->{auth};
diff --git a/lib/PublicInbox/NetReader.pm b/lib/PublicInbox/NetReader.pm
index 4da19ab969b5..032b4fda557c 100644
--- a/lib/PublicInbox/NetReader.pm
+++ b/lib/PublicInbox/NetReader.pm
@@ -359,6 +359,7 @@ sub imap_common_init ($;$) {
 				mic_for($self, $uri, $mic_common, $lei) //
 				die "Unable to continue\n";
 		next unless $self->isa('PublicInbox::NetWriter');
+		next if $self->{-skip_creat};
 		my $dst = $orig_uri->mailbox // next;
 		next if $mic->exists($dst); # already exists
 		$mic->create($dst) or die "CREATE $dst failed <$orig_uri>: $@";
diff --git a/xt/net_writer-imap.t b/xt/net_writer-imap.t
index cb2ea61ff8e3..afa4bcc3e881 100644
--- a/xt/net_writer-imap.t
+++ b/xt/net_writer-imap.t
@@ -83,8 +83,11 @@ my $mics = do {
 };
 my $mic = (values %$mics)[0];
 my $cleanup = PublicInbox::OnDestroy->new($$, sub {
-	my $mic = $nwr->mic_get($uri);
-	$mic->delete($folder) or fail "delete $folder <$folder_uri>: $@";
+	if (defined($folder)) {
+		my $mic = $nwr->mic_get($uri);
+		$mic->delete($folder) or
+			fail "delete $folder <$folder_uri>: $@";
+	}
 	if ($tmpdir && -f "$tmpdir/.gitconfig") {
 		local $ENV{HOME} = $tmpdir;
 		system(qw(git credential-cache exit));
@@ -250,6 +253,14 @@ EOM
 	lei_ok qw(q m:testmessage --no-external -o), $folder_url;
 	lei_ok qw(up), $folder_url;
 	lei_ok qw(up --all=remote);
+	$mic = $nwr->mic_get($uri);
+	$mic->delete($folder) or fail "delete $folder <$folder_uri>: $@";
+	$mic->expunge;
+	undef $mic;
+	undef $folder;
+	ok(!lei(qw(export-kw), $folder_url),
+		'export-kw fails w/ non-existent folder');
+
 });
 
 undef $cleanup; # remove temporary folder

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH 2/3] lei export-kw: completion returns all Maildir+IMAP
  2021-10-22  8:22 [PATCH 0/3] lei: some cleanup stuff Eric Wong
  2021-10-22  8:22 ` [PATCH 1/3] lei export-kw: don't recreate deleted IMAP folders Eric Wong
@ 2021-10-22  8:22 ` Eric Wong
  2021-10-22  8:22 ` [PATCH 3/3] lei forget-search: support --prune=<local|remote> Eric Wong
  2 siblings, 0 replies; 4+ messages in thread
From: Eric Wong @ 2021-10-22  8:22 UTC (permalink / raw)
  To: meta

It's theoretically possible an AUTH=ANONYMOUS login could be
writable and allowed to store flags for various people (e.g.
within a private network).
---
 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 12c8f4067e9e..5be9e51ff311 100644
--- a/lib/PublicInbox/LeiExportKw.pm
+++ b/lib/PublicInbox/LeiExportKw.pm
@@ -128,7 +128,7 @@ sub _complete_export_kw {
 	my $lms = $lei->lms or return ();
 	my $match_cb = $lei->complete_url_prepare(\@argv);
 	# filter-out read-only sources:
-	my @k = grep(!m!(?://;AUTH=ANONYMOUS\@|\A(?:nntps?|s?news)://)!,
+	my @k = grep(m!(?:maildir|imaps?):!,
 			$lms->folders($argv[-1] // undef, 1));
 	my @m = map { $match_cb->($_) } @k;
 	@m ? @m : @k;

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH 3/3] lei forget-search: support --prune=<local|remote>
  2021-10-22  8:22 [PATCH 0/3] lei: some cleanup stuff Eric Wong
  2021-10-22  8:22 ` [PATCH 1/3] lei export-kw: don't recreate deleted IMAP folders Eric Wong
  2021-10-22  8:22 ` [PATCH 2/3] lei export-kw: completion returns all Maildir+IMAP Eric Wong
@ 2021-10-22  8:22 ` Eric Wong
  2 siblings, 0 replies; 4+ messages in thread
From: Eric Wong @ 2021-10-22  8:22 UTC (permalink / raw)
  To: meta

Instead of:

	lei forget-search $OUTPUT && rm -r $OUTPUT

we'll also allow a user to do:

	rm -r $OUTPUT && lei forget-search --prune

This gives users flexibility to choose whatever flow
is most natural to them.
---
 Documentation/lei-forget-search.pod | 13 ++++++++
 lib/PublicInbox/LEI.pm              |  4 +--
 lib/PublicInbox/LeiForgetSearch.pm  | 52 +++++++++++++++++++++++++++--
 lib/PublicInbox/LeiUp.pm            | 26 +++++++++------
 t/lei-q-save.t                      |  7 ++++
 5 files changed, 87 insertions(+), 15 deletions(-)

diff --git a/Documentation/lei-forget-search.pod b/Documentation/lei-forget-search.pod
index f3f043f93252..3a7f493cee8e 100644
--- a/Documentation/lei-forget-search.pod
+++ b/Documentation/lei-forget-search.pod
@@ -10,6 +10,19 @@ lei forget-search [OPTIONS] OUTPUT
 
 Forget a saved search at C<OUTPUT>.
 
+=head1 OPTIONS
+
+=over
+
+=item --prune[=<local|remote>]
+
+C<--prune> will forget saved searches if the C<OUTPUT> no longer
+exists.  C<--all=local> only prunes local mailboxes,
+C<--all=remote> only prunes remote mailboxes (currently
+C<imap://> and C<imaps://>).
+
+=back
+
 =head1 CONTACT
 
 Feedback welcome via plain-text mail to L<mailto:meta@public-inbox.org>
diff --git a/lib/PublicInbox/LEI.pm b/lib/PublicInbox/LEI.pm
index 43baeeb3d51c..cb1e5433cc2d 100644
--- a/lib/PublicInbox/LEI.pm
+++ b/lib/PublicInbox/LEI.pm
@@ -222,8 +222,8 @@ our %CMD = ( # sorted in order of importance/use:
 
 'ls-search' => [ '[PREFIX]', 'list saved search queries',
 		qw(format|f=s pretty l ascii z|0), @c_opt ],
-'forget-search' => [ 'OUTPUT...', 'forget a saved search',
-		qw(verbose|v+), @c_opt ],
+'forget-search' => [ 'OUTPUT...|--prune', 'forget a saved search',
+		qw(verbose|v+ prune:s), @c_opt ],
 'edit-search' => [ 'OUTPUT', "edit saved search via `git config --edit'",
 			@c_opt ],
 'rm' => [ '--stdin|LOCATION...',
diff --git a/lib/PublicInbox/LeiForgetSearch.pm b/lib/PublicInbox/LeiForgetSearch.pm
index 0db9c75b8be3..f353fe52a958 100644
--- a/lib/PublicInbox/LeiForgetSearch.pm
+++ b/lib/PublicInbox/LeiForgetSearch.pm
@@ -5,12 +5,12 @@
 package PublicInbox::LeiForgetSearch;
 use strict;
 use v5.10.1;
+use parent qw(PublicInbox::LeiUp);
 use PublicInbox::LeiSavedSearch;
-use PublicInbox::LeiUp;
 use File::Path ();
 use SelectSaver;
 
-sub lei_forget_search {
+sub do_forget_search {
 	my ($lei, @outs) = @_;
 	my @dirs; # paths in ~/.local/share/lei/saved-search/
 	my $cwd;
@@ -30,9 +30,55 @@ sub lei_forget_search {
 		$save = SelectSaver->new($lei->{2});
 	}
 	File::Path::remove_tree(@dirs, $opt);
-	$lei->fail if defined $cwd;
+	$lei->child_error(0) if defined $cwd;
+}
+
+sub lei_forget_search {
+	my ($lei, @outs) = @_;
+	my $prune = $lei->{opt}->{prune};
+	$prune // return do_forget_search($lei, @outs);
+	return $lei->fail("--prune and @outs incompatible") if @outs;
+	my @tmp = PublicInbox::LeiSavedSearch::list($lei);
+	my $self = bless { -mail_sync => 1 }, __PACKAGE__;
+	$self->filter_lss($lei, $prune) // return
+			$lei->fail("only --prune=$prune not understood");
+	if ($self->{o_remote}) { # setup lei->{auth}
+		$self->prepare_inputs($lei, $self->{o_remote}) or return;
+	}
+	my $ops = {};
+	$lei->{auth}->op_merge($ops, $self) if $lei->{auth};
+	(my $op_c, $ops) = $lei->workers_start($self, 1, $ops);
+	$lei->{wq1} = $self;
+	net_merge_all_done($self) unless $lei->{auth};
+	$lei->wait_wq_events($op_c, $ops);
+}
+
+sub do_prune {
+	my ($self) = @_;
+	my $lei = $self->{lei};
+	for my $o (@{$self->{o_local} // []}) {
+		next if -e $o;
+		$lei->qerr("# pruning $o");
+		eval { do_forget_search($lei, $o) };
+		$lei->child_error(0, "E: $@") if $@;
+	}
+	for my $o (@{$self->{o_remote} // []}) {
+		my $uri = PublicInbox::URIimap->new($o);
+		next if $lei->{net}->mic_for_folder($uri);
+		$lei->qerr("# pruning $uri");
+		eval { do_forget_search($lei, $o) };
+		$lei->child_error(0, "E: $@") if $@;
+	}
+}
+
+# called in top-level lei-daemon when LeiAuth is done
+sub net_merge_all_done {
+	my ($self) = @_;
+	$self->wq_do('do_prune');
+	$self->wq_close;
 }
 
+*_wq_done_wait = \&PublicInbox::LEI::wq_done_wait;
 *_complete_forget_search = \&PublicInbox::LeiUp::_complete_up;
 
 1;
diff --git a/lib/PublicInbox/LeiUp.pm b/lib/PublicInbox/LeiUp.pm
index dac0fc287885..79639d5e62a4 100644
--- a/lib/PublicInbox/LeiUp.pm
+++ b/lib/PublicInbox/LeiUp.pm
@@ -93,6 +93,21 @@ sub redispatch_all ($$) {
 	}
 }
 
+sub filter_lss {
+	my ($self, $lei, $all) = @_;
+	my @outs = PublicInbox::LeiSavedSearch::list($lei);
+	if ($all eq 'local') {
+		$self->{o_local} = [ grep(!/$REMOTE_RE/, @outs) ];
+	} elsif ($all eq 'remote') {
+		$self->{o_remote} = [ grep(/$REMOTE_RE/, @outs) ];
+	} elsif ($all eq '') {
+		$self->{o_remote} = [ grep(/$REMOTE_RE/, @outs) ];
+		$self->{o_local} = [ grep(!/$REMOTE_RE/, @outs) ];
+	} else {
+		undef;
+	}
+}
+
 sub lei_up {
 	my ($lei, @outs) = @_;
 	my $opt = $lei->{opt};
@@ -101,17 +116,8 @@ sub lei_up {
 		return $lei->fail("--all and @outs incompatible") if @outs;
 		defined($opt->{mua}) and return
 			$lei->fail('--all and --mua= are incompatible');
-		@outs = PublicInbox::LeiSavedSearch::list($lei);
-		if ($all eq 'local') {
-			$self->{o_local} = [ grep(!/$REMOTE_RE/, @outs) ];
-		} elsif ($all eq 'remote') {
-			$self->{o_remote} = [ grep(/$REMOTE_RE/, @outs) ];
-		} elsif ($all eq '') {
-			$self->{o_remote} = [ grep(/$REMOTE_RE/, @outs) ];
-			$self->{o_local} = [ grep(!/$REMOTE_RE/, @outs) ];
-		} else {
+		filter_lss($self, $lei, $all) // return
 			$lei->fail("only --all=$all not understood");
-		}
 	} elsif ($lei->{lse}) { # redispatched
 		scalar(@outs) == 1 or die "BUG: lse set w/ >1 out[@outs]";
 		return up1($lei, $outs[0]);
diff --git a/t/lei-q-save.t b/t/lei-q-save.t
index 05d5d9f4436c..cd35461ce1b7 100644
--- a/t/lei-q-save.t
+++ b/t/lei-q-save.t
@@ -4,6 +4,7 @@
 use strict; use v5.10.1; use PublicInbox::TestCommon;
 use PublicInbox::Smsg;
 use List::Util qw(sum);
+use File::Path qw(remove_tree);
 
 my $doc1 = eml_load('t/plack-qp.eml');
 $doc1->header_set('Date', PublicInbox::Smsg::date({ds => time - (86400 * 5)}));
@@ -233,5 +234,11 @@ test_lei(sub {
 		and xbail "-ipe $lss[0]: $?";
 	lei_ok qw(ls-search);
 	is($lei_err, '', 'no errors w/ fixed config');
+
+	like($lei_out, qr!\Q$home/after\E!, "`after' in ls-search");
+	remove_tree("$home/after");
+	lei_ok qw(forget-search --prune);
+	lei_ok qw(ls-search);
+	unlike($lei_out, qr!\Q$home/after\E!, "`after' pruned");
 });
 done_testing;

^ permalink raw reply related	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2021-10-22  8:22 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-10-22  8:22 [PATCH 0/3] lei: some cleanup stuff Eric Wong
2021-10-22  8:22 ` [PATCH 1/3] lei export-kw: don't recreate deleted IMAP folders Eric Wong
2021-10-22  8:22 ` [PATCH 2/3] lei export-kw: completion returns all Maildir+IMAP Eric Wong
2021-10-22  8:22 ` [PATCH 3/3] lei forget-search: support --prune=<local|remote> Eric Wong

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).