]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/LeiInput.pm
lei import: support adding keywords and labels on import
[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         # XXX: should this handle {gz,bz2,xz}? that's currently in LeiToMail
54         PublicInbox::MboxReader->reads($fmt) or
55                 return $lei->fail("--$opt_key=$fmt unrecognized");
56         1;
57 }
58
59 # import a single file handle of $name
60 # Subclass must define ->input_eml_cb and ->input_mbox_cb
61 sub input_fh {
62         my ($self, $ifmt, $fh, $name, @args) = @_;
63         if ($ifmt eq 'eml') {
64                 my $buf = do { local $/; <$fh> } //
65                         return $self->{lei}->child_error(1 << 8, <<"");
66 error reading $name: $!
67
68                 # mutt pipes single RFC822 messages with a "From " line,
69                 # but no Content-Length or "From " escaping.
70                 # "git format-patch" also generates such files by default.
71                 $buf =~ s/\A[\r\n]*From [^\r\n]*\r?\n//s;
72                 $self->input_eml_cb(PublicInbox::Eml->new(\$buf), @args);
73         } else {
74                 # prepare_inputs already validated $ifmt
75                 my $cb = PublicInbox::MboxReader->reads($ifmt) //
76                                 die "BUG: bad fmt=$ifmt";
77                 $cb->(undef, $fh, $self->can('input_mbox_cb'), $self, @args);
78         }
79 }
80
81 sub input_path_url {
82         my ($self, $input, @args) = @_;
83         my $lei = $self->{lei};
84         my $ifmt = lc($lei->{opt}->{'in-format'} // '');
85         # TODO auto-detect?
86         if ($input =~ m!\Aimaps?://!i) {
87                 $lei->{net}->imap_each($input, $self->can('input_net_cb'),
88                                         $self, @args);
89                 return;
90         } elsif ($input =~ m!\A(?:nntps?|s?news)://!i) {
91                 $lei->{net}->nntp_each($input, $self->can('input_net_cb'),
92                                         $self, @args);
93                 return;
94         }
95         if ($input =~ s!\A([a-z0-9]+):!!i) {
96                 $ifmt = lc($1);
97         } elsif ($input =~ /\.(?:patch|eml)\z/i) {
98                 $ifmt = 'eml';
99         }
100         my $devfd = $lei->path_to_fd($input) // return;
101         if ($devfd >= 0) {
102                 $self->input_fh($ifmt, $lei->{$devfd}, $input, @args);
103         } elsif (-f $input && $ifmt eq 'eml') {
104                 open my $fh, '<', $input or
105                                         return $lei->fail("open($input): $!");
106                 $self->input_fh($ifmt, $fh, $input, @args);
107         } elsif (-f _) {
108                 my $m = $lei->{opt}->{'lock'} //
109                         PublicInbox::MboxLock->defaults;
110                 my $mbl = PublicInbox::MboxLock->acq($input, 0, $m);
111                 my $zsfx = PublicInbox::MboxReader::zsfx($input);
112                 if ($zsfx) {
113                         my $in = delete $mbl->{fh};
114                         $mbl->{fh} =
115                              PublicInbox::MboxReader::zsfxcat($in, $zsfx, $lei);
116                 }
117                 local $PublicInbox::DS::in_loop = 0 if $zsfx; # dwaitpid
118                 $self->input_fh($ifmt, $mbl->{fh}, $input, @args);
119         } elsif (-d _ && (-d "$input/cur" || -d "$input/new")) {
120                 return $lei->fail(<<EOM) if $ifmt && $ifmt ne 'maildir';
121 $input appears to a be a maildir, not $ifmt
122 EOM
123                 PublicInbox::MdirReader->new->maildir_each_eml($input,
124                                         $self->can('input_maildir_cb'),
125                                         $self, @args);
126         } else {
127                 $lei->fail("$input unsupported (TODO)");
128         }
129 }
130
131 sub prepare_inputs { # returns undef on error
132         my ($self, $lei, $inputs) = @_;
133         my $in_fmt = $lei->{opt}->{'in-format'};
134         if ($lei->{opt}->{stdin}) {
135                 @$inputs and return
136                         $lei->fail("--stdin and @$inputs do not mix");
137                 check_input_format($lei) or return;
138                 push @$inputs, '/dev/stdin';
139         }
140         my $net = $lei->{net}; # NetWriter may be created by l2m
141         my (@f, @d);
142         # e.g. Maildir:/home/user/Mail/ or imaps://example.com/INBOX
143         for my $input (@$inputs) {
144                 my $input_path = $input;
145                 if ($input =~ m!\A(?:imaps?|nntps?|s?news)://!i) {
146                         require PublicInbox::NetReader;
147                         $net //= PublicInbox::NetReader->new;
148                         $net->add_url($input);
149                 } elsif ($input_path =~ s/\A([a-z0-9]+)://is) {
150                         my $ifmt = lc $1;
151                         if (($in_fmt // $ifmt) ne $ifmt) {
152                                 return $lei->fail(<<"");
153 --in-format=$in_fmt and `$ifmt:' conflict
154
155                         }
156                         my $devfd = $lei->path_to_fd($input_path) // return;
157                         if ($devfd >= 0 || (-f $input_path || -p _)) {
158                                 require PublicInbox::MboxLock;
159                                 require PublicInbox::MboxReader;
160                                 PublicInbox::MboxReader->reads($ifmt) or return
161                                         $lei->fail("$ifmt not supported");
162                         } elsif (-d $input_path) {
163                                 require PublicInbox::MdirReader;
164                                 $ifmt eq 'maildir' or return
165                                         $lei->fail("$ifmt not supported");
166                         } else {
167                                 return $lei->fail("Unable to handle $input");
168                         }
169                 } elsif ($input =~ /\.(eml|patch)\z/i && -f $input) {
170                         lc($in_fmt//'eml') eq 'eml' or return $lei->fail(<<"");
171 $input is `eml', not --in-format=$in_fmt
172
173                         require PublicInbox::Eml;
174                 } else {
175                         my $devfd = $lei->path_to_fd($input) // return;
176                         if ($devfd >= 0 || -f $input || -p _) {
177                                 push @f, $input
178                         } elsif (-d $input) {
179                                 push @d, $input
180                         } else {
181                                 return $lei->fail("Unable to handle $input")
182                         }
183                 }
184         }
185         if (@f) { check_input_format($lei, \@f) or return }
186         if (@d) { # TODO: check for MH vs Maildir, here
187                 require PublicInbox::MdirReader;
188         }
189         if ($net) {
190                 if (my $err = $net->errors) {
191                         return $lei->fail($err);
192                 }
193                 $net->{quiet} = $lei->{opt}->{quiet};
194                 require PublicInbox::LeiAuth;
195                 $lei->{auth} //= PublicInbox::LeiAuth->new;
196                 $lei->{net} //= $net;
197         }
198         $self->{inputs} = $inputs;
199 }
200
201 sub process_inputs {
202         my ($self) = @_;
203         for my $input (@{$self->{inputs}}) {
204                 $self->input_path_url($input);
205         }
206         my $wait = $self->{lei}->{sto}->ipc_do('done') if $self->{lei}->{sto};
207 }
208
209 sub input_only_atfork_child {
210         my ($self) = @_;
211         my $lei = $self->{lei};
212         $lei->_lei_atfork_child;
213         PublicInbox::IPC::ipc_atfork_child($self);
214         $lei->{auth}->do_auth_atfork($self) if $lei->{auth};
215         undef;
216 }
217
218 # like Getopt::Long, but for +kw:FOO and -kw:FOO to prepare
219 # for update_xvmd -> update_vmd
220 sub vmd_mod_extract {
221         my $argv = $_[-1];
222         my $vmd_mod = {};
223         my @new_argv;
224         for my $x (@$argv) {
225                 if ($x =~ /\A(\+|\-)(kw|L):(.+)\z/) {
226                         my ($op, $pfx, $val) = ($1, $2, $3);
227                         if (my $err = $ERR{$pfx}->($val)) {
228                                 push @{$vmd_mod->{err}}, $err;
229                         } else { # set "+kw", "+L", "-L", "-kw"
230                                 push @{$vmd_mod->{$op.$pfx}}, $val;
231                         }
232                 } else {
233                         push @new_argv, $x;
234                 }
235         }
236         @$argv = @new_argv;
237         $vmd_mod;
238 }
239
240 1;