unofficial mirror of meta@public-inbox.org
 help / color / mirror / Atom feed
From: Eric Wong <e@80x24.org>
To: meta@public-inbox.org
Subject: [PATCH 2/3] import_mbox: use MboxReader
Date: Fri, 12 Feb 2021 00:05:51 -0700	[thread overview]
Message-ID: <20210212070552.13901-3-e@80x24.org> (raw)
In-Reply-To: <20210212070552.13901-1-e@80x24.org>

It supports more mbox variants and it's trailing newline
behavior is probably more correct despite the previous change
to PublicInbox::Filter::Vger.
---
 lib/PublicInbox/InboxWritable.pm | 44 +++++++-------------------------
 1 file changed, 9 insertions(+), 35 deletions(-)

diff --git a/lib/PublicInbox/InboxWritable.pm b/lib/PublicInbox/InboxWritable.pm
index c3acc4f9..d4a9040f 100644
--- a/lib/PublicInbox/InboxWritable.pm
+++ b/lib/PublicInbox/InboxWritable.pm
@@ -158,49 +158,23 @@ sub import_maildir {
 	$im->done;
 }
 
-# asctime: From example@example.com Fri Jun 23 02:56:55 2000
-my $from_strict = qr/^From \S+ +\S+ \S+ +\S+ [^:]+:[^:]+:[^:]+ [^:]+/;
-
-sub mb_add ($$$$) {
-	my ($im, $variant, $filter, $msg) = @_;
-	$$msg =~ s/(\r?\n)+\z/$1/s;
-	if ($variant eq 'mboxrd') {
-		$$msg =~ s/^>(>*From )/$1/gms;
-	} elsif ($variant eq 'mboxo') {
-		$$msg =~ s/^>From /From /gms;
-	}
-	my $mime = PublicInbox::Eml->new($msg);
+sub _mbox_eml_cb { # MboxReader->mbox* callback
+	my ($eml, $im, $filter) = @_;
 	if ($filter) {
-		my $ret = $filter->scrub($mime) or return;
+		my $ret = $filter->scrub($eml) or return;
 		return if $ret == REJECT();
-		$mime = $ret;
+		$eml = $ret;
 	}
-	$im->add($mime)
+	$im->add($eml);
 }
 
 sub import_mbox {
 	my ($self, $fh, $variant) = @_;
-	if ($variant !~ /\A(?:mboxrd|mboxo)\z/) {
-		die "variant must be 'mboxrd' or 'mboxo'\n";
-	}
+	require PublicInbox::MboxReader;
+	my $cb = PublicInbox::MboxReader->can($variant) or
+		die "$variant not supported\n";
 	my $im = $self->importer(1);
-	my $prev = undef;
-	my $msg = '';
-	my $filter = $self->filter;
-	while (defined(my $l = <$fh>)) {
-		if ($l =~ /$from_strict/o) {
-			if (!defined($prev) || $prev =~ /^\r?$/) {
-				mb_add($im, $variant, $filter, \$msg) if $msg;
-				$msg = '';
-				$prev = $l;
-				next;
-			}
-			warn "W[$.] $l\n";
-		}
-		$prev = $l;
-		$msg .= $l;
-	}
-	mb_add($im, $variant, $filter, \$msg) if $msg;
+	$cb->(undef, $fh, \&_mbox_eml_cb, $im, $self->filter);
 	$im->done;
 }
 

  parent reply	other threads:[~2021-02-12  7:05 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-02-12  7:05 [PATCH 0/3] MboxReader usage and changes Eric Wong
2021-02-12  7:05 ` [PATCH 1/3] filter/vger: kill trailing newlines aggressively Eric Wong
2021-02-12  7:05 ` Eric Wong [this message]
2021-02-12  7:05 ` [PATCH 3/3] mbox_reader: do not chomp non-blank EOL Eric Wong

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://public-inbox.org/README

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210212070552.13901-3-e@80x24.org \
    --to=e@80x24.org \
    --cc=meta@public-inbox.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).