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::InboxWritable qw(eml_from_path);
11 use PublicInbox::PktOp;
13 sub _import_eml { # MboxReader callback
14 my ($eml, $sto, $set_kw) = @_;
15 $sto->ipc_do('set_eml', $eml, $set_kw ? $sto->mbox_keywords($eml) : ());
18 sub import_done_wait { # dwaitpid callback
20 my ($imp, $lei) = @$arg;
21 $lei->child_error($?, 'non-fatal errors during import') if $?;
22 my $ign = $lei->{sto}->ipc_do('done'); # PublicInbox::LeiStore::done
26 sub import_done { # EOF callback for main daemon
28 my $imp = delete $lei->{imp} or return;
29 $imp->wq_wait_old(\&import_done_wait, $lei);
35 '!' => [ $lei->can('fail_handler'), $lei ],
36 'x_it' => [ $lei->can('x_it'), $lei ],
37 'child_error' => [ $lei->can('child_error'), $lei ],
38 '' => [ \&import_done, $lei ],
40 ($lei->{pkt_op_c}, $lei->{pkt_op_p}) = PublicInbox::PktOp->pair($ops);
41 my $self = $lei->{imp};
42 my $j = $lei->{opt}->{jobs} // scalar(@{$self->{inputs}}) || 1;
43 if (my $nrd = $lei->{nrd}) {
44 # $j = $nrd->net_concurrency($j); TODO
46 my $nproc = $self->detect_nproc;
47 $j = $nproc if $j > $nproc;
49 $self->wq_workers_start('lei_import', $j, $lei->oldset, {lei => $lei});
50 my $op = delete $lei->{pkt_op_c};
51 delete $lei->{pkt_op_p};
52 $self->wq_io_do('import_stdin', []) if $self->{0};
53 for my $input (@{$self->{inputs}}) {
54 $self->wq_io_do('import_path_url', [], $input);
57 $lei->event_step_init; # wait for shutdowns
58 if ($lei->{oneshot}) {
59 while ($op->{sock}) { $op->event_step }
63 sub call { # the main "lei import" method
64 my ($cls, $lei, @inputs) = @_;
65 my $sto = $lei->_lei_store(1);
66 $sto->write_prepare($lei);
68 $lei->{opt}->{kw} //= 1;
69 my $self = $lei->{imp} = bless { inputs => \@inputs }, $cls;
70 if ($lei->{opt}->{stdin}) {
71 @inputs and return $lei->fail("--stdin and @inputs do not mix");
72 $lei->check_input_format or return;
73 $self->{0} = $lei->{0};
76 # TODO: do we need --format for non-stdin?
77 my $fmt = $lei->{opt}->{'format'};
78 # e.g. Maildir:/home/user/Mail/ or imaps://example.com/INBOX
79 for my $input (@inputs) {
80 my $input_path = $input;
81 if ($input =~ m!\A(?:imap|nntp)s?://!i) {
82 require PublicInbox::NetReader;
83 $nrd //= PublicInbox::NetReader->new;
84 $nrd->add_url($input);
85 } elsif ($input_path =~ s/\A([a-z0-9]+)://is) {
87 if (($fmt // $ifmt) ne $ifmt) {
88 return $lei->fail(<<"");
89 --format=$fmt and `$ifmt:' conflict
93 require PublicInbox::MboxReader;
94 PublicInbox::MboxReader->can($ifmt) or return
95 $lei->fail("$ifmt not supported");
97 require PublicInbox::MdirReader;
98 $ifmt eq 'maildir' or return
99 $lei->fail("$ifmt not supported");
101 return $lei->fail("Unable to handle $input");
103 } elsif (-f $input) { push @f, $input
104 } elsif (-d _) { push @d, $input
105 } else { return $lei->fail("Unable to handle $input") }
107 if (@f) { $lei->check_input_format(\@f) or return }
108 if (@d) { # TODO: check for MH vs Maildir, here
109 require PublicInbox::MdirReader;
111 $self->{inputs} = \@inputs;
112 return import_start($lei) if !$nrd;
114 if (my $err = $nrd->errors) {
115 return $lei->fail($err);
117 $nrd->{quiet} = $lei->{opt}->{quiet};
119 require PublicInbox::LeiAuth;
120 my $auth = $lei->{auth} = PublicInbox::LeiAuth->new($nrd);
121 $auth->auth_start($lei, \&import_start, $lei);
124 sub ipc_atfork_child {
126 delete $self->{lei}->{imp}; # drop circular ref
127 $self->{lei}->lei_atfork_child;
128 $self->SUPER::ipc_atfork_child;
132 my ($lei, $fh, $input, $ifmt) = @_;
133 my $set_kw = $lei->{opt}->{kw};
135 if ($ifmt eq 'eml') {
136 my $buf = do { local $/; <$fh> } //
137 return $lei->child_error(1 << 8, <<"");
138 error reading $input: $!
140 my $eml = PublicInbox::Eml->new(\$buf);
141 _import_eml($eml, $lei->{sto}, $set_kw);
142 } else { # some mbox (->can already checked in call);
143 my $cb = PublicInbox::MboxReader->can($ifmt) //
144 die "BUG: bad fmt=$ifmt";
145 $cb->(undef, $fh, \&_import_eml, $lei->{sto}, $set_kw);
148 $lei->child_error(1 << 8, "<stdin>: $@") if $@;
151 sub _import_maildir { # maildir_each_file cb
152 my ($f, $sto, $set_kw) = @_;
153 $sto->ipc_do('set_eml_from_maildir', $f, $set_kw);
156 sub _import_imap { # imap_each cb
157 my ($url, $uid, $kw, $eml, $sto, $set_kw) = @_;
159 $sto->ipc_do('set_eml', $eml, $set_kw ? @$kw : ());
162 sub import_path_url {
163 my ($self, $input) = @_;
164 my $lei = $self->{lei};
165 my $ifmt = lc($lei->{opt}->{'format'} // '');
167 if ($input =~ m!\A(imap|nntp)s?://!i) {
168 $lei->{nrd}->imap_each($input, \&_import_imap, $lei->{sto},
171 } elsif ($input =~ s!\A([a-z0-9]+):!!i) {
175 open my $fh, '<', $input or return $lei->child_error(1 << 8, <<"");
176 unable to open $input: $!
178 _import_fh($lei, $fh, $input, $ifmt);
179 } elsif (-d _ && (-d "$input/cur" || -d "$input/new")) {
180 return $lei->fail(<<EOM) if $ifmt && $ifmt ne 'maildir';
181 $input appears to a be a maildir, not $ifmt
183 PublicInbox::MdirReader::maildir_each_file($input,
185 $lei->{sto}, $lei->{opt}->{kw});
187 $lei->fail("$input unsupported (TODO)");
193 my $lei = $self->{lei};
194 _import_fh($lei, delete $self->{0}, '<stdin>', $lei->{opt}->{'format'});