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 ($uri, $uid, $kw, $eml, $self) = @_;
47 my $vmd = $self->{-import_kw} ? { kw => $kw } : undef;
48 $vmd->{sync_info} = [ $$uri, $uid ] if $self->{-mail_sync};
49 $self->input_eml_cb($eml, $vmd);
50 } elsif (my $ikw = $self->{lei}->{ikw}) { # old message, kw only
51 # we send $uri as a bare SCALAR and not a URIimap ref to
52 # reduce socket traffic:
53 $ikw->wq_io_do('ck_update_kw', [], $$uri, $uid, $kw);
57 sub do_import_index ($$@) {
58 my ($self, $lei, @inputs) = @_;
59 my $sto = $lei->_lei_store(1);
60 $sto->write_prepare($lei);
61 $self->{-import_kw} = $lei->{opt}->{kw} // 1;
62 my $vmd_mod = $self->vmd_mod_extract(\@inputs);
63 return $lei->fail(join("\n", @{$vmd_mod->{err}})) if $vmd_mod->{err};
64 $self->{all_vmd} = $vmd_mod if scalar keys %$vmd_mod;
65 $self->prepare_inputs($lei, \@inputs) or return;
66 $self->{-mail_sync} = $lei->{opt}->{'mail-sync'} // 1;
68 $lei->ale; # initialize for workers to read
69 my $j = $lei->{opt}->{jobs} // scalar(@{$self->{inputs}}) || 1;
71 if (my $net = $lei->{net}) {
72 # $j = $net->net_concurrency($j); TODO
73 if ($lei->{opt}->{incremental} // 1) {
74 $net->{incremental} = 1;
75 $net->{-lms_ro} = $sto->search->lms // 0;
76 if ($self->{-import_kw} && $net->{-lms_ro} &&
78 require PublicInbox::LeiImportKw;
79 $ikw = PublicInbox::LeiImportKw->new($lei);
84 my $nproc = $self->detect_nproc;
85 $j = $nproc if $j > $nproc;
88 $lei->{auth}->op_merge($ops, $self) if $lei->{auth};
89 $self->{-wq_nr_workers} = $j // 1; # locked
90 $lei->{-eml_noisy} = 1;
91 (my $op_c, $ops) = $lei->workers_start($self, $j, $ops);
93 $lei->{-err_type} = 'non-fatal';
94 net_merge_all_done($self) unless $lei->{auth};
95 $lei->wait_wq_events($op_c, $ops);
98 sub lei_import { # the main "lei import" method
99 my ($lei, @inputs) = @_;
100 my $self = bless {}, __PACKAGE__;
101 do_import_index($self, $lei, @inputs);
104 sub _complete_import {
105 my ($lei, @argv) = @_;
106 my $sto = $lei->_lei_store or return;
107 my $lms = $sto->search->lms or return;
108 my $match_cb = $lei->complete_url_prepare(\@argv);
109 map { $match_cb->($_) } $lms->folders;
113 *ipc_atfork_child = \&PublicInbox::LeiInput::input_only_atfork_child;
114 *net_merge_all_done = \&PublicInbox::LeiInput::input_only_net_merge_all_done;
116 # the following works even when LeiAuth is lazy-loaded
117 *net_merge_all = \&PublicInbox::LeiAuth::net_merge_all;