]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/LeiSavedSearch.pm
lei up: further improve Maildir canonicalization
[public-inbox.git] / lib / PublicInbox / LeiSavedSearch.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 # pretends to be like LeiDedupe and also PublicInbox::Inbox
5 package PublicInbox::LeiSavedSearch;
6 use strict;
7 use v5.10.1;
8 use parent qw(PublicInbox::Lock);
9 use PublicInbox::OverIdx;
10 use PublicInbox::LeiSearch;
11 use PublicInbox::Config;
12 use PublicInbox::Spawn qw(run_die);
13 use PublicInbox::ContentHash qw(git_sha);
14 use Digest::SHA qw(sha256_hex);
15
16 *squote_maybe = \&PublicInbox::Config::squote_maybe;
17
18 sub lss_dir_for ($$) {
19         my ($lei, $dstref) = @_;
20         my @n;
21         if ($$dstref =~ m,\Aimaps?://,i) { # already canonicalized
22                 require PublicInbox::URIimap;
23                 my $uri = PublicInbox::URIimap->new($$dstref)->canonical;
24                 $$dstref = $$uri;
25                 @n = ($uri->mailbox);
26         } else { # basename
27                 $$dstref = $lei->rel2abs($$dstref);
28                 $$dstref .= '/' if -d $$dstref;
29                 $$dstref =~ tr!/!/!s;
30                 @n = ($$dstref =~ m{([^/]+)/*\z});
31         }
32         push @n, sha256_hex($$dstref);
33         $lei->share_path . '/saved-searches/' . join('-', @n);
34 }
35
36 sub new {
37         my ($cls, $lei, $dst) = @_;
38         my $self = bless { ale => $lei->ale }, $cls;
39         my $dir;
40         if (defined $dst) { # updating existing saved search via "lei up"
41                 my $f;
42                 $dir = $dst;
43                 output2lssdir($self, $lei, \$dir, \$f) or
44                         return $lei->fail("--save was not used with $dst cwd=".
45                                                 $lei->rel2abs('.'));
46                 $self->{-cfg} //= PublicInbox::Config::git_config_dump($f);
47                 $self->{'-f'} = $f;
48         } else { # new saved search "lei q --save"
49                 my $dst = $lei->{ovv}->{dst};
50                 $dir = lss_dir_for($lei, \$dst);
51                 require File::Path;
52                 File::Path::make_path($dir); # raises on error
53                 $self->{-cfg} = {};
54                 my $f = $self->{'-f'} = "$dir/lei.saved-search";
55                 open my $fh, '>', $f or return $lei->fail("open $f: $!");
56                 my $sq_dst = squote_maybe($dst);
57                 print $fh <<EOM or return $lei->fail("print $f: $!");
58 ; to refresh with new results, run: lei up $sq_dst
59 EOM
60                 close $fh or return $lei->fail("close $f: $!");
61                 my $q = $lei->{mset_opt}->{q_raw} // die 'BUG: {q_raw} missing';
62                 if (ref $q) {
63                         cfg_set($self, '--add', 'lei.q', $_) for @$q;
64                 } else {
65                         cfg_set($self, 'lei.q', $q);
66                 }
67                 $dst = "$lei->{ovv}->{fmt}:$dst" if $dst !~ m!\Aimaps?://!i;
68                 cfg_set($self, 'lei.q.output', $dst);
69                 for my $k (qw(only include exclude)) {
70                         my $ary = $lei->{opt}->{$k} // next;
71                         for my $x (@$ary) {
72                                 cfg_set($self, '--add', "lei.q.$k", $x);
73                         }
74                 }
75                 for my $k (qw(external local remote import-remote
76                                 import-before threads)) {
77                         my $val = $lei->{opt}->{$k} // next;
78                         cfg_set($self, "lei.q.$k", $val);
79                 }
80         }
81         bless $self->{-cfg}, 'PublicInbox::Config';
82         $self->{lock_path} = "$self->{-f}.flock";
83         $self->{-ovf} = "$dir/over.sqlite3";
84         $self;
85 }
86
87 sub description { $_[0]->{qstr} } # for WWW
88
89 sub cfg_set {
90         my ($self, @args) = @_;
91         my $lk = $self->lock_for_scope; # git-config doesn't wait
92         run_die([qw(git config -f), $self->{'-f'}, @args]);
93 }
94
95 # drop-in for LeiDedupe API
96 sub is_dup {
97         my ($self, $eml, $smsg) = @_;
98         my $oidx = $self->{oidx} // die 'BUG: no {oidx}';
99         my $blob = $smsg ? $smsg->{blob} : undef;
100         return 1 if $blob && $oidx->blob_exists($blob);
101         my $lk = $self->lock_for_scope_fast;
102         if (my $xoids = PublicInbox::LeiSearch::xoids_for($self, $eml, 1)) {
103                 for my $docid (values %$xoids) {
104                         $oidx->add_xref3($docid, -1, $blob, '.');
105                 }
106                 $oidx->commit_lazy;
107                 1;
108         } else {
109                 # n.b. above xoids_for fills out eml->{-lei_fake_mid} if needed
110                 unless ($smsg) {
111                         $smsg = bless {}, 'PublicInbox::Smsg';
112                         $smsg->{bytes} = 0;
113                         $smsg->populate($eml);
114                 }
115                 $oidx->begin_lazy;
116                 $smsg->{num} = $oidx->adj_counter('eidx_docid', '+');
117                 $smsg->{blob} //= git_sha(1, $eml)->hexdigest;
118                 $oidx->add_overview($eml, $smsg);
119                 $oidx->add_xref3($smsg->{num}, -1, $smsg->{blob}, '.');
120                 $oidx->commit_lazy;
121                 undef;
122         }
123 }
124
125 sub prepare_dedupe {
126         my ($self) = @_;
127         $self->{oidx} //= do {
128                 my $creat = !-f $self->{-ovf};
129                 my $lk = $self->lock_for_scope; # git-config doesn't wait
130                 my $oidx = PublicInbox::OverIdx->new($self->{-ovf});
131                 $oidx->{-no_fsync} = 1;
132                 $oidx->dbh;
133                 if ($creat) {
134                         $oidx->{dbh}->do('PRAGMA journal_mode = WAL');
135                         $oidx->eidx_prep; # for xref3
136                 }
137                 $oidx
138         };
139 }
140
141 sub over { $_[0]->{oidx} } # for xoids_for
142
143 sub git { $_[0]->{ale}->git }
144
145 sub pause_dedupe {
146         my ($self) = @_;
147         $self->{ale}->git->cleanup;
148         my $oidx = delete($self->{oidx}) // return;
149         $oidx->commit_lazy;
150 }
151
152 sub mm { undef }
153
154 sub altid_map { {} }
155
156 sub cloneurl { [] }
157
158 # find existing directory containing a `lei.saved-search' file based on
159 # $dir_ref which is an output
160 sub output2lssdir {
161         my ($self, $lei, $dir_ref, $fn_ref) = @_;
162         my $dst = $$dir_ref; # imap://$MAILBOX, /path/to/maildir, /path/to/mbox
163         my $dir = lss_dir_for($lei, \$dst);
164         my $f = "$dir/lei.saved-search";
165         if (-f $f && -r _) {
166                 $self->{-cfg} = PublicInbox::Config::git_config_dump($f);
167                 $$dir_ref = $dir;
168                 $$fn_ref = $f;
169                 return 1;
170         }
171         undef;
172 }
173
174 no warnings 'once';
175 *nntp_url = \&cloneurl;
176 *base_url = \&PublicInbox::Inbox::base_url;
177 *smsg_eml = \&PublicInbox::Inbox::smsg_eml;
178 *smsg_by_mid = \&PublicInbox::Inbox::smsg_by_mid;
179 *msg_by_mid = \&PublicInbox::Inbox::msg_by_mid;
180 *modified = \&PublicInbox::Inbox::modified;
181 *recent = \&PublicInbox::Inbox::recent;
182 *max_git_epoch = *nntp_usable = *msg_by_path = \&mm; # undef
183 *isrch = *search = \&mm; # TODO
184 *DESTROY = \&pause_dedupe;
185
186 1;