+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
+}