1 # Copyright (C) 2021 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 # Make remote externals HTTP(S) inboxes behave like
5 # PublicInbox::Inbox and PublicInbox::Search/ExtSearch.
6 # This exists solely for SolverGit. It is a high-latency a
7 # synchronous API that is not at all fast.
8 package PublicInbox::LeiRemote;
11 use IO::Uncompress::Gunzip;
12 use PublicInbox::OnDestroy;
13 use PublicInbox::MboxReader;
14 use PublicInbox::Spawn qw(popen_rd);
15 use PublicInbox::LeiCurl;
16 use PublicInbox::ContentHash qw(git_sha);
19 my ($cls, $lei, $uri) = @_;
20 bless { uri => $uri, lei => $lei }, $cls;
23 sub isrch { $_[0] } # SolverGit expcets this
25 sub _each_mboxrd_eml { # callback for MboxReader->mboxrd
26 my ($eml, $self) = @_;
27 my $lei = $self->{lei};
28 my $xoids = $lei->{ale}->xoids_for($eml, 1);
29 if ($lei->{sto} && !$xoids) { # memoize locally
30 $lei->{sto}->ipc_do('add_eml', $eml);
32 my $smsg = bless {}, 'PublicInbox::Smsg';
33 $smsg->{blob} = $xoids ? (keys(%$xoids))[0]
34 : git_sha(1, $eml)->hexdigest;
35 $smsg->populate($eml);
36 $smsg->{mid} //= '(none)';
37 push @{$self->{smsg}}, $smsg;
41 my ($self, $qstr, undef) = @_; # $opt ($_[2]) ignored
42 my $lei = $self->{lei};
43 my $curl = PublicInbox::LeiCurl->new($lei, $lei->{curl});
44 push @$curl, '-s', '-d', '';
45 my $uri = $self->{uri}->clone;
46 $uri->query_form(q => $qstr, x => 'm', r => 1); # r=1: relevance
47 my $cmd = $curl->for_uri($self->{lei}, $uri);
48 $self->{lei}->qerr("# $cmd");
49 my $rdr = { 2 => $lei->{2}, pgid => 0 };
50 my ($fh, $pid) = popen_rd($cmd, undef, $rdr);
51 my $reap = PublicInbox::OnDestroy->new($lei->can('sigint_reap'), $pid);
53 $fh = IO::Uncompress::Gunzip->new($fh, MultiStream => 1);
54 PublicInbox::MboxReader->mboxrd($fh, \&_each_mboxrd_eml, $self);
55 my $err = waitpid($pid, 0) == $pid ? undef
56 : "BUG: waitpid($cmd): $!";
57 @$reap = (); # cancel OnDestroy
58 my $wait = $self->{lei}->{sto}->ipc_do('done');
60 $self; # we are the mset (and $ibx, and $self)
63 sub size { scalar @{$_[0]->{smsg}} } # size of previous results
66 my ($self, $ibx, $mset) = @_; # all 3 are $self
67 wantarray ? ($self->size, @{$self->{smsg}}) : $self->{smsg};
70 sub base_url { "$_[0]->{uri}" }
73 my ($self, $smsg) = @_;
74 if (my $bref = $self->{lei}->ale->git->cat_file($smsg->{blob})) {
75 return PublicInbox::Eml->new($bref);
77 $self->{lei}->err("E: $self->{uri} $smsg->{blob} gone <$smsg->{mid}>");