1 # Copyright (C) 2015 all contributors <meta@public-inbox.org>
2 # License: AGPLv3 or later (https://www.gnu.org/licenses/agpl-3.0.txt)
3 package PublicInbox::ExtMsg;
6 use URI::Escape qw(uri_escape_utf8);
8 use PublicInbox::MID qw/mid_compress mid2path/;
12 my $pi_config = $ctx->{pi_config};
13 my $listname = $ctx->{listname};
14 my $mid = $ctx->{mid};
16 eval { require PublicInbox::Search };
17 my $have_xap = $@ ? 0 : 1;
20 foreach my $k (keys %$pi_config) {
21 $k =~ /\Apublicinbox\.([A-Z0-9a-z-]+)\.url\z/ or next;
23 next if $list eq $listname;
25 my $git_dir = $pi_config->{"publicinbox.$list.mainrepo"};
26 defined $git_dir or next;
28 my $url = $pi_config->{"publicinbox.$list.url"};
33 # try to find the URL with Xapian to avoid forking
36 my $s = PublicInbox::Search->new($git_dir);
37 $s->find_unique_doc_id('mid', $mid);
40 # xapian not configured for this repo
43 return r302($url, $mid) if (defined $doc_id);
45 # no point in trying the fork fallback if we
46 # know Xapian is up-to-date but missing the
47 # message in the current repo
52 # queue up for forking after we've tried Xapian on all of them
53 push @nox, { git_dir => $git_dir, url => $url };
56 # Xapian not installed or configured for some repos
57 my $path = "HEAD:" . mid2path($mid);
59 foreach my $n (@nox) {
60 my @cmd = ('git', "--git-dir=$n->{git_dir}", 'cat-file',
62 my $pid = open my $fh, '-|';
63 defined $pid or die "fork failed: $!\n";
66 open STDERR, '>', '/dev/null'; # ignore errors
67 exec @cmd or die "exec failed: $!\n";
69 my $type = eval { local $/; <$fh> };
71 if ($? == 0 && $type eq "blob\n") {
72 return r302($n->{url}, $mid);
77 # Fall back to external repos
79 [404, ['Content-Type'=>'text/plain'], ['Not found']];
82 # Redirect to another public-inbox which is mapped by $pi_config
85 $url .= '/' . uri_escape_utf8($mid) . '/';
87 [ 'Location' => $url, 'Content-Type' => 'text/plain' ],
88 [ "Redirecting to\n$url\n" ] ]