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