]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/LeiMark.pm
lei mark: command for (un)setting keywords and labels
[public-inbox.git] / lib / PublicInbox / LeiMark.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 mark" command
5 package PublicInbox::LeiMark;
6 use strict;
7 use v5.10.1;
8 use parent qw(PublicInbox::IPC PublicInbox::LeiInput);
9 use PublicInbox::Eml;
10 use PublicInbox::PktOp qw(pkt_do);
11
12 # JMAP RFC 8621 4.1.1
13 my @KW = (qw(seen answered flagged draft), # system
14         qw(forwarded phishing junk notjunk)); # reserved
15 # note: RFC 8621 states "Users may add arbitrary keywords to an Email",
16 # but is it good idea?  Stick to the system and reserved ones, for now.
17 # The "system" ones map to Maildir flags 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} ?
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
41 # like Getopt::Long, but for +kw:FOO and -kw:FOO to prepare
42 # for update_xvmd -> update_vmd
43 sub vmd_mod_extract {
44         my $argv = $_[-1];
45         my $vmd_mod = {};
46         my @new_argv;
47         for my $x (@$argv) {
48                 if ($x =~ /\A(\+|\-)(kw|L):(.+)\z/) {
49                         my ($op, $pfx, $val) = ($1, $2, $3);
50                         if (my $err = $ERR{$pfx}->($val)) {
51                                 push @{$vmd_mod->{err}}, $err;
52                         } else { # set "+kw", "+L", "-L", "-kw"
53                                 push @{$vmd_mod->{$op.$pfx}}, $val;
54                         }
55                 } else {
56                         push @new_argv, $x;
57                 }
58         }
59         @$argv = @new_argv;
60         $vmd_mod;
61 }
62
63 sub eml_cb { # used by PublicInbox::LeiInput::input_fh
64         my ($self, $eml) = @_;
65         if (my $xoids = $self->{lei}->{ale}->xoids_for($eml)) {
66                 $self->{lei}->{sto}->ipc_do('update_xvmd', $xoids,
67                                                 $self->{vmd_mod});
68         } else {
69                 ++$self->{missing};
70         }
71 }
72
73 sub mbox_cb { eml_cb($_[1], $_[0]) } # used by PublicInbox::LeiInput::input_fh
74
75 sub mark_done_wait { # dwaitpid callback
76         my ($arg, $pid) = @_;
77         my ($mark, $lei) = @$arg;
78         $lei->child_error($?, 'non-fatal errors during mark') if $?;
79         my $sto = delete $lei->{sto};
80         my $wait = $sto->ipc_do('done') if $sto; # PublicInbox::LeiStore::done
81         $lei->dclose;
82 }
83
84 sub mark_done { # EOF callback for main daemon
85         my ($lei) = @_;
86         my $mark = delete $lei->{mark} or return;
87         $mark->wq_wait_old(\&mark_done_wait, $lei);
88 }
89
90 sub net_merge_complete { # callback used by LeiAuth
91         my ($self) = @_;
92         for my $input (@{$self->{inputs}}) {
93                 $self->wq_io_do('mark_path_url', [], $input);
94         }
95         $self->wq_close(1);
96 }
97
98 sub _mark_maildir { # maildir_each_eml cb
99         my ($f, $kw, $eml, $self) = @_;
100         eml_cb($self, $eml);
101 }
102
103 sub _mark_net { # imap_each, nntp_each cb
104         my ($url, $uid, $kw, $eml, $self) = @_;
105         eml_cb($self, $eml)
106 }
107
108 sub lei_mark { # the "lei mark" method
109         my ($lei, @argv) = @_;
110         my $sto = $lei->_lei_store(1);
111         my $self = $lei->{mark} = bless { missing => 0 }, __PACKAGE__;
112         $sto->write_prepare($lei);
113         $lei->ale; # refresh and prepare
114         my $vmd_mod = vmd_mod_extract(\@argv);
115         return $lei->fail(join("\n", @{$vmd_mod->{err}})) if $vmd_mod->{err};
116         $self->prepare_inputs($lei, \@argv) or return;
117         grep(defined, @$vmd_mod{qw(+kw +L -L -kw)}) or
118                 return $lei->fail('no keywords or labels specified');
119         my $ops = { '' => [ \&mark_done, $lei ] };
120         $lei->{auth}->op_merge($ops, $self) if $lei->{auth};
121         $self->{vmd_mod} = $vmd_mod;
122         my $op = $lei->workers_start($self, 'lei_mark', 1, $ops);
123         $self->wq_io_do('mark_stdin', []) if $self->{0};
124         net_merge_complete($self) unless $lei->{auth};
125         while ($op && $op->{sock}) { $op->event_step }
126 }
127
128 sub mark_path_url {
129         my ($self, $input) = @_;
130         my $lei = $self->{lei};
131         my $ifmt = lc($lei->{opt}->{'in-format'} // '');
132         # TODO auto-detect?
133         if ($input =~ m!\Aimaps?://!i) {
134                 $lei->{net}->imap_each($input, \&_mark_net, $self);
135                 return;
136         } elsif ($input =~ m!\A(?:nntps?|s?news)://!i) {
137                 $lei->{net}->nntp_each($input, \&_mark_net, $self);
138                 return;
139         } elsif ($input =~ s!\A([a-z0-9]+):!!i) {
140                 $ifmt = lc $1;
141         }
142         if (-f $input) {
143                 my $m = $lei->{opt}->{'lock'} // ($ifmt eq 'eml' ? ['none'] :
144                                 PublicInbox::MboxLock->defaults);
145                 my $mbl = PublicInbox::MboxLock->acq($input, 0, $m);
146                 $self->input_fh($ifmt, $mbl->{fh}, $input);
147         } elsif (-d _ && (-d "$input/cur" || -d "$input/new")) {
148                 return $lei->fail(<<EOM) if $ifmt && $ifmt ne 'maildir';
149 $input appears to a be a maildir, not $ifmt
150 EOM
151                 PublicInbox::MdirReader::maildir_each_eml($input,
152                                         \&_mark_maildir, $self);
153         } else {
154                 $lei->fail("$input unsupported (TODO)");
155         }
156 }
157
158 sub mark_stdin {
159         my ($self) = @_;
160         my $lei = $self->{lei};
161         my $in = delete $self->{0};
162         $self->input_fh($lei->{opt}->{'in-format'}, $in, '<stdin>');
163 }
164
165 sub note_missing {
166         my ($self) = @_;
167         $self->{lei}->child_error(1 << 8) if $self->{missing};
168 }
169
170 sub ipc_atfork_child {
171         my ($self) = @_;
172         PublicInbox::LeiInput::input_only_atfork_child($self);
173         # this goes out-of-scope at worker process exit:
174         PublicInbox::OnDestroy->new($$, \&note_missing, $self);
175 }
176
177 1;