]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/LeiInput.pm
lei mark: command for (un)setting keywords and labels
[public-inbox.git] / lib / PublicInbox / LeiInput.pm
index 89585a5289e84e8179b81595b5d18a582eda0aff..6ad5777207fdf1ad1a13547ec2b7125dc70200ac 100644 (file)
@@ -18,13 +18,34 @@ sub check_input_format ($;$) {
        require PublicInbox::MboxReader;
        return 1 if $fmt eq 'eml';
        # XXX: should this handle {gz,bz2,xz}? that's currently in LeiToMail
-       PublicInbox::MboxReader->can($fmt) or
+       PublicInbox::MboxReader->reads($fmt) or
                return $lei->fail("--$opt_key=$fmt unrecognized");
        1;
 }
 
+# import a single file handle of $name
+# Subclass must define ->eml_cb and ->mbox_cb
+sub input_fh {
+       my ($self, $ifmt, $fh, $name, @args) = @_;
+       if ($ifmt eq 'eml') {
+               my $buf = do { local $/; <$fh> } //
+                       return $self->{lei}->child_error(1 << 8, <<"");
+error reading $name: $!
 
-sub prepare_inputs {
+               # mutt pipes single RFC822 messages with a "From " line,
+               # but no Content-Length or "From " escaping.
+               # "git format-patch" also generates such files by default.
+               $buf =~ s/\A[\r\n]*From [^\r\n]*\r?\n//s;
+               $self->eml_cb(PublicInbox::Eml->new(\$buf), @args);
+       } else {
+               # prepare_inputs already validated $ifmt
+               my $cb = PublicInbox::MboxReader->reads($ifmt) //
+                               die "BUG: bad fmt=$ifmt";
+               $cb->(undef, $fh, $self->can('mbox_cb'), $self, @args);
+       }
+}
+
+sub prepare_inputs { # returns undef on error
        my ($self, $lei, $inputs) = @_;
        my $in_fmt = $lei->{opt}->{'in-format'};
        if ($lei->{opt}->{stdin}) {
@@ -53,7 +74,7 @@ sub prepare_inputs {
                        if (-f $input_path) {
                                require PublicInbox::MboxLock;
                                require PublicInbox::MboxReader;
-                               PublicInbox::MboxReader->can($ifmt) or return
+                               PublicInbox::MboxReader->reads($ifmt) or return
                                        $lei->fail("$ifmt not supported");
                        } elsif (-d _) {
                                require PublicInbox::MdirReader;
@@ -82,4 +103,13 @@ sub prepare_inputs {
        $self->{inputs} = $inputs;
 }
 
+sub input_only_atfork_child {
+       my ($self) = @_;
+       my $lei = $self->{lei};
+       $lei->lei_atfork_child;
+       PublicInbox::IPC::ipc_atfork_child($self);
+       $lei->{auth}->do_auth_atfork($self) if $lei->{auth};
+       undef;
+}
+
 1;