]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/LeiInput.pm
0114f5ee9246e2ad06e160624041bf05340b2f70
[public-inbox.git] / lib / PublicInbox / LeiInput.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 # parent class for LeiImport, LeiConvert
5 package PublicInbox::LeiInput;
6 use strict;
7 use v5.10.1;
8 use PublicInbox::DS;
9
10 # JMAP RFC 8621 4.1.1
11 # https://www.iana.org/assignments/imap-jmap-keywords/imap-jmap-keywords.xhtml
12 our @KW = (qw(seen answered flagged draft), # widely-compatible
13         qw(forwarded), # IMAP + Maildir
14         qw(phishing junk notjunk)); # rarely supported
15
16 # note: RFC 8621 states "Users may add arbitrary keywords to an Email",
17 # but is it good idea?  Stick to the system and reserved ones, for now.
18 # The widely-compatible ones map to IMAP system flags, Maildir flags
19 # and mbox Status/X-Status headers.
20 my %KW = map { $_ => 1 } @KW;
21 my $L_MAX = 244; # Xapian term limit - length('L')
22
23 # RFC 8621, sec 2 (Mailboxes) a "label" for us is a JMAP Mailbox "name"
24 # "Servers MAY reject names that violate server policy"
25 my %ERR = (
26         L => sub {
27                 my ($label) = @_;
28                 length($label) >= $L_MAX and
29                         return "`$label' too long (must be <= $L_MAX)";
30                 $label =~ m{\A[a-z0-9_](?:[a-z0-9_\-\./\@,]*[a-z0-9])?\z}i ?
31                         undef : "`$label' is invalid";
32         },
33         kw => sub {
34                 my ($kw) = @_;
35                 $KW{$kw} ? undef : <<EOM;
36 `$kw' is not one of: `seen', `flagged', `answered', `draft'
37 `junk', `notjunk', `phishing' or `forwarded'
38 EOM
39         }
40 );
41
42 sub check_input_format ($;$) {
43         my ($lei, $files) = @_;
44         my $opt_key = 'in-format';
45         my $fmt = $lei->{opt}->{$opt_key};
46         if (!$fmt) {
47                 my $err = $files ? "regular file(s):\n@$files" : '--stdin';
48                 return $lei->fail("--$opt_key unset for $err");
49         }
50         return 1 if $fmt eq 'eml';
51         require PublicInbox::MboxLock if $files;
52         require PublicInbox::MboxReader;
53         PublicInbox::MboxReader->reads($fmt) or
54                 return $lei->fail("--$opt_key=$fmt unrecognized");
55         1;
56 }
57
58 # import a single file handle of $name
59 # Subclass must define ->input_eml_cb and ->input_mbox_cb
60 sub input_fh {
61         my ($self, $ifmt, $fh, $name, @args) = @_;
62         if ($ifmt eq 'eml') {
63                 my $buf = do { local $/; <$fh> } //
64                         return $self->{lei}->child_error(1 << 8, <<"");
65 error reading $name: $!
66
67                 # mutt pipes single RFC822 messages with a "From " line,
68                 # but no Content-Length or "From " escaping.
69                 # "git format-patch" also generates such files by default.
70                 $buf =~ s/\A[\r\n]*From [^\r\n]*\r?\n//s;
71                 $self->input_eml_cb(PublicInbox::Eml->new(\$buf), @args);
72         } else {
73                 # prepare_inputs already validated $ifmt
74                 my $cb = PublicInbox::MboxReader->reads($ifmt) //
75                                 die "BUG: bad fmt=$ifmt";
76                 $cb->(undef, $fh, $self->can('input_mbox_cb'), $self, @args);
77         }
78 }
79
80 sub input_path_url {
81         my ($self, $input, @args) = @_;
82         my $lei = $self->{lei};
83         my $ifmt = lc($lei->{opt}->{'in-format'} // '');
84         # TODO auto-detect?
85         if ($input =~ m!\Aimaps?://!i) {
86                 $lei->{net}->imap_each($input, $self->can('input_net_cb'),
87                                         $self, @args);
88                 return;
89         } elsif ($input =~ m!\A(?:nntps?|s?news)://!i) {
90                 $lei->{net}->nntp_each($input, $self->can('input_net_cb'),
91                                         $self, @args);
92                 return;
93         }
94         if ($input =~ s!\A([a-z0-9]+):!!i) {
95                 $ifmt = lc($1);
96         } elsif ($input =~ /\.(?:patch|eml)\z/i) {
97                 $ifmt = 'eml';
98         }
99         my $devfd = $lei->path_to_fd($input) // return;
100         if ($devfd >= 0) {
101                 $self->input_fh($ifmt, $lei->{$devfd}, $input, @args);
102         } elsif (-f $input && $ifmt eq 'eml') {
103                 open my $fh, '<', $input or
104                                         return $lei->fail("open($input): $!");
105                 $self->input_fh($ifmt, $fh, $input, @args);
106         } elsif (-f _) {
107                 my $m = $lei->{opt}->{'lock'} //
108                         PublicInbox::MboxLock->defaults;
109                 my $mbl = PublicInbox::MboxLock->acq($input, 0, $m);
110                 my $zsfx = PublicInbox::MboxReader::zsfx($input);
111                 if ($zsfx) {
112                         my $in = delete $mbl->{fh};
113                         $mbl->{fh} =
114                              PublicInbox::MboxReader::zsfxcat($in, $zsfx, $lei);
115                 }
116                 local $PublicInbox::DS::in_loop = 0 if $zsfx; # dwaitpid
117                 $self->input_fh($ifmt, $mbl->{fh}, $input, @args);
118         } elsif (-d _ && (-d "$input/cur" || -d "$input/new")) {
119                 return $lei->fail(<<EOM) if $ifmt && $ifmt ne 'maildir';
120 $input appears to a be a maildir, not $ifmt
121 EOM
122                 PublicInbox::MdirReader->new->maildir_each_eml($input,
123                                         $self->can('input_maildir_cb'),
124                                         $self, @args);
125         } else {
126                 $lei->fail("$input unsupported (TODO)");
127         }
128 }
129
130 sub prepare_inputs { # returns undef on error
131         my ($self, $lei, $inputs) = @_;
132         my $in_fmt = $lei->{opt}->{'in-format'};
133         if ($lei->{opt}->{stdin}) {
134                 @$inputs and return
135                         $lei->fail("--stdin and @$inputs do not mix");
136                 check_input_format($lei) or return;
137                 push @$inputs, '/dev/stdin';
138         }
139         my $net = $lei->{net}; # NetWriter may be created by l2m
140         my (@f, @d);
141         # e.g. Maildir:/home/user/Mail/ or imaps://example.com/INBOX
142         for my $input (@$inputs) {
143                 my $input_path = $input;
144                 if ($input =~ m!\A(?:imaps?|nntps?|s?news)://!i) {
145                         require PublicInbox::NetReader;
146                         $net //= PublicInbox::NetReader->new;
147                         $net->add_url($input);
148                 } elsif ($input_path =~ s/\A([a-z0-9]+)://is) {
149                         my $ifmt = lc $1;
150                         if (($in_fmt // $ifmt) ne $ifmt) {
151                                 return $lei->fail(<<"");
152 --in-format=$in_fmt and `$ifmt:' conflict
153
154                         }
155                         my $devfd = $lei->path_to_fd($input_path) // return;
156                         if ($devfd >= 0 || (-f $input_path || -p _)) {
157                                 require PublicInbox::MboxLock;
158                                 require PublicInbox::MboxReader;
159                                 PublicInbox::MboxReader->reads($ifmt) or return
160                                         $lei->fail("$ifmt not supported");
161                         } elsif (-d $input_path) {
162                                 require PublicInbox::MdirReader;
163                                 $ifmt eq 'maildir' or return
164                                         $lei->fail("$ifmt not supported");
165                         } else {
166                                 return $lei->fail("Unable to handle $input");
167                         }
168                 } elsif ($input =~ /\.(eml|patch)\z/i && -f $input) {
169                         lc($in_fmt//'eml') eq 'eml' or return $lei->fail(<<"");
170 $input is `eml', not --in-format=$in_fmt
171
172                         require PublicInbox::Eml;
173                 } else {
174                         my $devfd = $lei->path_to_fd($input) // return;
175                         if ($devfd >= 0 || -f $input || -p _) {
176                                 push @f, $input
177                         } elsif (-d $input) {
178                                 push @d, $input
179                         } else {
180                                 return $lei->fail("Unable to handle $input")
181                         }
182                 }
183         }
184         if (@f) { check_input_format($lei, \@f) or return }
185         if (@d) { # TODO: check for MH vs Maildir, here
186                 require PublicInbox::MdirReader;
187         }
188         if ($net) {
189                 if (my $err = $net->errors) {
190                         return $lei->fail($err);
191                 }
192                 $net->{quiet} = $lei->{opt}->{quiet};
193                 require PublicInbox::LeiAuth;
194                 $lei->{auth} //= PublicInbox::LeiAuth->new;
195                 $lei->{net} //= $net;
196         }
197         $self->{inputs} = $inputs;
198 }
199
200 sub process_inputs {
201         my ($self) = @_;
202         for my $input (@{$self->{inputs}}) {
203                 $self->input_path_url($input);
204         }
205         my $wait = $self->{lei}->{sto}->ipc_do('done') if $self->{lei}->{sto};
206 }
207
208 sub input_only_atfork_child {
209         my ($self) = @_;
210         my $lei = $self->{lei};
211         $lei->_lei_atfork_child;
212         PublicInbox::IPC::ipc_atfork_child($self);
213         $lei->{auth}->do_auth_atfork($self) if $lei->{auth};
214         undef;
215 }
216
217 # like Getopt::Long, but for +kw:FOO and -kw:FOO to prepare
218 # for update_xvmd -> update_vmd
219 sub vmd_mod_extract {
220         my $argv = $_[-1];
221         my $vmd_mod = {};
222         my @new_argv;
223         for my $x (@$argv) {
224                 if ($x =~ /\A(\+|\-)(kw|L):(.+)\z/) {
225                         my ($op, $pfx, $val) = ($1, $2, $3);
226                         if (my $err = $ERR{$pfx}->($val)) {
227                                 push @{$vmd_mod->{err}}, $err;
228                         } else { # set "+kw", "+L", "-L", "-kw"
229                                 push @{$vmd_mod->{$op.$pfx}}, $val;
230                         }
231                 } else {
232                         push @new_argv, $x;
233                 }
234         }
235         @$argv = @new_argv;
236         $vmd_mod;
237 }
238
239 1;