+sub each_eml { # callback for MboxReader->mboxrd
+ my ($eml, $self, $lei, $each_smsg) = @_;
+ my $smsg = bless {}, 'PublicInbox::Smsg';
+ $smsg->populate($eml);
+ $smsg->parse_references($eml, mids($eml));
+ $smsg->{$_} //= '' for qw(from to cc ds subject references mid);
+ delete @$smsg{qw(From Subject -ds -ts)};
+ if (my $startq = delete($lei->{startq})) { wait_startq($startq) }
+ ++$lei->{-nr_remote_eml};
+ if (!$lei->{opt}->{quiet}) {
+ my $now = now();
+ my $next = $lei->{-next_progress} //= ($now + 1);
+ if ($now > $next) {
+ $lei->{-next_progress} = $now + 1;
+ my $nr = $lei->{-nr_remote_eml};
+ $lei->err("# $lei->{-current_url} $nr/?");
+ }
+ }
+ $each_smsg->($smsg, undef, $eml);
+}
+
+# PublicInbox::OnDestroy callback
+sub kill_reap {
+ my ($pid) = @_;
+ kill('KILL', $pid); # spawn() blocks other signals
+ waitpid($pid, 0);
+}
+
+sub query_remote_mboxrd {
+ my ($self, $lei, $uris) = @_;
+ local $0 = "$0 query_remote_mboxrd";
+ $lei->atfork_child_wq($self);
+ my ($opt, $env) = @$lei{qw(opt env)};
+ my @qform = (q => $lei->{mset_opt}->{qstr}, x => 'm');
+ push(@qform, t => 1) if $opt->{thread};
+ my @cmd = ($self->{curl}, qw(-sSf -d), '');
+ my $verbose = $opt->{verbose};
+ my $reap;
+ my $cerr = File::Temp->new(TEMPLATE => 'curl.err-XXXX', TMPDIR => 1);
+ fcntl($cerr, F_SETFL, O_APPEND|O_RDWR) or warn "set O_APPEND: $!";
+ my $rdr = { 2 => $cerr };
+ my $coff = 0;
+ if ($verbose) {
+ # spawn a process to force line-buffering, otherwise curl
+ # will write 1 character at-a-time and parallel outputs
+ # mmmaaayyy llloookkk llliiikkkeee ttthhhiiisss
+ push @cmd, '-v';
+ my $o = { 1 => $lei->{2}, 2 => $lei->{2} };
+ my $pid = spawn(['tail', '-f', $cerr->filename], undef, $o);
+ $reap = PublicInbox::OnDestroy->new(\&kill_reap, $pid);
+ }
+ for my $o ($lei->curl_opt) {
+ $o =~ s/\|[a-z0-9]\b//i; # remove single char short option
+ if ($o =~ s/=[is]@\z//) {
+ my $ary = $opt->{$o} or next;
+ push @cmd, map { ("--$o", $_) } @$ary;
+ } elsif ($o =~ s/=[is]\z//) {
+ my $val = $opt->{$o} // next;
+ push @cmd, "--$o", $val;
+ } elsif ($opt->{$o}) {
+ push @cmd, "--$o";
+ }
+ }
+ $opt->{torsocks} = 'false' if $opt->{'no-torsocks'};
+ my $tor = $opt->{torsocks} //= 'auto';
+ my $each_smsg = $lei->{ovv}->ovv_each_smsg_cb($lei);
+ for my $uri (@$uris) {
+ $lei->{-current_url} = $uri->as_string;
+ $lei->{-nr_remote_eml} = 0;
+ $uri->query_form(@qform);
+ my $cmd = [ @cmd, $uri->as_string ];
+ if ($tor eq 'auto' && substr($uri->host, -6) eq '.onion' &&
+ (($env->{LD_PRELOAD}//'') !~ /torsocks/)) {
+ unshift @$cmd, which('torsocks');
+ } elsif (PublicInbox::Config::git_bool($tor)) {
+ unshift @$cmd, which('torsocks');
+ }
+
+ # continue anyways if torsocks is missing; a proxy may be
+ # specified via CLI, curlrc, environment variable, or even
+ # firewall rule
+ shift(@$cmd) if !$cmd->[0];
+
+ $lei->err("# @$cmd") if $verbose;
+ $? = 0;
+ my $fh = popen_rd($cmd, $env, $rdr);
+ $fh = IO::Uncompress::Gunzip->new($fh);
+ eval {
+ PublicInbox::MboxReader->mboxrd($fh, \&each_eml, $self,
+ $lei, $each_smsg);
+ };
+ return $lei->fail("E: @$cmd: $@") if $@;
+ if ($? == 0) {
+ my $nr = $lei->{-nr_remote_eml};
+ pkt_do($lei->{pkt_op}, 'mset_progress',
+ $lei->{-current_url}, $nr, $nr);
+ next;
+ }
+ seek($cerr, $coff, SEEK_SET) or warn "seek(curl stderr): $!\n";
+ my $e = do { local $/; <$cerr> } //
+ die "read(curl stderr): $!\n";
+ $coff += length($e);
+ next if (($? >> 8) == 22 && $e =~ /\b404\b/);
+ $lei->child_error($?);
+ $uri->query_form(q => $lei->{mset_opt}->{qstr});
+ # --verbose already showed the error via tail(1)
+ $lei->err("E: $uri \$?=$?\n", $verbose ? () : $e);
+ }
+ undef $each_smsg;
+ $lei->{ovv}->ovv_atexit_child($lei);
+}
+