]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/LeiTag.pm
lei_tag: fix comments w.r.t support levels
[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 # https://www.iana.org/assignments/imap-jmap-keywords/imap-jmap-keywords.xhtml
12 my @KW = (qw(seen answered flagged draft), # widely-compatible
13         qw(forwarded phishing junk notjunk)); # rarely supported
14 # note: RFC 8621 states "Users may add arbitrary keywords to an Email",
15 # but is it good idea?  Stick to the system and reserved ones, for now.
16 # The widely-compatible ones map to IMAP system flags, Maildir flags
17 # and mbox Status/X-Status headers.
18 my %KW = map { $_ => 1 } @KW;
19 my $L_MAX = 244; # Xapian term limit - length('L')
20
21 # RFC 8621, sec 2 (Mailboxes) a "label" for us is a JMAP Mailbox "name"
22 # "Servers MAY reject names that violate server policy"
23 my %ERR = (
24         L => sub {
25                 my ($label) = @_;
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}i ?
29                         undef : "`$label' is invalid";
30         },
31         kw => sub {
32                 my ($kw) = @_;
33                 $KW{$kw} ? undef : <<EOM;
34 `$kw' is not one of: `seen', `flagged', `answered', `draft'
35 `junk', `notjunk', `phishing' or `forwarded'
36 EOM
37         }
38 );
39
40 # like Getopt::Long, but for +kw:FOO and -kw:FOO to prepare
41 # for update_xvmd -> update_vmd
42 sub vmd_mod_extract {
43         my $argv = $_[-1];
44         my $vmd_mod = {};
45         my @new_argv;
46         for my $x (@$argv) {
47                 if ($x =~ /\A(\+|\-)(kw|L):(.+)\z/) {
48                         my ($op, $pfx, $val) = ($1, $2, $3);
49                         if (my $err = $ERR{$pfx}->($val)) {
50                                 push @{$vmd_mod->{err}}, $err;
51                         } else { # set "+kw", "+L", "-L", "-kw"
52                                 push @{$vmd_mod->{$op.$pfx}}, $val;
53                         }
54                 } else {
55                         push @new_argv, $x;
56                 }
57         }
58         @$argv = @new_argv;
59         $vmd_mod;
60 }
61
62 sub input_eml_cb { # used by PublicInbox::LeiInput::input_fh
63         my ($self, $eml) = @_;
64         if (my $xoids = $self->{lei}->{ale}->xoids_for($eml)) {
65                 $self->{lei}->{sto}->ipc_do('update_xvmd', $xoids, $eml,
66                                                 $self->{vmd_mod});
67         } else {
68                 ++$self->{missing};
69         }
70 }
71
72 sub input_mbox_cb { input_eml_cb($_[1], $_[0]) }
73
74 sub tag_done_wait { # dwaitpid callback
75         my ($arg, $pid) = @_;
76         my ($tag, $lei) = @$arg;
77         $lei->child_error($?, 'non-fatal errors during tag') if $?;
78         $lei->dclose;
79 }
80
81 sub tag_done { # EOF callback for main daemon
82         my ($lei) = @_;
83         my $tag = delete $lei->{tag} or return;
84         $tag->wq_wait_old(\&tag_done_wait, $lei);
85 }
86
87 sub net_merge_complete { # callback used by LeiAuth
88         my ($self) = @_;
89         $self->wq_io_do('process_inputs');
90         $self->wq_close(1);
91 }
92
93 sub input_maildir_cb { # maildir_each_eml cb
94         my ($f, $kw, $eml, $self) = @_;
95         input_eml_cb($self, $eml);
96 }
97
98 sub input_net_cb { # imap_each, nntp_each cb
99         my ($url, $uid, $kw, $eml, $self) = @_;
100         input_eml_cb($self, $eml);
101 }
102
103 sub lei_tag { # the "lei tag" method
104         my ($lei, @argv) = @_;
105         my $sto = $lei->_lei_store(1);
106         $sto->write_prepare($lei);
107         my $self = bless { missing => 0 }, __PACKAGE__;
108         $lei->ale; # refresh and prepare
109         my $vmd_mod = vmd_mod_extract(\@argv);
110         return $lei->fail(join("\n", @{$vmd_mod->{err}})) if $vmd_mod->{err};
111         $self->prepare_inputs($lei, \@argv) or return;
112         grep(defined, @$vmd_mod{qw(+kw +L -L -kw)}) or
113                 return $lei->fail('no keywords or labels specified');
114         my $ops = { '' => [ \&tag_done, $lei ] };
115         $lei->{auth}->op_merge($ops, $self) if $lei->{auth};
116         $self->{vmd_mod} = $vmd_mod;
117         my $j = $self->{-wq_nr_workers} = 1; # locked for now
118         (my $op_c, $ops) = $lei->workers_start($self, 'lei_tag', $j, $ops);
119         $lei->{tag} = $self;
120         net_merge_complete($self) unless $lei->{auth};
121         $op_c->op_wait_event($ops);
122 }
123
124 sub note_missing {
125         my ($self) = @_;
126         my $n = $self->{missing} or return;
127         $self->{lei}->child_error(1 << 8, "$n missed messages");
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_tag {
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 no warnings 'once'; # the following works even when LeiAuth is lazy-loaded
183 *net_merge_all = \&PublicInbox::LeiAuth::net_merge_all;
184
185 1;