1 # Copyright (C) 2021 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 # handles "lei tag" command
5 package PublicInbox::LeiTag;
8 use parent qw(PublicInbox::IPC PublicInbox::LeiInput);
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')
21 # RFC 8621, sec 2 (Mailboxes) a "label" for us is a JMAP Mailbox "name"
22 # "Servers MAY reject names that violate server policy"
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";
33 $KW{$kw} ? undef : <<EOM;
34 `$kw' is not one of: `seen', `flagged', `answered', `draft'
35 `junk', `notjunk', `phishing' or `forwarded'
40 # like Getopt::Long, but for +kw:FOO and -kw:FOO to prepare
41 # for update_xvmd -> update_vmd
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;
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,
72 sub input_mbox_cb { input_eml_cb($_[1], $_[0]) }
74 sub tag_done_wait { # dwaitpid callback
76 my ($tag, $lei) = @$arg;
77 $lei->child_error($?, 'non-fatal errors during tag') if $?;
81 sub tag_done { # EOF callback for main daemon
83 my $tag = delete $lei->{tag} or return;
84 $tag->wq_wait_old(\&tag_done_wait, $lei);
87 sub net_merge_complete { # callback used by LeiAuth
89 $self->wq_io_do('process_inputs');
93 sub input_maildir_cb { # maildir_each_eml cb
94 my ($f, $kw, $eml, $self) = @_;
95 input_eml_cb($self, $eml);
98 sub input_net_cb { # imap_each, nntp_each cb
99 my ($url, $uid, $kw, $eml, $self) = @_;
100 input_eml_cb($self, $eml);
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);
120 net_merge_complete($self) unless $lei->{auth};
121 $op_c->op_wait_event($ops);
126 my $n = $self->{missing} or return;
127 $self->{lei}->child_error(1 << 8, "$n missed messages");
130 sub ipc_atfork_child {
132 PublicInbox::LeiInput::input_only_atfork_child($self);
133 # this goes out-of-scope at worker process exit:
134 PublicInbox::OnDestroy->new($$, \¬e_missing, $self);
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 ($) {
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
146 my $cur = pop(@$argv) // '';
149 if ($cur eq ':' && @x) {
153 while (@x > 2 && $x[0] !~ /\A[+\-](?:kw|L)\z/ &&
157 if (@x >= 2) { # qw(kw : $KEYWORD) or qw(kw :)
159 } else { # just return everything and hope for the best
160 $re = join('', @$argv);
162 $re = quotemeta($re);
167 # FIXME: same problems as _complete_forget_external and similar
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);
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);
182 no warnings 'once'; # the following works even when LeiAuth is lazy-loaded
183 *net_merge_all = \&PublicInbox::LeiAuth::net_merge_all;