]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/LeiTag.pm
lei tag: fix tagging of IMAP inputs
[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         $self->{lei}->child_error(1 << 8) if $self->{missing};
125 }
126
127 sub ipc_atfork_child {
128         my ($self) = @_;
129         PublicInbox::LeiInput::input_only_atfork_child($self);
130         # this goes out-of-scope at worker process exit:
131         PublicInbox::OnDestroy->new($$, \&note_missing, $self);
132 }
133
134 # Workaround bash word-splitting s to ['kw', ':', 'keyword' ...]
135 # Maybe there's a better way to go about this in
136 # contrib/completion/lei-completion.bash
137 sub _complete_mark_common ($) {
138         my ($argv) = @_;
139         # Workaround bash word-splitting URLs to ['https', ':', '//' ...]
140         # Maybe there's a better way to go about this in
141         # contrib/completion/lei-completion.bash
142         my $re = '';
143         my $cur = pop(@$argv) // '';
144         if (@$argv) {
145                 my @x = @$argv;
146                 if ($cur eq ':' && @x) {
147                         push @x, $cur;
148                         $cur = '';
149                 }
150                 while (@x > 2 && $x[0] !~ /\A[+\-](?:kw|L)\z/ &&
151                                         $x[1] ne ':') {
152                         shift @x;
153                 }
154                 if (@x >= 2) { # qw(kw : $KEYWORD) or qw(kw :)
155                         $re = join('', @x);
156                 } else { # just return everything and hope for the best
157                         $re = join('', @$argv);
158                 }
159                 $re = quotemeta($re);
160         }
161         ($cur, $re);
162 }
163
164 # FIXME: same problems as _complete_forget_external and similar
165 sub _complete_tag {
166         my ($self, @argv) = @_;
167         my @L = eval { $self->_lei_store->search->all_terms('L') };
168         my @all = ((map { ("+kw:$_", "-kw:$_") } @KW),
169                 (map { ("+L:$_", "-L:$_") } @L));
170         return @all if !@argv;
171         my ($cur, $re) = _complete_mark_common(\@argv);
172         map {
173                 # only return the part specified on the CLI
174                 # don't duplicate if already 100% completed
175                 /\A$re(\Q$cur\E.*)/ ? ($cur eq $1 ? () : $1) : ();
176         } grep(/$re\Q$cur/, @all);
177 }
178
179 no warnings 'once'; # the following works even when LeiAuth is lazy-loaded
180 *net_merge_all = \&PublicInbox::LeiAuth::net_merge_all;
181
182 1;