]> Sergey Matveev's repositories - public-inbox.git/commitdiff
mbox_reader: add ->reads method to avoid nonsensical formats
authorEric Wong <e@80x24.org>
Mon, 22 Mar 2021 07:53:59 +0000 (07:53 +0000)
committerEric Wong <e@80x24.org>
Tue, 23 Mar 2021 00:07:12 +0000 (00:07 +0000)
Relying on UNIVERSAL::can may cause internal helper methods
to be used, which can lead to failures or nonsensical results.

lib/PublicInbox/InboxWritable.pm
lib/PublicInbox/LeiImport.pm
lib/PublicInbox/LeiInput.pm
lib/PublicInbox/MboxReader.pm

index c2baeba666904ce128435ef36b4e3642cf2f001d..eeebc485156025f1aa1ff38f4b3ba1ebfde31a25 100644 (file)
@@ -172,7 +172,7 @@ sub _mbox_eml_cb { # MboxReader->mbox* callback
 sub import_mbox {
        my ($self, $fh, $variant) = @_;
        require PublicInbox::MboxReader;
-       my $cb = PublicInbox::MboxReader->can($variant) or
+       my $cb = PublicInbox::MboxReader->reads($variant) or
                die "$variant not supported\n";
        my $im = $self->importer(1);
        $cb->(undef, $fh, \&_mbox_eml_cb, $im, $self->filter);
index e769fba828c9a85241a6956d6fffe3856dfd0b78..e587ada86b47304654e050ab09deeae4662cc123 100644 (file)
@@ -96,7 +96,7 @@ error reading $input: $!
                        my $eml = PublicInbox::Eml->new(\$buf);
                        _import_eml($eml, $lei, $kw);
                } else { # some mbox (->can already checked in call);
-                       my $cb = PublicInbox::MboxReader->can($ifmt) //
+                       my $cb = PublicInbox::MboxReader->reads($ifmt) //
                                die "BUG: bad fmt=$ifmt";
                        $cb->(undef, $fh, \&_import_eml, $lei, $kw);
                }
index 89585a5289e84e8179b81595b5d18a582eda0aff..776b31515439ee0d6fb2a3fd86c395a016e282e4 100644 (file)
@@ -18,7 +18,7 @@ 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;
 }
@@ -53,7 +53,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;
index f93c2ec634496d8c4ba8d4a7bfde0c8b9ab260a3..6302d1fa540dfada479eb12d4c884512b9137568 100644 (file)
@@ -133,4 +133,9 @@ sub mboxcl2 {
 
 sub new { bless \(my $x), __PACKAGE__ }
 
+sub reads {
+       my $ifmt = $_[-1];
+       $ifmt =~ /\Ambox(?:rd|cl|cl2|o)\z/ ? __PACKAGE__->can($ifmt) : undef
+}
+
 1;