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 $ign = $lei->{sto}->ipc_do('done'); # PublicInbox::LeiStore::done
25 sub import_done { # EOF callback for main daemon
27 my $imp = delete $lei->{imp} or return;
28 $imp->wq_wait_old(\&import_done_wait, $lei);
31 sub net_merge_complete { # callback used by LeiAuth
33 for my $input (@{$self->{inputs}}) {
34 $self->wq_io_do('import_path_url', [], $input);
41 my $self = $lei->{imp};
42 my $j = $lei->{opt}->{jobs} // scalar(@{$self->{inputs}}) || 1;
43 if (my $net = $lei->{net}) {
44 # $j = $net->net_concurrency($j); TODO
46 my $nproc = $self->detect_nproc;
47 $j = $nproc if $j > $nproc;
49 my $ops = { '' => [ \&import_done, $lei ] };
50 $lei->{auth}->op_merge($ops, $self) if $lei->{auth};
51 $self->{-wq_nr_workers} = $j // 1; # locked
52 my $op = $lei->workers_start($self, 'lei_import', undef, $ops);
53 $self->wq_io_do('import_stdin', []) if $self->{0};
54 net_merge_complete($self) unless $lei->{auth};
55 while ($op && $op->{sock}) { $op->event_step }
58 sub call { # the main "lei import" method
59 my ($cls, $lei, @inputs) = @_;
60 my $sto = $lei->_lei_store(1);
61 $sto->write_prepare($lei);
63 $lei->{opt}->{kw} //= 1;
64 my $self = $lei->{imp} = bless { inputs => \@inputs }, $cls;
65 if ($lei->{opt}->{stdin}) {
66 @inputs and return $lei->fail("--stdin and @inputs do not mix");
67 $lei->check_input_format or return;
68 $self->{0} = $lei->{0};
71 my $fmt = $lei->{opt}->{'in-format'};
72 # e.g. Maildir:/home/user/Mail/ or imaps://example.com/INBOX
73 for my $input (@inputs) {
74 my $input_path = $input;
75 if ($input =~ m!\A(?:imaps?|nntps?|s?news)://!i) {
76 require PublicInbox::NetReader;
77 $net //= PublicInbox::NetReader->new;
78 $net->add_url($input);
79 } elsif ($input_path =~ s/\A([a-z0-9]+)://is) {
81 if (($fmt // $ifmt) ne $ifmt) {
82 return $lei->fail(<<"");
83 --in-format=$fmt and `$ifmt:' conflict
87 require PublicInbox::MboxLock;
88 require PublicInbox::MboxReader;
89 PublicInbox::MboxReader->can($ifmt) or return
90 $lei->fail("$ifmt not supported");
92 require PublicInbox::MdirReader;
93 $ifmt eq 'maildir' or return
94 $lei->fail("$ifmt not supported");
96 return $lei->fail("Unable to handle $input");
98 } elsif (-f $input) { push @f, $input
99 } elsif (-d _) { push @d, $input
100 } else { return $lei->fail("Unable to handle $input") }
102 if (@f) { $lei->check_input_format(\@f) or return }
103 if (@d) { # TODO: check for MH vs Maildir, here
104 require PublicInbox::MdirReader;
106 $self->{inputs} = \@inputs;
108 if (my $err = $net->errors) {
109 return $lei->fail($err);
111 $net->{quiet} = $lei->{opt}->{quiet};
113 require PublicInbox::LeiAuth;
114 $lei->{auth} = PublicInbox::LeiAuth->new;
119 sub ipc_atfork_child {
121 my $lei = $self->{lei};
122 delete $lei->{imp}; # drop circular ref
123 $lei->lei_atfork_child;
124 $self->SUPER::ipc_atfork_child;
125 $lei->{auth}->do_auth_atfork($self) if $lei->{auth};
130 my ($lei, $fh, $input, $ifmt) = @_;
131 my $set_kw = $lei->{opt}->{kw};
133 if ($ifmt eq 'eml') {
134 my $buf = do { local $/; <$fh> } //
135 return $lei->child_error(1 << 8, <<"");
136 error reading $input: $!
138 my $eml = PublicInbox::Eml->new(\$buf);
139 _import_eml($eml, $lei->{sto}, $set_kw);
140 } else { # some mbox (->can already checked in call);
141 my $cb = PublicInbox::MboxReader->can($ifmt) //
142 die "BUG: bad fmt=$ifmt";
143 $cb->(undef, $fh, \&_import_eml, $lei->{sto}, $set_kw);
146 $lei->child_error(1 << 8, "$input: $@") if $@;
149 sub _import_maildir { # maildir_each_file cb
150 my ($f, $sto, $set_kw) = @_;
151 $sto->ipc_do('set_eml_from_maildir', $f, $set_kw);
154 sub _import_net { # imap_each, nntp_each cb
155 my ($url, $uid, $kw, $eml, $sto, $set_kw) = @_;
156 $sto->ipc_do('set_eml', $eml, $set_kw ? @$kw : ());
159 sub import_path_url {
160 my ($self, $input) = @_;
161 my $lei = $self->{lei};
162 my $ifmt = lc($lei->{opt}->{'in-format'} // '');
164 if ($input =~ m!\Aimaps?://!i) {
165 $lei->{net}->imap_each($input, \&_import_net, $lei->{sto},
168 } elsif ($input =~ m!\A(?:nntps?|s?news)://!i) {
169 $lei->{net}->nntp_each($input, \&_import_net, $lei->{sto}, 0);
171 } elsif ($input =~ s!\A([a-z0-9]+):!!i) {
175 my $m = $lei->{opt}->{'lock'} // ($ifmt eq 'eml' ? ['none'] :
176 PublicInbox::MboxLock->defaults);
177 my $mbl = PublicInbox::MboxLock->acq($input, 0, $m);
178 _import_fh($lei, $mbl->{fh}, $input, $ifmt);
179 } elsif (-d _ && (-d "$input/cur" || -d "$input/new")) {
180 return $lei->fail(<<EOM) if $ifmt && $ifmt ne 'maildir';
181 $input appears to a be a maildir, not $ifmt
183 PublicInbox::MdirReader::maildir_each_file($input,
185 $lei->{sto}, $lei->{opt}->{kw});
187 $lei->fail("$input unsupported (TODO)");
193 my $lei = $self->{lei};
194 _import_fh($lei, delete $self->{0}, '<stdin>', $lei->{opt}->{'in-format'});
197 no warnings 'once'; # the following works even when LeiAuth is lazy-loaded
198 *net_merge_all = \&PublicInbox::LeiAuth::net_merge_all;