our %PATH2CFG; # persistent for socket daemon
our $MDIR2CFGPATH; # /path/to/maildir => { /path/to/config => [ ino watches ] }
+our %LIVE_SOCK; # "GLOB(0x....)" => $lei->{sock}
# TBD: this is a documentation mechanism to show a subcommand
# (may) pass options through to another command:
sub qerr ($;@) { $_[0]->{opt}->{quiet} or err(shift, @_) }
+sub qfin { # show message on finalization (LeiFinmsg)
+ my ($lei, $msg) = @_;
+ return if $lei->{opt}->{quiet};
+ $lei->{fmsg} ? push(@{$lei->{fmsg}}, "$msg\n") : qerr($lei, $msg);
+}
+
sub fail_handler ($;$$) {
my ($lei, $code, $io) = @_;
close($io) if $io; # needed to avoid warnings on SIGPIPE
$dir_idle->force_close if $dir_idle;
%PATH2CFG = ();
$MDIR2CFGPATH = {};
+ %LIVE_SOCK = ();
eval 'no warnings; undef $PublicInbox::LeiNoteEvent::to_flush';
undef $errors_log;
$quit = \&CORE::exit;
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) = @_;
my $cfg = _lei_cfg($lei) or return;
for my $w (grep(/\Awatch\..+\.state\z/, keys %$cfg)) {
my $url = substr($w, length('watch.'), -length('.state'));
require PublicInbox::LeiWatch;
- my $lw = $watches->{$url} //= PublicInbox::LeiWatch->new($url);
+ $watches->{$url} //= PublicInbox::LeiWatch->new($url);
$seen{$url} = undef;
my $state = $cfg->get_1("watch.$url", 'state');
if (!watch_state_ok($state)) {
my $d = canonpath_harder($1);
if ($state eq 'pause') {
cancel_maildir_watch($d, $cfg_f);
- } elsif (!exists($MDIR2CFGPATH->{$d}->{$cfg_f})) {
- my @w = $dir_idle->add_watches(
- ["$d/cur", "$d/new"], 1);
- push @{$MDIR2CFGPATH->{$d}->{$cfg_f}}, @w if @w;
+ } else {
+ add_maildir_watch($d, $cfg_f);
}
} else { # TODO: imap/nntp/jmap
- $lei->child_error(1,
- "E: watch $url not supported, yet");
+ $lei->child_error(1, "E: watch $url not supported, yet")
+ }
+ }
+
+ # add all known Maildir folders as implicit watches
+ my $sto = $lei->_lei_store;
+ my $renames = 0;
+ if (my $lms = $sto ? $sto->search->lms : undef) {
+ for my $d ($lms->folders('maildir:')) {
+ substr($d, 0, length('maildir:')) = '';
+ my $cd = canonpath_harder($d);
+ my $f = "maildir:$cd";
+
+ # fixup old bugs while we're iterating:
+ if ($d ne $cd) {
+ $sto->ipc_do('lms_rename_folder',
+ "maildir:$d", $f);
+ ++$renames;
+ }
+ 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);
}
}
+ $lei->sto_done_request if $renames;
if ($old) { # cull old non-existent entries
for my $url (keys %$old) {
next if exists $seen{$url};
$lse ? $lse->lms : undef;
}
+sub sto_done_request { # only call this from lei-daemon process (not workers)
+ my ($lei, $sock) = @_;
+ if ($sock //= $lei->{sock}) {
+ $LIVE_SOCK{"$sock"} = $sock;
+ $lei->{sto}->ipc_do('done', "$sock"); # issue, async wait
+ } else { # forcibly wait
+ my $wait = $lei->{sto}->ipc_do('done');
+ }
+}
+
+sub sto_done_complete { # called in lei-daemon when LeiStore->done is complete
+ my ($sock_str) = @_;
+ delete $LIVE_SOCK{$sock_str}; # frees {sock} for waiting lei clients
+}
+
1;