]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/LeiInput.pm
lei_input: set and prepare watches early
[public-inbox.git] / lib / PublicInbox / LeiInput.pm
index 92d67715194b4f10bdc7750793b3e656a2fdf773..1b28f36f68c57136c90823d0abf0004bab5fc2bc 100644 (file)
@@ -7,6 +7,7 @@ use strict;
 use v5.10.1;
 use PublicInbox::DS;
 use PublicInbox::Spawn qw(which popen_rd);
+use PublicInbox::InboxWritable qw(eml_from_path);
 
 # JMAP RFC 8621 4.1.1
 # https://www.iana.org/assignments/imap-jmap-keywords/imap-jmap-keywords.xhtml
@@ -127,6 +128,16 @@ sub input_path_url {
                $ifmt = lc($1);
        } elsif ($input =~ /\.(?:patch|eml)\z/i) {
                $ifmt = 'eml';
+       } elsif (-f $input && $input =~ m{\A(?:.+)/(?:new|cur)/([^/]+)\z}) {
+               my $bn = $1;
+               my $fl = PublicInbox::MdirReader::maildir_basename_flags($bn);
+               return if index($fl, 'T') >= 0;
+               return $self->pmdir_cb($input, $fl) if $self->can('pmdir_cb');
+               my $eml = eml_from_path($input) or return
+                       $lei->qerr("# $input not readable");
+               my $kw = PublicInbox::MdirReader::flags2kw($fl);
+               $self->can('input_maildir_cb')->($input, $kw, $eml, $self);
+               return;
        }
        my $devfd = $lei->path_to_fd($input) // return;
        if ($devfd >= 0) {
@@ -214,6 +225,7 @@ sub prepare_inputs { # returns undef on error
        my ($self, $lei, $inputs) = @_;
        my $in_fmt = $lei->{opt}->{'in-format'};
        my $sync = $lei->{opt}->{'mail-sync'} ? {} : undef; # using LeiMailSync
+       my $may_sync = $sync || $self->{-mail_sync};
        if ($lei->{opt}->{stdin}) {
                @$inputs and return
                        $lei->fail("--stdin and @$inputs do not mix");
@@ -256,9 +268,13 @@ sub prepare_inputs { # returns undef on error
                        } elsif (-d $input_path) {
                                $ifmt eq 'maildir' or return
                                        $lei->fail("$ifmt not supported");
-                               $sync and $input = 'maildir:'.
+                               $may_sync and $input = 'maildir:'.
                                                $lei->abs_path($input_path);
                                push @md, $input;
+                       } elsif ($self->{missing_ok} && !-e _) {
+                               # for "lei rm-watch" on missing Maildir
+                               $may_sync and $input = 'maildir:'.
+                                               $lei->abs_path($input_path);
                        } else {
                                return $lei->fail("Unable to handle $input");
                        }
@@ -266,19 +282,38 @@ sub prepare_inputs { # returns undef on error
                        lc($in_fmt//'eml') eq 'eml' or return $lei->fail(<<"");
 $input is `eml', not --in-format=$in_fmt
 
-                       require PublicInbox::Eml;
                        push @{$sync->{no}}, $input if $sync;
+               } elsif (-f $input && $input =~ m{\A(.+)/(new|cur)/([^/]+)\z}) {
+                       # single file in a Maildir
+                       my ($mdir, $nc, $bn) = ($1, $2, $3);
+                       my $other = $mdir . ($nc eq 'new' ? '/cur' : '/new');
+                       return $lei->fail(<<EOM) if !-d $other;
+No `$other' directory for `$input'
+EOM
+                       lc($in_fmt//'eml') eq 'eml' or return $lei->fail(<<"");
+$input is `eml', not --in-format=$in_fmt
+
+                       if ($sync) {
+                               $input = $lei->abs_path($mdir) . "/$nc/$bn";
+                               push @{$sync->{ok}}, $input if $sync;
+                       }
+                       require PublicInbox::MdirReader;
                } else {
                        my $devfd = $lei->path_to_fd($input) // return;
                        if ($devfd >= 0 || -f $input || -p _) {
                                push @{$sync->{no}}, $input if $sync;
                                push @f, $input;
                        } elsif (-d "$input/new" && -d "$input/cur") {
-                               if ($sync) {
-                                       $input = $lei->abs_path($input);
-                                       push @{$sync->{ok}}, $input;
+                               if ($may_sync) {
+                                       $input = 'maildir:'.
+                                               $lei->abs_path($input);
+                                       push @{$sync->{ok}}, $input if $sync;
                                }
                                push @md, $input;
+                       } elsif ($self->{missing_ok} && !-e $input) {
+                               # for lei rm-watch
+                               $may_sync and $input = 'maildir:'.
+                                               $lei->abs_path($input);
                        } else {
                                return $lei->fail("Unable to handle $input")
                        }
@@ -309,6 +344,13 @@ $input is `eml', not --in-format=$in_fmt
                        require PublicInbox::LeiPmdir;
                        $self->{pmd} = PublicInbox::LeiPmdir->new($lei, $self);
                }
+
+               # start watching Maildirs ASAP
+               if ($may_sync && $lei->{sto}) {
+                       grep(!m!\Amaildir:/!i, @md) and die "BUG: @md (no pfx)";
+                       my $wait = $lei->{sto}->ipc_do('add_sync_folders', @md);
+                       $lei->refresh_watches;
+               }
        }
        $self->{inputs} = $inputs;
 }
@@ -346,6 +388,7 @@ sub input_only_net_merge_all_done {
 
 # like Getopt::Long, but for +kw:FOO and -kw:FOO to prepare
 # for update_xvmd -> update_vmd
+# returns something like { "+L" => [ @Labels ], ... }
 sub vmd_mod_extract {
        my $argv = $_[-1];
        my $vmd_mod = {};