1 # Copyright (C) 2020-2021 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 # Local storage (cache/memo) for lei(1), suitable for personal/private
5 # mail iff on encrypted device/FS. Based on v2, but only deduplicates
8 # for xref3, the following are constant: $eidx_key = '.', $xnum = -1
9 package PublicInbox::LeiStore;
12 use parent qw(PublicInbox::Lock PublicInbox::IPC);
13 use PublicInbox::SearchIdx qw(crlf_adjust);
14 use PublicInbox::ExtSearchIdx;
15 use PublicInbox::Import;
16 use PublicInbox::InboxWritable;
17 use PublicInbox::V2Writable;
18 use PublicInbox::ContentHash qw(content_hash content_digest);
19 use PublicInbox::MID qw(mids mids_in);
20 use PublicInbox::LeiSearch;
21 use List::Util qw(max);
24 my (undef, $dir, $opt) = @_;
25 my $eidx = PublicInbox::ExtSearchIdx->new($dir, $opt);
26 my $self = bless { priv_eidx => $eidx }, __PACKAGE__;
28 PublicInbox::SearchIdx::load_xapian_writable();
34 sub git { $_[0]->{priv_eidx}->git } # read-only
36 sub packing_factor { $PublicInbox::V2Writable::PACKING_FACTOR }
39 $_[0]->{rotate_bytes} // ((1024 * 1024 * 1024) / $_[0]->packing_factor)
42 sub git_pfx { "$_[0]->{priv_eidx}->{topdir}/local" };
46 if (opendir(my $dh, $self->git_pfx)) {
48 substr($_, 0, -4) + 0; # drop ".git" suffix
49 } grep(/\A[0-9]+\.git\z/, readdir($dh))) // 0;
51 $!{ENOENT} ? 0 : die("opendir ${\$self->git_pfx}: $!\n");
57 open my $null, '>', '/dev/null' or die "open /dev/null: $!";
58 my $opt = { 2 => $null };
59 chomp(my $i = $git->qx(qw(var GIT_COMMITTER_IDENT), undef, $opt));
60 warn "$git->{git_dir} GIT_COMMITTER_IDENT failed\n" if $?;
61 $i =~ /\A(.+) <([^>]+)> [0-9]+ [-\+]?[0-9]+$/ ? ($1, $2) :
62 ('lei user', 'x@example.com')
70 return $im if $im->{bytes_added} < $self->rotate_bytes;
76 $max = $self->git_epoch_max + 1;
78 my $pfx = $self->git_pfx;
79 $max //= $self->git_epoch_max;
81 my $latest = "$pfx/$max.git";
83 PublicInbox::Import::init_bare($latest);
84 my $git = PublicInbox::Git->new($latest);
85 $git->qx(qw(config core.sharedRepository 0600)) if !$old;
86 my $packed_bytes = $git->packed_bytes;
87 my $unpacked_bytes = $packed_bytes / $self->packing_factor;
88 if ($unpacked_bytes >= $self->rotate_bytes) {
92 my ($n, $e) = git_ident($git);
93 $self->{im} = $im = PublicInbox::Import->new($git, $n, $e);
94 $im->{bytes_added} = int($packed_bytes / $self->packing_factor);
95 $im->{lock_path} = undef;
96 $im->{path_type} = 'v2';
102 PublicInbox::LeiSearch->new($_[0]->{priv_eidx}->{topdir});
107 my $eidx = $self->{priv_eidx};
108 $eidx->idx_init({-private => 1});
112 # when a message has no Message-IDs at all, this is needed for
113 # unsent Draft messages, at least
114 sub _fake_mid_for ($$) {
115 my ($eml, $dig) = @_;
116 my $mids = mids_in($eml, qw(X-Alt-Message-ID Resent-Message-ID));
117 $eml->{-lei_fake_mid} =
118 $mids->[0] // PublicInbox::Import::digest2mid($dig, $eml);
121 sub _docids_for ($$) {
122 my ($self, $eml) = @_;
124 my $dig = content_digest($eml);
125 my $chash = $dig->clone->digest;
126 my $eidx = eidx_init($self);
127 my $oidx = $eidx->{oidx};
128 my $im = $self->{im};
129 my $mids = mids($eml);
130 $mids->[0] //= _fake_mid_for($eml, $dig);
131 for my $mid (@$mids) {
133 while (my $cur = $oidx->next_by_mid($mid, \$id, \$prev)) {
134 my $oid = $cur->{blob};
135 my $docid = $cur->{num};
136 my $bref = $im ? $im->cat_blob($oid) : undef;
137 $bref //= $eidx->git->cat_file($oid) // do {
138 warn "W: $oid (#$docid) <$mid> not found\n";
141 local $self->{current_info} = $oid;
142 my $x = PublicInbox::Eml->new($bref);
143 $docids{$docid} = $docid if content_hash($x) eq $chash;
146 sort { $a <=> $b } values %docids;
149 sub set_eml_keywords {
150 my ($self, $eml, @kw) = @_;
151 my $eidx = eidx_init($self);
152 my @docids = _docids_for($self, $eml);
153 for my $docid (@docids) {
154 $eidx->idx_shard($docid)->shard_set_keywords($docid, @kw);
159 sub add_eml_keywords {
160 my ($self, $eml, @kw) = @_;
161 my $eidx = eidx_init($self);
162 my @docids = _docids_for($self, $eml);
163 for my $docid (@docids) {
164 $eidx->idx_shard($docid)->shard_add_keywords($docid, @kw);
169 sub remove_eml_keywords {
170 my ($self, $eml, @kw) = @_;
171 my $eidx = eidx_init($self);
172 my @docids = _docids_for($self, $eml);
173 for my $docid (@docids) {
174 $eidx->idx_shard($docid)->shard_remove_keywords($docid, @kw);
179 # cf: https://doc.dovecot.org/configuration_manual/mail_location/mbox/
180 my %status2kw = (F => 'flagged', A => 'answered', R => 'seen', T => 'draft');
181 # O (old/non-recent), and D (deleted) aren't in JMAP,
182 # so probably won't be supported by us.
185 my $s = "@{[$eml->header_raw('X-Status'),$eml->header_raw('Status')]}";
187 $s =~ s/([FART])/$kw{$status2kw{$1}} = 1/sge;
191 # cf: https://cr.yp.to/proto/maildir.html
192 my %c2kw = ('D' => 'draft', F => 'flagged', R => 'answered', S => 'seen');
193 sub maildir_keywords {
194 $_[-1] =~ /:2,([A-Z]+)\z/i ?
195 sort(map { $c2kw{$_} // () } split(//, $1)) : ();
199 my ($self, $eml, @kw) = @_;
200 my $eidx = eidx_init($self);
201 my $oidx = $eidx->{oidx};
202 my $smsg = bless { -oidx => $oidx }, 'PublicInbox::Smsg';
203 my $im = $self->importer;
204 $im->add($eml, undef, $smsg) or return; # duplicate returns undef
205 my $msgref = delete $smsg->{-raw_email};
206 $smsg->{bytes} = $smsg->{raw_bytes} + crlf_adjust($$msgref);
208 local $self->{current_info} = $smsg->{blob};
209 if (my @docids = _docids_for($self, $eml)) {
210 for my $docid (@docids) {
211 my $idx = $eidx->idx_shard($docid);
212 $oidx->add_xref3($docid, -1, $smsg->{blob}, '.');
213 $idx->shard_add_eidx_info($docid, '.', $eml); # List-Id
214 $idx->shard_add_keywords($docid, @kw) if @kw;
218 $smsg->{num} = $oidx->adj_counter('eidx_docid', '+');
219 $oidx->add_overview($eml, $smsg);
220 $oidx->add_xref3($smsg->{num}, -1, $smsg->{blob}, '.');
221 my $idx = $eidx->idx_shard($smsg->{num});
222 $idx->index_raw($msgref, $eml, $smsg);
223 $idx->shard_add_keywords($smsg->{num}, @kw) if @kw;
229 my ($self, $eml, @kw) = @_;
230 add_eml($self, $eml, @kw) // set_eml_keywords($self, $eml, @kw);
236 if (my $im = delete($self->{im})) {
239 $err .= "import done: $@\n";
243 $self->{priv_eidx}->done;