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