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 ?
15 @{PublicInbox::MboxReader::mbox_keywords($eml)} : ());
18 sub import_done_wait { # dwaitpid callback
20 my ($imp, $lei) = @$arg;
21 $lei->child_error($?, 'non-fatal errors during import') if $?;
22 my $sto = delete $lei->{sto};
23 my $wait = $sto->ipc_do('done') if $sto; # PublicInbox::LeiStore::done
27 sub import_done { # EOF callback for main daemon
29 my $imp = delete $lei->{imp} or return;
30 $imp->wq_wait_old(\&import_done_wait, $lei);
33 sub net_merge_complete { # callback used by LeiAuth
35 for my $input (@{$self->{inputs}}) {
36 $self->wq_io_do('import_path_url', [], $input);
43 my $self = $lei->{imp};
44 my $j = $lei->{opt}->{jobs} // scalar(@{$self->{inputs}}) || 1;
45 if (my $net = $lei->{net}) {
46 # $j = $net->net_concurrency($j); TODO
48 my $nproc = $self->detect_nproc;
49 $j = $nproc if $j > $nproc;
51 my $ops = { '' => [ \&import_done, $lei ] };
52 $lei->{auth}->op_merge($ops, $self) if $lei->{auth};
53 $self->{-wq_nr_workers} = $j // 1; # locked
54 my $op = $lei->workers_start($self, 'lei_import', undef, $ops);
55 $self->wq_io_do('import_stdin', []) if $self->{0};
56 net_merge_complete($self) unless $lei->{auth};
57 while ($op && $op->{sock}) { $op->event_step }
60 sub call { # the main "lei import" method
61 my ($cls, $lei, @inputs) = @_;
62 my $sto = $lei->_lei_store(1);
63 $sto->write_prepare($lei);
65 $lei->{opt}->{kw} //= 1;
66 my $self = $lei->{imp} = bless { inputs => \@inputs }, $cls;
67 if ($lei->{opt}->{stdin}) {
68 @inputs and return $lei->fail("--stdin and @inputs do not mix");
69 $lei->check_input_format or return;
70 $self->{0} = $lei->{0};
73 my $fmt = $lei->{opt}->{'in-format'};
74 # e.g. Maildir:/home/user/Mail/ or imaps://example.com/INBOX
75 for my $input (@inputs) {
76 my $input_path = $input;
77 if ($input =~ m!\A(?:imaps?|nntps?|s?news)://!i) {
78 require PublicInbox::NetReader;
79 $net //= PublicInbox::NetReader->new;
80 $net->add_url($input);
81 } elsif ($input_path =~ s/\A([a-z0-9]+)://is) {
83 if (($fmt // $ifmt) ne $ifmt) {
84 return $lei->fail(<<"");
85 --in-format=$fmt and `$ifmt:' conflict
89 require PublicInbox::MboxLock;
90 require PublicInbox::MboxReader;
91 PublicInbox::MboxReader->can($ifmt) or return
92 $lei->fail("$ifmt not supported");
94 require PublicInbox::MdirReader;
95 $ifmt eq 'maildir' or return
96 $lei->fail("$ifmt not supported");
98 return $lei->fail("Unable to handle $input");
100 } elsif (-f $input) { push @f, $input
101 } elsif (-d _) { push @d, $input
102 } else { return $lei->fail("Unable to handle $input") }
104 if (@f) { $lei->check_input_format(\@f) or return }
105 if (@d) { # TODO: check for MH vs Maildir, here
106 require PublicInbox::MdirReader;
108 $self->{inputs} = \@inputs;
110 if (my $err = $net->errors) {
111 return $lei->fail($err);
113 $net->{quiet} = $lei->{opt}->{quiet};
115 require PublicInbox::LeiAuth;
116 $lei->{auth} = PublicInbox::LeiAuth->new;
121 sub ipc_atfork_child {
123 my $lei = $self->{lei};
124 delete $lei->{imp}; # drop circular ref
125 $lei->lei_atfork_child;
126 $self->SUPER::ipc_atfork_child;
127 $lei->{auth}->do_auth_atfork($self) if $lei->{auth};
132 my ($lei, $fh, $input, $ifmt) = @_;
133 my $set_kw = $lei->{opt}->{kw};
135 if ($ifmt eq 'eml') {
136 my $buf = do { local $/; <$fh> } //
137 return $lei->child_error(1 << 8, <<"");
138 error reading $input: $!
140 my $eml = PublicInbox::Eml->new(\$buf);
141 _import_eml($eml, $lei->{sto}, $set_kw);
142 } else { # some mbox (->can already checked in call);
143 my $cb = PublicInbox::MboxReader->can($ifmt) //
144 die "BUG: bad fmt=$ifmt";
145 $cb->(undef, $fh, \&_import_eml, $lei->{sto}, $set_kw);
148 $lei->child_error(1 << 8, "$input: $@") if $@;
151 sub _import_maildir { # maildir_each_eml cb
152 my ($f, $kw, $eml, $sto, $set_kw) = @_;
153 $sto->ipc_do('set_eml', $eml, $set_kw ? @$kw : ());
156 sub _import_net { # imap_each, nntp_each cb
157 my ($url, $uid, $kw, $eml, $sto, $set_kw) = @_;
158 $sto->ipc_do('set_eml', $eml, $set_kw ? @$kw : ());
161 sub import_path_url {
162 my ($self, $input) = @_;
163 my $lei = $self->{lei};
164 my $ifmt = lc($lei->{opt}->{'in-format'} // '');
166 if ($input =~ m!\Aimaps?://!i) {
167 $lei->{net}->imap_each($input, \&_import_net, $lei->{sto},
170 } elsif ($input =~ m!\A(?:nntps?|s?news)://!i) {
171 $lei->{net}->nntp_each($input, \&_import_net, $lei->{sto}, 0);
173 } elsif ($input =~ s!\A([a-z0-9]+):!!i) {
177 my $m = $lei->{opt}->{'lock'} // ($ifmt eq 'eml' ? ['none'] :
178 PublicInbox::MboxLock->defaults);
179 my $mbl = PublicInbox::MboxLock->acq($input, 0, $m);
180 _import_fh($lei, $mbl->{fh}, $input, $ifmt);
181 } elsif (-d _ && (-d "$input/cur" || -d "$input/new")) {
182 return $lei->fail(<<EOM) if $ifmt && $ifmt ne 'maildir';
183 $input appears to a be a maildir, not $ifmt
185 PublicInbox::MdirReader::maildir_each_eml($input,
187 $lei->{sto}, $lei->{opt}->{kw});
189 $lei->fail("$input unsupported (TODO)");
195 my $lei = $self->{lei};
196 _import_fh($lei, delete $self->{0}, '<stdin>', $lei->{opt}->{'in-format'});
199 no warnings 'once'; # the following works even when LeiAuth is lazy-loaded
200 *net_merge_all = \&PublicInbox::LeiAuth::net_merge_all;