]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/LeiStore.pm
update copyrights for 2021
[public-inbox.git] / lib / PublicInbox / LeiStore.pm
index 56f668b8cc768015420ea5aa82f59346312f7c1b..a23efed55198e96c4ac5446c7681af235b58781c 100644 (file)
@@ -1,4 +1,4 @@
-# Copyright (C) 2020 all contributors <meta@public-inbox.org>
+# Copyright (C) 2020-2021 all contributors <meta@public-inbox.org>
 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
 #
 # Local storage (cache/memo) for lei(1), suitable for personal/private
@@ -9,20 +9,26 @@
 package PublicInbox::LeiStore;
 use strict;
 use v5.10.1;
-use parent qw(PublicInbox::Lock);
+use parent qw(PublicInbox::Lock PublicInbox::IPC);
 use PublicInbox::SearchIdx qw(crlf_adjust);
 use PublicInbox::ExtSearchIdx;
 use PublicInbox::Import;
 use PublicInbox::InboxWritable;
 use PublicInbox::V2Writable;
-use PublicInbox::ContentHash qw(content_hash);
-use PublicInbox::MID qw(mids);
+use PublicInbox::ContentHash qw(content_hash content_digest);
+use PublicInbox::MID qw(mids mids_in);
 use PublicInbox::LeiSearch;
+use List::Util qw(max);
 
 sub new {
        my (undef, $dir, $opt) = @_;
        my $eidx = PublicInbox::ExtSearchIdx->new($dir, $opt);
-       bless { priv_eidx => $eidx }, __PACKAGE__;
+       my $self = bless { priv_eidx => $eidx }, __PACKAGE__;
+       if ($opt->{creat}) {
+               PublicInbox::SearchIdx::load_xapian_writable();
+               eidx_init($self);
+       }
+       $self;
 }
 
 sub git { $_[0]->{priv_eidx}->git } # read-only
