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__;
27 eidx_init($self) if $opt->{creat};
31 sub git { $_[0]->{priv_eidx}->git } # read-only
33 sub packing_factor { $PublicInbox::V2Writable::PACKING_FACTOR }
36 $_[0]->{rotate_bytes} // ((1024 * 1024 * 1024) / $_[0]->packing_factor)
39 sub git_pfx { "$_[0]->{priv_eidx}->{topdir}/local" };
43 if (opendir(my $dh, $self->git_pfx)) {
45 substr($_, 0, -4) + 0; # drop ".git" suffix
46 } grep(/\A[0-9]+\.git\z/, readdir($dh))) // 0;
48 $!{ENOENT} ? 0 : die("opendir ${\$self->git_pfx}: $!\n");
54 open my $null, '>', '/dev/null' or die "open /dev/null: $!";
55 my $opt = { 2 => $null };
56 chomp(my $i = $git->qx(qw(var GIT_COMMITTER_IDENT), undef, $opt));
57 warn "$git->{git_dir} GIT_COMMITTER_IDENT failed\n" if $?;
58 $i =~ /\A(.+) <([^>]+)> [0-9]+ [-\+]?[0-9]+$/ ? ($1, $2) :
59 ('lei user', 'x@example.com')
67 return $im if $im->{bytes_added} < $self->rotate_bytes;
73 $max = $self->git_epoch_max + 1;
75 my $pfx = $self->git_pfx;
76 $max //= $self->git_epoch_max;
78 my $latest = "$pfx/$max.git";
80 PublicInbox::Import::init_bare($latest);
81 my $git = PublicInbox::Git->new($latest);
82 $git->qx(qw(config core.sharedRepository 0600)) if !$old;
83 my $packed_bytes = $git->packed_bytes;
84 my $unpacked_bytes = $packed_bytes / $self->packing_factor;
85 if ($unpacked_bytes >= $self->rotate_bytes) {
89 my ($n, $e) = git_ident($git);
90 $self->{im} = $im = PublicInbox::Import->new($git, $n, $e);
91 $im->{bytes_added} = int($packed_bytes / $self->packing_factor);
92 $im->{lock_path} = undef;
93 $im->{path_type} = 'v2';
99 PublicInbox::LeiSearch->new($_[0]->{priv_eidx}->{topdir});
104 my $eidx = $self->{priv_eidx};
105 $eidx->idx_init({-private => 1});
109 # when a message has no Message-IDs at all, this is needed for
110 # unsent Draft messages, at least
111 sub _fake_mid_for ($$) {
112 my ($eml, $dig) = @_;
113 my $mids = mids_in($eml, qw(X-Alt-Message-ID Resent-Message-ID));
114 $eml->{-lei_fake_mid} =
115 $mids->[0] // PublicInbox::Import::digest2mid($dig, $eml);
118 sub _docids_for ($$) {
119 my ($self, $eml) = @_;
121 my $dig = content_digest($eml);
122 my $chash = $dig->clone->digest;
123 my $eidx = eidx_init($self);
124 my $oidx = $eidx->{oidx};
125 my $im = $self->{im};
126 my $mids = mids($eml);
127 $mids->[0] //= _fake_mid_for($eml, $dig);
128 for my $mid (@$mids) {
130 while (my $cur = $oidx->next_by_mid($mid, \$id, \$prev)) {
131 my $oid = $cur->{blob};
132 my $docid = $cur->{num};
133 my $bref = $im ? $im->cat_blob($oid) : undef;
134 $bref //= $eidx->git->cat_file($oid) // do {
135 warn "W: $oid (#$docid) <$mid> not found\n";
138 local $self->{current_info} = $oid;
139 my $x = PublicInbox::Eml->new($bref);
140 $docids{$docid} = $docid if content_hash($x) eq $chash;
143 sort { $a <=> $b } values %docids;
146 sub set_eml_keywords {
147 my ($self, $eml, @kw) = @_;
148 my $eidx = eidx_init($self);
149 my @docids = _docids_for($self, $eml);
150 for my $docid (@docids) {
151 $eidx->idx_shard($docid)->shard_set_keywords($docid, @kw);
156 sub add_eml_keywords {
157 my ($self, $eml, @kw) = @_;
158 my $eidx = eidx_init($self);
159 my @docids = _docids_for($self, $eml);
160 for my $docid (@docids) {
161 $eidx->idx_shard($docid)->shard_add_keywords($docid, @kw);
166 sub remove_eml_keywords {
167 my ($self, $eml, @kw) = @_;
168 my $eidx = eidx_init($self);
169 my @docids = _docids_for($self, $eml);
170 for my $docid (@docids) {
171 $eidx->idx_shard($docid)->shard_remove_keywords($docid, @kw);
176 # cf: https://doc.dovecot.org/configuration_manual/mail_location/mbox/
177 my %status2kw = (F => 'flagged', A => 'answered', R => 'seen', T => 'draft');
178 # O (old/non-recent), and D (deleted) aren't in JMAP,
179 # so probably won't be supported by us.
182 my $s = "@{[$eml->header_raw('X-Status'),$eml->header_raw('Status')]}";
184 $s =~ s/([FART])/$kw{$status2kw{$1}} = 1/sge;
188 # cf: https://cr.yp.to/proto/maildir.html
189 my %c2kw = ('D' => 'draft', F => 'flagged', R => 'answered', S => 'seen');
190 sub maildir_keywords {
191 $_[-1] =~ /:2,([A-Z]+)\z/i ?
192 sort(map { $c2kw{$_} // () } split(//, $1)) : ();
196 my ($self, $eml, @kw) = @_;
197 my $eidx = eidx_init($self);
198 my $oidx = $eidx->{oidx};
199 my $smsg = bless { -oidx => $oidx }, 'PublicInbox::Smsg';
200 my $im = $self->importer;
201 $im->add($eml, undef, $smsg) or return; # duplicate returns undef
202 my $msgref = delete $smsg->{-raw_email};
203 $smsg->{bytes} = $smsg->{raw_bytes} + crlf_adjust($$msgref);
205 local $self->{current_info} = $smsg->{blob};
206 if (my @docids = _docids_for($self, $eml)) {
207 for my $docid (@docids) {
208 my $idx = $eidx->idx_shard($docid);
209 $oidx->add_xref3($docid, -1, $smsg->{blob}, '.');
210 $idx->shard_add_eidx_info($docid, '.', $eml); # List-Id
211 $idx->shard_add_keywords($docid, @kw) if @kw;
215 $smsg->{num} = $oidx->adj_counter('eidx_docid', '+');
216 $oidx->add_overview($eml, $smsg);
217 $oidx->add_xref3($smsg->{num}, -1, $smsg->{blob}, '.');
218 my $idx = $eidx->idx_shard($smsg->{num});
219 $idx->index_raw($msgref, $eml, $smsg);
220 $idx->shard_add_keywords($smsg->{num}, @kw) if @kw;
226 my ($self, $eml, @kw) = @_;
227 add_eml($self, $eml, @kw) // set_eml_keywords($self, $eml, @kw);
233 if (my $im = delete($self->{im})) {
236 $err .= "import done: $@\n";
240 $self->{priv_eidx}->done;