]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/LeiToMail.pm
lei: hide *_atfork_child from command-line
[public-inbox.git] / lib / PublicInbox / LeiToMail.pm
index 13764d79fbf79433ab49af0f1853b0ffcb75988e..1be15707ac19ee305a6dab2a62a8c849aeba0c1f 100644 (file)
@@ -42,10 +42,7 @@ sub _mbox_hdr_buf ($$$) {
        my ($eml, $type, $smsg) = @_;
        $eml->header_set($_) for (qw(Lines Bytes Content-Length));
 
-       # Messages are always 'O' (non-\Recent in IMAP), it saves
-       # MUAs the trouble of rewriting the mbox if no other
-       # changes are made
-       my %hdr = (Status => [ 'O' ]); # set Status, X-Status
+       my %hdr = (Status => []); # set Status, X-Status
        for my $k (@{$smsg->{kw} // []}) {
                if (my $ent = $kw2status{$k}) {
                        push @{$hdr{$ent->[0]}}, $ent->[1];
@@ -53,8 +50,13 @@ sub _mbox_hdr_buf ($$$) {
                        warn "TODO: keyword `$k' not supported for mbox\n";
                }
        }
-       while (my ($name, $chars) = each %hdr) {
-               $eml->header_set($name, join('', sort @$chars));
+       # Messages are always 'O' (non-\Recent in IMAP), it saves
+       # MUAs the trouble of rewriting the mbox if no other
+       # changes are made.  We put 'O' at the end (e.g. "Status: RO")
+       # to match mutt(1) output.
+       $eml->header_set('Status', join('', sort(@{$hdr{Status}})). 'O');
+       if (my $chars = delete $hdr{'X-Status'}) {
+               $eml->header_set('X-Status', join('', sort(@$chars)));
        }
        my $buf = delete $eml->{hdr};
 
@@ -246,8 +248,8 @@ sub _augment { # MboxReader eml_cb
 
 sub _mbox_augment_kw_maybe {
        my ($eml, $lei, $lse, $augment) = @_;
-       my @kw = PublicInbox::LeiStore::mbox_keywords($eml);
-       update_kw_maybe($lei, $lse, $eml, \@kw);
+       my $kw = PublicInbox::MboxReader::mbox_keywords($eml);
+       update_kw_maybe($lei, $lse, $eml, $kw);
        _augment($eml, $lei) if $augment;
 }
 
@@ -261,10 +263,12 @@ sub _mbox_write_cb ($$) {
        my $atomic_append = !defined($ovv->{lock_path});
        my $dedupe = $lei->{dedupe};
        $dedupe->prepare_dedupe;
+       my $lse = $lei->{sto} ? $lei->{sto}->search : undef;
        sub { # for git_to_mail
                my ($buf, $smsg, $eml) = @_;
                $eml //= PublicInbox::Eml->new($buf);
                return if $dedupe->is_dup($eml, $smsg->{blob});
+               $lse->xsmsg_vmd($smsg) if $lse;
                $buf = $eml2mbox->($eml, $smsg);
                return atomic_append($lei, $buf) if $atomic_append;
                my $lk = $ovv->lock_for_scope;
@@ -276,10 +280,15 @@ sub update_kw_maybe ($$$$) {
        my ($lei, $lse, $eml, $kw) = @_;
        return unless $lse;
        my $x = $lse->kw_changed($eml, $kw);
+       my $vmd = { kw => $kw };
        if ($x) {
-               $lei->{sto}->ipc_do('set_eml', $eml, @$kw);
+               $lei->{sto}->ipc_do('set_eml', $eml, $vmd);
        } elsif (!defined($x)) {
-               # TODO: xkw
+               if (my $xoids = $lei->{ale}->xoids_for($eml)) {
+                       $lei->{sto}->ipc_do('set_xvmd', $xoids, $eml, $vmd);
+               } else {
+                       $lei->{sto}->ipc_do('set_eml', $eml, $vmd);
+               }
        }
 }
 
@@ -343,10 +352,12 @@ sub _maildir_write_cb ($$) {
        my $dedupe = $lei->{dedupe};
        $dedupe->prepare_dedupe if $dedupe;
        my $dst = $lei->{ovv}->{dst};
+       my $lse = $lei->{sto} ? $lei->{sto}->search : undef;
        sub { # for git_to_mail
                my ($buf, $smsg, $eml) = @_;
                $dst // return $lei->fail; # dst may be undef-ed in last run
                $buf //= \($eml->as_string);
+               $lse->xsmsg_vmd($smsg) if $lse;
                return _buf2maildir($dst, $buf, $smsg) if !$dedupe;
                $eml //= PublicInbox::Eml->new($$buf); # copy buf
                return if $dedupe->is_dup($eml, $smsg->{blob});
@@ -362,6 +373,7 @@ sub _imap_write_cb ($$) {
        my $imap_append = $lei->{net}->can('imap_append');
        my $mic = $lei->{net}->mic_get($self->{uri});
        my $folder = $self->{uri}->mailbox;
+       my $lse = $lei->{sto} ? $lei->{sto}->search : undef;
        sub { # for git_to_mail
                my ($bref, $smsg, $eml) = @_;
                $mic // return $lei->fail; # dst may be undef-ed in last run
@@ -369,6 +381,7 @@ sub _imap_write_cb ($$) {
                        $eml //= PublicInbox::Eml->new($$bref); # copy bref
                        return if $dedupe->is_dup($eml, $smsg->{blob});
                }
+               $lse->xsmsg_vmd($smsg) if $lse;
                eval { $imap_append->($mic, $folder, $bref, $smsg, $eml) };
                if (my $err = $@) {
                        undef $mic;
@@ -623,7 +636,7 @@ sub do_post_auth {
 sub ipc_atfork_child {
        my ($self) = @_;
        my $lei = $self->{lei};
-       $lei->lei_atfork_child;
+       $lei->_lei_atfork_child;
        $lei->{auth}->do_auth_atfork($self) if $lei->{auth};
        $SIG{__WARN__} = PublicInbox::Eml::warn_ignore_cb();
        $self->SUPER::ipc_atfork_child;
@@ -637,23 +650,20 @@ sub poke_dst {
        my ($self) = @_;
        if ($self->{base_type} eq 'maildir') {
                my $t = time + 1;
-               utime($t, $t, "$self->{dst}/cur");
+               utime($t, $t, $self->{dst} . 'cur');
        }
 }
 
 sub write_mail { # via ->wq_io_do
-       my ($self, $git_dir, $smsg) = @_;
-       my $git = $self->{"$$\0$git_dir"} //= PublicInbox::Git->new($git_dir);
-       git_async_cat($git, $smsg->{blob}, \&git_to_mail,
+       my ($self, $smsg) = @_;
+       git_async_cat($self->{lei}->{ale}->git, $smsg->{blob}, \&git_to_mail,
                                [$self->{wcb}, $smsg]);
 }
 
 sub wq_atexit_child {
        my ($self) = @_;
        delete $self->{wcb};
-       for my $git (delete @$self{grep(/\A$$\0/, keys %$self)}) {
-               $git->async_wait_all;
-       }
+       $self->{lei}->{ale}->git->async_wait_all;
        $SIG{__WARN__} = 'DEFAULT';
 }