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 PublicInbox::LeiInput);
10 use PublicInbox::PktOp qw(pkt_do);
12 sub eml_cb { # used by PublicInbox::LeiInput::input_fh
13 my ($self, $eml, $vmd) = @_;
14 my $xoids = $self->{lei}->{ale}->xoids_for($eml);
15 $self->{lei}->{sto}->ipc_do('set_eml', $eml, $vmd, $xoids);
18 sub mbox_cb { # MboxReader callback used by PublicInbox::LeiInput::input_fh
19 my ($eml, $self) = @_;
21 if ($self->{-import_kw}) {
22 my $kw = PublicInbox::MboxReader::mbox_keywords($eml);
23 $vmd = { kw => $kw } if scalar(@$kw);
25 eml_cb($self, $eml, $vmd);
28 sub import_done_wait { # dwaitpid callback
30 my ($imp, $lei) = @$arg;
31 $lei->child_error($?, 'non-fatal errors during import') if $?;
32 my $sto = delete $lei->{sto};
33 my $wait = $sto->ipc_do('done') if $sto; # PublicInbox::LeiStore::done
37 sub import_done { # EOF callback for main daemon
39 my $imp = delete $lei->{imp} or return;
40 $imp->wq_wait_old(\&import_done_wait, $lei);
43 sub net_merge_complete { # callback used by LeiAuth
45 for my $input (@{$self->{inputs}}) {
46 $self->wq_io_do('import_path_url', [], $input);
53 my $self = $lei->{imp};
54 $lei->ale; # initialize for workers to read
55 my $j = $lei->{opt}->{jobs} // scalar(@{$self->{inputs}}) || 1;
56 if (my $net = $lei->{net}) {
57 # $j = $net->net_concurrency($j); TODO
59 my $nproc = $self->detect_nproc;
60 $j = $nproc if $j > $nproc;
62 my $ops = { '' => [ \&import_done, $lei ] };
63 $lei->{auth}->op_merge($ops, $self) if $lei->{auth};
64 $self->{-wq_nr_workers} = $j // 1; # locked
65 my $op = $lei->workers_start($self, 'lei_import', undef, $ops);
66 $self->wq_io_do('import_stdin', []) if $self->{0};
67 net_merge_complete($self) unless $lei->{auth};
68 while ($op && $op->{sock}) { $op->event_step }
71 sub lei_import { # the main "lei import" method
72 my ($lei, @inputs) = @_;
73 my $sto = $lei->_lei_store(1);
74 $sto->write_prepare($lei);
75 my $self = $lei->{imp} = bless {}, __PACKAGE__;
76 $self->{-import_kw} = $lei->{opt}->{kw} // 1;
77 $self->prepare_inputs($lei, \@inputs) or return;
81 sub _import_maildir { # maildir_each_eml cb
82 my ($f, $kw, $eml, $sto, $set_kw) = @_;
83 $sto->ipc_do('set_eml', $eml, $set_kw ? { kw => $kw }: ());
86 sub _import_net { # imap_each, nntp_each cb
87 my ($url, $uid, $kw, $eml, $sto, $set_kw) = @_;
88 $sto->ipc_do('set_eml', $eml, $set_kw ? { kw => $kw } : ());
92 my ($self, $input) = @_;
93 my $lei = $self->{lei};
94 my $ifmt = lc($lei->{opt}->{'in-format'} // '');
96 if ($input =~ m!\Aimaps?://!i) {
97 $lei->{net}->imap_each($input, \&_import_net, $lei->{sto},
100 } elsif ($input =~ m!\A(?:nntps?|s?news)://!i) {
101 $lei->{net}->nntp_each($input, \&_import_net, $lei->{sto}, 0);
103 } elsif ($input =~ s!\A([a-z0-9]+):!!i) {
107 my $m = $lei->{opt}->{'lock'} // ($ifmt eq 'eml' ? ['none'] :
108 PublicInbox::MboxLock->defaults);
109 my $mbl = PublicInbox::MboxLock->acq($input, 0, $m);
110 $self->input_fh($ifmt, $mbl->{fh}, $input);
111 } elsif (-d _ && (-d "$input/cur" || -d "$input/new")) {
112 return $lei->fail(<<EOM) if $ifmt && $ifmt ne 'maildir';
113 $input appears to a be a maildir, not $ifmt
115 PublicInbox::MdirReader::maildir_each_eml($input,
117 $lei->{sto}, $self->{-import_kw});
119 $lei->fail("$input unsupported (TODO)");
125 my $lei = $self->{lei};
126 my $in = delete $self->{0};
127 $self->input_fh($lei->{opt}->{'in-format'}, $in, '<stdin>');
131 *ipc_atfork_child = \&PublicInbox::LeiInput::input_only_atfork_child;
133 # the following works even when LeiAuth is lazy-loaded
134 *net_merge_all = \&PublicInbox::LeiAuth::net_merge_all;