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 mark" command
5 package PublicInbox::LeiMark;
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} ?
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,
70 sub input_mbox_cb { input_eml_cb($_[1], $_[0]) }
72 sub mark_done_wait { # dwaitpid callback
74 my ($mark, $lei) = @$arg;
75 $lei->child_error($?, 'non-fatal errors during mark') if $?;
76 my $sto = delete $lei->{sto};
77 my $wait = $sto->ipc_do('done') if $sto; # PublicInbox::LeiStore::done
81 sub mark_done { # EOF callback for main daemon
83 my $mark = delete $lei->{mark} or return;
84 $mark->wq_wait_old(\&mark_done_wait, $lei);
87 sub net_merge_complete { # callback used by LeiAuth
89 for my $input (@{$self->{inputs}}) {
90 $self->wq_io_do('input_path_url', [], $input);
95 sub input_maildir_cb { # maildir_each_eml cb
96 my ($f, $kw, $eml, $self) = @_;
97 input_eml_cb($self, $eml);
100 sub input_net_cb { # imap_each, nntp_each cb
101 my ($url, $uid, $kw, $eml, $self) = @_;
102 input_eml_cb($self, $eml);
105 sub lei_mark { # the "lei mark" method
106 my ($lei, @argv) = @_;
107 my $sto = $lei->_lei_store(1);
108 my $self = $lei->{mark} = bless { missing => 0 }, __PACKAGE__;
109 $sto->write_prepare($lei);
110 $lei->ale; # refresh and prepare
111 my $vmd_mod = vmd_mod_extract(\@argv);
112 return $lei->fail(join("\n", @{$vmd_mod->{err}})) if $vmd_mod->{err};
113 $self->prepare_inputs($lei, \@argv) or return;
114 grep(defined, @$vmd_mod{qw(+kw +L -L -kw)}) or
115 return $lei->fail('no keywords or labels specified');
116 my $ops = { '' => [ \&mark_done, $lei ] };
117 $lei->{auth}->op_merge($ops, $self) if $lei->{auth};
118 $self->{vmd_mod} = $vmd_mod;
119 my $op = $lei->workers_start($self, 'lei_mark', 1, $ops);
120 $lei->{mark} = $self;
121 $self->wq_io_do('input_stdin', []) if $self->{0};
122 net_merge_complete($self) unless $lei->{auth};
123 while ($op && $op->{sock}) { $op->event_step }
128 $self->{lei}->child_error(1 << 8) if $self->{missing};
131 sub ipc_atfork_child {
133 PublicInbox::LeiInput::input_only_atfork_child($self);
134 # this goes out-of-scope at worker process exit:
135 PublicInbox::OnDestroy->new($$, \¬e_missing, $self);
138 # Workaround bash word-splitting s to ['kw', ':', 'keyword' ...]
139 # Maybe there's a better way to go about this in
140 # contrib/completion/lei-completion.bash
141 sub _complete_mark_common ($) {
143 # Workaround bash word-splitting URLs to ['https', ':', '//' ...]
144 # Maybe there's a better way to go about this in
145 # contrib/completion/lei-completion.bash
147 my $cur = pop(@$argv) // '';
150 if ($cur eq ':' && @x) {
154 while (@x > 2 && $x[0] !~ /\A[+\-](?:kw|L)\z/ &&
158 if (@x >= 2) { # qw(kw : $KEYWORD) or qw(kw :)
160 } else { # just return everything and hope for the best
161 $re = join('', @$argv);
163 $re = quotemeta($re);
168 # FIXME: same problems as _complete_forget_external and similar
170 my ($self, @argv) = @_;
171 my @all = map { ("+kw:$_", "-kw:$_") } @KW;
172 return @all if !@argv;
173 my ($cur, $re) = _complete_mark_common(\@argv);
175 # only return the part specified on the CLI
176 # don't duplicate if already 100% completed
177 /\A$re(\Q$cur\E.*)/ ? ($cur eq $1 ? () : $1) : ();
178 } grep(/$re\Q$cur/, @all);