@@ -37,15 +43,21 @@ sub git_pfx { "$_[0]->{priv_eidx}->{topdir}/local" };
 
 sub git_epoch_max  {
        my ($self) = @_;
-       my $pfx = $self->git_pfx;
-       my $max = 0;
-       return $max unless -d $pfx ;
-       opendir my $dh, $pfx or die "opendir $pfx: $!\n";
-       while (defined(my $git_dir = readdir($dh))) {
-               $git_dir =~ m!\A([0-9]+)\.git\z! or next;
-               $max = $1 + 0 if $1 > $max;
+       if (opendir(my $dh, $self->git_pfx)) {
+               max(map {
+                       substr($_, 0, -4) + 0; # drop ".git" suffix
+               } grep(/\A[0-9]+\.git\z/, readdir($dh))) // 0;
+       } else {
+               $!{ENOENT} ? 0 : die("opendir ${\$self->git_pfx}: $!\n");
        }
-       $max;
+}
+
+sub git_ident ($) {
+       my ($git) = @_;
+       chomp(my $i = $git->qx(qw(var GIT_COMMITTER_IDENT)));
+       warn "$git->{git_dir} GIT_COMMITTER_IDENT failed\n" if $?;
+       $i =~ /\A(.+) <([^>]+)> [0-9]+ [-\+]?[0-9]+$/ ? ($1, $2) :
+               ('lei user', 'x@example.com')
 }
 
 sub importer {
@@ -66,8 +78,8 @@ sub importer {
        while (1) {
                my $latest = "$pfx/$max.git";
                my $old = -e $latest;
+               PublicInbox::Import::init_bare($latest);
                my $git = PublicInbox::Git->new($latest);
-               PublicInbox::Import::init_bare({ git => $git });
                $git->qx(qw(config core.sharedRepository 0600)) if !$old;
                my $packed_bytes = $git->packed_bytes;
                my $unpacked_bytes = $packed_bytes / $self->packing_factor;
@@ -75,10 +87,7 @@ sub importer {
                        $max++;
                        next;
                }
-               chomp(my $i = $git->qx(qw(var GIT_COMMITTER_IDENT)));
-               die "$git->{git_dir} GIT_COMMITTER_IDENT failed\n" if $?;
-               my ($n, $e) = ($i =~ /\A(.+) <([^>]+)> [0-9]+ [-\+]?[0-9]+$/g)
-                       or die "could not extract name/email from `$i'\n";
+               my ($n, $e) = git_ident($git);
                $self->{im} = $im = PublicInbox::Import->new($git, $n, $e);
                $im->{bytes_added} = int($packed_bytes / $self->packing_factor);
                $im->{lock_path} = undef;
@@ -98,14 +107,26 @@ sub eidx_init {
        $eidx;
 }
 
+# when a message has no Message-IDs at all, this is needed for
+# unsent Draft messages, at least
+sub _fake_mid_for ($$) {
+       my ($eml, $dig) = @_;
+       my $mids = mids_in($eml, qw(X-Alt-Message-ID Resent-Message-ID));
+       $eml->{-lei_fake_mid} =
+               $mids->[0] // PublicInbox::Import::digest2mid($dig, $eml);
+}
+
 sub _docids_for ($$) {
        my ($self, $eml) = @_;
        my %docids;
-       my $chash = content_hash($eml);
+       my $dig = content_digest($eml);
+       my $chash = $dig->clone->digest;
        my $eidx = eidx_init($self);
        my $oidx = $eidx->{oidx};
        my $im = $self->{im};
-       for my $mid (@{mids($eml)}) {
+       my $mids = mids($eml);
+       $mids->[0] //= _fake_mid_for($eml, $dig);
+       for my $mid (@$mids) {
                my ($id, $prev);
                while (my $cur = $oidx->next_by_mid($mid, \$id, \$prev)) {
                        my $oid = $cur->{blob};
@@ -153,8 +174,27 @@ sub remove_eml_keywords {
        \@docids;
 }
 
+# cf: https://doc.dovecot.org/configuration_manual/mail_location/mbox/
+my %status2kw = (F => 'flagged', A => 'answered', R => 'seen', T => 'draft');
+# O (old/non-recent), and D (deleted) aren't in JMAP,
+# so probably won't be supported by us.
+sub mbox_keywords {
+       my $eml = $_[-1];
+       my $s = "@{[$eml->header_raw('X-Status'),$eml->header_raw('Status')]}";
+       my %kw;
+       $s =~ s/([FART])/$kw{$status2kw{$1}} = 1/sge;
+       sort(keys %kw);
+}
+
+# cf: https://cr.yp.to/proto/maildir.html
+my %c2kw = ('D' => 'draft', F => 'flagged', R => 'answered', S => 'seen');
+sub maildir_keywords {
+       $_[-1] =~ /:2,([A-Z]+)\z/i ?
+               sort(map { $c2kw{$_} // () } split(//, $1)) : ();
+}
+
 sub add_eml {
-       my ($self, $eml) = @_;
+       my ($self, $eml, @kw) = @_;
        my $eidx = eidx_init($self);
        my $oidx = $eidx->{oidx};
        my $smsg = bless { -oidx => $oidx }, 'PublicInbox::Smsg';
@@ -169,15 +209,23 @@ sub add_eml {
                        my $idx = $eidx->idx_shard($docid);
                        $oidx->add_xref3($docid, -1, $smsg->{blob}, '.');
                        $idx->shard_add_eidx_info($docid, '.', $eml); # List-Id
+                       $idx->shard_add_keywords($docid, @kw) if @kw;
                }
+               \@docids;
        } else {
                $smsg->{num} = $oidx->adj_counter('eidx_docid', '+');
                $oidx->add_overview($eml, $smsg);
                $oidx->add_xref3($smsg->{num}, -1, $smsg->{blob}, '.');
                my $idx = $eidx->idx_shard($smsg->{num});
                $idx->index_raw($msgref, $eml, $smsg);
+               $idx->shard_add_keywords($smsg->{num}, @kw) if @kw;
+               $smsg;
        }
-       $smsg->{blob}
+}
+
+sub set_eml {
+       my ($self, $eml, @kw) = @_;
+       add_eml($self, $eml, @kw) // set_eml_keywords($self, $eml, @kw);
 }
 
 sub done {