user/dev discussion of public-inbox itself
 help / color / mirror / code / Atom feed
a0d79282b618ce53d176eee71c28df98bc3ba5e4 blob 5306 bytes (raw)

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
 
# Copyright (C) 2021 all contributors <meta@public-inbox.org>
# License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>

# front-end for the "lei import" sub-command
package PublicInbox::LeiImport;
use strict;
use v5.10.1;
use parent qw(PublicInbox::IPC);
use PublicInbox::Eml;
use PublicInbox::InboxWritable qw(eml_from_path);

sub _import_eml { # MboxReader callback
	my ($eml, $sto, $set_kw) = @_;
	$sto->ipc_do('set_eml', $eml, $set_kw ? $sto->mbox_keywords($eml) : ());
}

sub import_done_wait { # dwaitpid callback
	my ($arg, $pid) = @_;
	my ($imp, $lei) = @$arg;
	$lei->child_error($?, 'non-fatal errors during import') if $?;
	my $ign = $lei->{sto}->ipc_do('done'); # PublicInbox::LeiStore::done
	$lei->dclose;
}

sub import_done { # EOF callback for main daemon
	my ($lei) = @_;
	my $imp = delete $lei->{imp} or return;
	$imp->wq_wait_old(\&import_done_wait, $lei);
}

sub import_start {
	my ($lei) = @_;
	my $self = $lei->{imp};
	my $j = $lei->{opt}->{jobs} // scalar(@{$self->{inputs}}) || 1;
	if (my $nrd = $lei->{nrd}) {
		# $j = $nrd->net_concurrency($j); TODO
	} else {
		my $nproc = $self->detect_nproc;
		$j = $nproc if $j > $nproc;
	}
	my $op = $lei->workers_start($self, 'lei_import', $j, {
		'' => [ \&import_done, $lei ],
	});
	$self->wq_io_do('import_stdin', []) if $self->{0};
	for my $input (@{$self->{inputs}}) {
		$self->wq_io_do('import_path_url', [], $input);
	}
	$self->wq_close(1);
	while ($op && $op->{sock}) { $op->event_step }
}

sub call { # the main "lei import" method
	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 { inputs => \@inputs }, $cls;
	if ($lei->{opt}->{stdin}) {
		@inputs and return $lei->fail("--stdin and @inputs do not mix");
		$lei->check_input_format or return;
		$self->{0} = $lei->{0};
	}

	# 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 $input_path) {
				require PublicInbox::MboxReader;
				PublicInbox::MboxReader->can($ifmt) or return
					$lei->fail("$ifmt not supported");
			} elsif (-d _) {
				$ifmt eq 'maildir' or return
					$lei->fail("$ifmt not supported");
			} else { return $lei->fail("Unable to handle $input_path") }
		} elsif (-f $input) { push @f, $input
		} elsif (-d _) { push @d, $input
		} else { return $lei->fail("Unable to handle $input") }
	}
	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, $input, $ifmt) = @_;
	my $set_kw = $lei->{opt}->{kw};
	eval {
		if ($ifmt eq 'eml') {
			my $buf = do { local $/; <$fh> } //
				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($ifmt) //
				die "BUG: bad fmt=$ifmt";
			$cb->(undef, $fh, \&_import_eml, $lei->{sto}, $set_kw);
		}
	};
	$lei->child_error(1 << 8, "<stdin>: $@") if $@;
}

sub _import_maildir { # maildir_each_file cb
	my ($f, $sto, $set_kw) = @_;
	$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, $input) = @_;
	my $lei = $self->{lei};
	my $ifmt = lc($lei->{opt}->{'format'} // '');
	# TODO auto-detect?
	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, $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("$input unsupported (TODO)");
	}
}

sub import_stdin {
	my ($self) = @_;
	my $lei = $self->{lei};
	_import_fh($lei, delete $self->{0}, '<stdin>', $lei->{opt}->{'format'});
}

1;
debug log:

solving a0d79282 ...
found a0d79282 in https://public-inbox.org/meta/20210217100707.6796-11-e@80x24.org/
found 4d225262 in https://public-inbox.org/meta/20210217100707.6796-10-e@80x24.org/
found 32f3a467 in public-inbox.git.git
preparing index
index prepared:
100644 32f3a4673d3f79c943bbec8f4d67fa708354955b	lib/PublicInbox/LeiImport.pm

applying [1/2] https://public-inbox.org/meta/20210217100707.6796-10-e@80x24.org/
diff --git a/lib/PublicInbox/LeiImport.pm b/lib/PublicInbox/LeiImport.pm
index 32f3a467..4d225262 100644


applying [2/2] https://public-inbox.org/meta/20210217100707.6796-11-e@80x24.org/
diff --git a/lib/PublicInbox/LeiImport.pm b/lib/PublicInbox/LeiImport.pm
index 4d225262..a0d79282 100644

Checking patch lib/PublicInbox/LeiImport.pm...
Applied patch lib/PublicInbox/LeiImport.pm cleanly.
Checking patch lib/PublicInbox/LeiImport.pm...
Applied patch lib/PublicInbox/LeiImport.pm cleanly.

index at:
100644 a0d79282b618ce53d176eee71c28df98bc3ba5e4	lib/PublicInbox/LeiImport.pm

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