]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/LeiImport.pm
lei import: vivify external-only messages
[public-inbox.git] / lib / PublicInbox / LeiImport.pm
1 # Copyright (C) 2021 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3
4 # front-end for the "lei import" sub-command
5 package PublicInbox::LeiImport;
6 use strict;
7 use v5.10.1;
8 use parent qw(PublicInbox::IPC);
9 use PublicInbox::Eml;
10 use PublicInbox::PktOp qw(pkt_do);
11
12 sub _import_eml { # MboxReader callback
13         my ($eml, $lei, $mbox_keywords) = @_;
14         my $vmd;
15         if ($mbox_keywords) {
16                 my $kw = $mbox_keywords->($eml);
17                 $vmd = { kw => $kw } if scalar(@$kw);
18         }
19         my $xoids = $lei->{ale}->xoids_for($eml);
20         $lei->{sto}->ipc_do('set_eml', $eml, $vmd, $xoids);
21 }
22
23 sub import_done_wait { # dwaitpid callback
24         my ($arg, $pid) = @_;
25         my ($imp, $lei) = @$arg;
26         $lei->child_error($?, 'non-fatal errors during import') if $?;
27         my $sto = delete $lei->{sto};
28         my $wait = $sto->ipc_do('done') if $sto; # PublicInbox::LeiStore::done
29         $lei->dclose;
30 }
31
32 sub import_done { # EOF callback for main daemon
33         my ($lei) = @_;
34         my $imp = delete $lei->{imp} or return;
35         $imp->wq_wait_old(\&import_done_wait, $lei);
36 }
37
38 sub net_merge_complete { # callback used by LeiAuth
39         my ($self) = @_;
40         for my $input (@{$self->{inputs}}) {
41                 $self->wq_io_do('import_path_url', [], $input);
42         }
43         $self->wq_close(1);
44 }
45
46 sub import_start {
47         my ($lei) = @_;
48         my $self = $lei->{imp};
49         $lei->ale;
50         my $j = $lei->{opt}->{jobs} // scalar(@{$self->{inputs}}) || 1;
51         if (my $net = $lei->{net}) {
52                 # $j = $net->net_concurrency($j); TODO
53         } else {
54                 my $nproc = $self->detect_nproc;
55                 $j = $nproc if $j > $nproc;
56         }
57         my $ops = { '' => [ \&import_done, $lei ] };
58         $lei->{auth}->op_merge($ops, $self) if $lei->{auth};
59         $self->{-wq_nr_workers} = $j // 1; # locked
60         my $op = $lei->workers_start($self, 'lei_import', undef, $ops);
61         $self->wq_io_do('import_stdin', []) if $self->{0};
62         net_merge_complete($self) unless $lei->{auth};
63         while ($op && $op->{sock}) { $op->event_step }
64 }
65
66 sub call { # the main "lei import" method
67         my ($cls, $lei, @inputs) = @_;
68         my $sto = $lei->_lei_store(1);
69         $sto->write_prepare($lei);
70         my ($net, @f, @d);
71         $lei->{opt}->{kw} //= 1;
72         my $self = $lei->{imp} = bless { inputs => \@inputs }, $cls;
73         if ($lei->{opt}->{stdin}) {
74                 @inputs and return $lei->fail("--stdin and @inputs do not mix");
75                 $lei->check_input_format or return;
76                 $self->{0} = $lei->{0};
77         }
78
79         my $fmt = $lei->{opt}->{'in-format'};
80         # e.g. Maildir:/home/user/Mail/ or imaps://example.com/INBOX
81         for my $input (@inputs) {
82                 my $input_path = $input;
83                 if ($input =~ m!\A(?:imaps?|nntps?|s?news)://!i) {
84                         require PublicInbox::NetReader;
85                         $net //= PublicInbox::NetReader->new;
86                         $net->add_url($input);
87                 } elsif ($input_path =~ s/\A([a-z0-9]+)://is) {
88                         my $ifmt = lc $1;
89                         if (($fmt // $ifmt) ne $ifmt) {
90                                 return $lei->fail(<<"");
91 --in-format=$fmt and `$ifmt:' conflict
92
93                         }
94                         if (-f $input_path) {
95                                 require PublicInbox::MboxLock;
96                                 require PublicInbox::MboxReader;
97                                 PublicInbox::MboxReader->can($ifmt) or return
98                                         $lei->fail("$ifmt not supported");
99                         } elsif (-d _) {
100                                 require PublicInbox::MdirReader;
101                                 $ifmt eq 'maildir' or return
102                                         $lei->fail("$ifmt not supported");
103                         } else {
104                                 return $lei->fail("Unable to handle $input");
105                         }
106                 } elsif (-f $input) { push @f, $input
107                 } elsif (-d _) { push @d, $input
108                 } else { return $lei->fail("Unable to handle $input") }
109         }
110         if (@f) { $lei->check_input_format(\@f) or return }
111         if (@d) { # TODO: check for MH vs Maildir, here
112                 require PublicInbox::MdirReader;
113         }
114         $self->{inputs} = \@inputs;
115         if ($net) {
116                 if (my $err = $net->errors) {
117                         return $lei->fail($err);
118                 }
119                 $net->{quiet} = $lei->{opt}->{quiet};
120                 $lei->{net} = $net;
121                 require PublicInbox::LeiAuth;
122                 $lei->{auth} = PublicInbox::LeiAuth->new;
123         }
124         import_start($lei);
125 }
126
127 sub ipc_atfork_child {
128         my ($self) = @_;
129         my $lei = $self->{lei};
130         delete $lei->{imp}; # drop circular ref
131         $lei->lei_atfork_child;
132         $self->SUPER::ipc_atfork_child;
133         $lei->{auth}->do_auth_atfork($self) if $lei->{auth};
134         undef;
135 }
136
137 sub _import_fh {
138         my ($lei, $fh, $input, $ifmt) = @_;
139         my $kw = $lei->{opt}->{kw} ?
140                 PublicInbox::MboxReader->can('mbox_keywords') : undef;
141         eval {
142                 if ($ifmt eq 'eml') {
143                         my $buf = do { local $/; <$fh> } //
144                                 return $lei->child_error(1 << 8, <<"");
145 error reading $input: $!
146
147                         my $eml = PublicInbox::Eml->new(\$buf);
148                         _import_eml($eml, $lei, $kw);
149                 } else { # some mbox (->can already checked in call);
150                         my $cb = PublicInbox::MboxReader->can($ifmt) //
151                                 die "BUG: bad fmt=$ifmt";
152                         $cb->(undef, $fh, \&_import_eml, $lei, $kw);
153                 }
154         };
155         $lei->child_error(1 << 8, "$input: $@") if $@;
156 }
157
158 sub _import_maildir { # maildir_each_eml cb
159         my ($f, $kw, $eml, $sto, $set_kw) = @_;
160         $sto->ipc_do('set_eml', $eml, $set_kw ? { kw => $kw }: ());
161 }
162
163 sub _import_net { # imap_each, nntp_each cb
164         my ($url, $uid, $kw, $eml, $sto, $set_kw) = @_;
165         $sto->ipc_do('set_eml', $eml, $set_kw ? { kw => $kw } : ());
166 }
167
168 sub import_path_url {
169         my ($self, $input) = @_;
170         my $lei = $self->{lei};
171         my $ifmt = lc($lei->{opt}->{'in-format'} // '');
172         # TODO auto-detect?
173         if ($input =~ m!\Aimaps?://!i) {
174                 $lei->{net}->imap_each($input, \&_import_net, $lei->{sto},
175                                         $lei->{opt}->{kw});
176                 return;
177         } elsif ($input =~ m!\A(?:nntps?|s?news)://!i) {
178                 $lei->{net}->nntp_each($input, \&_import_net, $lei->{sto}, 0);
179                 return;
180         } elsif ($input =~ s!\A([a-z0-9]+):!!i) {
181                 $ifmt = lc $1;
182         }
183         if (-f $input) {
184                 my $m = $lei->{opt}->{'lock'} // ($ifmt eq 'eml' ? ['none'] :
185                                 PublicInbox::MboxLock->defaults);
186                 my $mbl = PublicInbox::MboxLock->acq($input, 0, $m);
187                 _import_fh($lei, $mbl->{fh}, $input, $ifmt);
188         } elsif (-d _ && (-d "$input/cur" || -d "$input/new")) {
189                 return $lei->fail(<<EOM) if $ifmt && $ifmt ne 'maildir';
190 $input appears to a be a maildir, not $ifmt
191 EOM
192                 PublicInbox::MdirReader::maildir_each_eml($input,
193                                         \&_import_maildir,
194                                         $lei->{sto}, $lei->{opt}->{kw});
195         } else {
196                 $lei->fail("$input unsupported (TODO)");
197         }
198 }
199
200 sub import_stdin {
201         my ($self) = @_;
202         my $lei = $self->{lei};
203         my $in = delete $self->{0};
204         _import_fh($lei, $in, '<stdin>', $lei->{opt}->{'in-format'});
205 }
206
207 no warnings 'once'; # the following works even when LeiAuth is lazy-loaded
208 *net_merge_all = \&PublicInbox::LeiAuth::net_merge_all;
209 1;