+ local $current_lei = $lei;
+ delete $lei->{wq1} // return $lei->fail; # already failed
+}
+
+sub watch_state_ok ($) {
+ my ($state) = $_[-1]; # $_[0] may be $self
+ $state =~ /\Apause|(?:import|index|tag)-(?:ro|rw)\z/;
+}
+
+sub cancel_maildir_watch ($$) {
+ my ($d, $cfg_f) = @_;
+ my $w = delete $MDIR2CFGPATH->{$d}->{$cfg_f};
+ scalar(keys %{$MDIR2CFGPATH->{$d}}) or
+ delete $MDIR2CFGPATH->{$d};
+ for my $x (@{$w // []}) { $x->cancel }
+}
+
+sub add_maildir_watch ($$) {
+ my ($d, $cfg_f) = @_;
+ if (!exists($MDIR2CFGPATH->{$d}->{$cfg_f})) {
+ my @w = $dir_idle->add_watches(["$d/cur", "$d/new"], 1);
+ push @{$MDIR2CFGPATH->{$d}->{$cfg_f}}, @w if @w;
+ }
+}
+
+sub refresh_watches {
+ my ($lei) = @_;
+ $dir_idle or return;
+ my $cfg = _lei_cfg($lei) or return;
+ my $old = $cfg->{-watches};
+ my $watches = $cfg->{-watches} //= {};
+ my %seen;
+ my $cfg_f = $cfg->{'-f'};
+ for my $w (grep(/\Awatch\..+\.state\z/, keys %$cfg)) {
+ my $url = substr($w, length('watch.'), -length('.state'));
+ require PublicInbox::LeiWatch;
+ $watches->{$url} //= PublicInbox::LeiWatch->new($url);
+ $seen{$url} = undef;
+ my $state = $cfg->get_1("watch.$url.state");
+ if (!watch_state_ok($state)) {
+ warn("watch.$url.state=$state not supported\n");
+ next;
+ }
+ if ($url =~ /\Amaildir:(.+)/i) {
+ my $d = canonpath_harder($1);
+ if ($state eq 'pause') {
+ cancel_maildir_watch($d, $cfg_f);
+ } else {
+ add_maildir_watch($d, $cfg_f);
+ }
+ } else { # TODO: imap/nntp/jmap
+ $lei->child_error(0, "E: watch $url not supported, yet")
+ }
+ }
+
+ # add all known Maildir folders as implicit watches
+ my $lms = $lei->lms;
+ if ($lms) {
+ $lms->lms_write_prepare;
+ for my $d ($lms->folders('maildir:')) {
+ substr($d, 0, length('maildir:')) = '';
+
+ # fixup old bugs while we're iterating:
+ my $cd = canonpath_harder($d);
+ my $f = "maildir:$cd";
+ $lms->rename_folder("maildir:$d", $f) if $d ne $cd;
+ next if $watches->{$f}; # may be set to pause
+ require PublicInbox::LeiWatch;
+ $watches->{$f} = PublicInbox::LeiWatch->new($f);
+ $seen{$f} = undef;
+ add_maildir_watch($cd, $cfg_f);
+ }
+ }
+ if ($old) { # cull old non-existent entries
+ for my $url (keys %$old) {
+ next if exists $seen{$url};
+ delete $old->{$url};
+ if ($url =~ /\Amaildir:(.+)/i) {
+ my $d = canonpath_harder($1);
+ cancel_maildir_watch($d, $cfg_f);
+ } else { # TODO: imap/nntp/jmap
+ $lei->child_error(0, "E: watch $url TODO");
+ }
+ }
+ }
+ if (scalar keys %$watches) {
+ $cfg->{-env} //= { %{$lei->{env}}, PWD => '/' }; # for cfg2lei
+ } else {
+ delete $cfg->{-watches};
+ }
+}
+
+# TODO: support SHA-256
+sub git_oid {
+ my $eml = $_[-1];
+ $eml->header_set($_) for @PublicInbox::Import::UNWANTED_HEADERS;
+ git_sha(1, $eml);
+}
+
+sub lms {
+ my ($lei, $rw) = @_;
+ my $sto = $lei->{sto} // _lei_store($lei) // return;
+ require PublicInbox::LeiMailSync;
+ my $f = "$sto->{priv_eidx}->{topdir}/mail_sync.sqlite3";
+ (-f $f || $rw) ? PublicInbox::LeiMailSync->new($f) : undef;
+}
+
+sub sto_done_request {
+ my ($lei, $sock) = @_;
+ local $current_lei = $lei;
+ eval {
+ if ($sock //= $lei->{sock}) { # issue, async wait
+ $lei->{sto}->wq_io_do('done', [ $sock ]);
+ } else { # forcibly wait
+ my $wait = $lei->{sto}->wq_do('done');
+ }
+ };
+ warn($@) if $@;
+}
+
+sub cfg_dump ($$) {
+ my ($lei, $f) = @_;
+ my $ret = eval { PublicInbox::Config->git_config_dump($f, $lei->{2}) };
+ return $ret if !$@;
+ warn($@);
+ undef;
+}
+
+sub request_umask {
+ my ($lei) = @_;
+ my $s = $lei->{sock} // return;
+ send($s, 'umask', MSG_EOR) // die "send: $!";
+ vec(my $rvec = '', fileno($s), 1) = 1;
+ select($rvec, undef, undef, 2) or die 'timeout waiting for umask';
+ recv($s, my $v, 5, 0) // die "recv: $!";
+ (my $u, $lei->{client_umask}) = unpack('AV', $v);
+ $u eq 'u' or warn "E: recv $v has no umask";