1 # Copyright (C) 2021 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 # parent class for LeiImport, LeiConvert
5 package PublicInbox::LeiInput;
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
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')
23 # RFC 8621, sec 2 (Mailboxes) a "label" for us is a JMAP Mailbox "name"
24 # "Servers MAY reject names that violate server policy"
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";
35 $KW{$kw} ? undef : <<EOM;
36 `$kw' is not one of: `seen', `flagged', `answered', `draft'
37 `junk', `notjunk', `phishing' or `forwarded'
42 sub check_input_format ($;$) {
43 my ($lei, $files) = @_;
44 my $opt_key = 'in-format';
45 my $fmt = $lei->{opt}->{$opt_key};
47 my $err = $files ? "regular file(s):\n@$files" : '--stdin';
48 return $lei->fail("--$opt_key unset for $err");
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");
58 # import a single file handle of $name
59 # Subclass must define ->input_eml_cb and ->input_mbox_cb
61 my ($self, $ifmt, $fh, $name, @args) = @_;
63 my $buf = do { local $/; <$fh> } //
64 return $self->{lei}->child_error(1 << 8, <<"");
65 error reading $name: $!
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);
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);
81 my ($self, $input, @args) = @_;
82 my $lei = $self->{lei};
83 my $ifmt = lc($lei->{opt}->{'in-format'} // '');
85 if ($input =~ m!\Aimaps?://!i) {
86 $lei->{net}->imap_each($input, $self->can('input_imap_cb') //
87 $self->can('input_net_cb'),
90 } elsif ($input =~ m!\A(?:nntps?|s?news)://!i) {
91 $lei->{net}->nntp_each($input, $self->can('input_nntp_cb') //
92 $self->can('input_net_cb'),
96 if ($input =~ s!\A([a-z0-9]+):!!i) {
98 } elsif ($input =~ /\.(?:patch|eml)\z/i) {
101 my $devfd = $lei->path_to_fd($input) // return;
103 $self->input_fh($ifmt, $lei->{$devfd}, $input, @args);
104 } elsif (-f $input && $ifmt eq 'eml') {
105 open my $fh, '<', $input or
106 return $lei->fail("open($input): $!");
107 $self->input_fh($ifmt, $fh, $input, @args);
109 my $m = $lei->{opt}->{'lock'} //
110 PublicInbox::MboxLock->defaults;
111 my $mbl = PublicInbox::MboxLock->acq($input, 0, $m);
112 my $zsfx = PublicInbox::MboxReader::zsfx($input);
114 my $in = delete $mbl->{fh};
116 PublicInbox::MboxReader::zsfxcat($in, $zsfx, $lei);
118 local $PublicInbox::DS::in_loop = 0 if $zsfx; # dwaitpid
119 $self->input_fh($ifmt, $mbl->{fh}, $input, @args);
120 } elsif (-d _ && (-d "$input/cur" || -d "$input/new")) {
121 return $lei->fail(<<EOM) if $ifmt && $ifmt ne 'maildir';
122 $input appears to a be a maildir, not $ifmt
124 PublicInbox::MdirReader->new->maildir_each_eml($input,
125 $self->can('input_maildir_cb'),
128 $lei->fail("$input unsupported (TODO)");
132 sub prepare_inputs { # returns undef on error
133 my ($self, $lei, $inputs) = @_;
134 my $in_fmt = $lei->{opt}->{'in-format'};
135 my $sync = $lei->{opt}->{sync} ? {} : undef; # using LeiMailSync
136 if ($lei->{opt}->{stdin}) {
138 $lei->fail("--stdin and @$inputs do not mix");
139 check_input_format($lei) or return;
140 push @$inputs, '/dev/stdin';
141 push @{$sync->{no}}, '/dev/stdin' if $sync;
143 my $net = $lei->{net}; # NetWriter may be created by l2m
145 # e.g. Maildir:/home/user/Mail/ or imaps://example.com/INBOX
146 for my $input (@$inputs) {
147 my $input_path = $input;
148 if ($input =~ m!\A(?:imaps?|nntps?|s?news)://!i) {
149 require PublicInbox::NetReader;
150 $net //= PublicInbox::NetReader->new;
151 $net->add_url($input);
153 if ($input =~ m!\Aimaps?://!) {
154 push @{$sync->{ok}}, $input;
156 push @{$sync->{no}}, $input;
159 } elsif ($input_path =~ s/\A([a-z0-9]+)://is) {
161 if (($in_fmt // $ifmt) ne $ifmt) {
162 return $lei->fail(<<"");
163 --in-format=$in_fmt and `$ifmt:' conflict
167 if ($ifmt =~ /\A(?:maildir|mh)\z/i) {
168 push @{$sync->{ok}}, $input;
170 push @{$sync->{no}}, $input;
173 my $devfd = $lei->path_to_fd($input_path) // return;
174 if ($devfd >= 0 || (-f $input_path || -p _)) {
175 require PublicInbox::MboxLock;
176 require PublicInbox::MboxReader;
177 PublicInbox::MboxReader->reads($ifmt) or return
178 $lei->fail("$ifmt not supported");
179 } elsif (-d $input_path) {
180 require PublicInbox::MdirReader;
181 $ifmt eq 'maildir' or return
182 $lei->fail("$ifmt not supported");
183 $input = $lei->abs_path($input) if $sync;
185 return $lei->fail("Unable to handle $input");
187 } elsif ($input =~ /\.(eml|patch)\z/i && -f $input) {
188 lc($in_fmt//'eml') eq 'eml' or return $lei->fail(<<"");
189 $input is `eml', not --in-format=$in_fmt
191 require PublicInbox::Eml;
192 push @{$sync->{no}}, $input if $sync;
194 my $devfd = $lei->path_to_fd($input) // return;
195 if ($devfd >= 0 || -f $input || -p _) {
196 push @{$sync->{no}}, $input if $sync;
198 } elsif (-d $input) {
200 $input = $lei->abs_path($input);
201 push @{$sync->{ok}}, $input;
205 return $lei->fail("Unable to handle $input")
209 if (@f) { check_input_format($lei, \@f) or return }
210 if (@d) { # TODO: check for MH vs Maildir, here
211 require PublicInbox::MdirReader;
213 if ($sync && $sync->{no}) {
214 return $lei->fail(<<"") if !$sync->{ok};
215 --sync specified but no inputs support it
217 # non-fatal if some inputs support support sync
218 $lei->err("# --sync will only be used for @{$sync->{ok}}");
219 $lei->err("# --sync is not supported for: @{$sync->{no}}");
222 if (my $err = $net->errors) {
223 return $lei->fail($err);
225 $net->{quiet} = $lei->{opt}->{quiet};
226 require PublicInbox::LeiAuth;
227 $lei->{auth} //= PublicInbox::LeiAuth->new;
228 $lei->{net} //= $net;
230 $self->{inputs} = $inputs;
235 for my $input (@{$self->{inputs}}) {
236 $self->input_path_url($input);
238 my $wait = $self->{lei}->{sto}->ipc_do('done') if $self->{lei}->{sto};
241 sub input_only_atfork_child {
243 my $lei = $self->{lei};
244 $lei->_lei_atfork_child;
245 PublicInbox::IPC::ipc_atfork_child($self);
246 $lei->{auth}->do_auth_atfork($self) if $lei->{auth};
250 # like Getopt::Long, but for +kw:FOO and -kw:FOO to prepare
251 # for update_xvmd -> update_vmd
252 sub vmd_mod_extract {
257 if ($x =~ /\A(\+|\-)(kw|L):(.+)\z/) {
258 my ($op, $pfx, $val) = ($1, $2, $3);
259 if (my $err = $ERR{$pfx}->($val)) {
260 push @{$vmd_mod->{err}}, $err;
261 } else { # set "+kw", "+L", "-L", "-kw"
262 push @{$vmd_mod->{$op.$pfx}}, $val;