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 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')
19 # RFC 8621, sec 2 (Mailboxes) a "label" for us is a JMAP Mailbox "name"
20 # "Servers MAY reject names that violate server policy"
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";
31 $KW{$kw} ? undef : <<EOM;
32 `$kw' is not one of: `seen', `flagged', `answered', `draft'
33 `junk', `notjunk', `phishing' or `forwarded'
38 # like Getopt::Long, but for +kw:FOO and -kw:FOO to prepare
39 # for update_xvmd -> update_vmd
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;
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,
70 sub input_mbox_cb { input_eml_cb($_[1], $_[0]) }
72 sub tag_done_wait { # dwaitpid callback
74 my ($tag, $lei) = @$arg;
75 $lei->child_error($?, 'non-fatal errors during tag') if $?;
79 sub tag_done { # EOF callback for main daemon
81 my $tag = delete $lei->{tag} or return;
82 $tag->wq_wait_old(\&tag_done_wait, $lei);
85 sub net_merge_complete { # callback used by LeiAuth
87 $self->wq_io_do('process_inputs');
91 sub input_maildir_cb { # maildir_each_eml cb
92 my ($f, $kw, $eml, $self) = @_;
93 input_eml_cb($self, $eml);
96 sub input_net_cb { # imap_each, nntp_each cb
97 my ($url, $uid, $kw, $eml, $self) = @_;
98 input_eml_cb($self, $eml);
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 $op_c, $ops) = $lei->workers_start($self, 'lei_tag', 1, $ops);
117 net_merge_complete($self) unless $lei->{auth};
118 $op_c->op_wait_event($ops);
123 $self->{lei}->child_error(1 << 8) if $self->{missing};
126 sub ipc_atfork_child {
128 PublicInbox::LeiInput::input_only_atfork_child($self);
129 # this goes out-of-scope at worker process exit:
130 PublicInbox::OnDestroy->new($$, \¬e_missing, $self);
133 # Workaround bash word-splitting s to ['kw', ':', 'keyword' ...]
134 # Maybe there's a better way to go about this in
135 # contrib/completion/lei-completion.bash
136 sub _complete_mark_common ($) {
138 # Workaround bash word-splitting URLs to ['https', ':', '//' ...]
139 # Maybe there's a better way to go about this in
140 # contrib/completion/lei-completion.bash
142 my $cur = pop(@$argv) // '';
145 if ($cur eq ':' && @x) {
149 while (@x > 2 && $x[0] !~ /\A[+\-](?:kw|L)\z/ &&
153 if (@x >= 2) { # qw(kw : $KEYWORD) or qw(kw :)
155 } else { # just return everything and hope for the best
156 $re = join('', @$argv);
158 $re = quotemeta($re);
163 # FIXME: same problems as _complete_forget_external and similar
165 my ($self, @argv) = @_;
166 my @L = eval { $self->_lei_store->search->all_terms('L') };
167 my @all = ((map { ("+kw:$_", "-kw:$_") } @KW),
168 (map { ("+L:$_", "-L:$_") } @L));
169 return @all if !@argv;
170 my ($cur, $re) = _complete_mark_common(\@argv);
172 # only return the part specified on the CLI
173 # don't duplicate if already 100% completed
174 /\A$re(\Q$cur\E.*)/ ? ($cur eq $1 ? () : $1) : ();
175 } grep(/$re\Q$cur/, @all);