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 # /^input_/ subs are used by (or override) PublicInbox::LeiInput superclass
12 sub input_eml_cb { # used by PublicInbox::LeiInput::input_fh
13 my ($self, $eml, $vmd) = @_;
14 my $xoids = $self->{lei}->{ale}->xoids_for($eml);
15 if (my $all_vmd = $self->{all_vmd}) {
16 @$vmd{keys %$all_vmd} = values %$all_vmd;
18 $self->{lei}->{sto}->ipc_do('set_eml', $eml, $vmd, $xoids);
21 sub input_mbox_cb { # MboxReader callback
22 my ($eml, $self) = @_;
24 if ($self->{-import_kw}) {
25 my $kw = PublicInbox::MboxReader::mbox_keywords($eml);
26 $vmd = { kw => $kw } if scalar(@$kw);
28 input_eml_cb($self, $eml, $vmd);
31 sub input_maildir_cb { # maildir_each_eml cb
32 my ($f, $kw, $eml, $self) = @_;
33 my $vmd = $self->{-import_kw} ? { kw => $kw } : undef;
34 if ($self->{-mail_sync}) {
35 if ($f =~ m!\A(.+?)/(?:new|cur)/([^/]+)\z!) { # ugh...
36 $vmd->{sync_info} = [ "maildir:$1", \(my $n = $2) ];
38 warn "E: $f was not from a Maildir?\n";
41 $self->input_eml_cb($eml, $vmd);
44 sub input_net_cb { # imap_each / nntp_each
45 my ($url, $uid, $kw, $eml, $self) = @_;
46 my $vmd = $self->{-import_kw} ? { kw => $kw } : undef;
47 $vmd->{sync_info} = [ $url, $uid ] if $self->{-mail_sync};
48 $self->input_eml_cb($eml, $vmd);
51 sub do_import_index ($$@) {
52 my ($self, $lei, @inputs) = @_;
53 my $sto = $lei->_lei_store(1);
54 $sto->write_prepare($lei);
55 $self->{-import_kw} = $lei->{opt}->{kw} // 1;
56 my $vmd_mod = $self->vmd_mod_extract(\@inputs);
57 return $lei->fail(join("\n", @{$vmd_mod->{err}})) if $vmd_mod->{err};
58 $self->{all_vmd} = $vmd_mod if scalar keys %$vmd_mod;
59 $self->prepare_inputs($lei, \@inputs) or return;
60 $self->{-mail_sync} = $lei->{opt}->{'mail-sync'} // 1;
62 $lei->ale; # initialize for workers to read
63 my $j = $lei->{opt}->{jobs} // scalar(@{$self->{inputs}}) || 1;
64 if (my $net = $lei->{net}) {
65 # $j = $net->net_concurrency($j); TODO
66 if ($lei->{opt}->{incremental} // 1) {
67 $net->{incremental} = 1;
68 $net->{-lms_ro} = $lei->_lei_store->search->lms // 0;
71 my $nproc = $self->detect_nproc;
72 $j = $nproc if $j > $nproc;
75 $lei->{auth}->op_merge($ops, $self) if $lei->{auth};
76 $self->{-wq_nr_workers} = $j // 1; # locked
77 $lei->{-eml_noisy} = 1;
78 (my $op_c, $ops) = $lei->workers_start($self, $j, $ops);
80 $lei->{-err_type} = 'non-fatal';
81 net_merge_all_done($self) unless $lei->{auth};
82 $op_c->op_wait_event($ops);
85 sub lei_import { # the main "lei import" method
86 my ($lei, @inputs) = @_;
87 my $self = bless {}, __PACKAGE__;
88 do_import_index($self, $lei, @inputs);
91 sub _complete_import {
92 my ($lei, @argv) = @_;
93 my $sto = $lei->_lei_store or return;
94 my $lms = $sto->search->lms or return;
95 my $match_cb = $lei->complete_url_prepare(\@argv);
96 map { $match_cb->($_) } $lms->folders;
100 *ipc_atfork_child = \&PublicInbox::LeiInput::input_only_atfork_child;
101 *net_merge_all_done = \&PublicInbox::LeiInput::input_only_net_merge_all_done;
103 # the following works even when LeiAuth is lazy-loaded
104 *net_merge_all = \&PublicInbox::LeiAuth::net_merge_all;