]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/LeiSavedSearch.pm
lei ls-search: command to list saved searches
[public-inbox.git] / lib / PublicInbox / LeiSavedSearch.pm
index ebc63091e075075296b3e738e49dfcf001af5d23..d67622c9b08ce6bfc30fa75cef75cb9206ba06ea 100644 (file)
@@ -13,43 +13,108 @@ use PublicInbox::Spawn qw(run_die);
 use PublicInbox::ContentHash qw(git_sha);
 use Digest::SHA qw(sha256_hex);
 
+# move this to PublicInbox::Config if other things use it:
+my %cquote = ("\n" => '\\n', "\t" => '\\t', "\b" => '\\b');
+sub cquote_val ($) { # cf. git-config(1)
+       my ($val) = @_;
+       $val =~ s/([\n\t\b])/$cquote{$1}/g;
+       $val;
+}
+
+sub ARRAY_FIELDS () { qw(only include exclude) }
+sub BOOL_FIELDS () {
+       qw(external local remote import-remote import-before threads)
+}
+
+sub lss_dir_for ($$) {
+       my ($lei, $dstref) = @_;
+       my @n;
+       if ($$dstref =~ m,\Aimaps?://,i) { # already canonicalized
+               require PublicInbox::URIimap;
+               my $uri = PublicInbox::URIimap->new($$dstref)->canonical;
+               $$dstref = $$uri;
+               @n = ($uri->mailbox);
+       } else { # basename
+               $$dstref = $lei->rel2abs($$dstref);
+               $$dstref .= '/' if -d $$dstref;
+               $$dstref =~ tr!/!/!s;
+               @n = ($$dstref =~ m{([^/]+)/*\z});
+       }
+       push @n, sha256_hex($$dstref);
+       $lei->share_path . '/saved-searches/' . join('-', @n);
+}
+
+sub list {
+       my ($lei, $pfx) = @_;
+       my $lss_dir = $lei->share_path.'/saved-searches/';
+       return () unless -d $lss_dir;
+       # TODO: persist the cache?  Use another format?
+       my $f = $lei->cache_dir."/saved-tmp.$$.".time.'.config';
+       open my $fh, '>', $f or die "open $f: $!";
+       print $fh "[include]\n";
+       for my $p (glob("$lss_dir/*/lei.saved-search")) {
+               print $fh "\tpath = ", cquote_val($p), "\n";
+       }
+       close $fh or die "close $f: $!";
+       my $cfg = PublicInbox::Config::git_config_dump($f);
+       unlink($f);
+       bless $cfg, 'PublicInbox::Config';
+       my $out = $cfg->get_all('lei.q.output') or return ();
+       map {;
+               if (s!\A(?:maildir|mh|mbox.+|mmdf):!!i) {
+                       -e $_ ? $_ : (); # TODO auto-prune somewhere?
+               } else { # IMAP, maybe JMAP
+                       $_;
+               }
+       } @$out
+}
+
 sub new {
-       my ($cls, $lei, $dir) = @_;
-       my $self = bless { ale => $lei->ale, -cfg => {} }, $cls;
-       if (defined $dir) { # updating existing saved search via "lei up"
-               my $f = $self->{'-f'} = "$dir/lei.saved-search";
-               -f $f && -r _ or
-                       return $lei->fail("$f non-existent or unreadable");
-               $self->{-cfg} = PublicInbox::Config::git_config_dump($f);
+       my ($cls, $lei, $dst) = @_;
+       my $self = bless { ale => $lei->ale }, $cls;
+       my $dir;
+       if (defined $dst) { # updating existing saved search via "lei up"
+               my $f;
+               $dir = $dst;
+               output2lssdir($self, $lei, \$dir, \$f) or
+                       return $lei->fail("--save was not used with $dst cwd=".
+                                               $lei->rel2abs('.'));
+               $self->{-cfg} //= PublicInbox::Config::git_config_dump($f);
+               $self->{'-f'} = $f;
        } else { # new saved search "lei q --save"
-               my $saved_dir = $lei->share_path . '/saved-searches/';
-               my (@n) = ($lei->{ovv}->{dst} =~ m{([\w\-\.]+)/*\z});
-               my $q = $lei->{mset_opt}->{q_raw} // die 'BUG: {q_raw} missing';
-               push @n, sha256_hex("$lei->{ovv}->{fmt}\0$lei->{ovv}->{dst}");
-
-               $dir = $saved_dir . join('-', @n);
+               my $dst = $lei->{ovv}->{dst};
+               $dir = lss_dir_for($lei, \$dst);
                require File::Path;
                File::Path::make_path($dir); # raises on error
-               $self->{'-f'} = "$dir/lei.saved-search";
+               $self->{-cfg} = {};
+               my $f = $self->{'-f'} = "$dir/lei.saved-search";
+               open my $fh, '>', $f or return $lei->fail("open $f: $!");
+               my $sq_dst = PublicInbox::Config::squote_maybe($dst);
+               my $q = $lei->{mset_opt}->{q_raw} // die 'BUG: {q_raw} missing';
                if (ref $q) {
-                       cfg_set($self, '--add', 'lei.q', $_) for @$q;
+                       $q = join("\n", map { "\tq = ".cquote_val($_) } @$q);
                } else {
-                       cfg_set($self, 'lei.q', $q);
+                       $q = "\tq = ".cquote_val($q);
                }
-               my $fmt = $lei->{opt}->{'format'};
-               cfg_set($self, 'lei.q.format', $fmt) if defined $fmt;
-               cfg_set($self, 'lei.q.output', $lei->{opt}->{output});
-               for my $k (qw(only include exclude)) {
+               $dst = "$lei->{ovv}->{fmt}:$dst" if $dst !~ m!\Aimaps?://!i;
+               print $fh <<EOM;
+; to refresh with new results, run: lei up $sq_dst
+[lei]
+$q
+[lei "q"]
+       output = $dst
+EOM
+               for my $k (ARRAY_FIELDS) {
                        my $ary = $lei->{opt}->{$k} // next;
                        for my $x (@$ary) {
-                               cfg_set($self, '--add', "lei.q.$k", $x);
+                               print $fh "\t$k = ".cquote_val($x)."\n";
                        }
                }
-               for my $k (qw(external local remote import-remote
-                               import-before threads)) {
+               for my $k (BOOL_FIELDS) {
                        my $val = $lei->{opt}->{$k} // next;
-                       cfg_set($self, "lei.q.$k", $val);
+                       print $fh "\t$k = ".($val ? 1 : 0)."\n";
                }
+               close($fh) or return $lei->fail("close $f: $!");
        }
        bless $self->{-cfg}, 'PublicInbox::Config';
        $self->{lock_path} = "$self->{-f}.flock";
@@ -59,7 +124,7 @@ sub new {
 
 sub description { $_[0]->{qstr} } # for WWW
 
-sub cfg_set {
+sub cfg_set { # called by LeiXSearch
        my ($self, @args) = @_;
        my $lk = $self->lock_for_scope; # git-config doesn't wait
        run_die([qw(git config -f), $self->{'-f'}, @args]);
@@ -127,6 +192,23 @@ sub mm { undef }
 sub altid_map { {} }
 
 sub cloneurl { [] }
+
+# find existing directory containing a `lei.saved-search' file based on
+# $dir_ref which is an output
+sub output2lssdir {
+       my ($self, $lei, $dir_ref, $fn_ref) = @_;
+       my $dst = $$dir_ref; # imap://$MAILBOX, /path/to/maildir, /path/to/mbox
+       my $dir = lss_dir_for($lei, \$dst);
+       my $f = "$dir/lei.saved-search";
+       if (-f $f && -r _) {
+               $self->{-cfg} = PublicInbox::Config::git_config_dump($f);
+               $$dir_ref = $dir;
+               $$fn_ref = $f;
+               return 1;
+       }
+       undef;
+}
+
 no warnings 'once';
 *nntp_url = \&cloneurl;
 *base_url = \&PublicInbox::Inbox::base_url;