]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/LeiToMail.pm
lei export-kw: support exporting keywords to IMAP
[public-inbox.git] / lib / PublicInbox / LeiToMail.pm
index 71acf952c046fc0c984b2205110723afba4df84f..f3c039699cc26bdd5c539fe6b3acc8ed5a4c70d7 100644 (file)
@@ -137,9 +137,15 @@ sub eml2mboxcl2 {
 
 sub git_to_mail { # git->cat_async callback
        my ($bref, $oid, $type, $size, $arg) = @_;
+       my ($write_cb, $smsg) = @$arg;
+       if ($type eq 'missing' && $smsg->{-lms_ro}) {
+               if ($bref = $smsg->{-lms_ro}->local_blob($oid, 1)) {
+                       $type = 'blob';
+                       $size = length($$bref);
+               }
+       }
        return warn("W: $oid is $type (!= blob)\n") if $type ne 'blob';
        return warn("E: $oid is empty\n") unless $size;
-       my ($write_cb, $smsg) = @$arg;
        die "BUG: expected=$smsg->{blob} got=$oid" if $smsg->{blob} ne $oid;
        $write_cb->($bref, $smsg);
 }
@@ -237,10 +243,14 @@ sub _rand () {
        sprintf('%x,%x,%x,%x', rand(0xffffffff), time, $$, ++$seq);
 }
 
+sub kw2suffix ($;@) {
+       my $kw = shift;
+       join('', sort(map { $kw2char{$_} // () } @$kw, @_));
+}
+
 sub _buf2maildir {
        my ($dst, $buf, $smsg) = @_;
        my $kw = $smsg->{kw} // [];
-       my $sfx = join('', sort(map { $kw2char{$_} // () } @$kw));
        my $rand = ''; # chosen by die roll :P
        my ($tmp, $fh, $base, $ok);
        my $common = $smsg->{blob} // _rand;
@@ -257,7 +267,7 @@ sub _buf2maildir {
                $dst .= 'cur/';
                $rand = '';
                do {
-                       $base = $rand.$common.':2,'.$sfx
+                       $base = $rand.$common.':2,'.kw2suffix($kw);
                } while (!($ok = link($tmp, $dst.$base)) && $!{EEXIST} &&
                        ($rand = _rand.','));
                die "link($tmp, $dst$base): $!" unless $ok;
@@ -283,7 +293,8 @@ sub _maildir_write_cb ($$) {
                my ($bref, $smsg, $eml) = @_;
                $dst // return $lei->fail; # dst may be undef-ed in last run
                return if $dedupe && $dedupe->is_dup($eml //
-                                               PublicInbox::Eml->new($$bref));
+                                               PublicInbox::Eml->new($$bref),
+                                               $smsg);
                $lse->xsmsg_vmd($smsg) if $lse;
                my $n = _buf2maildir($dst, $bref // \($eml->as_string), $smsg);
                $sto->ipc_do('set_sync_info', $smsg->{blob}, $out, $n) if $sto;
@@ -296,16 +307,18 @@ sub _imap_write_cb ($$) {
        my $dedupe = $lei->{dedupe};
        $dedupe->prepare_dedupe if $dedupe;
        my $append = $lei->{net}->can('imap_append');
-       my $mic = $lei->{net}->mic_get($self->{uri});
-       my $folder = $self->{uri}->mailbox;
+       my $uri = $self->{uri};
+       my $mic = $lei->{net}->mic_get($uri);
+       my $folder = $uri->mailbox;
+       $uri->uidvalidity($mic->uidvalidity($folder));
        my $lse = $lei->{lse}; # may be undef
        my $sto = $lei->{opt}->{'mail-sync'} ? $lei->{sto} : undef;
-       my $out = $lei->{ovv}->{dst};
        sub { # for git_to_mail
                my ($bref, $smsg, $eml) = @_;
                $mic // return $lei->fail; # mic may be undef-ed in last run
                return if $dedupe && $dedupe->is_dup($eml //
-                                               PublicInbox::Eml->new($$bref));
+                                               PublicInbox::Eml->new($$bref),
+                                               $smsg);
                $lse->xsmsg_vmd($smsg) if $lse;
                my $uid = eval { $append->($mic, $folder, $bref, $smsg, $eml) };
                if (my $err = $@) {
@@ -315,7 +328,7 @@ sub _imap_write_cb ($$) {
                # imap_append returns UID if IMAP server has UIDPLUS extension
                ($sto && $uid =~ /\A[0-9]+\z/) and
                        $sto->ipc_do('set_sync_info',
-                                       $smsg->{blob}, $out, $uid + 0);
+                                       $smsg->{blob}, $$uri, $uid + 0);
                ++$lei->{-nr_write};
        }
 }
@@ -440,10 +453,10 @@ sub _do_augment_maildir {
 }
 
 sub _imap_augment_or_delete { # PublicInbox::NetReader::imap_each cb
-       my ($url, $uid, $kw, $eml, $lei, $lse, $delete_mic) = @_;
+       my ($uri, $uid, $kw, $eml, $lei, $lse, $delete_mic) = @_;
        update_kw_maybe($lei, $lse, $eml, $kw);
        if ($delete_mic) {
-               $lei->{net}->imap_delete_1($url, $uid, $delete_mic);
+               $lei->{net}->imap_delete_1($uri, $uid, $delete_mic);
        } else {
                _augment($eml, $lei);
        }
@@ -642,6 +655,9 @@ sub ipc_atfork_child {
        my ($self) = @_;
        my $lei = $self->{lei};
        $lei->_lei_atfork_child;
+       if (my $lse = $lei->{lse}) {
+               $self->{-lms_ro} = $lse->{-lms_ro} //= $lse->lms;
+       }
        $lei->{auth}->do_auth_atfork($self) if $lei->{auth};
        $SIG{__WARN__} = PublicInbox::Eml::warn_ignore_cb();
        $self->SUPER::ipc_atfork_child;
@@ -663,6 +679,7 @@ sub poke_dst {
 sub write_mail { # via ->wq_io_do
        my ($self, $smsg, $eml) = @_;
        return $self->{wcb}->(undef, $smsg, $eml) if $eml;
+       $smsg->{-lms_ro} = $self->{-lms_ro};
        $self->{lei}->{ale}->git->cat_async($smsg->{blob}, \&git_to_mail,
                                [$self->{wcb}, $smsg]);
 }