]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/LeiTag.pm
lei tag: note message mismatches on failure
[public-inbox.git] / lib / PublicInbox / LeiTag.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 tag" command
5 package PublicInbox::LeiTag;
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 tag_done_wait { # dwaitpid callback
73         my ($arg, $pid) = @_;
74         my ($tag, $lei) = @$arg;
75         $lei->child_error($?, 'non-fatal errors during tag') if $?;
76         $lei->dclose;
77 }
78
79 sub tag_done { # EOF callback for main daemon
80         my ($lei) = @_;
81         my $tag = delete $lei->{tag} or return;
82         $tag->wq_wait_old(\&tag_done_wait, $lei);
83 }
84
85 sub net_merge_complete { # callback used by LeiAuth
86         my ($self) = @_;
87         $self->wq_io_do('process_inputs');
88         $self->wq_close(1);
89 }
90
91 sub input_maildir_cb { # maildir_each_eml cb
92         my ($f, $kw, $eml, $self) = @_;
93         input_eml_cb($self, $eml);
94 }
95
96 sub input_net_cb { # imap_each, nntp_each cb
97         my ($url, $uid, $kw, $eml, $self) = @_;
98         input_eml_cb($self, $eml);
99 }
100
101 sub lei_tag { # the "lei tag" method
102         my ($lei, @argv) = @_;
103         my $sto = $lei->_lei_store(1);
104         $sto->write_prepare($lei);
105         my $self = bless { missing => 0 }, __PACKAGE__;
106         $lei->ale; # refresh and prepare
107         my $vmd_mod = vmd_mod_extract(\@argv);
108         return $lei->fail(join("\n", @{$vmd_mod->{err}})) if $vmd_mod->{err};
109         $self->prepare_inputs($lei, \@argv) or return;
110         grep(defined, @$vmd_mod{qw(+kw +L -L -kw)}) or
111                 return $lei->fail('no keywords or labels specified');
112         my $ops = { '' => [ \&tag_done, $lei ] };
113         $lei->{auth}->op_merge($ops, $self) if $lei->{auth};
114         $self->{vmd_mod} = $vmd_mod;
115         my $j = $self->{-wq_nr_workers} = 1; # locked for now
116         (my $op_c, $ops) = $lei->workers_start($self, 'lei_tag', $j, $ops);
117         $lei->{tag} = $self;
118         net_merge_complete($self) unless $lei->{auth};
119         $op_c->op_wait_event($ops);
120 }
121
122 sub note_missing {
123         my ($self) = @_;
124         my $n = $self->{missing} or return;
125         $self->{lei}->child_error(1 << 8, "$n missed messages");
126 }
127
128 sub ipc_atfork_child {
129         my ($self) = @_;
130         PublicInbox::LeiInput::input_only_atfork_child($self);
131         # this goes out-of-scope at worker process exit:
132         PublicInbox::OnDestroy->new($$, \&note_missing, $self);
133 }
134
135 # Workaround bash word-splitting s to ['kw', ':', 'keyword' ...]
136 # Maybe there's a better way to go about this in
137 # contrib/completion/lei-completion.bash
138 sub _complete_mark_common ($) {
139         my ($argv) = @_;
140         # Workaround bash word-splitting URLs to ['https', ':', '//' ...]
141         # Maybe there's a better way to go about this in
142         # contrib/completion/lei-completion.bash
143         my $re = '';
144         my $cur = pop(@$argv) // '';
145         if (@$argv) {
146                 my @x = @$argv;
147                 if ($cur eq ':' && @x) {
148                         push @x, $cur;
149                         $cur = '';
150                 }
151                 while (@x > 2 && $x[0] !~ /\A[+\-](?:kw|L)\z/ &&
152                                         $x[1] ne ':') {
153                         shift @x;
154                 }
155                 if (@x >= 2) { # qw(kw : $KEYWORD) or qw(kw :)
156                         $re = join('', @x);
157                 } else { # just return everything and hope for the best
158                         $re = join('', @$argv);
159                 }
160                 $re = quotemeta($re);
161         }
162         ($cur, $re);
163 }
164
165 # FIXME: same problems as _complete_forget_external and similar
166 sub _complete_tag {
167         my ($self, @argv) = @_;
168         my @L = eval { $self->_lei_store->search->all_terms('L') };
169         my @all = ((map { ("+kw:$_", "-kw:$_") } @KW),
170                 (map { ("+L:$_", "-L:$_") } @L));
171         return @all if !@argv;
172         my ($cur, $re) = _complete_mark_common(\@argv);
173         map {
174                 # only return the part specified on the CLI
175                 # don't duplicate if already 100% completed
176                 /\A$re(\Q$cur\E.*)/ ? ($cur eq $1 ? () : $1) : ();
177         } grep(/$re\Q$cur/, @all);
178 }
179
180 no warnings 'once'; # the following works even when LeiAuth is lazy-loaded
181 *net_merge_all = \&PublicInbox::LeiAuth::net_merge_all;
182
183 1;