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};
15 my $cmid = mid_compress($mid);
17 eval { require PublicInbox::Search };
18 my $have_xap = $@ ? 0 : 1;
21 foreach my $k (keys %$pi_config) {
22 $k =~ /\Apublicinbox\.([A-Z0-9a-z-]+)\.url\z/ or next;
24 next if $list eq $listname;
26 my $git_dir = $pi_config->{"publicinbox.$list.mainrepo"};
27 defined $git_dir or next;
29 my $url = $pi_config->{"publicinbox.$list.url"};
34 # try to find the URL with Xapian to avoid forking
37 my $s = PublicInbox::Search->new($git_dir);
38 $s->find_unique_doc_id('mid', $cmid);
41 # xapian not configured for this repo
44 return r302($url, $cmid) if (defined $doc_id);
46 # no point in trying the fork fallback if we
47 # know Xapian is up-to-date but missing the
48 # message in the current repo
53 # queue up for forking after we've tried Xapian on all of them
54 push @nox, { git_dir => $git_dir, url => $url };
57 # Xapian not installed or configured for some repos
58 my $path = "HEAD:" . mid2path($cmid);
60 foreach my $n (@nox) {
61 my @cmd = ('git', "--git-dir=$n->{git_dir}", 'cat-file',
63 my $pid = open my $fh, '-|';
64 defined $pid or die "fork failed: $!\n";
67 open STDERR, '>', '/dev/null'; # ignore errors
68 exec @cmd or die "exec failed: $!\n";
70 my $type = eval { local $/; <$fh> };
72 if ($? == 0 && $type eq "blob\n") {
73 return r302($n->{url}, $cmid);
78 # Fall back to external repos
80 [404, ['Content-Type'=>'text/plain'], ['Not found']];
83 # Redirect to another public-inbox which is mapped by $pi_config
86 $url .= '/' . uri_escape_utf8($mid) . '/';
88 [ 'Location' => $url, 'Content-Type' => 'text/plain' ],
89 [ "Redirecting to\n$url\n" ] ]