]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/LeiImport.pm
lei import: add IMAP and (maildir|mbox*):$PATHNAME support
[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;
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 import_start {
33         my ($lei) = @_;
34         my $ops = {
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 ],
39         };
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
45         } else {
46                 my $nproc = $self->detect_nproc;
47                 $j = $nproc if $j > $nproc;
48         }
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);
55         }
56         $self->wq_close(1);
57         $lei->event_step_init; # wait for shutdowns
58         if ($lei->{oneshot}) {
59                 while ($op->{sock}) { $op->event_step }
60         }
61 }
62
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);
67         my ($nrd, @f, @d);
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};
74         }
75
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) {
86                         my $ifmt = lc $1;
87                         if (($fmt // $ifmt) ne $ifmt) {
88                                 return $lei->fail(<<"");
89 --format=$fmt and `$ifmt:' conflict
90
91                         }
92                         if (-f $input_path) {
93                                 require PublicInbox::MboxReader;
94                                 PublicInbox::MboxReader->can($ifmt) or return
95                                         $lei->fail("$ifmt not supported");
96                         } elsif (-d _) {
97                                 require PublicInbox::MdirReader;
98                                 $ifmt eq 'maildir' or return
99                                         $lei->fail("$ifmt not supported");
100                         } else {
101                                 return $lei->fail("Unable to handle $input");
102                         }
103                 } elsif (-f $input) { push @f, $input
104                 } elsif (-d _) { push @d, $input
105                 } else { return $lei->fail("Unable to handle $input") }
106         }
107         if (@f) { $lei->check_input_format(\@f) or return }
108         if (@d) { # TODO: check for MH vs Maildir, here
109                 require PublicInbox::MdirReader;
110         }
111         $self->{inputs} = \@inputs;
112         return import_start($lei) if !$nrd;
113
114         if (my $err = $nrd->errors) {
115                 return $lei->fail($err);
116         }
117         $nrd->{quiet} = $lei->{opt}->{quiet};
118         $lei->{nrd} = $nrd;
119         require PublicInbox::LeiAuth;
120         my $auth = $lei->{auth} = PublicInbox::LeiAuth->new($nrd);
121         $auth->auth_start($lei, \&import_start, $lei);
122 }
123
124 sub ipc_atfork_child {
125         my ($self) = @_;
126         delete $self->{lei}->{imp}; # drop circular ref
127         $self->{lei}->lei_atfork_child;
128         $self->SUPER::ipc_atfork_child;
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, "<stdin>: $@") if $@;
149 }
150
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);
154 }
155
156 sub _import_imap { # imap_each cb
157         my ($url, $uid, $kw, $eml, $sto, $set_kw) = @_;
158         warn "$url $uid";
159         $sto->ipc_do('set_eml', $eml, $set_kw ? @$kw : ());
160 }
161
162 sub import_path_url {
163         my ($self, $input) = @_;
164         my $lei = $self->{lei};
165         my $ifmt = lc($lei->{opt}->{'format'} // '');
166         # TODO auto-detect?
167         if ($input =~ m!\A(imap|nntp)s?://!i) {
168                 $lei->{nrd}->imap_each($input, \&_import_imap, $lei->{sto},
169                                         $lei->{opt}->{kw});
170                 return;
171         } elsif ($input =~ s!\A([a-z0-9]+):!!i) {
172                 $ifmt = lc $1;
173         }
174         if (-f $input) {
175                 open my $fh, '<', $input or return $lei->child_error(1 << 8, <<"");
176 unable to open $input: $!
177
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
182 EOM
183                 PublicInbox::MdirReader::maildir_each_file($input,
184                                         \&_import_maildir,
185                                         $lei->{sto}, $lei->{opt}->{kw});
186         } else {
187                 $lei->fail("$input unsupported (TODO)");
188         }
189 }
190
191 sub import_stdin {
192         my ($self) = @_;
193         my $lei = $self->{lei};
194         _import_fh($lei, delete $self->{0}, '<stdin>', $lei->{opt}->{'format'});
195 }
196
197 1;