user/dev discussion of public-inbox itself
 help / color / mirror / code / Atom feed
From: Eric Wong <e@80x24.org>
To: meta@public-inbox.org
Subject: [PATCHv3 2/4] lei import: add IMAP and (maildir|mbox*):$PATHNAME support
Date: Thu, 18 Feb 2021 23:22:23 +0300	[thread overview]
Message-ID: <20210218202225.85440-3-e@80x24.org> (raw)
In-Reply-To: <20210218110647.9822-2-e@80x24.org>

This makes "lei import" more similar to "lei convert" and
allows importing from disparate sources simultaneously.

We'll also fix some ->child_error usage errors and make
the style of the code more similar to the "lei convert"
code.

v2: fix missing requires
---
 MANIFEST                     |   1 +
 lib/PublicInbox/LeiImport.pm | 129 ++++++++++++++++++++++++-----------
 t/lei-import-imap.t          |  28 ++++++++
 t/lei-import-maildir.t       |   4 +-
 t/lei_to_mail.t              |  10 +++
 5 files changed, 130 insertions(+), 42 deletions(-)
 create mode 100644 t/lei-import-imap.t

diff --git a/MANIFEST b/MANIFEST
index 4f146771..19f73356 100644
--- a/MANIFEST
+++ b/MANIFEST
@@ -365,6 +365,7 @@ t/kqnotify.t
 t/lei-convert.t
 t/lei-daemon.t
 t/lei-externals.t
+t/lei-import-imap.t
 t/lei-import-maildir.t
 t/lei-import.t
 t/lei-mirror.t
diff --git a/lib/PublicInbox/LeiImport.pm b/lib/PublicInbox/LeiImport.pm
index 32f3a467..62a2a412 100644
--- a/lib/PublicInbox/LeiImport.pm
+++ b/lib/PublicInbox/LeiImport.pm
@@ -29,7 +29,7 @@ sub import_done { # EOF callback for main daemon
 	$imp->wq_wait_old(\&import_done_wait, $lei);
 }
 
