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