From 60e5bddd086a8a90d5eaff32bc5c2026fc784ca0 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Mon, 22 Mar 2021 07:54:00 +0000 Subject: lei_input: common filehandle reader for eml + mbox This improve code regularity, and will let us deal with the "RFC822" messages with "From " line that mutt pipes to. --- lib/PublicInbox/LeiConvert.pm | 30 ++++++++++-------------------- 1 file changed, 10 insertions(+), 20 deletions(-) (limited to 'lib/PublicInbox/LeiConvert.pm') diff --git a/lib/PublicInbox/LeiConvert.pm b/lib/PublicInbox/LeiConvert.pm index 8685c194..51a233bd 100644 --- a/lib/PublicInbox/LeiConvert.pm +++ b/lib/PublicInbox/LeiConvert.pm @@ -10,13 +10,18 @@ use PublicInbox::Eml; use PublicInbox::LeiStore; use PublicInbox::LeiOverview; -sub mbox_cb { +sub mbox_cb { # MboxReader callback used by PublicInbox::LeiInput::input_fh my ($eml, $self) = @_; my $kw = PublicInbox::MboxReader::mbox_keywords($eml); $eml->header_set($_) for qw(Status X-Status); $self->{wcb}->(undef, { kw => $kw }, $eml); } +sub eml_cb { # used by PublicInbox::LeiInput::input_fh + my ($self, $eml) = @_; + $self->{wcb}->(undef, { kw => [] }, $eml); +} + sub net_cb { # callback for ->imap_each, ->nntp_each my (undef, undef, $kw, $eml, $self) = @_; # @_[0,1]: url + uid ignored $self->{wcb}->(undef, { kw => $kw }, $eml); @@ -27,30 +32,15 @@ sub mdir_cb { $self->{wcb}->(undef, { kw => $kw }, $eml); } -sub convert_fh ($$$$) { - my ($self, $ifmt, $fh, $name) = @_; - if ($ifmt eq 'eml') { - my $buf = do { local $/; <$fh> } // - return $self->{lei}->child_error(1 << 8, <<""); -error reading $name: $! - - my $eml = PublicInbox::Eml->new(\$buf); - $self->{wcb}->(undef, { kw => [] }, $eml); - } else { - PublicInbox::MboxReader->$ifmt($fh, \&mbox_cb, $self); - } -} - sub do_convert { # via wq_do my ($self) = @_; my $lei = $self->{lei}; - my $in_fmt = $lei->{opt}->{'in-format'}; - my $mics; + my $ifmt = $lei->{opt}->{'in-format'}; if (my $stdin = delete $self->{0}) { - convert_fh($self, $in_fmt, $stdin, ''); + $self->input_fh($ifmt, $stdin, ''); } for my $input (@{$self->{inputs}}) { - my $ifmt = lc($in_fmt // ''); + my $ifmt = lc($ifmt // ''); if ($input =~ m!\Aimaps?://!) { $lei->{net}->imap_each($input, \&net_cb, $self); next; @@ -65,7 +55,7 @@ sub do_convert { # via wq_do ($ifmt eq 'eml' ? ['none'] : PublicInbox::MboxLock->defaults); my $mbl = PublicInbox::MboxLock->acq($input, 0, $m); - convert_fh($self, $ifmt, $mbl->{fh}, $input); + $self->input_fh($ifmt, $mbl->{fh}, $input); } elsif (-d _) { PublicInbox::MdirReader::maildir_each_eml($input, \&mdir_cb, $self); -- cgit v1.2.3-24-ge0c7