]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/LeiMark.pm
b187d6e76686d6b94ad0fae9dfa3ae466aa266e0
[public-inbox.git] / lib / PublicInbox / LeiMark.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 # handles "lei mark" command
5 package PublicInbox::LeiMark;
6 use strict;
7 use v5.10.1;
8 use parent qw(PublicInbox::IPC PublicInbox::LeiInput);
9
10 # JMAP RFC 8621 4.1.1
11 my @KW = (qw(seen answered flagged draft), # system
12         qw(forwarded phishing junk notjunk)); # reserved
13 # note: RFC 8621 states "Users may add arbitrary keywords to an Email",
14 # but is it good idea?  Stick to the system and reserved ones, for now.
15 # The "system" ones map to Maildir flags and mbox Status/X-Status headers.
16 my %KW = map { $_ => 1 } @KW;
17 my $L_MAX = 244; # Xapian term limit - length('L')
18
19 # RFC 8621, sec 2 (Mailboxes) a "label" for us is a JMAP Mailbox "name"
20 # "Servers MAY reject names that violate server policy"
21 my %ERR = (
22         L => sub {
23                 my ($label) = @_;
24                 length($label) >= $L_MAX and
25                         return "`$label' too long (must be <= $L_MAX)";
26                 $label =~ m{\A[a-z0-9_](?:[a-z0-9_\-\./\@,]*[a-z0-9])?\z}i ?
27                         undef : "`$label' is invalid";
28         },
29         kw => sub {
30                 my ($kw) = @_;
31                 $KW{$kw} ? undef : <<EOM;
32 `$kw' is not one of: `seen', `flagged', `answered', `draft'
33 `junk', `notjunk', `phishing' or `forwarded'
34 EOM
35         }
36 );
37
38 # like Getopt::Long, but for +kw:FOO and -kw:FOO to prepare
39 # for update_xvmd -> update_vmd
40 sub vmd_mod_extract {
41         my $argv = $_[-1];
42         my $vmd_mod = {};
43         my @new_argv;
44         for my $x (@$argv) {
45                 if ($x =~ /\A(\+|\-)(kw|L):(.+)\z/) {
46                         my ($op, $pfx, $val) = ($1, $2, $3);
47                         if (my $err = $ERR{$pfx}->($val)) {
48                                 push @{$vmd_mod->{err}}, $err;
49                         } else { # set "+kw", "+L", "-L", "-kw"
50                                 push @{$vmd_mod->{$op.$pfx}}, $val;
51                         }
52                 } else {
53                         push @new_argv, $x;
54                 }
55         }
56         @$argv = @new_argv;
57         $vmd_mod;
58 }
59
60 sub input_eml_cb { # used by PublicInbox::LeiInput::input_fh
61         my ($self, $eml) = @_;
62         if (my $xoids = $self->{lei}->{ale}->xoids_for($eml)) {
63                 $self->{lei}->{sto}->ipc_do('update_xvmd', $xoids, $eml,
64                                                 $self->{vmd_mod});
65         } else {
66                 ++$self->{missing};
67         }
68 }
69
70 sub input_mbox_cb { input_eml_cb($_[1], $_[0]) }
71
72 sub mark_done_wait { # dwaitpid callback
73         my ($arg, $pid) = @_;
74         my ($mark, $lei) = @$arg;
75         $lei->child_error($?, 'non-fatal errors during mark') if $?;
76         my $sto = delete $lei->{sto};
77         my $wait = $sto->ipc_do('done') if $sto; # PublicInbox::LeiStore::done
78         $lei->dclose;
79 }
80
81 sub mark_done { # EOF callback for main daemon
82         my ($lei) = @_;
83         my $mark = delete $lei->{mark} or return;
84         $mark->wq_wait_old(\&mark_done_wait, $lei);
85 }
86
87 sub net_merge_complete { # callback used by LeiAuth
88         my ($self) = @_;
89         for my $input (@{$self->{inputs}}) {
90                 $self->wq_io_do('input_path_url', [], $input);
91         }
92         $self->wq_close(1);
93 }
94
95 sub input_maildir_cb { # maildir_each_eml cb
96         my ($f, $kw, $eml, $self) = @_;
97         input_eml_cb($self, $eml);
98 }
99
100 sub input_net_cb { # imap_each, nntp_each cb
101         my ($url, $uid, $kw, $eml, $self) = @_;
102         input_eml_cb($self, $eml);
103 }
104
105 sub lei_mark { # the "lei mark" method
106         my ($lei, @argv) = @_;
107         my $sto = $lei->_lei_store(1);
108         $sto->write_prepare($lei);
109         my $self = bless { missing => 0 }, __PACKAGE__;
110         $lei->ale; # refresh and prepare
111         my $vmd_mod = vmd_mod_extract(\@argv);
112         return $lei->fail(join("\n", @{$vmd_mod->{err}})) if $vmd_mod->{err};
113         $self->prepare_inputs($lei, \@argv) or return;
114         grep(defined, @$vmd_mod{qw(+kw +L -L -kw)}) or
115                 return $lei->fail('no keywords or labels specified');
116         my $ops = { '' => [ \&mark_done, $lei ] };
117         $lei->{auth}->op_merge($ops, $self) if $lei->{auth};
118         $self->{vmd_mod} = $vmd_mod;
119         my ($op_c, undef) = $lei->workers_start($self, 'lei_mark', 1, $ops);
120         $lei->{mark} = $self;
121         net_merge_complete($self) unless $lei->{auth};
122         $op_c->op_wait_event($ops);
123 }
124
125 sub note_missing {
126         my ($self) = @_;
127         $self->{lei}->child_error(1 << 8) if $self->{missing};
128 }
129
130 sub ipc_atfork_child {
131         my ($self) = @_;
132         PublicInbox::LeiInput::input_only_atfork_child($self);
133         # this goes out-of-scope at worker process exit:
134         PublicInbox::OnDestroy->new($$, \&note_missing, $self);
135 }
136
137 # Workaround bash word-splitting s to ['kw', ':', 'keyword' ...]
138 # Maybe there's a better way to go about this in
139 # contrib/completion/lei-completion.bash
140 sub _complete_mark_common ($) {
141         my ($argv) = @_;
142         # Workaround bash word-splitting URLs to ['https', ':', '//' ...]
143         # Maybe there's a better way to go about this in
144         # contrib/completion/lei-completion.bash
145         my $re = '';
146         my $cur = pop(@$argv) // '';
147         if (@$argv) {
148                 my @x = @$argv;
149                 if ($cur eq ':' && @x) {
150                         push @x, $cur;
151                         $cur = '';
152                 }
153                 while (@x > 2 && $x[0] !~ /\A[+\-](?:kw|L)\z/ &&
154                                         $x[1] ne ':') {
155                         shift @x;
156                 }
157                 if (@x >= 2) { # qw(kw : $KEYWORD) or qw(kw :)
158                         $re = join('', @x);
159                 } else { # just return everything and hope for the best
160                         $re = join('', @$argv);
161                 }
162                 $re = quotemeta($re);
163         }
164         ($cur, $re);
165 }
166
167 # FIXME: same problems as _complete_forget_external and similar
168 sub _complete_mark {
169         my ($self, @argv) = @_;
170         my @L = eval { $self->_lei_store->search->all_terms('L') };
171         my @all = ((map { ("+kw:$_", "-kw:$_") } @KW),
172                 (map { ("+L:$_", "-L:$_") } @L));
173         return @all if !@argv;
174         my ($cur, $re) = _complete_mark_common(\@argv);
175         map {
176                 # only return the part specified on the CLI
177                 # don't duplicate if already 100% completed
178                 /\A$re(\Q$cur\E.*)/ ? ($cur eq $1 ? () : $1) : ();
179         } grep(/$re\Q$cur/, @all);
180 }
181
182 1;