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 # handles "lei mark" command
5 package PublicInbox::LeiMark;
8 use parent qw(PublicInbox::IPC PublicInbox::LeiInput);
10 use PublicInbox::PktOp qw(pkt_do);
13 my @KW = (qw(seen answered flagged draft), # system
14 qw(forwarded phishing junk notjunk)); # reserved
15 # note: RFC 8621 states "Users may add arbitrary keywords to an Email",
16 # but is it good idea? Stick to the system and reserved ones, for now.
17 # The "system" ones map to Maildir flags and mbox Status/X-Status headers.
18 my %KW = map { $_ => 1 } @KW;
19 my $L_MAX = 244; # Xapian term limit - length('L')
21 # RFC 8621, sec 2 (Mailboxes) a "label" for us is a JMAP Mailbox "name"
22 # "Servers MAY reject names that violate server policy"
26 length($label) >= $L_MAX and
27 return "`$label' too long (must be <= $L_MAX)";
28 $label =~ m{\A[a-z0-9_][a-z0-9_\-\./\@\!,]*[a-z0-9]\z} ?
29 undef : "`$label' is invalid";
33 $KW{$kw} ? undef : <<EOM;
34 `$kw' is not one of: `seen', `flagged', `answered', `draft'
35 `junk', `notjunk', `phishing' or `forwarded'
41 # like Getopt::Long, but for +kw:FOO and -kw:FOO to prepare
42 # for update_xvmd -> update_vmd
48 if ($x =~ /\A(\+|\-)(kw|L):(.+)\z/) {
49 my ($op, $pfx, $val) = ($1, $2, $3);
50 if (my $err = $ERR{$pfx}->($val)) {
51 push @{$vmd_mod->{err}}, $err;
52 } else { # set "+kw", "+L", "-L", "-kw"
53 push @{$vmd_mod->{$op.$pfx}}, $val;
63 sub eml_cb { # used by PublicInbox::LeiInput::input_fh
64 my ($self, $eml) = @_;
65 if (my $xoids = $self->{lei}->{ale}->xoids_for($eml)) {
66 $self->{lei}->{sto}->ipc_do('update_xvmd', $xoids,
73 sub mbox_cb { eml_cb($_[1], $_[0]) } # used by PublicInbox::LeiInput::input_fh
75 sub mark_done_wait { # dwaitpid callback
77 my ($mark, $lei) = @$arg;
78 $lei->child_error($?, 'non-fatal errors during mark') if $?;
79 my $sto = delete $lei->{sto};
80 my $wait = $sto->ipc_do('done') if $sto; # PublicInbox::LeiStore::done
84 sub mark_done { # EOF callback for main daemon
86 my $mark = delete $lei->{mark} or return;
87 $mark->wq_wait_old(\&mark_done_wait, $lei);
90 sub net_merge_complete { # callback used by LeiAuth
92 for my $input (@{$self->{inputs}}) {
93 $self->wq_io_do('mark_path_url', [], $input);
98 sub _mark_maildir { # maildir_each_eml cb
99 my ($f, $kw, $eml, $self) = @_;
103 sub _mark_net { # imap_each, nntp_each cb
104 my ($url, $uid, $kw, $eml, $self) = @_;
108 sub lei_mark { # the "lei mark" method
109 my ($lei, @argv) = @_;
110 my $sto = $lei->_lei_store(1);
111 my $self = $lei->{mark} = bless { missing => 0 }, __PACKAGE__;
112 $sto->write_prepare($lei);
113 $lei->ale; # refresh and prepare
114 my $vmd_mod = vmd_mod_extract(\@argv);
115 return $lei->fail(join("\n", @{$vmd_mod->{err}})) if $vmd_mod->{err};
116 $self->prepare_inputs($lei, \@argv) or return;
117 grep(defined, @$vmd_mod{qw(+kw +L -L -kw)}) or
118 return $lei->fail('no keywords or labels specified');
119 my $ops = { '' => [ \&mark_done, $lei ] };
120 $lei->{auth}->op_merge($ops, $self) if $lei->{auth};
121 $self->{vmd_mod} = $vmd_mod;
122 my $op = $lei->workers_start($self, 'lei_mark', 1, $ops);
123 $self->wq_io_do('mark_stdin', []) if $self->{0};
124 net_merge_complete($self) unless $lei->{auth};
125 while ($op && $op->{sock}) { $op->event_step }
129 my ($self, $input) = @_;
130 my $lei = $self->{lei};
131 my $ifmt = lc($lei->{opt}->{'in-format'} // '');
133 if ($input =~ m!\Aimaps?://!i) {
134 $lei->{net}->imap_each($input, \&_mark_net, $self);
136 } elsif ($input =~ m!\A(?:nntps?|s?news)://!i) {
137 $lei->{net}->nntp_each($input, \&_mark_net, $self);
139 } elsif ($input =~ s!\A([a-z0-9]+):!!i) {
143 my $m = $lei->{opt}->{'lock'} // ($ifmt eq 'eml' ? ['none'] :
144 PublicInbox::MboxLock->defaults);
145 my $mbl = PublicInbox::MboxLock->acq($input, 0, $m);
146 $self->input_fh($ifmt, $mbl->{fh}, $input);
147 } elsif (-d _ && (-d "$input/cur" || -d "$input/new")) {
148 return $lei->fail(<<EOM) if $ifmt && $ifmt ne 'maildir';
149 $input appears to a be a maildir, not $ifmt
151 PublicInbox::MdirReader::maildir_each_eml($input,
152 \&_mark_maildir, $self);
154 $lei->fail("$input unsupported (TODO)");
160 my $lei = $self->{lei};
161 my $in = delete $self->{0};
162 $self->input_fh($lei->{opt}->{'in-format'}, $in, '<stdin>');
167 $self->{lei}->child_error(1 << 8) if $self->{missing};
170 sub ipc_atfork_child {
172 PublicInbox::LeiInput::input_only_atfork_child($self);
173 # this goes out-of-scope at worker process exit:
174 PublicInbox::OnDestroy->new($$, \¬e_missing, $self);