]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/LeiImport.pm
lei_auth: migrate common auth code from lei_import
[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::InboxWritable qw(eml_from_path);
11 use PublicInbox::PktOp qw(pkt_do);
12
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) : ());
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 $ign = $lei->{sto}->ipc_do('done'); # PublicInbox::LeiStore::done
23         $lei->dclose;
24 }
25
26 sub import_done { # EOF callback for main daemon
27         my ($lei) = @_;
28         my $imp = delete $lei->{imp} or return;
29         $imp->wq_wait_old(\&import_done_wait, $lei);
30 }
31
32 sub net_merge_complete { # callback used by LeiAuth
33         my ($self) = @_;
34         for my $input (@{$self->{inputs}}) {
35                 $self->wq_io_do('import_path_url', [], $input);
36         }
37         $self->wq_close(1);
38 }
39
40 sub import_start {
41         my ($lei) = @_;
42         my $self = $lei->{imp};
43         my $j = $lei->{opt}->{jobs} // scalar(@{$self->{inputs}}) || 1;
44         if (my $net = $lei->{net}) {
45                 # $j = $net->net_concurrency($j); TODO
46         } else {
47                 my $nproc = $self->detect_nproc;
48                 $j = $nproc if $j > $nproc;
49         }
50         my $ops = { '' => [ \&import_done, $lei ] };
51         $lei->{auth}->op_merge($ops, $self) if $lei->{auth};
52         $self->{-wq_nr_workers} = $j // 1; # locked
53         my $op = $lei->workers_start($self, 'lei_import', undef, $ops);
54         $self->wq_io_do('import_stdin', []) if $self->{0};
55         net_merge_complete($self) unless $lei->{auth};
56         while ($op && $op->{sock}) { $op->event_step }
57 }
58
59 sub call { # the main "lei import" method
60         my ($cls, $lei, @inputs) = @_;
61         my $sto = $lei->_lei_store(1);
62         $sto->write_prepare($lei);
63         my ($net, @f, @d);
64         $lei->{opt}->{kw} //= 1;
65         my $self = $lei->{imp} = bless { inputs => \@inputs }, $cls;
66         if ($lei->{opt}->{stdin}) {
67                 @inputs and return $lei->fail("--stdin and @inputs do not mix");
68                 $lei->check_input_format or return;
69                 $self->{0} = $lei->{0};
70         }
71
72         # TODO: do we need --format for non-stdin?
73         my $fmt = $lei->{opt}->{'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(?:imap|nntp)s?://!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 --format=$fmt and `$ifmt:' conflict
86
87                         }
88                         if (-f $input_path) {
89                                 require PublicInbox::MboxReader;
90                                 PublicInbox::MboxReader->can($ifmt) or return
91                                         $lei->fail("$ifmt not supported");
92                         } elsif (-d _) {
93                                 require PublicInbox::MdirReader;
94                                 $ifmt eq 'maildir' or return
95                                         $lei->fail("$ifmt not supported");
96                         } else {
97                                 return $lei->fail("Unable to handle $input");
98                         }
99                 } elsif (-f $input) { push @f, $input
100                 } elsif (-d _) { push @d, $input
101                 } else { return $lei->fail("Unable to handle $input") }
102         }
103         if (@f) { $lei->check_input_format(\@f) or return }
104         if (@d) { # TODO: check for MH vs Maildir, here
105                 require PublicInbox::MdirReader;
106         }
107         $self->{inputs} = \@inputs;
108         if ($net) {
109                 if (my $err = $net->errors) {
110                         return $lei->fail($err);
111                 }
112                 $net->{quiet} = $lei->{opt}->{quiet};
113                 $lei->{net} = $net;
114                 require PublicInbox::LeiAuth;
115                 $lei->{auth} = PublicInbox::LeiAuth->new($net);
116         }
117         import_start($lei);
118 }
119
120 sub ipc_atfork_child {
121         my ($self) = @_;
122         my $lei = $self->{lei};
123         delete $lei->{imp}; # drop circular ref
124         $lei->lei_atfork_child;
125         $self->SUPER::ipc_atfork_child;
126         $lei->{auth}->do_auth_atfork($self) if $lei->{auth};
127         undef;
128 }
129
130 sub _import_fh {
131         my ($lei, $fh, $input, $ifmt) = @_;
132         my $set_kw = $lei->{opt}->{kw};
133         eval {
134                 if ($ifmt eq 'eml') {
135                         my $buf = do { local $/; <$fh> } //
136                                 return $lei->child_error(1 << 8, <<"");
137 error reading $input: $!
138
139                         my $eml = PublicInbox::Eml->new(\$buf);
140                         _import_eml($eml, $lei->{sto}, $set_kw);
141                 } else { # some mbox (->can already checked in call);
142                         my $cb = PublicInbox::MboxReader->can($ifmt) //
143                                 die "BUG: bad fmt=$ifmt";
144                         $cb->(undef, $fh, \&_import_eml, $lei->{sto}, $set_kw);
145                 }
146         };
147         $lei->child_error(1 << 8, "<stdin>: $@") if $@;
148 }
149
150 sub _import_maildir { # maildir_each_file cb
151         my ($f, $sto, $set_kw) = @_;
152         $sto->ipc_do('set_eml_from_maildir', $f, $set_kw);
153 }
154
155 sub _import_imap { # imap_each cb
156         my ($url, $uid, $kw, $eml, $sto, $set_kw) = @_;
157         warn "$url $uid";
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}->{'format'} // '');
165         # TODO auto-detect?
166         if ($input =~ m!\A(imap|nntp)s?://!i) {
167                 $lei->{net}->imap_each($input, \&_import_imap, $lei->{sto},
168                                         $lei->{opt}->{kw});
169                 return;
170         } elsif ($input =~ s!\A([a-z0-9]+):!!i) {
171                 $ifmt = lc $1;
172         }
173         if (-f $input) {
174                 open my $fh, '<', $input or return $lei->child_error(1 << 8, <<"");
175 unable to open $input: $!
176
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
181 EOM
182                 PublicInbox::MdirReader::maildir_each_file($input,
183                                         \&_import_maildir,
184                                         $lei->{sto}, $lei->{opt}->{kw});
185         } else {
186                 $lei->fail("$input unsupported (TODO)");
187         }
188 }
189
190 sub import_stdin {
191         my ($self) = @_;
192         my $lei = $self->{lei};
193         _import_fh($lei, delete $self->{0}, '<stdin>', $lei->{opt}->{'format'});
194 }
195
196 no warnings 'once'; # the following works even when LeiAuth is lazy-loaded
197 *net_merge_all = \&PublicInbox::LeiAuth::net_merge_all;
198 1;