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