+ 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),
+ $smsg);
+ $lse->xsmsg_vmd($smsg) if $lse;
+ my $n = _buf2maildir($dst, $bref // \($eml->as_string),
+ $smsg, $dir);
+ $lms->set_src($smsg->oidbin, $out, $n) if $lms;
+ ++$lei->{-nr_write};
+ }
+}
+
+sub _imap_write_cb ($$) {
+ my ($self, $lei) = @_;
+ my $dedupe = $lei->{dedupe};
+ $dedupe->prepare_dedupe if $dedupe;
+ my $append = $lei->{net}->can('imap_append');
+ 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 $lms = $self->{-lms_rw};
+ $lms->lms_write_prepare if $lms;
+ 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),
+ $smsg);
+ $lse->xsmsg_vmd($smsg) if $lse;
+ my $uid = eval { $append->($mic, $folder, $bref, $smsg, $eml) };
+ if (my $err = $@) {
+ undef $mic;
+ die $err;
+ }
+ # imap_append returns UID if IMAP server has UIDPLUS extension
+ ($lms && $uid =~ /\A[0-9]+\z/) and
+ $lms->set_src($smsg->oidbin, $$uri, $uid + 0);
+ ++$lei->{-nr_write};
+ }
+}
+
+sub _text_write_cb ($$) {
+ my ($self, $lei) = @_;
+ my $dedupe = $lei->{dedupe};
+ $dedupe->prepare_dedupe if $dedupe;
+ my $lvt = $lei->{lvt};
+ my $ovv = $lei->{ovv};
+ $lei->{1} // die "no stdout ($ovv->{dst})"; # redirected earlier
+ $lei->{1}->autoflush(1);
+ binmode $lei->{1}, ':utf8';
+ my $lse = $lei->{lse}; # may be undef
+ sub { # for git_to_mail
+ my ($bref, $smsg, $eml) = @_;
+ $lse->xsmsg_vmd($smsg) if $lse;
+ $eml //= PublicInbox::Eml->new($bref);
+ return if $dedupe && $dedupe->is_dup($eml, $smsg);
+ my $lk = $ovv->lock_for_scope;
+ $lei->out(${$lvt->eml_to_text($smsg, $eml)}, "\n");
+ }
+}
+
+sub _v2_write_cb ($$) {
+ my ($self, $lei) = @_;
+ my $dedupe = $lei->{dedupe};
+ $dedupe->prepare_dedupe if $dedupe;
+ sub { # for git_to_mail
+ my ($bref, $smsg, $eml) = @_;
+ $eml //= PublicInbox::Eml->new($bref);
+ return if $dedupe && $dedupe->is_dup($eml, $smsg);
+ $lei->{v2w}->wq_do('add', $eml); # V2Writable->add
+ ++$lei->{-nr_write};