1 # Copyright (C) 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);
9 use PublicInbox::InboxWritable qw(eml_from_path);
11 # /^input_/ subs are used by (or override) PublicInbox::LeiInput superclass
13 sub input_eml_cb { # used by PublicInbox::LeiInput::input_fh
14 my ($self, $eml, $vmd) = @_;
15 my $xoids = $self->{lei}->{ale}->xoids_for($eml);
16 if (my $all_vmd = $self->{all_vmd}) {
17 @$vmd{keys %$all_vmd} = values %$all_vmd;
19 $self->{lei}->{sto}->wq_do('set_eml', $eml, $vmd, $xoids);
22 sub input_mbox_cb { # MboxReader callback
23 my ($eml, $self) = @_;
25 if ($self->{-import_kw}) {
26 my $kw = PublicInbox::MboxReader::mbox_keywords($eml);
27 $vmd = { kw => $kw } if scalar(@$kw);
29 input_eml_cb($self, $eml, $vmd);
32 sub pmdir_cb { # called via wq_io_do from LeiPmdir->each_mdir_fn
33 my ($self, $f, $fl) = @_;
34 my ($folder, $bn) = ($f =~ m!\A(.+?)/(?:new|cur)/([^/]+)\z!) or
35 die "BUG: $f was not from a Maildir?\n";
36 my $kw = PublicInbox::MdirReader::flags2kw($fl);
37 substr($folder, 0, 0) = 'maildir:'; # add prefix
38 my $lse = $self->{lse} //= $self->{lei}->{sto}->search;
39 my $lms = $self->{-lms_rw} //= $self->{lei}->lms; # may be 0 or undef
40 my @oidbin = $lms ? $lms->name_oidbin($folder, $bn) : ();
41 @oidbin > 1 and warn("W: $folder/*/$$bn not unique:\n",
42 map { "\t".unpack('H*', $_)."\n" } @oidbin);
44 my @docids = sort { $a <=> $b } grep { !$seen{$_}++ }
45 map { $lse->over->oidbin_exists($_) } @oidbin;
46 my $vmd = $self->{-import_kw} ? { kw => $kw } : undef;
48 $lse->kw_changed(undef, $kw, \@docids) or return;
50 if (my $eml = eml_from_path($f)) {
51 $vmd->{sync_info} = [ $folder, \$bn ] if $self->{-mail_sync};
52 $self->input_eml_cb($eml, $vmd);
56 sub input_net_cb { # imap_each / nntp_each
57 my ($uri, $uid, $kw, $eml, $self) = @_;
59 my $vmd = $self->{-import_kw} ? { kw => $kw } : undef;
60 $vmd->{sync_info} = [ $$uri, $uid ] if $self->{-mail_sync};
61 $self->input_eml_cb($eml, $vmd);
62 } elsif (my $ikw = $self->{lei}->{ikw}) { # old message, kw only
63 # we send $uri as a bare SCALAR and not a URIimap ref to
64 # reduce socket traffic:
65 $ikw->wq_io_do('ck_update_kw', [], $$uri, $uid, $kw);
69 sub do_import_index ($$@) {
70 my ($self, $lei, @inputs) = @_;
71 my $sto = $lei->_lei_store(1);
72 $sto->write_prepare($lei);
73 $self->{-import_kw} = $lei->{opt}->{kw} // 1;
74 my $vmd_mod = $self->vmd_mod_extract(\@inputs);
75 return $lei->fail(join("\n", @{$vmd_mod->{err}})) if $vmd_mod->{err};
76 $self->{all_vmd} = $vmd_mod if scalar keys %$vmd_mod;
77 $lei->ale; # initialize for workers to read (before LeiPmdir->new)
78 $self->{-mail_sync} = $lei->{opt}->{'mail-sync'} // 1;
79 $self->prepare_inputs($lei, \@inputs) or return;
81 my $j = $lei->{opt}->{jobs} // 0;
82 $j =~ /\A([0-9]+),[0-9]+\z/ and $j = $1 + 0;
83 $j ||= scalar(@{$self->{inputs}}) || 1;
85 my $net = $lei->{net};
87 # $j = $net->net_concurrency($j); TODO
88 if ($lei->{opt}->{incremental} // 1) {
89 $net->{incremental} = 1;
90 $net->{-lms_rw} = $lei->lms // 0;
91 if ($self->{-import_kw} && $net->{-lms_rw} &&
92 !$lei->{opt}->{'new-only'} &&
94 require PublicInbox::LeiImportKw;
95 $ikw = PublicInbox::LeiImportKw->new($lei);
100 my $nproc = $self->detect_nproc;
101 $j = $nproc if $j > $nproc;
103 ($lei->{opt}->{'new-only'} && (!$net || !$net->{imap_order})) and
104 warn "# --new-only is only for IMAP\n";
105 $lei->{-eml_noisy} = 1;
106 $lei->{-err_type} = 'non-fatal';
107 $lei->wq1_start($self, $j);
110 sub lei_import { # the main "lei import" method
111 my ($lei, @inputs) = @_;
112 my $self = bless {}, __PACKAGE__;
113 do_import_index($self, $lei, @inputs);
116 sub _complete_import {
117 my ($lei, @argv) = @_;
118 my ($re, $cur, $match_cb) = $lei->complete_url_prepare(\@argv);
119 my @k = $lei->url_folder_cache->keys($argv[-1] // undef, 1);
120 my @L = eval { $lei->_lei_store->search->all_terms('L') };
121 push(@k, map { "+L:$_" } @L);
122 my @m = map { $match_cb->($_) } @k;
123 my %f = map { $_ => 1 } (@m ? @m : @k);
124 if (my $lms = $lei->lms) {
125 @k = $lms->folders($argv[-1] // undef, 1);
126 @m = map { $match_cb->($_) } @k;
127 if (@m) { @f{@m} = @m } else { @f{@k} = @k }
133 *ipc_atfork_child = \&PublicInbox::LeiInput::input_only_atfork_child;
134 *net_merge_all_done = \&PublicInbox::LeiInput::input_only_net_merge_all_done;