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 --format=$fmt and `$ifmt:' conflict
87 require PublicInbox::MboxReader;
88 PublicInbox::MboxReader->can($ifmt) or return
89 $lei->fail("$ifmt not supported");
91 require PublicInbox::MdirReader;
92 $ifmt eq 'maildir' or return
93 $lei->fail("$ifmt not supported");
95 return $lei->fail("Unable to handle $input");
97 } elsif (-f $input) { push @f, $input
98 } elsif (-d _) { push @d, $input
99 } else { return $lei->fail("Unable to handle $input") }
101 if (@f) { $lei->check_input_format(\@f) or return }
102 if (@d) { # TODO: check for MH vs Maildir, here
103 require PublicInbox::MdirReader;
105 $self->{inputs} = \@inputs;
107 if (my $err = $net->errors) {
108 return $lei->fail($err);
110 $net->{quiet} = $lei->{opt}->{quiet};
112 require PublicInbox::LeiAuth;
113 $lei->{auth} = PublicInbox::LeiAuth->new;
118 sub ipc_atfork_child {
120 my $lei = $self->{lei};
121 delete $lei->{imp}; # drop circular ref
122 $lei->lei_atfork_child;
123 $self->SUPER::ipc_atfork_child;
124 $lei->{auth}->do_auth_atfork($self) if $lei->{auth};
129 my ($lei, $fh, $input, $ifmt) = @_;
130 my $set_kw = $lei->{opt}->{kw};
132 if ($ifmt eq 'eml') {
133 my $buf = do { local $/; <$fh> } //
134 return $lei->child_error(1 << 8, <<"");
135 error reading $input: $!
137 my $eml = PublicInbox::Eml->new(\$buf);
138 _import_eml($eml, $lei->{sto}, $set_kw);
139 } else { # some mbox (->can already checked in call);
140 my $cb = PublicInbox::MboxReader->can($ifmt) //
141 die "BUG: bad fmt=$ifmt";
142 $cb->(undef, $fh, \&_import_eml, $lei->{sto}, $set_kw);
145 $lei->child_error(1 << 8, "<stdin>: $@") if $@;
148 sub _import_maildir { # maildir_each_file cb
149 my ($f, $sto, $set_kw) = @_;
150 $sto->ipc_do('set_eml_from_maildir', $f, $set_kw);
153 sub _import_net { # imap_each, nntp_each cb
154 my ($url, $uid, $kw, $eml, $sto, $set_kw) = @_;
155 $sto->ipc_do('set_eml', $eml, $set_kw ? @$kw : ());
158 sub import_path_url {
159 my ($self, $input) = @_;
160 my $lei = $self->{lei};
161 my $ifmt = lc($lei->{opt}->{'in-format'} // '');
163 if ($input =~ m!\Aimaps?://!i) {
164 $lei->{net}->imap_each($input, \&_import_net, $lei->{sto},
167 } elsif ($input =~ m!\A(?:nntps?|s?news)://!i) {
168 $lei->{net}->nntp_each($input, \&_import_net, $lei->{sto}, 0);
170 } elsif ($input =~ s!\A([a-z0-9]+):!!i) {
174 open my $fh, '<', $input or return $lei->child_error(1 << 8, <<"");
175 unable to open $input: $!
177 _import_fh($lei, $fh, $input, $ifmt);
178 } elsif (-d _ && (-d "$input/cur" || -d "$input/new")) {
179 return $lei->fail(<<EOM) if $ifmt && $ifmt ne 'maildir';
180 $input appears to a be a maildir, not $ifmt
182 PublicInbox::MdirReader::maildir_each_file($input,
184 $lei->{sto}, $lei->{opt}->{kw});
186 $lei->fail("$input unsupported (TODO)");
192 my $lei = $self->{lei};
193 _import_fh($lei, delete $self->{0}, '<stdin>', $lei->{opt}->{'in-format'});
196 no warnings 'once'; # the following works even when LeiAuth is lazy-loaded
197 *net_merge_all = \&PublicInbox::LeiAuth::net_merge_all;