-sub do_import {
+sub import_start {
 	my ($lei) = @_;
 	my $ops = {
 		'!' => [ $lei->can('fail_handler'), $lei ],
@@ -39,7 +39,7 @@ sub do_import {
 	};
 	($lei->{pkt_op_c}, $lei->{pkt_op_p}) = PublicInbox::PktOp->pair($ops);
 	my $self = $lei->{imp};
-	my $j = $lei->{opt}->{jobs} // scalar(@{$self->{argv}}) || 1;
+	my $j = $lei->{opt}->{jobs} // scalar(@{$self->{inputs}}) || 1;
 	if (my $nrd = $lei->{nrd}) {
 		# $j = $nrd->net_concurrency($j); TODO
 	} else {
@@ -50,8 +50,8 @@ sub do_import {
 	my $op = delete $lei->{pkt_op_c};
 	delete $lei->{pkt_op_p};
 	$self->wq_io_do('import_stdin', []) if $self->{0};
-	for my $x (@{$self->{argv}}) {
-		$self->wq_io_do('import_path_url', [], $x);
+	for my $input (@{$self->{inputs}}) {
+		$self->wq_io_do('import_path_url', [], $input);
 	}
 	$self->wq_close(1);
 	$lei->event_step_init; # wait for shutdowns
@@ -61,60 +61,91 @@ sub do_import {
 }
 
 sub call { # the main "lei import" method
-	my ($cls, $lei, @argv) = @_;
+	my ($cls, $lei, @inputs) = @_;
 	my $sto = $lei->_lei_store(1);
 	$sto->write_prepare($lei);
+	my ($nrd, @f, @d);
 	$lei->{opt}->{kw} //= 1;
-	my $self = $lei->{imp} = bless { argv => \@argv }, $cls;
+	my $self = $lei->{imp} = bless { inputs => \@inputs }, $cls;
 	if ($lei->{opt}->{stdin}) {
-		@argv and return
-			$lei->fail("--stdin and locations (@argv) do not mix");
+		@inputs and return $lei->fail("--stdin and @inputs do not mix");
 		$lei->check_input_format or return;
 		$self->{0} = $lei->{0};
-	} else {
-		my @f;
-		for my $x (@argv) {
-			if (-f $x) { push @f, $x }
-			elsif (-d _) { require PublicInbox::MdirReader }
-			else {
-				require PublicInbox::NetReader;
-				$lei->{nrd} //= PublicInbox::NetReader->new;
-				$lei->{nrd}->add_url($x);
+	}
+
+	# TODO: do we need --format for non-stdin?
+	my $fmt = $lei->{opt}->{'format'};
+	# e.g. Maildir:/home/user/Mail/ or imaps://example.com/INBOX
+	for my $input (@inputs) {
+		my $input_path = $input;
+		if ($input =~ m!\A(?:imap|nntp)s?://!i) {
+			require PublicInbox::NetReader;
+			$nrd //= PublicInbox::NetReader->new;
+			$nrd->add_url($input);
+		} elsif ($input_path =~ s/\A([a-z0-9]+)://is) {
+			my $ifmt = lc $1;
+			if (($fmt // $ifmt) ne $ifmt) {
+				return $lei->fail(<<"");
+--format=$fmt and `$ifmt:' conflict
+
 			}
-		}
-		if (@f) { $lei->check_input_format(\@f) or return }
-		if ($lei->{nrd} && (my @err = $lei->{nrd}->errors)) {
-			return $lei->fail(@err);
-		}
+			if (-f $input_path) {
+				require PublicInbox::MboxReader;
+				PublicInbox::MboxReader->can($ifmt) or return
+					$lei->fail("$ifmt not supported");
+			} elsif (-d _) {
+				require PublicInbox::MdirReader;
+				$ifmt eq 'maildir' or return
+					$lei->fail("$ifmt not supported");
+			} else {
+				return $lei->fail("Unable to handle $input");
+			}
+		} elsif (-f $input) { push @f, $input
+		} elsif (-d _) { push @d, $input
+		} else { return $lei->fail("Unable to handle $input") }
 	}
-	do_import($lei);
+	if (@f) { $lei->check_input_format(\@f) or return }
+	if (@d) { # TODO: check for MH vs Maildir, here
+		require PublicInbox::MdirReader;
+	}
+	$self->{inputs} = \@inputs;
+	return import_start($lei) if !$nrd;
+
+	if (my $err = $nrd->errors) {
+		return $lei->fail($err);
+	}
+	$nrd->{quiet} = $lei->{opt}->{quiet};
+	$lei->{nrd} = $nrd;
+	require PublicInbox::LeiAuth;
+	my $auth = $lei->{auth} = PublicInbox::LeiAuth->new($nrd);
+	$auth->auth_start($lei, \&import_start, $lei);
 }
 
 sub ipc_atfork_child {
 	my ($self) = @_;
+	delete $self->{lei}->{imp}; # drop circular ref
 	$self->{lei}->lei_atfork_child;
 	$self->SUPER::ipc_atfork_child;
 }
 
 sub _import_fh {
-	my ($lei, $fh, $x) = @_;
+	my ($lei, $fh, $input, $ifmt) = @_;
 	my $set_kw = $lei->{opt}->{kw};
-	my $fmt = $lei->{opt}->{'format'};
 	eval {
-		if ($fmt eq 'eml') {
+		if ($ifmt eq 'eml') {
 			my $buf = do { local $/; <$fh> } //
-				return $lei->child_error(1 >> 8, <<"");
-error reading $x: $!
+				return $lei->child_error(1 << 8, <<"");
+error reading $input: $!
 
 			my $eml = PublicInbox::Eml->new(\$buf);
 			_import_eml($eml, $lei->{sto}, $set_kw);
 		} else { # some mbox (->can already checked in call);
-			my $cb = PublicInbox::MboxReader->can($fmt) //
-				die "BUG: bad fmt=$fmt";
+			my $cb = PublicInbox::MboxReader->can($ifmt) //
+				die "BUG: bad fmt=$ifmt";
 			$cb->(undef, $fh, \&_import_eml, $lei->{sto}, $set_kw);
 		}
 	};
-	$lei->child_error(1 >> 8, "<stdin>: $@") if $@;
+	$lei->child_error(1 << 8, "<stdin>: $@") if $@;
 }
 
 sub _import_maildir { # maildir_each_file cb
@@ -122,27 +153,45 @@ sub _import_maildir { # maildir_each_file cb
 	$sto->ipc_do('set_eml_from_maildir', $f, $set_kw);
 }
 
+sub _import_imap { # imap_each cb
+	my ($url, $uid, $kw, $eml, $sto, $set_kw) = @_;
+	warn "$url $uid";
+	$sto->ipc_do('set_eml', $eml, $set_kw ? @$kw : ());
+}
+
 sub import_path_url {
-	my ($self, $x) = @_;
+	my ($self, $input) = @_;
 	my $lei = $self->{lei};
+	my $ifmt = lc($lei->{opt}->{'format'} // '');
 	# TODO auto-detect?
-	if (-f $x) {
-		open my $fh, '<', $x or return $lei->child_error(1 >> 8, <<"");
-unable to open $x: $!
+	if ($input =~ m!\A(imap|nntp)s?://!i) {
+		$lei->{nrd}->imap_each($input, \&_import_imap, $lei->{sto},
+					$lei->{opt}->{kw});
+		return;
+	} elsif ($input =~ s!\A([a-z0-9]+):!!i) {
+		$ifmt = lc $1;
+	}
+	if (-f $input) {
+		open my $fh, '<', $input or return $lei->child_error(1 << 8, <<"");
+unable to open $input: $!
 
-		_import_fh($lei, $fh, $x);
-	} elsif (-d _ && (-d "$x/cur" || -d "$x/new")) {
-		PublicInbox::MdirReader::maildir_each_file($x,
+		_import_fh($lei, $fh, $input, $ifmt);
+	} elsif (-d _ && (-d "$input/cur" || -d "$input/new")) {
+		return $lei->fail(<<EOM) if $ifmt && $ifmt ne 'maildir';
+$input appears to a be a maildir, not $ifmt
+EOM
+		PublicInbox::MdirReader::maildir_each_file($input,
 					\&_import_maildir,
 					$lei->{sto}, $lei->{opt}->{kw});
 	} else {
-		$lei->fail("$x unsupported (TODO)");
+		$lei->fail("$input unsupported (TODO)");
 	}
 }
 
 sub import_stdin {
 	my ($self) = @_;
-	_import_fh($self->{lei}, $self->{0}, '<stdin>');
+	my $lei = $self->{lei};
+	_import_fh($lei, delete $self->{0}, '<stdin>', $lei->{opt}->{'format'});
 }
 
 1;
diff --git a/t/lei-import-imap.t b/t/lei-import-imap.t
new file mode 100644
index 00000000..ee308723
--- /dev/null
+++ b/t/lei-import-imap.t
@@ -0,0 +1,28 @@
+#!perl -w
+# Copyright (C) 2021 all contributors <meta@public-inbox.org>
+# License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
+use strict; use v5.10.1; use PublicInbox::TestCommon;
+require_git 2.6;
+require_mods(qw(DBD::SQLite Search::Xapian));
+my ($ro_home, $cfg_path) = setup_public_inboxes;
+my ($tmpdir, $for_destroy) = tmpdir;
+my $sock = tcp_server;
+my $cmd = [ '-imapd', '-W0', "--stdout=$tmpdir/1", "--stderr=$tmpdir/2" ];
+my $env = { PI_CONFIG => $cfg_path };
+my $td = start_script($cmd, $env, { 3 => $sock }) or BAIL_OUT("-imapd: $?");
+my $host_port = tcp_host_port($sock);
+undef $sock;
+test_lei({ tmpdir => $tmpdir }, sub {
+	lei_ok(qw(q bytes:1..));
+	my $out = json_utf8->decode($lei_out);
+	is_deeply($out, [ undef ], 'nothing imported, yet');
+	lei_ok('import', "imap://$host_port/t.v2.0");
+	lei_ok(qw(q bytes:1..));
+	$out = json_utf8->decode($lei_out);
+	ok(scalar(@$out) > 1, 'got imported messages');
+	is(pop @$out, undef, 'trailing JSON null element was null');
+	my %r;
+	for (@$out) { $r{ref($_)}++ }
+	is_deeply(\%r, { 'HASH' => scalar(@$out) }, 'all hashes');
+});
+done_testing;
diff --git a/t/lei-import-maildir.t b/t/lei-import-maildir.t
index 5842e19e..d2b059ad 100644
--- a/t/lei-import-maildir.t
+++ b/t/lei-import-maildir.t
@@ -23,8 +23,8 @@ test_lei(sub {
 	is_deeply($r2, $res, 'idempotent import');
 
 	rename("$md/cur/x:2,S", "$md/cur/x:2,SR") or BAIL_OUT "rename: $!";
-	ok($lei->(qw(import), $md), 'import Maildir after +answered');
-	ok($lei->(qw(q -d none s:boolean)), 'lei q after +answered');
+	lei_ok('import', "maildir:$md", \'import Maildir after +answered');
+	lei_ok(qw(q -d none s:boolean), \'lei q after +answered');
 	$res = json_utf8->decode($lei_out);
 	like($res->[0]->{'s'}, qr/use boolean/, 'got expected result');
 	is_deeply($res->[0]->{kw}, ['answered', 'seen'], 'keywords set');
diff --git a/t/lei_to_mail.t b/t/lei_to_mail.t
index 6a571660..72b90700 100644
--- a/t/lei_to_mail.t
+++ b/t/lei_to_mail.t
@@ -139,6 +139,16 @@ test_lei(sub {
 	is($res->[1], undef, 'only one result');
 });
 
+test_lei(sub {
+	lei_ok('import', "$mbox:$fn", \'imported mbox:/path') or diag $lei_err;
+	lei_ok(qw(q s:x), \'lei q works') or diag $lei_err;
+	my $res = json_utf8->decode($lei_out);
+	my $x = $res->[0];
+	is($x->{'s'}, 'x', 'subject imported') or diag $lei_out;
+	is_deeply($x->{'kw'}, ['seen'], 'kw imported') or diag $lei_out;
+	is($res->[1], undef, 'only one result');
+});
+
 for my $zsfx (qw(gz bz2 xz)) { # XXX should we support zst, zz, lzo, lzma?
 	my $zsfx2cmd = PublicInbox::LeiToMail->can('zsfx2cmd');
 	SKIP: {

  parent reply	other threads:[~2021-02-18 20:22 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-02-17 10:06 [PATCH 00/11] lei IMAP read support Eric Wong
2021-02-17 10:06 ` [PATCH 01/11] lei: bless config Eric Wong
2021-02-17 10:06 ` [PATCH 02/11] watch: move imap_common_init to NetReader Eric Wong
2021-02-17 10:06 ` [PATCH 03/11] watch: connect to NNTP and IMAP in config order Eric Wong
2021-02-17 10:07 ` [PATCH 04/11] lei import: start rearranging code for IMAP support Eric Wong
2021-02-17 10:07 ` [PATCH 05/11] lei import: move check_input_format to lei Eric Wong
2021-02-17 10:07 ` [PATCH 06/11] tests: setup_public_inboxes: use IMAP-friendly newsgroups Eric Wong
2021-02-17 10:07 ` [PATCH 07/11] t/lei_to_mail: remove unnecessary arg passing Eric Wong
2021-02-17 10:07 ` [PATCH 08/11] lei convert: mail format conversion sub-command Eric Wong
2021-02-17 10:53   ` Eric Wong
2021-02-18 11:06     ` [PATCHv2 0/4] lei IMAP support take #2 Eric Wong
2021-02-18 11:06       ` [PATCHv2 1/4] lei convert: mail format conversion sub-command Eric Wong
2021-02-18 20:22         ` [PATCHv3 0/4] lei convert IMAP support Eric Wong
2021-02-18 20:22         ` [PATCHv3 1/4] lei convert: mail format conversion sub-command Eric Wong
2021-02-18 20:22         ` Eric Wong [this message]
2021-02-18 20:22         ` [PATCHv3 3/4] lei: consolidate the bulk of the IPC code Eric Wong
2021-02-18 20:22         ` [PATCHv3 4/4] lei: check for IMAP auth errors Eric Wong
2021-02-18 11:06       ` [PATCHv2 2/4] lei import: add IMAP and (maildir|mbox*):$PATHNAME support Eric Wong
2021-02-18 11:06       ` [PATCH (resend) 3/4] lei: consolidate the bulk of the IPC code Eric Wong
2021-02-18 11:06       ` [PATCHv2 4/4] lei: check for IMAP auth errors Eric Wong
2021-02-17 10:07 ` [PATCH 09/11] lei import: add IMAP, (maildir|mbox*):$PATHNAME support Eric Wong
2021-02-17 10:07 ` [PATCH 10/11] lei: consolidate the bulk of the IPC code Eric Wong
2021-02-17 10:07 ` [PATCH 11/11] lei: check for IMAP auth errors 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=20210218202225.85440-3-e@80x24.org \
    --to=e@80x24.org \
    --cc=meta@public-inbox.org \
    --subject='Re: [PATCHv3 2/4] lei import: add IMAP and (maildir|mbox*):$PATHNAME support' \
    /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

Code repositories for project(s) associated with this inbox:

	https://80x24.org/public-inbox.git

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).