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/mid2path/;
10 # TODO: user-configurable
12 'http://mid.gmane.org/%s',
13 'https://lists.debian.org/msgid-search/%s',
14 'http://mid.mail-archive.com/%s',
15 'http://marc.info/?i=%s',
20 my $pi_config = $ctx->{pi_config};
21 my $listname = $ctx->{listname};
22 my $mid = $ctx->{mid};
24 eval { require PublicInbox::Search };
25 my $have_xap = $@ ? 0 : 1;
28 foreach my $k (keys %$pi_config) {
29 $k =~ /\Apublicinbox\.([A-Z0-9a-z-]+)\.url\z/ or next;
31 next if $list eq $listname;
33 my $git_dir = $pi_config->{"publicinbox.$list.mainrepo"};
34 defined $git_dir or next;
36 my $url = $pi_config->{"publicinbox.$list.url"};
41 # try to find the URL with Xapian to avoid forking
44 my $s = PublicInbox::Search->new($git_dir);
45 $s->find_unique_doc_id('mid', $mid);
48 # xapian not configured for this repo
51 return r302($url, $mid) if (defined $doc_id);
53 # no point in trying the fork fallback if we
54 # know Xapian is up-to-date but missing the
55 # message in the current repo
60 # queue up for forking after we've tried Xapian on all of them
61 push @nox, { git_dir => $git_dir, url => $url };
64 # Xapian not installed or configured for some repos
65 my $path = "HEAD:" . mid2path($mid);
67 foreach my $n (@nox) {
68 my @cmd = ('git', "--git-dir=$n->{git_dir}", 'cat-file',
70 my $pid = open my $fh, '-|';
71 defined $pid or die "fork failed: $!\n";
74 open STDERR, '>', '/dev/null'; # ignore errors
75 exec @cmd or die "exec failed: $!\n";
77 my $type = eval { local $/; <$fh> };
79 if ($? == 0 && $type eq "blob\n") {
80 return r302($n->{url}, $mid);
86 my $h = PublicInbox::Hval->new_msgid($mid, 1);
87 my $href = $h->as_href;
88 my $html = $h->as_html;
89 my $title = "Message-ID <$html> not found";
91 # Fall back to external repos if configured
92 my $s = "<html><head><title>$title</title>" .
93 "</head><body><pre><b>$title</b>";
97 $s .= "\n\nPerhaps try an external site:\n\n";
98 foreach my $u (@EXT_URL) {
99 my $r = sprintf($u, $href);
100 my $t = sprintf($u, $html);
101 $s .= qq{<a\nhref="$r">$t</a>\n};
104 $s .= '</pre></body></html>';
106 [300, ['Content-Type'=>'text/html; charset=UTF-8'], [$s]];
109 # Redirect to another public-inbox which is mapped by $pi_config
111 my ($url, $mid) = @_;
112 $url .= '/' . uri_escape_utf8($mid) . '/';
114 [ 'Location' => $url, 'Content-Type' => 'text/plain' ],
115 [ "Redirecting to\n$url\n" ] ]