1 # Copyright (C) 2021 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 # front-end for the "lei import" sub-command
5 package PublicInbox::LeiImport;
8 use parent qw(PublicInbox::IPC);
10 use PublicInbox::PktOp qw(pkt_do);
12 sub _import_eml { # MboxReader callback
13 my ($eml, $sto, $set_kw) = @_;
14 $sto->ipc_do('set_eml', $eml, $set_kw ? $sto->mbox_keywords($eml) : ());
17 sub import_done_wait { # dwaitpid callback
19 my ($imp, $lei) = @$arg;
20 $lei->child_error($?, 'non-fatal errors during import') if $?;
21 my $sto = delete $lei->{sto};
22 my $wait = $sto->ipc_do('done') if $sto; # PublicInbox::LeiStore::done
26 sub import_done { # EOF callback for main daemon
28 my $imp = delete $lei->{imp} or return;
29 $imp->wq_wait_old(\&import_done_wait, $lei);
32 sub net_merge_complete { # callback used by LeiAuth
34 for my $input (@{$self->{inputs}}) {
35 $self->wq_io_do('import_path_url', [], $input);
42 my $self = $lei->{imp};
43 my $j = $lei->{opt}->{jobs} // scalar(@{$self->{inputs}}) || 1;
44 if (my $net = $lei->{net}) {
45 # $j = $net->net_concurrency($j); TODO
47 my $nproc = $self->detect_nproc;
48 $j = $nproc if $j > $nproc;
50 my $ops = { '' => [ \&import_done, $lei ] };
51 $lei->{auth}->op_merge($ops, $self) if $lei->{auth};
52 $self->{-wq_nr_workers} = $j // 1; # locked
53 my $op = $lei->workers_start($self, 'lei_import', undef, $ops);
54 $self->wq_io_do('import_stdin', []) if $self->{0};
55 net_merge_complete($self) unless $lei->{auth};
56 while ($op && $op->{sock}) { $op->event_step }
59 sub call { # the main "lei import" method
60 my ($cls, $lei, @inputs) = @_;
61 my $sto = $lei->_lei_store(1);
62 $sto->write_prepare($lei);
64 $lei->{opt}->{kw} //= 1;
65 my $self = $lei->{imp} = bless { inputs => \@inputs }, $cls;
66 if ($lei->{opt}->{stdin}) {
67 @inputs and return $lei->fail("--stdin and @inputs do not mix");
68 $lei->check_input_format or return;
69 $self->{0} = $lei->{0};
72 my $fmt = $lei->{opt}->{'in-format'};
73 # e.g. Maildir:/home/user/Mail/ or imaps://example.com/INBOX
74 for my $input (@inputs) {
75 my $input_path = $input;
76 if ($input =~ m!\A(?:imaps?|nntps?|s?news)://!i) {
77 require PublicInbox::NetReader;
78 $net //= PublicInbox::NetReader->new;
79 $net->add_url($input);
80 } elsif ($input_path =~ s/\A([a-z0-9]+)://is) {
82 if (($fmt // $ifmt) ne $ifmt) {
83 return $lei->fail(<<"");
84 --in-format=$fmt and `$ifmt:' conflict
88 require PublicInbox::MboxLock;
89 require PublicInbox::MboxReader;
90 PublicInbox::MboxReader->can($ifmt) or return
91 $lei->fail("$ifmt not supported");
93 require PublicInbox::MdirReader;
94 $ifmt eq 'maildir' or return
95 $lei->fail("$ifmt not supported");
97 return $lei->fail("Unable to handle $input");
99 } elsif (-f $input) { push @f, $input
100 } elsif (-d _) { push @d, $input
101 } else { return $lei->fail("Unable to handle $input") }
103 if (@f) { $lei->check_input_format(\@f) or return }
104 if (@d) { # TODO: check for MH vs Maildir, here
105 require PublicInbox::MdirReader;
107 $self->{inputs} = \@inputs;
109 if (my $err = $net->errors) {
110 return $lei->fail($err);
112 $net->{quiet} = $lei->{opt}->{quiet};
114 require PublicInbox::LeiAuth;
115 $lei->{auth} = PublicInbox::LeiAuth->new;
120 sub ipc_atfork_child {
122 my $lei = $self->{lei};
123 delete $lei->{imp}; # drop circular ref
124 $lei->lei_atfork_child;
125 $self->SUPER::ipc_atfork_child;
126 $lei->{auth}->do_auth_atfork($self) if $lei->{auth};
131 my ($lei, $fh, $input, $ifmt) = @_;
132 my $set_kw = $lei->{opt}->{kw};
134 if ($ifmt eq 'eml') {
135 my $buf = do { local $/; <$fh> } //
136 return $lei->child_error(1 << 8, <<"");
137 error reading $input: $!
139 my $eml = PublicInbox::Eml->new(\$buf);
140 _import_eml($eml, $lei->{sto}, $set_kw);
141 } else { # some mbox (->can already checked in call);
142 my $cb = PublicInbox::MboxReader->can($ifmt) //
143 die "BUG: bad fmt=$ifmt";
144 $cb->(undef, $fh, \&_import_eml, $lei->{sto}, $set_kw);
147 $lei->child_error(1 << 8, "$input: $@") if $@;
150 sub _import_maildir { # maildir_each_file cb
151 my ($f, $sto, $set_kw) = @_;
152 $sto->ipc_do('set_eml_from_maildir', $f, $set_kw);
155 sub _import_net { # imap_each, nntp_each cb
156 my ($url, $uid, $kw, $eml, $sto, $set_kw) = @_;
157 $sto->ipc_do('set_eml', $eml, $set_kw ? @$kw : ());
160 sub import_path_url {
161 my ($self, $input) = @_;
162 my $lei = $self->{lei};
163 my $ifmt = lc($lei->{opt}->{'in-format'} // '');
165 if ($input =~ m!\Aimaps?://!i) {
166 $lei->{net}->imap_each($input, \&_import_net, $lei->{sto},
169 } elsif ($input =~ m!\A(?:nntps?|s?news)://!i) {
170 $lei->{net}->nntp_each($input, \&_import_net, $lei->{sto}, 0);
172 } elsif ($input =~ s!\A([a-z0-9]+):!!i) {
176 my $m = $lei->{opt}->{'lock'} // ($ifmt eq 'eml' ? ['none'] :
177 PublicInbox::MboxLock->defaults);
178 my $mbl = PublicInbox::MboxLock->acq($input, 0, $m);
179 _import_fh($lei, $mbl->{fh}, $input, $ifmt);
180 } elsif (-d _ && (-d "$input/cur" || -d "$input/new")) {
181 return $lei->fail(<<EOM) if $ifmt && $ifmt ne 'maildir';
182 $input appears to a be a maildir, not $ifmt
184 PublicInbox::MdirReader::maildir_each_file($input,
186 $lei->{sto}, $lei->{opt}->{kw});
188 $lei->fail("$input unsupported (TODO)");
194 my $lei = $self->{lei};
195 _import_fh($lei, delete $self->{0}, '<stdin>', $lei->{opt}->{'in-format'});
198 no warnings 'once'; # the following works even when LeiAuth is lazy-loaded
199 *net_merge_all = \&PublicInbox::LeiAuth::net_merge_all;