]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/LeiInput.pm
lei_input: common filehandle reader for eml + mbox
[public-inbox.git] / lib / PublicInbox / LeiInput.pm
index 776b31515439ee0d6fb2a3fd86c395a016e282e4..c62b0893b000c05fdfd0050be489a01dc83e10bb 100644 (file)
@@ -23,6 +23,23 @@ sub check_input_format ($;$) {
        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: $!
+
+               $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 {
        my ($self, $lei, $inputs) = @_;